2

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 have an activity number 1 called VoeuxActivity.java with 8 buttons (and 8 TextViews), 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), when I quit the app and I come back again on my app, the button is always invisible thanks to the SharedPreferences and a member of this forum. But I want now when I clicked on this button named "totoB" it will be invisible and another button becomes Visible on another activity number 2 called PersoActivity.java because the first activity is for unlock some characters locked, when I choose a new character in activity 1, it will be invisible in activity 1 and will be visible on activity 2 in order to choose 2 characters for a fight (that's why there's a boolean name "isClicked") but I tried to use SharedPreferences for stay the button visible on the second activity but it doesn't work at all. When a new character is unlock if I quit the app and come back again in my app, the new character unlock isn't save as visible and he's Invisible again but I want he's visible always by the SharedPreferences. I post a court code with the same button on my first activity then on the second activity (I try to do the same as the first activity but my solution can't work), maybe u could help me to resolve my issue.

The code of the first activity can work and save the changements:

public class VoeuxActivity extends Activity {
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);
        }
    });
}

I try to do the same thing for the second activity but it can't work this time, the changement aren't saved.

public class PersoActivity extends Activity {

public static Personnage p1, p2;

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

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

    final Button totoPersoBtn = (Button) findViewById(R.id.perso1);
    final TextView totoPersoTv = (TextView) findViewById(R.id.perso1Text);
    totoAdversaireBtn = (Button) findViewById(R.id.adversaire1);
    totoAdversaireTv = (TextView) findViewById(R.id.adversaire1Text);
    totoPersoBtn.setVisibility(prefs.getBoolean("isTotoPersoBtnInvisible", true) ? View.INVISIBLE : View.VISIBLE);
    totoPersoTv.setVisibility(prefs.getBoolean("isTotoPersoTvInvisible", true) ? View.INVISIBLE : View.VISIBLE);

totoAdversaireBtn.setVisibility(prefs.getBoolean("isTotoAdversaireBtnInvisible", true) ? View.INVISIBLE : View.VISIBLE);

totoAdversaireTv.setVisibility(prefs.getBoolean("isTotoAdversaireTvInvisible", true) ? View.INVISIBLE : View.VISIBLE);

    if(VoeuxActivity.isClicked) {
        prefs.edit().putBoolean("isTotoPersoBtnInvisible", false).apply();
        prefs.edit().putBoolean("isTotoPersoTvInvisible", false).apply();
        prefs.edit().putBoolean("isTotoAdversaireBtnInvisible", false).apply();
        prefs.edit().putBoolean("isTotoAdversaireTvInvisible", false).apply();
        totoPersoTv.setVisibility(View.VISIBLE);
        totoPersoBtn.setVisibility(View.VISIBLE);
        totoAdversaireBtn.setVisibility(View.VISIBLE);
        totoAdversaireTv.setVisibility(View.VISIBLE);
    } else {
        totoPersoBtn.setVisibility(View.INVISIBLE);
        totoPersoTv.setVisibility(View.INVISIBLE);
        totoAdversaireBtn.setVisibility(View.INVISIBLE);
        totoAdversaireTv.setVisibility(View.INVISIBLE);
    }}}

How can I save the changement of the Button and the TextView from Visible to Invisible in the second activity? Thank u very much if someone can helps me because I really don't know why it's not work at all. Giggs

Ghiggz Pikkoro
  • 136
  • 1
  • 14

1 Answers1

1

I made an example app and tested it. It saves the last state of textviews(visible or invisible) no matter on which activity you make changes. And even when you exit and come back to the app it loads the last state.

Manifest file

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.key.hs.invisiblebuttons">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".Activity1"
            android:label="Test1"
            android:alwaysRetainTaskState="true"
            android:configChanges="keyboardHidden|orientation|screenSize"
            >
        <intent-filter>
            <action android:name="android.intent.action.MAIN"></action>
            <category android:name="android.intent.category.HOME"></category>
            <category android:name="android.intent.category.LAUNCHER"></category>

        </intent-filter>
        </activity>

        <activity
            android:name=".Activity2"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:hardwareAccelerated="true">
        </activity>
    </application>
</manifest>

Activity1.class

package com.key.hs.invisiblebuttons;

    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;

    /**
     * Created by Hasan on 26.04.2017.
     */

    public class  Activity1 extends AppCompatActivity {

        private boolean btn1visiblity, btn2visibility;
        private TextView tv1, tv2;
        private Button btn1, btn2, act2, reset;


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

            tv1= (TextView)findViewById(R.id.tv1);
            tv2=(TextView)findViewById(R.id.tv2);
            btn1= (Button)findViewById(R.id.btn1);
            btn2=(Button)findViewById(R.id.btn2);
            act2=(Button)findViewById(R.id.act2);
            reset=(Button)findViewById(R.id.reset);


            //Create or load preferences
            final SharedPreferences prefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
            btn1visiblity = prefs.getBoolean("TV1visibility", true);
            btn2visibility = prefs.getBoolean("TV2visibility", true);

            //take into effect saved booleans
            if(btn1visiblity){
                tv1.setVisibility(View.VISIBLE);
            }else{
                tv1.setVisibility(View.INVISIBLE);
            }
            if(btn2visibility){
                tv2.setVisibility(View.VISIBLE);
            }else{
                tv2.setVisibility(View.INVISIBLE);
            }


            reset.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final SharedPreferences prefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
                    btn1visiblity = prefs.getBoolean("TV1visibility", true);
                    btn2visibility = prefs.getBoolean("TV2visibility", true);

                    SharedPreferences.Editor editor = prefs.edit();
                    editor.putBoolean("TV1visibility", true);
                    editor.putBoolean("TV2visibility", true);
                    editor.commit();

                    tv1.setVisibility(View.VISIBLE);
                    tv2.setVisibility(View.VISIBLE);

                }
            });

            act2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(Activity1.this, Activity2.class);
                    startActivity(intent);
                    finish();

                }
            });

            btn1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Create or load preferences
                    final SharedPreferences prefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
                    btn1visiblity = prefs.getBoolean("TV1visibility", true);

                    //save new boolean
                    SharedPreferences.Editor editor = prefs.edit();
                    editor.putBoolean("TV1visibility", false);
                    editor.commit();
                    tv1.setVisibility(View.INVISIBLE);
                }
            });

            btn2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Create or load preferences
                    final SharedPreferences prefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
                    btn2visibility = prefs.getBoolean("TV2visibility", true);

                    //save new boolean
                    SharedPreferences.Editor editor = prefs.edit();
                    editor.putBoolean("TV2visibility", false);
                    editor.commit();
                    tv2.setVisibility(View.INVISIBLE);
                }
            });

    }
    }

Activity2.class

     package com.key.hs.invisiblebuttons;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * Created by Hasan on 26.04.2017.
 */

public class  Activity2 extends AppCompatActivity {

    private boolean btn1visiblity, btn2visibility;
    private TextView tv1, tv2;
    private Button btn1, btn2, act1, reset;


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

        tv1= (TextView)findViewById(R.id.tv1);
        tv2=(TextView)findViewById(R.id.tv2);
        btn1= (Button)findViewById(R.id.btn1);
        btn2=(Button)findViewById(R.id.btn2);
        act1=(Button)findViewById(R.id.act1);
        reset=(Button)findViewById(R.id.reset);

        //Create or load preferences
        final SharedPreferences prefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
        btn1visiblity = prefs.getBoolean("TV1visibility", true);
        btn2visibility = prefs.getBoolean("TV2visibility", true);

        //if tvs invisible in activity1 make them visible in activity2
        if(!btn1visiblity){
            tv1.setVisibility(View.VISIBLE);
        }else{
            tv1.setVisibility(View.INVISIBLE);
        }
        if(!btn2visibility){
            tv2.setVisibility(View.VISIBLE);
        }else{
            tv2.setVisibility(View.INVISIBLE);
        }

        //resets booleans
        reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final SharedPreferences prefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
                btn1visiblity = prefs.getBoolean("TV1visibility", true);
                btn2visibility = prefs.getBoolean("TV2visibility", true);

                SharedPreferences.Editor editor = prefs.edit();
                editor.putBoolean("TV1visibility", true);
                editor.putBoolean("TV2visibility", true);
                editor.commit();
                tv1.setVisibility(View.INVISIBLE);
                tv2.setVisibility(View.INVISIBLE);

            }
        });


        //go to activity1
        act1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Activity2.this, Activity1.class);
                startActivity(intent);
                finish();

            }
        });

        //makes tv1 invible
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Create or load preferences
                final SharedPreferences prefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
                btn1visiblity = prefs.getBoolean("TV1visibility", true);

                //save new boolean
                SharedPreferences.Editor editor = prefs.edit();
                editor.putBoolean("TV1visibility", true);
                editor.commit();
                tv1.setVisibility(View.INVISIBLE);
            }
        });

        //makes tv2 invisible
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Create or load preferences
                final SharedPreferences prefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
                btn2visibility = prefs.getBoolean("TV2visibility", true);

                //save new boolean
                SharedPreferences.Editor editor = prefs.edit();
                editor.putBoolean("TV2visibility", true);
                editor.commit();
                tv2.setVisibility(View.INVISIBLE);
            }
        });



    }
}

activity1lay.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv1"
        android:text="example tv1"
        android:layout_alignParentStart="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv2"
        android:text="example tv2"
        android:layout_alignParentEnd="true"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn2"
        android:text="TV2 invisible"
        android:layout_alignParentBottom="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn1"
        android:text="TV1 invisible"
        android:layout_above="@id/btn2"/>


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/act2"
        android:text="ACTIVITY2"
        android:layout_alignParentEnd="true"
        android:layout_centerInParent="true"></Button>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/reset"
        android:text="RESET VISIBILITY"
        android:layout_alignParentStart="true"
        android:layout_centerInParent="true"/>

</RelativeLayout>

activity2lay.xml

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv1"
            android:text="example tv1"
            android:layout_centerInParent="true"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv2"
            android:text="example tv2"
            android:layout_above="@id/tv1"
            android:layout_centerHorizontal="true"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn1"
            android:text="TV1 invisible"
            android:layout_above="@id/btn2"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn2"
            android:text="TV2 invisible"
            android:layout_alignParentBottom="true"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/act1"
            android:text="ACTIVITY1"
            android:layout_alignParentEnd="true"
            android:layout_centerInParent="true"></Button>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/reset"
            android:text="RESET VISIBILITY"
            android:layout_alignParentStart="true"
            android:layout_centerInParent="true"/>
    </RelativeLayout>
Hasan Saykın
  • 138
  • 1
  • 8
  • Hi Hasan, thank for yo answer. Maybe my english is bad so I don't understand very well, can u please more explicite about what u explain me? Can u maybe post a small example of code? I have an onClickListener on each button for create a new personnage then unlock the click of some button but nothing about SharedPreferences here because I want the buttons are always visible before the click. In fact at the beginning of the game the buttons on the second activity are all invisible (just the default buttons aren't). – Ghiggz Pikkoro Apr 23 '17 at 01:50
  • //booleans private boolean visibility1, visibility2; //define a sharedpreferences final SharedPreferences prefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE); visibility1 = prefs.getBoolean("visibility1", true); visibility2 = prefs.getBoolean("visibility2", true); totoB.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SharedPreferences.Editor editor = prefs.edit(); editor.putBoolean("visibility1", false); editor.commit(); } }); – Hasan Saykın Apr 23 '17 at 18:39
  • //second activity //booleans private boolean visibility1, visibility2; final SharedPreferences prefs = getSharedPreferences("sharedPreferences", Context.MODE_PRIVATE); visibility1 = prefs.getBoolean("visibility1", true); visibility2 = prefs.getBoolean("visiblity2", true); if(visibility1=true){ x..setVisibility(View.VISIBLE);}else{x.setVisibility(View.IN‌​VISIBLE);} I hope it helps. I would use sharedprefrences like this to save and get data. – Hasan Saykın Apr 23 '17 at 18:40
  • But if you don't want to save booleans to retrieve even when you kill your app you can simply pass booleans through intent. Intent intent = new Intent(xclass.this, y.class); Bundle b = new Bundle(); b.putBoolean("visibility1", visiblity1); intent.putExtras(b); startActivity(intent); finish(); //In y activity to get passed boolean in oncreate Bundle b = getIntent().getExtras(); boolean visibility1= b.getBoolean("visibility1"); and after this you can use this boolean to determine something. – Hasan Saykın Apr 23 '17 at 18:47
  • Hi thank u, I try it but in the second class the save can't work, maybe there's something I don't understand or maybe I don't explain what I want exactly. In fact, at the beginning, on the first activity all fighters are visibles, on the second activity all fighters are invisibles (just 2 of them are visibles for begin a fight). Then (after find some objects u can unlocked some new fighters), if u click on a new fighter on the first activity it's for unlock it on the second activity. – Ghiggz Pikkoro Apr 25 '17 at 22:36
  • So in the first activity if I choose a new fighter he disappears on the first activity (the next time I can't choose this fighter because he's already unlocked), and he becomes visible on the second activity. So I want the changement are save by SharedPreferences (for the first activity it's working good, but on the second activity it's not working). Sorry if my explanation was not good because my english isn't good too. – Ghiggz Pikkoro Apr 25 '17 at 22:36
  • This is the best I could do, check out the edited answer. I hope it inspires you. – Hasan Saykın Apr 26 '17 at 11:55
  • Check out my updated answer. Now if you make a textview invisible in activity1, it becomes visible in activity2. But visible textviews in activity1 are not visible in activity2. – Hasan Saykın Apr 26 '17 at 16:35
  • Wow Hasan u're so amazing, thank u very much, I just see it, I will test it right now and come back. It's clear – Ghiggz Pikkoro Apr 27 '17 at 13:41
  • The reset button is for reset all save like the beginning right? It's a good idea, I want to do it too but I would use it in the main activity, reset all in a third activity, do u think is it possible ? – Ghiggz Pikkoro Apr 27 '17 at 13:45
  • Yes. As long as you use the same sharedpreferences it does not matter where you reset(make them true for example) booleans. You reset it in the main activity and then proceed to other activities and it will be reset(true) in those activities too. – Hasan Saykın Apr 27 '17 at 15:59
  • Ok Thank u for yo answer Hasan, u help me very much again – Ghiggz Pikkoro Apr 28 '17 at 14:27
  • No problem. If you solved your problem, please accept my post as the answer. – Hasan Saykın Apr 28 '17 at 14:37
  • Ok, I'm not sure if I accepted yo post as the answer, can u check it out? – Ghiggz Pikkoro Apr 28 '17 at 15:57
  • You have to click "the check mark" at the begining of my post. It's a bit left to the beginning of my post, under the up and down buttons. :) – Hasan Saykın Apr 28 '17 at 17:04
  • Hasan, I have another question to ask u about SharedPreferences but I think I need to create a new subject because it's about display a TextView who changes then I want to save this changement but I don't know how to do with the putBoolean() method – Ghiggz Pikkoro May 03 '17 at 22:40
  • Probably. Explain your problem in detail and make new post. But first, please accept my post as the answer in this question. ;) – Hasan Saykın May 03 '17 at 23:51
  • Ok I did it, u can see it here : http://stackoverflow.com/questions/43771605/sharedpreferences-save-a-text-entered-in-an-edittext-and-display-it-in-a-textvie – Ghiggz Pikkoro May 04 '17 at 01:00