How to set Visibility for a button from another activity in android
Introduction :
This article shows How to set Visibility for a button from another activity in android. If you follow my steps can easyly control visibisity And Anythig frist activity to second activity in android studio projects.
Procedure :
- Open Your Android Studio or AIDE.
- Create a new empty Project.
Do this Activity1 :
- Go to activity_main.xml.
- Create Button.
- Go to Activity1.java File.
- Initialize Your Button.
- Add Click Event.
Do this Activity2 :
- Go to activity_second.xml and design your layout.
- Set id in every main layout what you gone/visible/invisible.
- Go to SecondActivity And Initialize Your Layout.
- Check Condation and Set Visibility.
Follow This For Activity1 :
activity1 xml :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/Button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:id="@+id/Button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button2" />
<Button
android:id="@+id/Button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button3" />
<Button
android:id="@+id/Button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button4" />
</LinearLayout>
Activity1.java : public class MainActivity extends AppCompatActivity {
Button Button1, Button2, Button3, Button4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button1 = findViewById(R.id.Button1);
Button2 = findViewById(R.id.Button2);
Button3 = findViewById(R.id.Button3);
Button4 = findViewById(R.id.Button4);
Button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Button 1
Intent nextActivity = new Intent(MainActivity.this, Activity2.class);
Bundle bundle = new Bundle();
bundle.putInt("VAL", 1);
nextActivity.putExtras(bundle);
startActivity(nextActivity);
}
});
Button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Button 2
Intent nextActivity = new Intent(MainActivity.this, Activity2.class);
Bundle bundle = new Bundle();
bundle.putInt("VAL", 2);
nextActivity.putExtras(bundle);
startActivity(nextActivity);
}
});
Button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Button 3
Intent nextActivity = new Intent(MainActivity.this, Activity2.class);
Bundle bundle = new Bundle();
bundle.putInt("VAL", 3);
nextActivity.putExtras(bundle);
startActivity(nextActivity);
}
});
Button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Button 4
Intent nextActivity = new Intent(MainActivity.this, Activity2.class);
Bundle bundle = new Bundle();
bundle.putInt("VAL", 4);
nextActivity.putExtras(bundle);
startActivity(nextActivity);
}
});
} // OnCreate Method Close Here ============
} // Public Class Close here ===================
Follow This For Activity2 :
activity_2.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=".Activity2">
<LinearLayout
android:visibility="gone"
android:id="@+id/Activity1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#8BC34A"
>
<TextView
android:text="Activity1"
android:textStyle="bold"
android:textColor="@color/black"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
/>
</LinearLayout>
<LinearLayout
android:visibility="gone"
android:id="@+id/Activity2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFEB3B"
>
<TextView
android:text="Activity2"
android:textStyle="bold"
android:textColor="@color/black"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
/>
</LinearLayout>
<LinearLayout
android:visibility="gone"
android:id="@+id/Activity3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF9800"
>
<TextView
android:text="Activity3"
android:textStyle="bold"
android:textColor="@color/black"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
/>
</LinearLayout>
<LinearLayout
android:visibility="gone"
android:id="@+id/Activity4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF5722"
>
<TextView
android:text="Activity4"
android:textStyle="bold"
android:textColor="@color/black"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
/>
</LinearLayout>
</RelativeLayout>
Activity2.java : public class Activity2 extends AppCompatActivity {
LinearLayout Activity1, Activity2, Activity3, Activity4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
Activity1 = findViewById(R.id.Activity1);
Activity2 = findViewById(R.id.Activity2);
Activity3 = findViewById(R.id.Activity3);
Activity4 = findViewById(R.id.Activity4);
Bundle bun=getIntent().getExtras();
int val =bun.getInt("VAL");
if(val==1) {
Activity1.setVisibility(View.VISIBLE);
Activity2.setVisibility(View.GONE);
Activity3.setVisibility(View.GONE);
Activity4.setVisibility(View.GONE);
} else if (val == 2){
Activity1.setVisibility(View.GONE);
Activity2.setVisibility(View.VISIBLE);
Activity3.setVisibility(View.GONE);
Activity4.setVisibility(View.GONE);
} else if (val == 3){
Activity1.setVisibility(View.GONE);
Activity2.setVisibility(View.GONE);
Activity3.setVisibility(View.VISIBLE);
Activity4.setVisibility(View.GONE);
} else if (val == 4){
Activity1.setVisibility(View.GONE);
Activity2.setVisibility(View.GONE);
Activity3.setVisibility(View.GONE);
Activity4.setVisibility(View.VISIBLE);
}
} // OnCreate Method close here ============
} // Public class close here====================
Layout Visible / Invisible And Gone Frist activity to second activity
Generating Source Code...