Hello , Guys Today in this article we will see how we use the radio button and how we will design it.We can design the radio button vertically or horizonally. We wish to use the radio button in the log in form to select the option like mail and female but today we will learn how to change the background color of the UI using the radio button.
Create new Project in Android studio
Choose "File", "New", select "New Project" and then click "Next". In the New Android Application window, enter your chosen Application, Project, and Package names and then choose "Next" and last finish. After creating the project, you will see two files, first activity_main and second MainActivity.java, then you have to paste the code given below activity_main and MainActivity.java.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#f9ffffff"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select your favourite Colour : "
android:textColor="#000"
android:textSize="20sp"
android:textStyle="bold" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/Red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:checked="true"
android:text="Red"
android:textColor="#154"
android:textSize="20sp"
android:textStyle="bold" />
<RadioButton
android:id="@+id/Green"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:checked="false"
android:text="Green"
android:textColor="#154"
android:textSize="20sp"
android:textStyle="bold" />
<RadioButton
android:id="@+id/Blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:checked="false"
android:text="Blue"
android:textColor="#154"
android:textSize="20sp"
android:textStyle="bold" />
<RadioButton
android:id="@+id/Yellow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:checked="false"
android:text="Yellow"
android:textColor="#154"
android:textSize="20sp"
android:textStyle="bold" />
<RadioButton
android:id="@+id/White"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:checked="false"
android:text="White"
android:textColor="#154"
android:textSize="20sp"
android:textStyle="bold" />
</RadioGroup>
<Button
android:id="@+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:background="#3f009e"
android:padding="10dp"
android:text="Submit"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
Here we have placed five radio buttons inside the RadioGroup which is inside a LinaerLayout whose orientation can be vertical or horizontal as per my choice. And in the last, we are putting a submit button, which will be selected in the radiobutton on color when pressed, it will be set in the background.
MainActivity.java
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class RadioColor extends AppCompatActivity {
RadioButton Red, Green, Blue, Yellow, White;
String selectedColor;
Button submit;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.radio_button);
Red = findViewById(R.id.Red);
Green = findViewById(R.id.Green);
Blue = findViewById(R.id.Blue);
Yellow = findViewById(R.id.Yellow);
White = findViewById(R.id.White);
submit = findViewById(R.id.submitButton);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Green.isChecked()) {
selectedColor = Green.getText().toString();
getWindow().getDecorView().setBackgroundColor(Color.GREEN);
} else if (White.isChecked()) {
getWindow().getDecorView().setBackgroundColor(Color.DKGRAY);
selectedColor = White.getText().toString();
} else if (Red.isChecked()) {
getWindow().getDecorView().setBackgroundColor(Color.RED);
selectedColor = Red.getText().toString();
} else if (Yellow.isChecked()) {
getWindow().getDecorView().setBackgroundColor(Color.YELLOW);
selectedColor = Yellow.getText().toString();
} else if (Blue.isChecked()) {
getWindow().getDecorView().setBackgroundColor(Color.BLUE);
selectedColor = Blue.getText().toString();
}
Toast.makeText(getApplicationContext(), selectedColor, Toast.LENGTH_LONG).show();
// print the value of selected Colour
}
});
}
}
Here we have created five variables of type RadioButton which are named according to color and a variable of type String selectedColor in which we will take the color code string by getTex ().
Then we print getWindow (). GetDecorView (). SetBackgroundColor (Color.GREEN) to print the color in the background; We will run the code and on the button we can print the color according to the if else condition and in the last we will print a toast which will be the color we have selected in selectedColor.
I hope that this article will be very useful for you.
Here are some screenshot of how this app works.
I hope this article is very useful for you.
Thank you ....
0 Comments
Please do not enter any spam link in the comment box
Emoji