3

Can somebody tell me how to hide views for always?

I'm hiding a view with view.setVisibility(view.GONE); but when reopen the app I have to re-hide the view. I want a view hide for always when button is clicked until clearing app data or uninstalling. Thanks!

Azeem
  • 11,148
  • 4
  • 27
  • 40
Niezwm
  • 87
  • 1
  • 8

4 Answers4

2

You need to save state by creating flag for hide and unhide view in SharedPreference and basis of that flag you need to GONE or VISIBLE View.

Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
1

You have to use SharedPreferences, when you hide the View then store a value in SharedPreferences, and when you come back to the app get value from SharedPreferences and according to that value hide your View by the same method that you are using.

SharedPreferences sp = getSharedPreferences("PREFS_NAME", Context.MODE_PRIVATE);
SharedPreferences.Editor et = sp.edit();
et.putBoolean("isViewHide", true);
et.commit();

Getting value from SharedPreferences

SharedPreferences sp = getSharedPreferences("PREFS_NAME", Context.MODE_PRIVATE);
boolean cb1 = sp.getBoolean("isViewHide", false);

reference from SharedPreferences example

Vishal Chhodwani
  • 2,567
  • 5
  • 27
  • 40
0

There is not option to permanent hide but you can achieve by this way

1.Remove the specific view by

parent.removeViewAt(index);

parent.removeView(view);
  1. you have to save the state to show/hide the view on permanent storage like

    Share preference / databases

Shared Preference Example

1.store value in sharedPrefernces:

SharedPreferences preferences = this.getSharedPreferences("SoldiPreferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("storevalue", false);
editor.commit();

2.get value from sharedPreferences:

SharedPreferences preferences = this.getSharedPreferences("SoldiPreferences", Context.MODE_PRIVATE);
preferences.getBoolean("storevalue", false);

if(preferences.getBoolean("storevalue", false))
{
view.setVisibility(View.VISIBLE);
}
else
view.setVisibility(View.GONE);
Chetan Pushpad
  • 345
  • 1
  • 9
Adnan Maqbool
  • 452
  • 3
  • 10
  • can you tell me please how to use 'SharedPreference' with '.setVisibility' – Niezwm Jul 07 '17 at 04:58
  • Can you give example of this with button with button click to 'view.setVisibility(view.GONE);' – Niezwm Jul 07 '17 at 05:28
  • button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { imageview.setVisibility(View.GONE); SharedPreferences preferences = this.getSharedPreferences("SoldiPreferences", Context.MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); editor.putBoolean("storevalue", true); editor.commit(); } }); – Adnan Maqbool Jul 07 '17 at 05:37
  • and you also have to check in on create , for neXt time to be visible/gone. – Adnan Maqbool Jul 07 '17 at 05:38
0

Your current approach is the programmatic one and is probably what you will have to use if you want a button to toggle the visibility of a view. The only other option of which I am aware would be to disable visibility from the XML layout file:

<TextView
    android:visibility="gone"
    ... />
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360