Reputation: 1
I have three Activities with one button in each of them. Act1 with btn1, Act2 with btn2, and Act3 with btn3. I have another Activity as MainActivity with three imageViews: ImageView1, imageView2 and imageView3, all of which are initially invisible. I want it so when I click on btn1 in act1, imageView1 in MainActivity will be visible and when click on btn1 again, imageView1 will be invisible again. And similarly for imageView2 and imageView3. I have this code so far:
Activity1
public class Activity1 extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
Button btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String visibilityStr = PublicSharedPreferences.getDefaults("keyVisibility", getApplicationContext());
if (visibilityStr != null) {
if (visibilityStr.equals("0")) {
Toast.makeText(act1.this, "it visibled", Toast.LENGTH_SHORT).show();
visibilityStr = "1";
btn1.setImageResource(R.mipmap.img1);
} else {
visibilityStr = "0";
Toast.makeText(act1.this, "it invisibled", Toast.LENGTH_SHORT).show();
btn1.setImageResource(R.mipmap.img2);
}
} else {
visibilityStr = "1";
Toast.makeText(act1.this, "it visibled", Toast.LENGTH_SHORT).show();
btn1.setImageResource(R.mipmap.img1);
}
PublicSharedPreferences.setDefaults("keyVisibility", visibilityStr, getApplicationContext());
}
});
}
Activity2
public class Activity2 extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
Button btn1 = (Button) findViewById(R.id.btn2);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String visibilityStr = PublicSharedPreferences.getDefaults("keyVisibility", getApplicationContext());
if (visibilityStr != null) {
if (visibilityStr.equals("0")) {
Toast.makeText(act2.this, "it visibled", Toast.LENGTH_SHORT).show();
btn2.setImageResource(R.mipmap.img1);
visibilityStr = "1";
} else {
visibilityStr = "0";
Toast.makeText(act2.this, "it invisibled", Toast.LENGTH_SHORT).show();
btn2.setImageResource(R.mipmap.img2);
}
} else {
visibilityStr = "1";
Toast.makeText(act2.this, "it visibled", Toast.LENGTH_SHORT).show();
btn2.setImageResource(R.mipmap.img1);
}
PublicSharedPreferences.setDefaults("keyVisibility", visibilityStr, getApplicationContext());
}
});
}
Activity3
public class Activity3 extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity3);
Button btn1 = (Button) findViewById(R.id.btn3);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String visibilityStr = PublicSharedPreferences.getDefaults("keyVisibility", getApplicationContext());
if (visibilityStr != null) {
if (visibilityStr.equals("0")) {
Toast.makeText(act3.this, "it visibled", Toast.LENGTH_SHORT).show();
Btn3.setImageResource(R.mipmap.img1);
visibilityStr = "1";
} else {
visibilityStr = "0";
Toast.makeText(act3.this, "it invisibled", Toast.LENGTH_SHORT).show();
btn3.setImageResource(R.mipmap.img2);
}
} else {
visibilityStr = "1";
Toast.makeText(act3.this, "it visibled", Toast.LENGTH_SHORT).show();
btn3.setImageResource(R.mipmap.img1);
}
PublicSharedPreferences.setDefaults("keyVisibility", visibilityStr, getApplicationContext());
}
});
}
MainActivity with three imageViews:
ImageView imgView1 = (ImageView) findViewById(R.id.imgView1);
String visibilityStr= PublicSharedPreferences.getDefaults("keyVisibility", getApplicationContext());
if (visibilityStr.equals("0"))
imgView1.setVisibility(View.INVISIBLE);
else
imgView1.setVisibility(View.VISIBLE);
ImageView imgView2 = (ImageView) findViewById(R.id.imgView2);
String visibilityStr= PublicSharedPreferences.getDefaults("keyVisibility", getApplicationContext());
if (visibilityStr.equals("0"))
imgView2.setVisibility(View.INVISIBLE);
else
imgView2.setVisibility(View.VISIBLE);
ImageView imgView3 = (ImageView) findViewById(R.id.imgView3);
String visibilityStr= PublicSharedPreferences.getDefaults("keyVisibility", getApplicationContext());
if (visibilityStr.equals("0"))
imgView3.setVisibility(View.INVISIBLE);
else
imgView3.setVisibility(View.VISIBLE);
They work well. But the problem is that when I click on btn1, all imageViews in MainActivity change (become visible or invisible) or when I click on btn3, all imageViews change. I want it so btn1 just changes imageView1 and btn2 just changes imageView2 and btn3 just changes imageView3, instead of one of the buttons changing all of the imageViews. How can I do that? Which part of the code is wrong?
Upvotes: 0
Views: 131
Reputation: 196
this is very easy ,you can use one of this methods
1 : Shared Preferences :
in your Activities (Act1,Act2,Act3)
write this codes on onClick
event :
// on Act1
final SharedPreferences sharedPreferences=getSharedPreferences("mainconf",MODE_PRIVATE);
buttonOnAct1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// check if isVisable and change value
if (sharedPreferences.getBoolean("isImg1Visable",true)){
sharedPreferences.edit().putBoolean("isImg1Visable",false).apply();
}else {
// show it again
sharedPreferences.edit().putBoolean("isImg1Visable",true).apply();
}
}
});
// on Act2
final SharedPreferences sharedPreferences=getSharedPreferences("mainconf",MODE_PRIVATE);
buttonOnAct2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// check if isVisable and change value
if (sharedPreferences.getBoolean("isImg2Visable",true)){
sharedPreferences.edit().putBoolean("isImg2Visable",false).apply();
}else {
// show it again
sharedPreferences.edit().putBoolean("isImg2Visable",true).apply();
}
}
});
// on Act3
final SharedPreferences sharedPreferences=getSharedPreferences("mainconf",MODE_PRIVATE);
buttonOnAct3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// check if isVisable and change value
if (sharedPreferences.getBoolean("isImg3Visable",true)){
sharedPreferences.edit().putBoolean("isImg3Visable",false).apply();
}else {
// show it again
sharedPreferences.edit().putBoolean("isImg3Visable",true).apply();
}
}
});
And code of your MainActivity
:
public class MainActivity extends AppCompatActivity {
private ImageView imageView1;
private ImageView imageView2;
private ImageView imageView3;
private SharedPreferences sharedPreferences;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
imageView1=(ImageView)findViewById(R.id.img1);
imageView2=(ImageView)findViewById(R.id.img2);
imageView3=(ImageView)findViewById(R.id.img3);
sharedPreferences=getSharedPreferences("mainconf",MODE_PRIVATE);
}
// important PART OF CODE
@Override
protected void onResume() {
super.onResume();
if (sharedPreferences.getBoolean("isImg1Visable",true)){
imageView1.setVisibility(View.VISIBLE);
}else {
imageView1.setVisibility(View.GONE);
}
if (sharedPreferences.getBoolean("isImg2Visable",true)){
imageView2.setVisibility(View.VISIBLE);
}else {
imageView2.setVisibility(View.GONE);
}
if (sharedPreferences.getBoolean("isImg3Visable",true)){
imageView3.setVisibility(View.VISIBLE);
}else {
imageView3.setVisibility(View.GONE);
}
}
}
2 : public static Variables:
write your variables as static example :
// in Your MainActivty under class
public static ImageView imageView1;
public static ImageView imageView2;
public static ImageView imageView3;
// in your Act1 Act2 Act3
// for hiding from mainactivity
MainActivity.imageView1.setVisibility(View.GONE); // or View.INVISIBLE
// for showing on main activity
MainActivity.imageView1.setVisibility(View.VISIBLE);
Upvotes: 0
Reputation: 415
the problem is you only have 1key Preference its all keyVisibility so when you click on any of your button this key changes to either 1 or 0 base on your code
here
ImageView imgView1 = (ImageView) findViewById(R.id.imgView1);
String visibilityStr1= PublicSharedPreferences.getDefaults("keyVisibility1", getApplicationContext());
if (visibilityStr1.equals("0"))
imgView1.setVisibility(View.INVISIBLE);
else
imgView1.setVisibility(View.VISIBLE);
ImageView imgView2 = (ImageView) findViewById(R.id.imgView2);
String visibilityStr2= PublicSharedPreferences.getDefaults("keyVisibility2", getApplicationContext());
if (visibilityStr2.equals("0"))
imgView2.setVisibility(View.INVISIBLE);
else
imgView2.setVisibility(View.VISIBLE);
ImageView imgView3 = (ImageView) findViewById(R.id.imgView3);
`String visibilityStr3= PublicSharedPreferences.getDefaults("keyVisibility3",` `getApplicationContext());`
if (visibilityStr3.equals("0"))
imgView3.setVisibility(View.INVISIBLE);
else
imgView3.setVisibility(View.VISIBLE);
and ofcoure you should change the keyVisibility on your act1,act2 and act3
Upvotes: 1
Reputation: 838
First of all, please do a little more study on Android SharedPreferences (how to save/get a sharepreference value properly in/from your local storage).
After you are done with your research, here's some hint for you. Hope you will be able to implement this on your own.
Save value - save your boolean value when you click the button
Context mContext = getApplicationContext();
SharedPreferences mPrefs = mContext.getSharedPreferences("MySharedPrefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean("IsImageViewVisible", true);
// use different keys for different imageviews;
//ex: for ImageView1, use IsImageView_1_Visible; for ImageView2, use IsImageView_2_Visible etc
editor.commit();
Get values in your MainActivity
Context mContext = getApplicationContext();
SharedPreferences mPrefs = mContext.getSharedPreferences("MySharedPrefs", Context.MODE_PRIVATE);
Boolean isImageViewVisible = mPrefs.getBoolean("IsImageViewVisible", false); // here,false is default value
Then, check all imageview's visibility in MainActivity:
if(isImageViewVisible){
// image is visible
} else{
// image is invisible
}
Upvotes: 0