Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a custom dialog with radio buttons list

I've got a method in which i have a list of values:

     /**      * ISO      * */     public void getISO(View view) {         // Open dialog with radio buttons         List<String> supported_isos = preview.getSupportedISOs();         SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);         String current_iso = sharedPreferences.getString(MainActivity.getISOPreferenceKey(), "auto");      } 

This method is enjected on onClick() of a ImageButton:

android:onClick="getISO" 

But i need to rapresent this list in a dialog with radio buttons. Possibly the preference values should be already selected in the dialog.. Is it possible?

like image 803
Atlas91 Avatar asked Sep 11 '15 10:09

Atlas91


People also ask

How do I create a custom dialogue?

This example demonstrate about how to make custom dialog in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I create a dynamic radio button?

For creating dynamic RadioButton, we need to use android. view. ViewGroup. LayoutParams which configures the width and height of views and implements setOnCheckedChangeListener() method of RadioGroup class.

What are the three buttons that can be added to a dialog?

There are three functions for adding Buttons to Android Dialog, setPositiveButton(int textId, DialogInterface.


2 Answers

best and easy way......

void dialog(){          AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);         //alt_bld.setIcon(R.drawable.icon);         alt_bld.setTitle("Select a Group Name");         alt_bld.setSingleChoiceItems(grpname, -1, new DialogInterface                 .OnClickListener() {             public void onClick(DialogInterface dialog, int item) {                 Toast.makeText(getApplicationContext(),                         "Group Name = "+grpname[item], Toast.LENGTH_SHORT).show();                 dialog.dismiss();// dismiss the alertbox after chose option              }         });         AlertDialog alert = alt_bld.create();         alert.show();   ///// grpname is a array where data is stored...        } 
like image 75
Abhisek Avatar answered Sep 21 '22 07:09

Abhisek


Call showRadioButtonDialog() from the button.

This is just an example:

private void showRadioButtonDialog() {   // custom dialog    final Dialog dialog = new Dialog(this);   dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);   dialog.setContentView(R.layout.dialog_layout);   List<String> stringList=new ArrayList<>();  // here is list       for(int i=0;i<2;i++) {           if (i==0){               stringList.add("Number Mode");           }else {               stringList.add("Character Mode");           }        }        RadioGroup rg = (RadioGroup) dialog.findViewById(R.id.radioGroup);        for(int i=0;i<stringList.size();i++){             RadioButton rb=new RadioButton(this); // dynamically creating RadioButton and adding to RadioGroup.             rb.setText(stringList.get(i));             rg.addView(rb);       } } 

Your layout view might be:radiobutton_dialog.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">       <RadioGroup           android:id="@+id/radio_group"           android:layout_width="wrap_content"           android:layout_height="match_parent"           android:layout_gravity="center_vertical"           android:orientation="vertical">     </RadioGroup>  </LinearLayout> 

enter image description here

Note: you can customize your dialog view (like setting title, message etc.)

Edit: To retrieving value of the selected RadioButton you have to implement setOnCheckedChangeListener listener for your RadioGroup as :

 rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {              @Override             public void onCheckedChanged(RadioGroup group, int checkedId) {                  int childCount = group.getChildCount();                  for (int x = 0; x < childCount; x++) {                     RadioButton btn = (RadioButton) group.getChildAt(x);                     if (btn.getId() == checkedId) {                          Log.e("selected RadioButton->",btn.getText().toString());                      }                  }             }         }); 
like image 22
Rustam Avatar answered Sep 24 '22 07:09

Rustam