how to use the radio button in android with source code

 how to use the radio button in android.

android listview radio button.

Radio button in android.

In the blog post, we store the radio button value in the SQLite database and retrieve it and show it in list view.

this widget is used for one option selection

In this blog pot, we will create two radio buttons with SQLite Database and store its value in listview.

We will create a radio button data display in listview.

Using if condition we will be able to the two-button status which is either checked or unchecked. 

radio buttons in android


First of all, we will create a database class file. 

DBmain.java

package com.pd.radiobuttonlistview;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class DBmain extends SQLiteOpenHelper {
    public static final String DB="student.db";
    public static final int VER=1;
    public DBmain(@Nullable Context context) {
        super(context, DB, null, VER);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String qauery="create table studenttable(id integer primary key,gender text)";
        db.execSQL(qauery);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String qauery="drop table if exists studenttable";
db.execSQL(qauery);
onCreate(db);
    }
}

After creating the database class file, we will make radio buttons in the default activity XML file. 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Select Gender"
            android:textSize="25dp" />

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

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <Button
                android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Submit" />

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="display" />
        </LinearLayout>
    </RadioGroup>
</LinearLayout>

Now, type logic in the java class file. 

MainActivity.java

MainActivity.java
package com.pd.radiobuttonlistview;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
DBmain dBmain;
SQLiteDatabase sqLiteDatabase;
RadioButton radioButton,radioButton2;
Button button,button2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dBmain=new DBmain(MainActivity.this);
        findid();
        insertData();
    }

    private void insertData() {
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ContentValues contentValues=new ContentValues();
                if (radioButton.isChecked()){
                    contentValues.put("gender",radioButton.getText().toString());
                }else {
                    contentValues.put("gender",radioButton2.getText().toString());
                }
                sqLiteDatabase=dBmain.getWritableDatabase();
                Long rec=sqLiteDatabase.insert("studenttable",null,contentValues);
                if (rec!=null){
                    Toast.makeText(MainActivity.this, "Data inserted", Toast.LENGTH_SHORT).show();
                }else {
                    AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
                    builder.setTitle("Error");
                    builder.setMessage("Data not Inserted");
                    builder.setPositiveButton("OK",null);
                    builder.setCancelable(true);
                    builder.show();
                }
            }
        });
        //display dta when click display button
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,DisplayData.class);
                startActivity(intent);
            }
        });
    }

    private void findid() {
        radioButton=(RadioButton)findViewById(R.id.radioButton);
        radioButton2=(RadioButton)findViewById(R.id.radioButton2);
        button=(Button)findViewById(R.id.button);
        button2=(Button)findViewById(R.id.button2);
    }
}

Now, take a new activity named DisplayData.java 
In the new Activity XML file, we will create a listview. 

activity_display_data.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".DisplayData">

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

Now, we want to show all data, so we will type some code in 

DisplayData.java

DisplayData.java
package com.pd.radiobuttonlistview;

import androidx.appcompat.app.AppCompatActivity;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class DisplayData extends AppCompatActivity {
DBmain dBmain;
SQLiteDatabase sqLiteDatabase;
ListView listView;
String[]name;
int[]id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_data);
        dBmain=new DBmain(DisplayData.this);
        findid();
        displayData();
    }

    private void displayData() {
        sqLiteDatabase=dBmain.getReadableDatabase();
        Cursor cursor=sqLiteDatabase.rawQuery("select *from studenttable",null);
        if (cursor.getCount()>0){
            id=new int[cursor.getCount()];
            name=new String[cursor.getCount()];
            int i=0;
            while (cursor.moveToNext()){
                id[i]=cursor.getInt(0);
                name[i]=cursor.getString(1);
                i++;
            }

            Custom custom=new Custom();
            listView.setAdapter(custom);
        }
    }

    private void findid() {
        listView=(ListView)findViewById(R.id.lv);
    }

    private class Custom extends BaseAdapter {
        @Override
        public int getCount() {
            return name.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView textView;
          convertView= LayoutInflater.from(DisplayData.this).inflate(R.layout.singledata,parent,false);
           textView=convertView.findViewById(R.id.displayname);
           textView.setText(name[position]);
            return convertView;
        }
    }
}

Now, we will create a single XML file to show data in text view. 

singledata.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">

    <TextView
        android:id="@+id/displayname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textSize="24dp" />
</LinearLayout>

AndroidManifest.xml

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.pd.radiobuttonlistview">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.RadioButtonListview">
        <activity android:name=".DisplayData"></activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

watch video 
https://youtu.be/ikzoNTf72XM 

SUBSCRIBE to download 

Download Source code
Post a Comment (0)
Previous Post Next Post