How to save the visibility of a button

1

I wanted to know what would be the best way to save the visibility of a button in shared preferences. I have several buttons and I need that some are visible others not according to what is chosen and when re-entering they stay that way

This is the way I was doing it but it is not saved

    public class MainActivity extends Activity {

    Button b1, b2;
    TextView tv1;
    private static final String boton1_Visible = "boton1_Visible";
    private static final String boton2_Visible = "boton2_Visible";
    private SharedPreferences mSharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        b1=(Button)findViewById(R.id.b1);
        b2=(Button)findViewById(R.id.b2);

        mSharedPreferences = getPreferences(Context.MODE_PRIVATE);

        boton1_Visible.setVisibility((mSharedPreferences.getBoolean(boton1_Visible, true)
                ? View.INVISIBLE : View.VISIBLE));

        boton2_Visible.setVisibility((mSharedPreferences.getBoolean(boton2_Visible, true)
                ? View.VISIBLE : View.INVISIBLE));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void a(View view){

        boton1_Visible.setVisibility(View.INVISIBLE);

        boolean visibility1 = boton1_Visible.getVisibility() == View.INVISIBLE;
        boton1_Visible.setVisibility(visibility1 ? View.INVISIBLE : View.VISIBLE);

        mSharedPreferences.edit().putBoolean(boton1_Visible, visibility1).apply();

        boton2_Visible.setVisibility(View.VISIBLE);

        boolean visibility2 = boton2_Visible.getVisibility() == View.VISIBLE;
        boton2_Visible.setVisibility(visibility2 ? View.VISIBLE : View.INVISIBLE);

        mSharedPreferences.edit().putBoolean(boton2_Visible, visibility2).apply();
    }
}
    
asked by Agustin Val 08.12.2016 в 00:32
source

2 answers

1

You must differentiate which button is receiving the event to change its visibility and / or change the preferences state.

I hope that you serve as a friend, greetings

public class MainTestActivity extends AppCompatActivity implements View.OnClickListener {

    Button b1, b2;
    TextView tv1;
    private static final String boton1_Visible = "boton1_Visible";
    private static final String boton2_Visible = "boton2_Visible";
    private SharedPreferences mSharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_2);

        b1=(Button)findViewById(R.id.b1);
        b2=(Button)findViewById(R.id.b2);

        mSharedPreferences = getPreferences(Context.MODE_PRIVATE);

        boolean vB1 = mSharedPreferences.getBoolean(boton1_Visible, true);


        b1.setVisibility((vB1     ? View.VISIBLE : View.INVISIBLE));
        b1.setOnClickListener(this);

        boolean vB2 = (mSharedPreferences.getBoolean(boton2_Visible, true));
        b2.setVisibility(vB2 ? View.VISIBLE : View.INVISIBLE);
        b2.setOnClickListener(this);
    }



    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.b1:
                boolean visibility1 = b1.getVisibility() == View.VISIBLE;
                b1.setVisibility(visibility1 ? View.INVISIBLE : View.INVISIBLE);
                mSharedPreferences.edit().putBoolean(boton1_Visible, !visibility1).apply();

                break;
            case R.id.b2:

                boolean visibility2 = b2.getVisibility() == View.VISIBLE;
                b2.setVisibility(visibility2 ? View.INVISIBLE : View.VISIBLE);
                mSharedPreferences.edit().putBoolean(boton2_Visible, !visibility2).apply();
                break;
        }
    }
}
    
answered by 08.12.2016 / 02:20
source
2

I suggest two methods, to save and get the value.

private String PREFS_KEY = "mispreferencias";

public void guardaValorPreferencia(Context context, String key, boolean value) {
    SharedPreferences settings = context.getSharedPreferences(PREFS_KEY, MODE_PRIVATE);
    SharedPreferences.Editor editor;
    editor = settings.edit();
    editor.putBoolean(key, value);
    editor.commit();
}



public boolean obtieneValorPreferencia(Context context, String key) {
    SharedPreferences preferences = context.getSharedPreferences(PREFS_KEY, MODE_PRIVATE);
    return  preferences.getBoolean(key, "");
}

To save and obtain the values that correspond to the visibility of the buttons, you would do the following, using the previous methods:

I guess that the a() method is a method defined in the layout when you click on the buttons, then:

public void a(View view){

    boton1_Visible.setVisibility(View.INVISIBLE);

    boolean visibility1 = boton1_Visible.getVisibility() == View.INVISIBLE;
    boton1_Visible.setVisibility(visibility1 ? View.INVISIBLE : View.VISIBLE);

   //mSharedPreferences.edit().putBoolean(boton1_Visible, visibility1).apply();
   //Guarda valores correspondientes a visibilidad.
    guardaValorPreferencia(getApplicationContext(), boton1_Visible, visibility1);

    boton2_Visible.setVisibility(View.VISIBLE);

    boolean visibility2 = boton2_Visible.getVisibility() == View.VISIBLE;
    boton2_Visible.setVisibility(visibility2 ? View.VISIBLE : View.INVISIBLE);

    //mSharedPreferences.edit().putBoolean(boton2_Visible, visibility2).apply();
   //Guarda valores correspondientes a visibilidad.
    guardaValorPreferencia(getApplicationContext(), boton2_Visible, visibility2);

}

and when you start your Activity you can get the values to assign visibility to the buttons:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        b1=(Button)findViewById(R.id.b1);
        b2=(Button)findViewById(R.id.b2);

        mSharedPreferences = getPreferences(Context.MODE_PRIVATE);

//Obtiene valores de visibilidad.   
boton1_Visible.setVisibility(obtieneValorPreferencia(getApplicationContext(), boton1_Visible) ? View.INVISIBLE : View.VISIBLE));

boton2_Visible.setVisibility(obtieneValorPreferencia(getApplicationContext(), boton2_Visible) ? View.VISIBLE : View.INVISIBLE));

    }
    
answered by 08.12.2016 в 16:01