0

If i'm click OK how to choose pick a value one service in radio button but different value.

void OpenDialogService() {

    closeLyt.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

    cancelTxt.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

    okTxt.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            dialog.dismiss();
            Bundle b    =   activity.getIntent().getExtras();
            if(b == null)
                b = new Bundle();
            b.putString("service_id", "1");
            startActivity(new Intent(activity, Step1.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtras(b));
            overridePendingTransition(R.anim.push_left_in,R.anim.push_left_out);
        }
    });

    dialog.show();
}

In this line for primary service :

b.putString("service_id", "1")

How i make conditional if service other give value :

b.putString("service_id", "2")

Pick Services

2 Answers2

0

You can get the selected item id of RadioGroup using, radioGroup.getCheckedRadioButtonId() and use if-else to choose different strings.

int id = radioGroup.getCheckedRadioButtonId();
if(id == radioButton1)
{
    b.putString("service_id", "1");
}
else if(id == radioButton1)
{
    b.putString("service_id", "2");
}
Bob
  • 13,447
  • 7
  • 35
  • 45
  • yes my friend i'm already used radio group now, before i only use radio button still 2 button choose, look this picture http://prnt.sc/azoqa8 Thanks ... but i have some problem,,, java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.RadioGroup.getCheckedRadioButtonId()' on a null object reference. – Septian Maulana May 03 '16 at 08:12
  • please look this, im so confuse for that error http://pastebin.com/BDdimhUY.. >/ – Septian Maulana May 03 '16 at 08:17
  • You should assign the radio group reference to the variable. RadioGroup radiogroup = findViewById(R.id.your_radio_group_ID); – Bob May 03 '16 at 08:26
  • yes bob already, my id radio group is radioService... but the error in runtime,,, in im execute OK button... unfortunately has stoped.. – Septian Maulana May 03 '16 at 08:37
  • The id of your RadioGroup is radioService. You should get the reference of that RadioGroup from the java class. RadioGroup radiogroup = (RadioGroup)findViewById(R.id.your_radio_group_ID); – Bob May 03 '16 at 08:44
  • then use that variable to get the checked button id. int id = radioGroup.getCheckedRadioButtonId(); – Bob May 03 '16 at 08:45
  • nah i got the error problem bob, im already reference radiogroup in java class,,, if im put the code int id = radioGroup.getCheckedRadioButtonId(); ,,, im always runtime error,,, if im hidden program runing correcly... error is : java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.RadioGroup.getCheckedRadioButtonId() on a null object reference at gomaid.co.id.Home$8.onClick how to solve it bob? – Septian Maulana May 03 '16 at 09:10
  • it says that the value of radioGroup is null. It has no reference to the RadioGroup. – Bob May 03 '16 at 09:37
  • ok wait bob,, here,,, Java file order : http://pastebin.com/xJQ7NtTX ... and this XML file order : http://pastebin.com/yFVTVGPB .... i try in documentation android radio button i see no false i do,,, confused >.< .. – Septian Maulana May 03 '16 at 09:50
  • can you move the line radioGroup = (RadioGroup) findViewById(R.id.radioService); to onCreate of the Activity, and try. – Bob May 03 '16 at 10:02
  • im already do bob,, but same result if i push ok button,,, runtime error,,, problem in getCheckedRadioButtonId(); i dont know why i cant get the value of checked button... but the button already checked use android:checked button... – Septian Maulana May 03 '16 at 10:09
  • I dont know. Maybe a workaround is to use ClickListener to the RadioGroup. When the RadioButton is clicked, you change some value, like boolean or int. refer here to set ClickListener to RadioGroup. http://stackoverflow.com/a/8323973/4586742 – Bob May 03 '16 at 10:15
  • oh my friend,,,,, i just forgot put "dialog" in final RadioGroup radioGroups = (RadioGroup) findViewById(R.id.radioService); ,,,,, the final code is final RadioGroup radioGroups = (RadioGroup) dialog.findViewById(R.id.radioService); ,,,,, lol,,, i dont know the bug not complete code,,, thanks a lot,,, you are always answer my question!,,, – Septian Maulana May 03 '16 at 12:18
0

This is code snippet how you can achieve this with the help of RadioGroups

Your dialog layout should look like this :-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.test.MainActivity" >

    <TextView
        android:id="@+id/headerTxt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:background="#ff669900"
        android:gravity="center_horizontal"
        android:padding="5dp"
        android:text="Pick Service"
        android:textColor="#FFF"
        android:textSize="20sp" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_margin="10dp"
        android:layout_marginTop="32dp" >

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/primaryTxt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:checked="true"
                android:text="Primary Service"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/primaryContentTxt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_below="@+id/primaryTxt"
                android:text=" -Dusting \n -Sweeping \n -Washing \n -Cleaning " />

            <RadioGroup
                android:id="@+id/radioGroup1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true" >

                <RadioButton
                    android:id="@+id/service_1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                     />

                <RadioButton
                    android:id="@+id/service_2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="50dp" />
            </RadioGroup>

            <TextView
                android:id="@+id/otherTxt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/primaryContentTxt"
                android:layout_marginTop="10dp"
                android:checked="true"
                android:gravity="left"
                android:text="Other Service(Soon)"
                android:textSize="18sp" />
        </RelativeLayout>
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/closeLyt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/darker_gray"
        android:weightSum="2" >

        <TextView
            android:id="@+id/cancelTxt"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="1dp"
            android:layout_weight="1"
            android:background="#fff"
            android:gravity="center"
            android:padding="5dp"
            android:text="CANCEL"
            android:textColor="#000"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/okTxt"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="1dp"
            android:layout_weight="1"
            android:background="#fff"
            android:gravity="center"
            android:padding="5dp"
            android:text="OK"
            android:textColor="#000"
            android:textSize="20sp" />
    </LinearLayout>

</LinearLayout>

And you can inflate this layout in custom dialog box and with the help of radio buttons you can choose service type

int serviceNumber = 0;

    void OpenDialogService() {

        final Dialog dialog = new Dialog(this);
        dialog.getWindow();
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.pop_new_order2);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);

        TextView headerTxt = (TextView) dialog.findViewById(R.id.headerTxt);
        TextView okTxt = (TextView) dialog.findViewById(R.id.okTxt);
        TextView cancelTxt = (TextView) dialog.findViewById(R.id.cancelTxt);
        TextView primaryTxt = (TextView) dialog.findViewById(R.id.primaryTxt);
        TextView otherTxt = (TextView) dialog.findViewById(R.id.otherTxt);
        TextView primaryContentTxt = (TextView) dialog.findViewById(R.id.primaryContentTxt);
        LinearLayout closeLyt = (LinearLayout) dialog.findViewById(R.id.closeLyt);

        // Radio Buttons

        final RadioGroup radio = (RadioGroup) dialog.findViewById(R.id.radioGroup1);
        radio.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                  View radioButton = radio.findViewById(checkedId);
                    int index = radio.indexOfChild(radioButton);

                    Toast.makeText(getApplicationContext(), "service" +index, 500).show();

                    serviceNumber = index+1;

            }
        });

        headerTxt.setText("Pick Services");

        //TODO change color here
        // headerTxt.setBackgroundResource(R.drawable.bg_dialog_header_success);
        // closeLyt.setBackgroundResource(R.color.selector_close_alert_dialog_success);

        closeLyt.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        cancelTxt.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        okTxt.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                Bundle b = getIntent().getExtras();
                if (b == null)
                    b = new Bundle();
                b.putString("service_id", String.valueOf(serviceNumber));

                Toast.makeText(getApplicationContext(), "service " + String.valueOf(serviceNumber), 500).show();

                //TODO Handle Activity Transition Here
                // startActivity(new Intent(MainActivity.this,
                // Step1.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtras(b));
                // overridePendingTransition(R.anim.push_left_in,
                // R.anim.push_left_out);
            }
        });

        dialog.show();
    }

Result

enter image description here

Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
  • thanks Hitesh Sahu i'm already try it,,, yes show the toast,,, but why if i put this code im always get error int selectedId = radioGroup.getCheckedRadioButtonId(); i want get checked button,,, but always error,,, – Septian Maulana May 03 '16 at 12:04
  • Because probably RadioGrooup is not decleared,have you took reference of radio group like RadioGroup radio = (RadioGroup) dialog.findViewById(R.id.radioGroup1); share your activity class and stack track if possible – Hitesh Sahu May 03 '16 at 12:08
  • oh my friend thanks a lot,,,,, i just not put "dialog" in final RadioGroup radioGroups = (RadioGroup) findViewById(R.id.radioService); ,,,,, the final code is final RadioGroup radioGroups = (RadioGroup) dialog.findViewById(R.id.radioService); ,,,,, thanks a lot! :)... im very frustased focus on conditional always failed,,, and bug is not put dialog ,,, :v,,, give me experience,,, thanks @hitesh Sahu,,, – Septian Maulana May 03 '16 at 12:17
  • and every code is conditional always right in first im write,,, problem only in not put "dialog",,, you save my day... ^-^ – Septian Maulana May 03 '16 at 12:20
  • I am glad I was able to help. – Hitesh Sahu May 03 '16 at 12:22
  • 1
    yeahh I can go home with comfortable now,,, i look every time stackoverlow i always to be silent reader,, but i know in this website, have great programmer in this world,,, i glad you can help me, :) – Septian Maulana May 03 '16 at 12:25