Hide Show imageview on button click android programmatically

Display imageview on button click event and set imageview visibility invisible after app run time dynamically.

ImageView supports two different type of visibility methods that can hide and show imageview on another button click event. So here is the complete step by step tutorial for Hide Show imageview on button click android programmatically.

android-project-download-code-button

How to Hide Show imageview on button click android programmatically.

Copy image inside drawable-hdpi folder.

drawable hdpi folder

Download below image and put it into drawable-hdpi folder.

image_view_check
Code for MainActivity.java file.

 package com.showhideimageview_android_examples.com;

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

public class MainActivity extends Activity {

 ImageView sampleImage;
 Button show,hide;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 sampleImage = (ImageView)findViewById(R.id.imageView1);
 show = (Button)findViewById(R.id.button2);
 hide = (Button)findViewById(R.id.button1);
 
 show.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 sampleImage.setVisibility(View.VISIBLE);
 
 }
 });
 
 hide.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 sampleImage.setVisibility(View.INVISIBLE);
 
 }
 });
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.showhideimageview_android_examples.com.MainActivity" >

 <ImageView
 android:id="@+id/imageView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="16dp"
 android:src="@drawable/image_view_check" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/imageView1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="22dp"
 android:text="Click here to hide imageView image" />

 <Button
 android:id="@+id/button2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/button1"
 android:layout_centerHorizontal="true"
 android:text="Click here to Show imageView image" />
 
</RelativeLayout>

Screenshot:

Hide Show imageview on button click android programmatically

hide imageview

Click Here to download Hide Show imageview on button click android programmatically project.

Leave a Reply

Your email address will not be published. Required fields are marked *