Septian Maulana Septian Maulana - 6 months ago 16
Java Question

How to Choose Radio Button Value using Java Android?

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

Answer

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

Comments