How to Hide or Show Images or Buttons on Button Click| How to Visible and Invisible Tutorial

How to Hide or Show Images or Buttons on Button Click| How to Visible and Invisible Tutorial




Full tutorial is given below. Dear Viewer we are working hard to maintain this website. We doesn't want any think from you. Its just a humble request If you thinks that this posts helps you please share this post with your friends.


How to hide or unhide any object or element on button on click, or visible or invisible any object or file or element in app in android studio


So first of all add this code into your layout activity or simply in activity_main.xml

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <ViewStub
        android:layout_marginTop="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/viewStub"
        android:layout_centerInParent="true"
        android:layout="@layout/new_layout"
        android:layout_alignParentTop="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show"
        android:id="@+id/show"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="65dp"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hide"
        android:id="@+id/hide"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginRight="65dp"
        android:layout_marginBottom="20dp"/>


</RelativeLayout>







After designing activity_main layout click on 
>> res 
>> then layout and right click on layout
>> then New
>> then Layout Resource File

>> then give name to your layout file and click on ok

>> then add this code into your this layout file



new_layout.xml


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

    
    <!--    image view-->
    <ImageView
        android:layout_width="350dp"
        android:layout_height="300dp"
        android:id="@+id/imageView"
        android:scaleType="centerInside"
        android:background="@drawable/usmtip_1_croped"/>



</LinearLayout>













To import image click on 
>> Resource Manager
>> then drag and drop your image/images into drawable section
>> then click on Project and start your coding






And then add this code into your .java file


MainActivity.java


package com.usmtip.demoappbyusmtip;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    
    
    //    declare your variables
    ViewStub viewStub;
    Button btnShow,btnHide;

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

        //     find by id
        btnShow=(Button)findViewById(R.id.show);
        btnHide=(Button)findViewById(R.id.hide);
        
        viewStub=(ViewStub)findViewById(R.id.viewStub);
        viewStub.inflate();
        
        
        
        //   set onclick listener on buttons
        btnShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewStub.setVisibility(View.VISIBLE);
            }
        });
        btnHide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewStub.setVisibility(View.GONE);
            }
        });
    }
}













All Server Links to Download This File:



Previous Post
Next Post

post written by:

0 Comments: