Twitter Facebook Delicious Digg Stumbleupon Favorites More

Wednesday 22 November 2017

Radio Button direct Click

package com.example.viraj.radiobuton;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private RadioGroup radioGroup;
    private RadioButton radioButton;
   

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioGroup = (RadioGroup) findViewById(R.id.id_radioGroup);
       
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                radioButton = (RadioButton) findViewById(i);

                switch (radioButton.getId()) {
                    case R.id.id_yes: {
                        if (radioButton.isChecked()) {
                            Toast.makeText(getApplicationContext(), "YES I AM COMING", Toast.LENGTH_LONG).show();
                        }

                    }
                    break;
                    case R.id.id_no: {
                        if (radioButton.isChecked()) ;
                        {
                            Toast.makeText(getApplicationContext(), "NO I AM NOT COMING", Toast.LENGTH_LONG).show();
                        }
                    }
                    break;
                    case R.id.id_maybe: {
                        if (radioButton.isChecked()) ;
                        {
                            Toast.makeText(getApplicationContext(), "MAY BE COMING", Toast.LENGTH_LONG).show();
                        }
                    }
                    break;
                }
            }
        });


    }
}


xml file

<?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"
    android:background="@drawable/one"
    tools:context="com.example.viraj.radiobuton.MainActivity">

    <RadioGroup
        android:id="@+id/id_radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="52dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView">

        <RadioButton
            android:id="@+id/id_yes"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="YES" />

        <RadioButton
            android:id="@+id/id_no"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="NO" />

        <RadioButton
            android:id="@+id/id_maybe"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="MAY BE" />
    </RadioGroup>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="76dp"
        android:text="ARE YOU COMING ...."
        android:textColor="@color/colorPrimaryDark"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.503"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</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