0

I'm new here. I'm french so maybe my english is not very good, sorry for that.

I'm a beginner in Android development, I got to create an app for finish my study.

I explain to u my problem : I have on an activity called VoeuxActivity.java 8 buttons, they are all VISIBLEs at the beginning, when an user click on one of them button change by INVISIBLE (user can't see the button after clicked on it), I have no problem for do that. But my problem is when I quit the app and I come back again on my app, the button is visible again, so there's no save on my app. I think I got to use SharedPreferences but I really don't know how to use that. If someone can help me, I will be very happy. I post a court code with one button and the text above the button (because the button is an image in background). I make the TextView INVISIBLE too and I want to save the changement too.

public class VoeuxActivity extends Activity {

public static boolean isClicked = false;
Button totoB;
TextView totoTv;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_voeux);

    totoB = (Button) findViewById(R.id.perso1);
    totoTV = (TextView) findViewById(R.id.perso1Text);
    totoB.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            isClicked = true;
            totoB.setVisibility(View.INVISIBLE);
            totoTv.setVisibility(View.INVISIBLE);
            Intent intentToto = new Intent(VoeuxActivity.this, JouerActivity.class);
            startActivity(intentToto);
        }
    });

} }

How can I save the changement of the Button and the TextView from Visible to Invisible?

Thank u for yo help. Giggs

Ghiggz Pikkoro
  • 136
  • 1
  • 14

3 Answers3

1

You can use SharedPreferences to save some settings and info, for your case follow the code below:

    Button totoB;
    TextView totoTv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_voeux);

        final SharedPreferences prefs = getSharedPreferences("sharedPreferences", Context.MODE_PRIVATE);

        totoB = (Button) findViewById(R.id.perso1);
        totoTv = (TextView) findViewById(R.id.perso1Text);
        totoB.setVisibility(prefs.getBoolean("isTotoBVisible", true) ? View.VISIBLE : View.INVISIBLE);
        totoTv.setVisibility(prefs.getBoolean("isTotoTVVisible", true) ? View.VISIBLE : View.INVISIBLE);

        totoB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                prefs.edit().putBoolean("isTotoBVisible", false).apply();
                prefs.edit().putBoolean("isTotoTVVisible", false).apply();

                totoB.setVisibility(View.INVISIBLE);
                totoTv.setVisibility(View.INVISIBLE);
                Intent intentToto = new Intent(VoeuxActivity.this, JouerActivity.class);
                startActivity(intentToto);
            }
        });
    }
  • Thank u for yo helps, but I have a new question for u, it seems I can't post it again in here, I don't understand why, do u know if I got to create a new subject ? – Ghiggz Pikkoro Apr 21 '17 at 21:35
1

Implement your SharedPreferences this way:

Boolean isFirstTime;

  SharedPreferences app_preferences = PreferenceManager
        .getDefaultSharedPreferences(Splash.this);

 SharedPreferences.Editor editor = app_preferences.edit();

 isFirstTime = app_preferences.getBoolean("isFirstTime", true);

 if (isFirstTime) {

//implement your first time logic
//SHow Button
editor.putBoolean("isFirstTime", false);
 editor.commit();

}else{
//Invisible button
//app open directly
}
1

Try this,

public static boolean isClicked = false;
Button totoB;
TextView totoTv;
SharedPreferences preferences;
SharedPreferences.Editor prefsEditor ;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_voeux);

    preferences = getSharedPreferences("AppPrefs", MODE_PRIVATE);
    prefsEditor = preferences.edit();

    //get value
    String buttonClick=preferences.getString("ButtonClick", "0");   
    if(buttonClick.equals("0"))//before button click
    {
        totoB.setVisibility(View.VISIBLE);
        totoTv.setVisibility(View.VISIBLE);
    }
    else
    {
        totoB.setVisibility(View.INVISIBLE);
        totoTv.setVisibility(View.INVISIBLE);
    }

    totoB = (Button) findViewById(R.id.perso1);
    totoTV = (TextView) findViewById(R.id.perso1Text);
    totoB.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            isClicked = true;
            prefsEditor.putString("ButtonClick", "1");
            totoB.setVisibility(View.INVISIBLE);
            totoTv.setVisibility(View.INVISIBLE);
            Intent intentToto = new Intent(VoeuxActivity.this, JouerActivity.class);
            startActivity(intentToto);
        }
    });
} 
Komal12
  • 3,340
  • 4
  • 16
  • 25