sai ram
sai ram

Reputation: 2129

Selecting only one RadioButton at a time

When I click on TextView reference I am getting AlertDialog builder in that I am showing a list of employeesNames with RadioButtons. Now when I check the wrong Employee and I want to select another employee, the wrong employee name should be unchecked, and by selecting one Employee, the ok Button should be enabled.

delivery.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LayoutInflater inflater1 = (LayoutInflater) ct.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v1 = inflater1.inflate(R.layout.activity_employees_list_for_pop_up, null);
            RadioButton employeechecked = (RadioButton) v1.findViewById(R.id.employeeChecked);
            final Button ok = (Button) v1.findViewById(R.id.do_ok);
            Button cancle = (Button) v1.findViewById(R.id.do_cancle);
            ok.setEnabled(false);
            listView = (ListView) v1.findViewById(R.id.employeePopUpList);
            employeePopUpAdapter = new EmployeePopUpAdapter(ct, employeeIdNameBeans);

            employeechecked.setOnCheckedChangeListener(new );


            listView.setAdapter(employeePopUpAdapter);

            AlertDialog.Builder builder;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                builder = new AlertDialog.Builder(ct, android.R.style.Theme_Material_Dialog_Alert);
            } else {
                builder = new AlertDialog.Builder(ct);
            }
            builder.setView(v1);
            builder.create().show();
            clearListView();
            update();
        }
    });

Please help me with the coding don't post theories that I can not understand I am a beginner.

Upvotes: 0

Views: 1630

Answers (1)

Saravanakumar
Saravanakumar

Reputation: 93

You can try RadioGroup rather than RadioButton.

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/a"
    android:onClick="onRGClick">

    <RadioButton
        android:text="Normal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/a1" />

    <RadioButton
        android:text="Tidak normal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/a2" />

</RadioGroup>

and try the setOnCheckedChangeListener method for selection and retrieve the selected item

radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == R.id.a1) {
            // do your stuff
        } else if (checkedId == R.id.a2) {
            // do your stuff
        }
    }
});

Upvotes: 1

Related Questions