Skip to main content

Android Dialog Demo

1 ) Activity Main

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.lenovo.all_dialog_example.MainActivity"    android:orientation="vertical">

    <Button        android:layout_width="368dp"        android:layout_height="wrap_content"        tools:layout_editor_absoluteY="0dp"        tools:layout_editor_absoluteX="8dp"        android:text="Button1"        android:id="@+id/b1"/>

    <Button        android:layout_width="368dp"        android:layout_height="wrap_content"        tools:layout_editor_absoluteY="0dp"        tools:layout_editor_absoluteX="8dp"        android:text="Button2"        android:id="@+id/b2"/>
    <Button        android:layout_width="368dp"        android:layout_height="wrap_content"        tools:layout_editor_absoluteY="0dp"        tools:layout_editor_absoluteX="8dp"        android:text="Button3"        android:id="@+id/b3"/>
    <Button        android:layout_width="368dp"        android:layout_height="wrap_content"        tools:layout_editor_absoluteY="0dp"        tools:layout_editor_absoluteX="8dp"        android:text="Button4"        android:id="@+id/b4"/>
    <Button        android:layout_width="368dp"        android:layout_height="wrap_content"        tools:layout_editor_absoluteY="0dp"        tools:layout_editor_absoluteX="8dp"        android:text="Button5"        android:id="@+id/b5"/>

    <Button        android:layout_width="368dp"        android:layout_height="wrap_content"        tools:layout_editor_absoluteY="0dp"        tools:layout_editor_absoluteX="8dp"        android:text="Button6"        android:id="@+id/b6"/>
</LinearLayout>

2 ) Alert Dialog Activity

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.lenovo.all_dialog_example.alertdialogactivity">

    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="This Is Alert Dialog Activity"/>
</android.support.constraint.ConstraintLayout>

3 ) Custom Dialog Activity

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.lenovo.all_dialog_example.customdialogactivity">

    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="This Is Custom Dialog Activity"/>

</LinearLayout>

4 ) Activity Country Code RAW

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:padding="4dp" >

    <!-- Views with default image and text, for demonstration.    They will be changed in the adapter -->    <ImageView        android:layout_width="wrap_content"        android:layout_height="35dp"        android:id="@+id/flag"        android:src="@mipmap/ic_launcher"        android:scaleType="centerInside"        android:layout_marginRight="8dp"        android:layout_marginLeft="4dp" />

    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textAppearance="?android:attr/textAppearanceMedium"        android:text="Title"        android:id="@+id/name"        android:layout_gravity="center_vertical" />

</LinearLayout>

5 ) Activity Date Picker

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.lenovo.all_dialog_example.datepickeractivity">

</android.support.constraint.ConstraintLayout>

6 ) List Dialog Activity

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.lenovo.all_dialog_example.listdialogactivity"    android:orientation="vertical">


    <Button        android:id="@+id/btnList"        android:layout_height="wrap_content"        android:layout_width="90dip"        android:text="List"        android:background="#00bfff"        android:textColor="#fff"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true"        android:layout_marginTop="42dp" />

    <Button        android:id="@+id/btnCheckBox"        android:layout_height="wrap_content"        android:layout_width="wrap_content"        android:text="CheckBox"        android:background="#00bfff"        android:textColor="#fff"        android:layout_marginTop="12dp"        android:layout_alignBaseline="@+id/btnRadio"        android:layout_alignBottom="@+id/btnRadio"        android:layout_alignLeft="@+id/btnRadio"        android:layout_alignStart="@+id/btnRadio" />

    <Button        android:id="@+id/btnRadio"        android:layout_height="wrap_content"        android:layout_width="wrap_content"        android:text="RadioButton"        android:background="#00bfff"        android:layout_marginTop="12dp"        android:textColor="#fff"        android:layout_centerVertical="true"        android:layout_centerHorizontal="true" />

</LinearLayout>

7 ) Acticity Progress Dialog

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.lenovo.all_dialog_example.progressdialogactivity">

</android.support.constraint.ConstraintLayout>

8 ) Activity Date Time Picker

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.lenovo.all_dialog_example.timepickeractivity"    android:orientation="vertical">

    <EditText        android:layout_width="200dp"        android:layout_height="wrap_content"        android:id="@+id/in_time"        android:layout_below="@+id/in_date"        android:layout_alignParentLeft="true"        android:layout_alignParentStart="true" />

    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="SELECT TIME"        android:id="@+id/btn_time"        android:layout_below="@+id/btn_date"        android:layout_alignLeft="@+id/btn_date"        android:layout_alignStart="@+id/btn_date" />

</LinearLayout>

9 ) Activity Custom Dialog

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    app:layout_behavior="@string/appbar_scrolling_view_behavior"    tools:context="com.example.lenovo.all_dialog_example.customdialogactivity"    tools:showIn="@layout/activity_customdialogactivity">

</android.support.constraint.ConstraintLayout>

10 ) Activity Time Picker

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    app:layout_behavior="@string/appbar_scrolling_view_behavior"    tools:context="com.example.lenovo.all_dialog_example.timepickeractivity"    tools:showIn="@layout/activity_timepickeractivity">

</android.support.constraint.ConstraintLayout>

11 ) Activity Custom Dialog

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">

    <ImageView        android:id="@+id/image"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginRight="5dp"        android:layout_marginTop="10dp"        android:src="@mipmap/ic_launcher_round"/>

    <TextView        android:id="@+id/text"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_toRightOf="@+id/image"        android:text="IMMEHTA"        android:textColor="@color/colorPrimaryDark"        tools:textSize="25dp" />/>

    <Button        android:id="@+id/dialogButtonOK"        android:layout_width="100px"        android:layout_height="wrap_content"        android:text=" Ok "        android:layout_marginTop="5dp"        android:layout_marginRight="5dp"        android:layout_below="@+id/image"        />

</LinearLayout>

12 ) Main Activity

public class MainActivity extends AppCompatActivity {

    Button b1, b2, b3, b4, b5, b6;
    final Context context = this;
    private DatePicker datePicker;
    private Calendar calendar;
    private TextView dateView;
    private int year, month, day;
    static final int DATE_PICKER_ID = 1111;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Calendar c = Calendar.getInstance();
        year  = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day   = c.get(Calendar.DAY_OF_MONTH);
        b1 = (Button) findViewById(R.id.b1);
        b2 = (Button) findViewById(R.id.b2);
        b3 = (Button) findViewById(R.id.b3);
        b4 = (Button) findViewById(R.id.b4);
        b5 = (Button) findViewById(R.id.b5);
        b6 = (Button) findViewById(R.id.b6);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                open(b1);
            }
        });


        b2.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v)
            {
                final Dialog dialog = new Dialog(context);
                dialog.setContentView(R.layout.custom_dialog);
                dialog.setTitle("Arpan...");

                // set the custom dialog components - text, image and button                TextView text = (TextView) dialog.findViewById(R.id.text);
                text.setText("I Love You Arpan!");
                ImageView image = (ImageView) dialog.findViewById(R.id.image);
                image.setImageResource(R.mipmap.ic_launcher);

                Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);

                dialogButton.setOnClickListener(new View.OnClickListener() {
                    @Override                    public void onClick(View v) {
                        Intent intent=new Intent(getApplicationContext(),customdialogactivity.class);
                        startActivity(intent);
                    }
                });
                dialog.show();
            }
        });


        b3.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                showDialog(DATE_PICKER_ID);
            }
        });


        b4.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                Intent intent=new Intent(getApplicationContext(),listdialogactivity.class);
                startActivity(intent);
            }
        });


        b5.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                Intent intent=new Intent(getApplicationContext(),timepickeractivity.class);
                startActivity(intent);
            }
        });


        b6.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                Intent intent=new Intent(getApplicationContext(),progressdialogactivity.class);
                startActivity(intent);
            }
        });
    }

    public void open(View view) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setMessage("Are you sure, You wanted to make decision");
        alertDialogBuilder.setPositiveButton("yes",
                new DialogInterface.OnClickListener() {
                    @Override                    public void onClick(DialogInterface arg0, int arg1) {
                        Intent intent=new Intent(getApplicationContext(),alertdialogactivity.class);
                        startActivity(intent);
                    }
                });

        alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(),"You Click On No",Toast.LENGTH_SHORT).show();
            }
        });

        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }

    @Override    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_PICKER_ID:

                // open datepicker dialog.                // set date picker for current date                // add pickerListener listner to date picker                return new DatePickerDialog(this, pickerListener, year, month,day);
        }
        return null;
    }
    private DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() {

        // when dialog box is closed, below method will be called.        @Override        public void onDateSet(DatePicker view, int selectedYear,
                              int selectedMonth, int selectedDay) {

            year  = selectedYear;
            month = selectedMonth;
            day   = selectedDay;

            // Show selected date            b3.setText(new StringBuilder().append(month + 1)
                    .append("-").append(day).append("-").append(year)
                    .append(" "));

        }
    };
}

13 ) List Dialog Activity

public class listdialogactivity extends AppCompatActivity implements View.OnClickListener {
    final int LIST_ALERTDIALOG = 0, CHECKBOX_ALERTDIALOG = 1, RADIOBUTTON_ALERTDIALOG = 2;
    final boolean checked_state[] = {false, false, false}; //The array that holds the checked state of the checkbox items    final CharSequence[] day_check = {"Sunday", "Monday", "Tuesday"}; //items in the alertdialog that displays checkboxes    final CharSequence[] day_list = {"Sunday", "Monday", "Monday"}; //items in the alertdialog that displays list    final CharSequence[] day_radio = {"Sunday", "Monday", "Monday"}; //items in the alertdialog that displays radiobuttons    private Button list, check, radio;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listdialogactivity);
        list = (Button) findViewById(R.id.btnList);
        check = (Button) findViewById(R.id.btnCheckBox);
        radio = (Button) findViewById(R.id.btnRadio);

        list.setOnClickListener(this);
        check.setOnClickListener(this);
        radio.setOnClickListener(this);
    }
    public Dialog onCreateDialog(int id) {
        switch (id) {
            case LIST_ALERTDIALOG:

                AlertDialog.Builder builder = new AlertDialog.Builder(listdialogactivity.this)
                        .setTitle("Choose a Day")
                        .setItems(day_list, new DialogInterface.OnClickListener() {
                            @Override                            public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub                                Toast.makeText(getApplicationContext(), "The selected color is " + day_list[which], Toast.LENGTH_LONG).show();
                            }
                        });
                AlertDialog alertdialog = builder.create();
                return alertdialog;

            case CHECKBOX_ALERTDIALOG:

                AlertDialog.Builder builder1 = new AlertDialog.Builder(listdialogactivity.this)
                        .setTitle("Choose a Days")
                        .setMultiChoiceItems(day_check, null, new DialogInterface.OnMultiChoiceClickListener() {

                            @Override                            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// TODO Auto-generated method stub
//storing the checked state of the items in an array                                checked_state[which] = isChecked;
                            }
                        })
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {

                            @Override                            public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub                                String display_checked_days = "";
                                for (int i = 0; i < 3; i++) {
                                    if (checked_state[i] == true) {
                                        display_checked_days = display_checked_days + " " + day_check[i];
                                    }
                                }
                                Toast.makeText(listdialogactivity.this, "The selected day(s) is" + display_checked_days, Toast.LENGTH_LONG).show();

//clears the String used to store the displayed text                                display_checked_days = null;

//clears the array used to store checked state                                for (int i = 0; i < checked_state.length; i++) {
                                    if (checked_state[i] == true) {
                                        checked_state[i] = false;
                                    }
                                }

//used to dismiss the dialog upon user selection.                                dialog.dismiss();
                            }
                        });
                AlertDialog alertdialog1 = builder1.create();
                return alertdialog1;

            case RADIOBUTTON_ALERTDIALOG:

                AlertDialog.Builder builder2 = new AlertDialog.Builder(listdialogactivity.this)
                        .setTitle("Choose a Day")
                        .setSingleChoiceItems(day_radio, -1, new DialogInterface.OnClickListener() {

                            @Override                            public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub                                Toast.makeText(getApplicationContext(),
                                        "The selected Day is " + day_radio[which], Toast.LENGTH_LONG).show();

//dismissing the dialog when the user makes a selection.                                dialog.dismiss();
                            }
                        });
                AlertDialog alertdialog2 = builder2.create();
                return alertdialog2;
        }
        return null;

    }

    @Override    protected void onPrepareDialog(int id, Dialog dialog) {
// TODO Auto-generated method stub
        switch (id) {
            case CHECKBOX_ALERTDIALOG:
                AlertDialog prepare_checkbox_dialog = (AlertDialog) dialog;
                ListView list_checkbox = prepare_checkbox_dialog.getListView();
                for (int i = 0; i < list_checkbox.getCount(); i++) {
                    list_checkbox.setItemChecked(i, false);
                }
                break;
            case RADIOBUTTON_ALERTDIALOG:
                AlertDialog prepare_radio_dialog = (AlertDialog) dialog;
                ListView list_radio = prepare_radio_dialog.getListView();
                for (int i = 0; i < list_radio.getCount(); i++) {
                    list_radio.setItemChecked(i, false);
                }
                break;

        }
    }

    @Override    public void onClick(View v) {
// TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.btnList:
//triggering the List alertdialog                showDialog(LIST_ALERTDIALOG);
                break;
            case R.id.btnCheckBox:
//triggering the checkbox alertdialog                showDialog(CHECKBOX_ALERTDIALOG);
                break;
            case R.id.btnRadio:
//triggering the radio alertdialog                showDialog(RADIOBUTTON_ALERTDIALOG);
                break;

            default:
                break;
        }

    }
}

14 ) Date Picker Activity

public class datepickeractivity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_datepickeractivity);
    }
}

15 ) Custom Dialog Activity

public class customdialogactivity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customdialogactivity);

    }

}

16 ) Alert Dialog Activity

public class alertdialogactivity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alertdialogactivity);
    }
}

17 ) Progress Dialog Activity

public class progressdialogactivity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_progressdialogactivity);
    }
}

18 ) Time Picker Activity

public class timepickeractivity extends AppCompatActivity implements        View.OnClickListener {

    Button btnTimePicker;
    EditText txtTime;
    private int mHour, mMinute;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_timepickeractivity);

        txtTime=(EditText)findViewById(R.id.in_time);
        btnTimePicker=(Button)findViewById(R.id.btn_time);
        btnTimePicker.setOnClickListener(this);

    }

    @Override    public void onClick(View v) {
        final Calendar c = Calendar.getInstance();
        mHour = c.get(Calendar.HOUR_OF_DAY);
        mMinute = c.get(Calendar.MINUTE);

        // Launch Time Picker Dialog        TimePickerDialog timePickerDialog = new TimePickerDialog(this,
                new TimePickerDialog.OnTimeSetListener() {

                    @Override                    public void onTimeSet(TimePicker view, int hourOfDay,
                                          int minute) {

                        txtTime.setText(hourOfDay + ":" + minute);
                    }
                }, mHour, mMinute, false);
        timePickerDialog.show();
    }
}

Notes :

1 ) Require Build Gaddle

compile 'com.android.support:appcompat-v7:25.3.1'compile 'com.android.support.constraint:constraint-layout:1.0.2'compile 'com.android.support:design:25.3.1'

Comments

Popular posts from this blog

Post data with retrofit

1 ) Retrofit Client Class public class RetrofitClient { private static final String AUTH = "Basic " + Base64. encodeToString (( "belalkhan:123456" ).getBytes(), Base64. NO_WRAP ); private static final String BASE_URL = "http://simplifiedlabs.xyz/MyApi/public/" ; private static RetrofitClient mInstance ; private Retrofit retrofit ; private RetrofitClient() { OkHttpClient okHttpClient = new OkHttpClient.Builder() .addInterceptor( new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request original = chain.request(); Request.Builder requestBuilder = original.newBuilder() .addHeader( "Authorization" , AUTH ) .meth...

Android MVVM (Model View ViewModel) Design Pattern

DataBinding Pdf Link:-   https://drive.google.com/open?id=1uL0xK9EktcEWOke5vqUqp-rgqpA1ARR3 DataBinding References : -  https://www.youtube.com/watch?v=v4XO_y3RErI Steps for applying MVVM design pattern to Android Project  1 ) Applying the changes in build.gradle (App Level) file      android {     compileSdkVersion 27     defaultConfig {         applicationId "com.credencys.databindingwithrecyclerview"         minSdkVersion 15         targetSdkVersion 27         versionCode 1         versionName "1.0"         testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"     }     buildTypes {         release {             minifyEnabled false             proguardFiles ge...

Complex Retrofit Class With Database

URL:-  http://www.mocky.io/v2/ 5b936d6f33000011002061d3 1 )   Create   RetrofitClient(Base URL Class) import retrofit2.GsonConverterFactory; import retrofit2.Retrofit; public class RetrofitClient { //http://www.mocky.io/v2/5b936d6f33000011002061d3 public static final String API_BASE_URL = "http://www.mocky.io/v2/" ; public static Retrofit retrofit = null ; public static Retrofit getApiClient() { if ( retrofit == null ) { retrofit = new Retrofit.Builder().baseUrl( API_BASE_URL ).addConverterFactory(GsonConverterFactory . create ()).build(); } return retrofit ; } } 2 ) Create Retrofit ApiInterface Class(End URL Class) import com.example.discusit.complexretrofitdemo.model.Data; import retrofit2.Call; import retrofit2.http. GET ; public interface ApiInterface { @GET ( "5b936d6f33000011002061d3" ) Call<Data> getData(); } 3 ) Create Require Model Clas...