Skip to content

Instantly share code, notes, and snippets.

@alitamoor65
Created April 2, 2018 14:49
Show Gist options
  • Save alitamoor65/d3f03bea7d7d2266281760ae8beb6964 to your computer and use it in GitHub Desktop.
Save alitamoor65/d3f03bea7d7d2266281760ae8beb6964 to your computer and use it in GitHub Desktop.
Radio Group Android Studio
<?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="jovial.etutorialscafe.radiobutton.MainActivity">
<TextView
android:gravity="center"
android:text="Rate the Application"
android:layout_width="368dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/radioGroup"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:id="@+id/radioGroup">
<RadioButton
android:id="@+id/excelentID"
android:text="Excelent"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/goodID"
android:text="Good"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/avdID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Average"/>
<RadioButton
android:id="@+id/poorID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Poor" />
</RadioGroup>
<Button
android:id="@+id/buttonID"
android:text="Sumbit rate"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/radioGroup"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>
package jovial.etutorialscafe.radiobutton;
import android.support.annotation.IdRes;
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.Toast;
public class MainActivity extends AppCompatActivity {
RadioGroup rate;
RadioButton exce,good,avg,poor;
String ratecomment = "";
Button buttonSumbit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rate = (RadioGroup)findViewById(R.id.radioGroup);
exce = (RadioButton)findViewById(R.id.excelentID);
good = (RadioButton)findViewById(R.id.goodID);
avg = (RadioButton)findViewById(R.id.avdID);
poor = (RadioButton)findViewById(R.id.poorID);
buttonSumbit = (Button)findViewById(R.id.buttonID);
rate.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
switch (rate.indexOfChild(findViewById(group.getCheckedRadioButtonId()))){
case 0:
ratecomment = "Excellent";
break;
case 1:
ratecomment = "Good";
break;
case 2:
ratecomment = "Average";
break;
case 3:
ratecomment = "Poor";
break;
}
}
});
buttonSumbit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, ratecomment + " : Ratting Submited", Toast.LENGTH_SHORT).show();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment