You are on page 1of 4

How to check radiobuttun with options

How to check radiobuttun with options

<?xml version="1.0" encoding="utf-8"?> <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">

<RadioGroup android:id="@+id/G" android:layout_width="match_parent"

android:layout_height="133dp"

android:layout_alignParentStart="true"

android:layout_alignParentTop="true"

android:orientation="horizontal">

<RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentTop="true"

android:layout_marginStart="113dp"

android:text="A" android:textSize="40sp" />

<RadioButton

android:layout_width="50dp"

android:layout_height="wrap_content"

android:layout_alignParentStart="true"

android:layout_alignParentTop="true"

android:text="B" android:textSize="40sp" />

<RadioButton

android:layout_width="50dp"

android:layout_height="wrap_content"

android:layout_alignParentStart="true"

android:layout_alignParentTop="true"

android:text="C" android:textSize="40sp" />

<RadioButton

android:layout_width="50dp"

android:layout_height="wrap_content"

android:layout_alignParentStart="true"

android:layout_alignParentTop="true"

android:text="D" android:textSize="40sp" />

</RadioGroup>

</RelativeLayout>

Main.java

package com.example.androidapps.myapplicationre;

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

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {

RadioGroup G;

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

G.findViewById(R.id.G); G.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId){ case 1:

{

Toast.makeText( MainActivity.this, "A",Toast.LENGTH_SHORT).show(); break;

}

case 2:

{

Toast.makeText(MainActivity.this,"B",Toast.LENGTH_SHORT).show();

break;

}

case 3:

{

Toast.makeText(MainActivity.this,"C",Toast.LENGTH_SHORT).show();

break;

}

case 4:

{

Toast.makeText(MainActivity.this,"C",Toast.LENGTH_SHORT).show();

}

}

});

}

}

break;

}

editeeted xml

<?xml version="1.0" encoding="utf-8"?> <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">

<RadioGroup

android:weightSum="20"

android:id="@+id/G"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

<RadioButton

android:layout_weight="5"

android:layout_width="match_parent" android:layout_height="wrap_content" android:text="A" android:textSize="40sp" />

<RadioButton

android:layout_weight="5"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignParentStart="true"

android:layout_alignParentTop="true"

android:text="B" android:textSize="40sp" />

<RadioButton

android:layout_weight="5"

android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:text="C" android:textSize="40sp" />

<RadioButton

android:layout_weight="5"

android:layout_width="match_parent" android:layout_height="wrap_content" android:text="D" android:textSize="40sp" />

</RadioGroup>

</RelativeLayout>

</ RelativeLayout >