First we are create a new project in android studio or paste code where want to use this function.First of all we will create a project in android studio or where we want to use this method we will paste it there.
activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="RadioButton "
android:textColor="@color/purple_500"
android:textSize="20sp"
android:textStyle="bold" />
<RadioGroup
android:id="@+id/radio_Group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Default" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Night Mode" />
</RadioGroup>
<Button
android:id="@+id/btn_summit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Summit" />
</LinearLayout>
MainActivitypackage com.example.darkmode;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private RadioGroup radioGroup;
private RadioButton radioButton1, radioButton2;
private Button btn;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio_button);
radioGroup = findViewById(R.id.radio_Group);
radioButton1 = findViewById(R.id.radioButton1);
radioButton2 = findViewById(R.id.radioButton2);
btn = findViewById(R.id.btn_summit);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
radioButton1.setChecked(sharedPreferences.getBoolean("btn1", false));
radioButton2.setChecked(sharedPreferences.getBoolean("btn2", false));
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (radioButton1.isChecked()) {
editor.putBoolean("btn1", true);
editor.apply();
radioButton1.setChecked(true);
Toast.makeText(RadioActivity.this, "Radio button 1", Toast.LENGTH_SHORT).show();
}
else
{
editor.putBoolean("btn1", false);
editor.apply();
radioButton1.setChecked(false);
} if (radioButton2.isChecked()) {
editor.putBoolean("btn2", true);
editor.apply();
radioButton2.setChecked(true);
Toast.makeText(RadioActivity.this, "Radio button 2", Toast.LENGTH_SHORT).show();
}
else{
editor.putBoolean("btn2", false);
editor.apply();
radioButton2.setChecked(false);
}
}
});
}
}
Thanks ....
0 Comments
Please do not enter any spam link in the comment box
Emoji