Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to improve selection criteria of your app using Radio button?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 465
    Comment on it

    Radio Button
    A radio button is a two-states button that can be either checked or unchecked. They are normally used together in a RadioGroup. When several radio buttons live inside a radio group, checking one radio button unchecks all the others.
    Example:-
    First create a layout

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout&#95;width="match&#95;parent"
        android:layout&#95;height="wrap&#95;content"
        android:orientation="vertical"
        android:padding="5dp" >
    
        <RadioGroup
            android:id="@+id/genderRadioGroup"
            android:layout&#95;width="match&#95;parent"
            android:layout&#95;height="wrap&#95;content"
            android:orientation="horizontal" >
    
            <RadioButton
                android:id="@+id/male"
                android:layout&#95;width="wrap&#95;content"
                android:layout&#95;height="wrap&#95;content"
                android:text="Male" />
    
            <RadioButton
                android:id="@+id/female"
                android:layout&#95;width="wrap&#95;content"
                android:layout&#95;height="wrap&#95;content"
                android:text="Female" />
        </RadioGroup>
    
        <Button
            android:id="@+id/knowGender"
            android:layout&#95;width="wrap&#95;content"
            android:layout&#95;height="wrap&#95;content"
    
            android:layout&#95;margin="5dp"
            android:text="Know Gender" />
    
    </LinearLayout>
    

    Now instantiate UI inside onCreate method of your activity class

    // instantiate UI
    maleRadioButton = (RadioButton) findViewById(R.id.male);
    femaleRadioButton = (RadioButton) findViewById(R.id.female);
    submitButton = (Button) findViewById(R.id.knowGender);
    

    Now handle click listener on submitButton

    submitButton.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    if(maleRadioButton.isChecked())
                    {
                        Toast.makeText(Test.this, "Selected gender is Male", Toast.LENGTH&#95;SHORT).show();
                    }
                    else
                    {
                        Toast.makeText(Test.this, "Selected gender is Female", Toast.LENGTH&#95;SHORT).show();
                    }
                }
            });
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: