0

I'm a rookie in android programming.

I have a small problem. When I click a ImageView, I make that ImageView invisible and set a Button to visible. My problem is that how do you save this?

For eg, I click the ImageView, Button shows up and ImageView disappears. And I exit the app and enter back into that same activity and I want that Button to remain there.

How do I go about doing that?

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;

public class Flightplan extends Activity {

Button a;
ImageView b; 
private boolean isVisible;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.flightplan);

b = (ImageView) findViewById(R.id.plus);
View v1 = findViewById(R.id.test); 
v1.setVisibility(View.GONE);
b.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

            View v2 = findViewById(R.id.test); 
            v2.setVisibility(View.VISIBLE);

            View v3 = findViewById(R.id.plus);
            v3.setVisibility(View.GONE); 
    }
});

} 
}

Thanks!

Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
WhiplashOne
  • 321
  • 1
  • 4
  • 11
  • you could do this with SharedPreferences http://developer.android.com/reference/android/content/SharedPreferences.html – nano_nano Mar 21 '13 at 08:40
  • You should [accept the answer](http://meta.stackexchange.com/a/5235) that helped you most, as well as not putting your [own question twice](http://stackoverflow.com/questions/15530081/how-to-save-a-result-in-android). Read the [faq](http://stackoverflow.com/faq) before writing new questions. This will help you increase answers and the quality of them. If you don't make an effort writing a good question, people won't bother writing good answers. – Sergi Juanola Mar 21 '13 at 13:38

2 Answers2

1

Save the state into a Shared Preference, and then in onCreate, check that preference. The answer of this question is pretty clear on how to use Shared Preferences, but instead of a Long, you could store and get a Boolean.

Community
  • 1
  • 1
Sergi Juanola
  • 6,531
  • 8
  • 56
  • 93
0

try this:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.content.SharedPreferences;

public class Flightplan extends Activity {

  Button a;
  ImageView b;
  private boolean isVisible;
  SharedPreferences prefs = null;

  public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.flightplan);
prefs = this.getSharedPreferences(
    "com.your.app", Context.MODE_PRIVATE);

boolean imageVisible = prefs.getBoolean("imageVisible", false); 

b = (ImageView) findViewById(R.id.plus);

View v1 = findViewById(R.id.test);
if (!imageVisible){
  v1.setVisibility(View.GONE);
  v3.setVisibility(View.VISIBLE);
}else{
  v1.setVisibility(View.VISIBLE);
  v3.setVisibility(View.GONE);
}

b.setOnClickListener(new View.OnClickListener() {

  @Override
  public void onClick(View v) {
    // TODO Auto-generated method stub

    View v2 = findViewById(R.id.test);
    v2.setVisibility(View.VISIBLE);

    View v3 = findViewById(R.id.plus);
    v3.setVisibility(View.GONE);

    prefs.edit().putBoolean("imageVisible", true).commit();
  }
});

v1.setOnClickListener(new View.OnClickListener() {

  @Override
  public void onClick(View v) {
    // TODO Auto-generated method stub

    View v2 = findViewById(R.id.test);
    v2.setVisibility(View.GONE);

    View v3 = findViewById(R.id.plus);
    v3.setVisibility(View.VISIBLE);

    prefs.edit().putBoolean("imageVisible", false).commit();
  }
});


 }

 }

I wrote that with a text editor. It could be that there are some compiler errors...

nano_nano
  • 12,351
  • 8
  • 55
  • 83