Custom Radio Buttons example in android

Custom Radio Buttons example in android - Hello friend inabnomaniiyaha, In the article that you read this time with the title Custom Radio Buttons example in android, we have prepared this article well for you to read and take information in it. hopefully the contents of the post Artikel android-example,what we write you can understand. Alright, happy reading.

Judul : Custom Radio Buttons example in android
link : Custom Radio Buttons example in android

Baca juga


Custom Radio Buttons example in android

Custom Radio Buttons example in android

Create One XML file in "res/drawable/rbtn_selector.xml" add below XML code for Radio Button background

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">


    <item android:state_checked="true"><shape>

            <solid android:color="#1c5fab" />


            <stroke android:width="1dp" android:color="#1c5fab" />

        </shape></item>

    <item android:state_checked="false"><shape android:shape="rectangle">

            <solid android:color="#ffffff" />


            <stroke android:width="1dp" android:color="#1c5fab" />

        </shape></item>


</selector>

res/drawable

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">


    <item android:drawable="@drawable/bg_radio_button_checked" android:state_checked="true"/>

    <item android:drawable="@drawable/bg_radio_button_unchecked" android:state_checked="false"/>


</selector>

Create One XML file in "res/drawable/rbtn_textcolor_selector.xml"

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">


    <item android:state_checked="true" android:color="#ffffffff"/>

    <item android:color="#ff1c5fab"/>


</selector>

create XML file in layout folder "res/layout/activity_customradiobutton.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"

    android:paddingTop="10dp" >


    <RadioGroup

        android:id="@+id/radioGroup1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_marginTop="10dp"

        android:gravity="center"

        android:orientation="horizontal" >


        <RadioButton

            android:id="@+id/radioAndroid"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:background="@drawable/rbtn_selector"

            android:button="@null"

            android:checked="true"

            android:gravity="center"

            android:padding="5dp"

            android:text="Android"

            android:textColor="@drawable/rbtn_textcolor_selector" />


        <RadioButton

            android:id="@+id/radioiPhone"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:background="@drawable/rbtn_selector"

            android:button="@null"

            android:gravity="center"

            android:padding="5dp"

            android:text="iPhone"

            android:textColor="@drawable/rbtn_textcolor_selector" />


        <RadioButton

            android:id="@+id/radioWindows"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:background="@drawable/rbtn_selector"

            android:button="@null"

            android:gravity="center"

            android:padding="5dp"

            android:text="Windows"

            android:textColor="@drawable/rbtn_textcolor_selector" />

    </RadioGroup>


</LinearLayout>

Create on activity java file "CustomRadioButtonActivity.java"

public class CustomRadioButtonActivity extends Activity

{


 private RadioGroup radioGroup1;


 @Override

 protected void onCreate(Bundle savedInstanceState) 

 {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_customradiobutton);


  radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);


  // Checked change Listener for RadioGroup 1

  radioGroup1.setOnCheckedChangeListener(new OnCheckedChangeListener() 

  {

   @Override

   public void onCheckedChanged(RadioGroup group, int checkedId) 

   {

    switch (checkedId) 

    {

    case R.id.radioAndroid:

     Toast.makeText(getApplicationContext(), "Android RadioButton checked", Toast.LENGTH_SHORT).show();

     break;

    case R.id.radioiPhone:

     Toast.makeText(getApplicationContext(), "iPhone RadioButton checked", Toast.LENGTH_SHORT).show();

     break;

    case R.id.radioWindows:

     Toast.makeText(getApplicationContext(), "windows RadioButton checked", Toast.LENGTH_SHORT).show();

     break;

    default:

     break;

    }

   }

  });

 }

}

Screen:

Custom Radio Buttons example in android


That's the articleCustom Radio Buttons example in android

That's it for the article Custom Radio Buttons example in android this time, hopefully can be useful for all of you. well, see you in another article post.

You are now reading the articleCustom Radio Buttons example in android with link addresshttps://inabnonapudyawanabing.blogspot.com/2020/11/custom-radio-buttons-example-in-android.html

0 Response to "Custom Radio Buttons example in android"

Post a Comment

Tips Tricks for Android Phone

Tips & Tricks for Android Phone is a free android app and Collection of Tips and Tricks related to using your android mobile device lik...