Skip to content Skip to sidebar Skip to footer

How To Make Visible And Invisible An Image By Clicking A Button In Android Studio?

i have two activities in android studio.act1 with a button and act2 with an imageView. i want to click the button in act1 and make the image in act2 visible. and when i click butto

Solution 1:

you must create this class;

public class PublicSharedPreferences {
    public static void setDefaults(String key, String value, Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(key, value);
        editor.commit();
    }

    public static String getDefaults(String key, Context context) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        return preferences.getString(key, null);
    }
}

and then learn sharedpreferences enter link description here

Activity1;

public class Activity1 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout1);

        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(Activity1.this, "it visibled", Toast.LENGTH_SHORT).show();
                    visibilityStr = "1";
                } else {
                    visibilityStr = "0";
                    Toast.makeText(Activity1.this, "it invisibled", Toast.LENGTH_SHORT).show();
                }
            } else {
                visibilityStr = "1";
                Toast.makeText(Activity1.this, "it visibled", Toast.LENGTH_SHORT).show();
            }

                PublicSharedPreferences.setDefaults("keyVisibility", visibilityStr, getApplicationContext());

                Intent intent = new Intent(Activity1.this, Activity2.class);
                Activity1.this.startActivity(intent);
            }
        });
    }
}

Activity2;

public class Activity2 extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout2);

        ImageView imgView = (ImageView) findViewById(R.id.imgView1);

        String visibilityStr = PublicSharedPreferences.getDefaults("keyVisibility", getApplicationContext());

        if (visibilityStr.equals("0"))
            imgView.setVisibility(View.INVISIBLE);
        else
            imgView.setVisibility(View.VISIBLE);
    }
}

Layout1;

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.c.a.myapplication.Activity1"><Buttonandroid:id="@+id/btn1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button1"/></LinearLayout>

Layout2;

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><ImageViewandroid:id="@+id/imgView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@android:drawable/ic_menu_camera"android:visibility="invisible"/></LinearLayout>

Its work.

Solution 2:

Try this code....

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

publicclassSampleextendsActivity {
    ImageView img;
    Button btn;
    booleanclicked=true;

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stubsuper.onCreate(savedInstanceState);
        setContentView(R.layout.sample);
        btn = (Button) findViewById(R.id.t1);
        btn.setOnClickListener(newOnClickListener() {

            @OverridepublicvoidonClick(View v) {
                // TODO Auto-generated method stub
                clicked = true;
                Intentintent=newIntent(Sample.this, Dample.class);
                intent.putExtra("value", true);
                startActivity(intent);
            }

        });

    }
}

//Dample.class In second activity

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.Button;

publicclassDampleextendsActivity {
     ImageView img;//use image view@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stubsuper.onCreate(savedInstanceState);
        setContentView(R.layout.pimple);
        img= (ImageView) findViewById(R.id.t1);
        BooleanyourBool= getIntent().getExtras().getBoolean("value");
        if (yourBool == true) {
            img.setVisibility(View.VISIBLE);///use visibility code for     imageview as mentioned above
        }
    }

}

Post a Comment for "How To Make Visible And Invisible An Image By Clicking A Button In Android Studio?"