-1

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

Rana
  • 3
  • 2
  • @RyanM Yes, i think this is what i wanted but never google with these keywords Thanks anyways – Rana May 28 '20 at 03:17

2 Answers2

0

Design your layout with all 7 buttons, but based on a user-driven condition, hide the relevant button(s) from the layout with buttonMonday.setVisibility(View.GONE);. View.GONE will remove the view from the layout, whereas View.INVISIBLE will just hide it.

codebod
  • 686
  • 6
  • 17
  • So if i get it right, i should make edit button or something like settings where user can check or uncheck all 7 buttons. – Rana May 27 '20 at 21:22
  • Correct. This might be a one-time thing for which you store the settings in shared preferences or a database, or you may need to ask the user on each use of the app. – codebod May 27 '20 at 21:28
  • Another thing, The solution seems helpful but I don't know how it will go. Just to inquire if I had a problem where the number of max buttons is not fixed, is there any solution that the user can add the number of toggle buttons of his choice apart from me making if visible or invisible? Like if he goes to work the toggle will be true else false – Rana May 27 '20 at 21:30
  • If you have a fixed max, use my approach. If you really need more flexibility, see https://stackoverflow.com/a/2397869/13373270, but clearly this will require A LOT more work. – codebod May 27 '20 at 21:39
0

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);
}
Ryan M
  • 18,333
  • 31
  • 67
  • 74