Twitter Facebook Delicious Digg Stumbleupon Favorites More

Saturday 17 June 2017

Radio Button Android Studio

JAVA CODE

package com.example.viraj.blog_8_radiobutton;

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

public class MainActivity extends AppCompatActivity {
private RadioGroup rgp;
    private RadioButton rb;
    private TextView txt1;
    private Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rgp=(RadioGroup)findViewById(R.id.id_rb_group);
        txt1=(TextView)findViewById(R.id.id_textdisplay) ;
        btn=(Button)findViewById(R.id.id_button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int abcchecked=rgp.getCheckedRadioButtonId();
                rb=(RadioButton)findViewById(abcchecked);
                txt1.setText(rb.getText());

            }
        });

    }
}

XML CODE
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="com.example.viraj.blog_8_radiobutton.MainActivity">


    <RadioGroup
        android:id="@+id/id_rb_group"
        android:layout_width="158dp"
        android:layout_height="151dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_editor_absoluteX="35dp"
        tools:layout_editor_absoluteY="106dp">

        <RadioButton
            android:id="@+id/id_rb_1"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:text="APPLE" />

        <RadioButton
            android:id="@+id/id_rb_2"
            android:layout_width="wrap_content"
            android:layout_height="51dp"
            android:text="MANGO" />
    </RadioGroup>

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

    <Button
        android:id="@+id/id_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:text="Button"
        app:layout_constraintBottom_toTopOf="@+id/id_textdisplay"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/id_rb_group" />

    <TextView
        android:id="@+id/id_textdisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="39dp" />
</android.support.constraint.ConstraintLayout>



Share:

0 comments:

Post a Comment

Search This Blog

Popular Posts

Pages

how to make crores from 1 lakh in stock markets in 1 year

how to make crores from 1 lakh in stock markets in 1 year

Blogger Tutorials

Blogger Templates

Sample Text

Copyright © ANDROID TUTORIAL CODE | Powered by Blogger
Design by SimpleWpThemes | Blogger Theme by NewBloggerThemes.com & Distributed By Protemplateslab