So I am working on my college project and still a newbie in android. I want to represent the number of days the user goes to work in seven buttons(7 because 7 days in a week).
Since many of us don't work all seven days a week I want the user to choose and create days in a week representing buttons. Suppose if he works Monday-Friday he should be able to add 5 buttons.
Searched google and android's site but was not able to figure this out. Please provide some explanation as it will be helpful for a beginner like me. Thanks
This is not the only way, but maybe you can try this:
activity_main.xml
<androidx.constraintlayout.widget.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=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<CheckBox
android:id="@+id/sunday"
android:layout_width="120dp"
android:layout_marginLeft="60dp"
android:layout_marginStart="60dp"
android:layout_height="wrap_content"
android:text="Sunday"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<CheckBox
android:id="@+id/monday"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Monday"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginRight="60dp"
android:layout_marginEnd="60dp"
app:layout_constraintTop_toTopOf="parent" />
<CheckBox
android:id="@+id/tuesday"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Tuesday"
android:layout_marginTop="10dp"
android:layout_marginLeft="60dp"
android:layout_marginStart="60dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/sunday" />
<CheckBox
android:id="@+id/wednesday"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Wednesday"
android:layout_marginRight="60dp"
android:layout_marginEnd="60dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/monday" />
<CheckBox
android:id="@+id/thrusday"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Thrusday"
android:layout_marginTop="10dp"
android:layout_marginLeft="60dp"
android:layout_marginStart="60dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tuesday" />
<CheckBox
android:id="@+id/friday"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Friday"
android:layout_marginRight="60dp"
android:layout_marginEnd="60dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tuesday" />
<CheckBox
android:id="@+id/saturday"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Saturday"
android:layout_marginTop="10dp"
android:layout_marginLeft="60dp"
android:layout_marginStart="60dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/thrusday" />
</androidx.constraintlayout.widget.ConstraintLayout>
<Button
android:id="@+id/open"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open"
android:background="@color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
app:layout_constraintTop_toBottomOf="@+id/parent">
<Button
android:id="@+id/btnSunday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sunday"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnMonday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Monday"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnSunday" />
<Button
android:id="@+id/btnTuesday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Tuesday"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnMonday" />
<Button
android:id="@+id/btnWednesday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Wednesday"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnTuesday" />
<Button
android:id="@+id/btnThrusday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Thrusday"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnWednesday" />
<Button
android:id="@+id/btnFriday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Friday"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnThrusday" />
<Button
android:id="@+id/btnSaturday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Saturday"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnFriday" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity
public class MainActivity extends AppCompatActivity {
CheckBox sunday, monday, tuesday, wednesday, thrusday, friday, saturday;
Button btnSunday, btnMonday, btnTuesday, btnWednesday, btnThrusday, btnFriday, btnSaturday, open;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
init();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
final SharedPreferences.Editor editor = preferences.edit();
if(preferences.contains("sunday") && preferences.getBoolean("sunday",false) == true) {
sunday.setChecked(true);
btnSunday.setVisibility(View.VISIBLE);
}else {
sunday.setChecked(false);
btnSunday.setVisibility(View.GONE);
}
if(preferences.contains("monday") && preferences.getBoolean("monday",false) == true) {
monday.setChecked(true);
btnMonday.setVisibility(View.VISIBLE);
}else {
monday.setChecked(false);
btnMonday.setVisibility(View.GONE);
}
if(preferences.contains("tuesday") && preferences.getBoolean("tuesday",false) == true) {
tuesday.setChecked(true);
btnTuesday.setVisibility(View.VISIBLE);
}else {
tuesday.setChecked(false);
btnTuesday.setVisibility(View.GONE);
}
if(preferences.contains("wednesday") && preferences.getBoolean("wednesday",false) == true) {
wednesday.setChecked(true);
btnWednesday.setVisibility(View.VISIBLE);
}else {
wednesday.setChecked(false);
btnWednesday.setVisibility(View.GONE);
}
if(preferences.contains("thrusday") && preferences.getBoolean("thrusday",false) == true) {
thrusday.setChecked(true);
btnThrusday.setVisibility(View.VISIBLE);
}else {
thrusday.setChecked(false);
btnThrusday.setVisibility(View.GONE);
}
if(preferences.contains("friday") && preferences.getBoolean("friday",false) == true) {
friday.setChecked(true);
btnFriday.setVisibility(View.VISIBLE);
}else {
friday.setChecked(false);
btnFriday.setVisibility(View.GONE);
}
if(preferences.contains("saturday") && preferences.getBoolean("saturday",false) == true) {
saturday.setChecked(true);
btnSaturday.setVisibility(View.VISIBLE);
}else {
saturday.setChecked(false);
btnSaturday.setVisibility(View.GONE);
}
open.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (sunday.isChecked()){
btnSunday.setVisibility(View.VISIBLE);
editor.putBoolean("sunday", true);
editor.apply();
} else {
btnSunday.setVisibility(View.GONE);
editor.putBoolean("sunday", false);
editor.apply();
}
if (monday.isChecked()){
btnMonday.setVisibility(View.VISIBLE);
editor.putBoolean("monday", true);
editor.apply();
} else {
btnMonday.setVisibility(View.GONE);
editor.putBoolean("monday", false);
editor.apply();
}
if (tuesday.isChecked()){
btnTuesday.setVisibility(View.VISIBLE);
editor.putBoolean("tuesday", true);
editor.apply();
} else {
btnTuesday.setVisibility(View.GONE);
editor.putBoolean("tuesday", false);
editor.apply();
}
if (wednesday.isChecked()){
btnWednesday.setVisibility(View.VISIBLE);
editor.putBoolean("wednesday", true);
editor.apply();
} else {
btnWednesday.setVisibility(View.GONE);
editor.putBoolean("wednesday", false);
editor.apply();
}
if (thrusday.isChecked()){
btnThrusday.setVisibility(View.VISIBLE);
editor.putBoolean("thrusday", true);
editor.apply();
} else {
btnThrusday.setVisibility(View.GONE);
editor.putBoolean("thrusday", false);
editor.apply();
}
if (friday.isChecked()){
btnFriday.setVisibility(View.VISIBLE);
editor.putBoolean("friday", true);
editor.apply();
} else {
btnFriday.setVisibility(View.GONE);
editor.putBoolean("friday", false);
editor.apply();
}
if (saturday.isChecked()){
btnSaturday.setVisibility(View.VISIBLE);
editor.putBoolean("saturday", true);
editor.apply();
} else {
btnSaturday.setVisibility(View.GONE);
editor.putBoolean("saturday", false);
editor.apply();
}
}
});
}
public void init(){
sunday = findViewById(R.id.sunday);
monday = findViewById(R.id.monday);
tuesday = findViewById(R.id.tuesday);
wednesday = findViewById(R.id.wednesday);
thrusday = findViewById(R.id.thrusday);
friday = findViewById(R.id.friday);
saturday = findViewById(R.id.saturday);
btnSunday = findViewById(R.id.btnSunday);
btnMonday = findViewById(R.id.btnMonday);
btnTuesday = findViewById(R.id.btnTuesday);
btnWednesday = findViewById(R.id.btnWednesday);
btnThrusday = findViewById(R.id.btnThrusday);
btnFriday = findViewById(R.id.btnFriday);
btnSaturday = findViewById(R.id.btnSaturday);
open = findViewById(R.id.open);
}