Android App Development With AndroidCource: RadioButton
Showing posts with label RadioButton. Show all posts
Showing posts with label RadioButton. Show all posts

Saturday, 30 June 2018

Android Listview RadioButton Example

Listview with radiobutton to use of single selection in listview item.you can not multiple item to selected.it can use single selection and change your radio button color in selection mode.when you can select item to display message show to fire click event in list item to perticular selection position to get item.
Listview to use of custom layout or build in layout.custom layout to use of design in xml file layout and build in layout to already desing available. In this example to use of build in layout of single selection item layout.

MainActivity.xml

<?xml version="1.0" encoding="utf-8"?&gt;
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>
</RelativeLayout>

MainActivity.java

package com.example.bhaumik.listviewradiobutton;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    ListView listView;
    ArrayList<String> list;
    ArrayAdapter adapter;
    String[] version = {"Aestro","Blender","CupCake","Donut","Eclair","Froyo","GingerBread","HoneyComb","IceCream Sandwich",
    "Jelly Bean","Kitkat","Lolipop","Marshmallow","Nought","Oreo"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = findViewById(R.id.list_view);

        list = new ArrayList<>();

        for (int i = 0;i<version.length;i++){

            list.add(version[i]);

        }
        listView.setChoiceMode(listView.CHOICE_MODE_SINGLE);

        adapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_single_choice,list);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                Toast.makeText(MainActivity.this, "Selected -> " + version[i], Toast.LENGTH_SHORT).show();
            }
        });
    }
}
Listview Single Choice in android Example.,Listview radiobutton in android

Monday, 21 August 2017

RadioButton Example in Android

RadioButtonActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:id="@+id/activity_radio_button"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:paddingBottom="@dimen/activity_vertical_margin"
              android:paddingLeft="@dimen/activity_horizontal_margin"
              android:paddingRight="@dimen/activity_horizontal_margin"
              android:paddingTop="@dimen/activity_vertical_margin"
              android:orientation="vertical"
              tools:context="com.bhaumik.programmingstudy.RadioButtonActivity">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Select Fruits Item"
            android:textAppearance="?android:textAppearanceLarge"
            android:layout_gravity="center_horizontal"
    />
    <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/apple"
                android:text="Apple"
                android:textSize="20dp"
                android:layout_gravity="center_horizontal"
                android:onClick="SelectFruit"
        />
        <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/orange"
                android:text="Orange"
                android:textSize="20dp"
                android:layout_gravity="center_horizontal"
                android:onClick="SelectFruit"
        />
        <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/cheery"
                android:text="Cheery"
                android:textSize="20dp"
                android:layout_gravity="center_horizontal"
                android:onClick="SelectFruit"
        />
    </RadioGroup>
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="hello"
            android:id="@+id/tv_radio_result"
            android:textAppearance="?android:textAppearanceLarge"
            android:layout_gravity="center_horizontal"
            android:enabled="false"/>

</LinearLayout>
RadioButtonActivity.java
package com.bhaumik.programmingstudy;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;

public class RadioButtonActivity extends AppCompatActivity {

    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio_button);
        textView = (TextView)findViewById(R.id.tv_radio_result);
    }
    public void SelectFruit(View view)
    {
        boolean checked = ((RadioButton)view).isChecked();
        switch (view.getId())
        {
            case R.id.apple:
                if (checked)
                {
                    textView.setText("You Select Apple...");
                    textView.setEnabled(true);
                }
                else
                {
                    textView.setEnabled(false);
                }
                break;
            case R.id.orange:
                if (checked)
                {
                    textView.setText("You Select Orange...");
                    textView.setEnabled(true);
                }
                else
                {
                    textView.setEnabled(false);
                }
                break;
            case R.id.cheery:
                if (checked)
                {
                    textView.setText("You Select Cheery...");
                    textView.setEnabled(true);
                }
                else
                {
                    textView.setEnabled(false);
                }
                break;
        }
    }
}

Ads