How to set Visibility for a button from another activity in Android Studio - RSM Developer

set Visibility for a button

 Step 1: Creating a new project

  • Open a new project.
  • We will be working on Empty Activity with language as Java. Leave all other options unchanged.
  • You can change the name of the project at your convenience.
  • There will be two default files named activity_main.xml and MainActivity.java.

Step 2: Open res -> layout ->activity_main.xml (or) main.xml and add following code:

In this step we open an XML file and add the code :-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:background="@color/white"
tools:context=".MainActivity">

<Button
android:id="@+id/Button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:backgroundTint="@color/teal_700"
android:text="Button1" />
<Button
android:id="@+id/Button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:backgroundTint="@color/teal_700"
android:text="Button2" />
<Button
android:id="@+id/Button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:backgroundTint="@color/teal_700"
android:text="Button3" />
<Button
android:id="@+id/Button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:backgroundTint="@color/teal_700"
android:text="Button4" />
</LinearLayout>

Step 3: Open Java -> package – > MainActivity.Java and add following code:

In this step we open an Java file and add the code :-

package com.rsmdeveloper.visibility;
//===============================================================
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;
//===============================================================
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);
Button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Button 1
Intent nextActivity = new Intent(MainActivity.this, MainActivity2.class);
Bundle bundle = new Bundle();
bundle.putInt("VAL", 1);
nextActivity.putExtras(bundle);
startActivity(nextActivity);
}
});
//===============================================================
Button2 = findViewById(R.id.Button2);
Button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Button 2
Intent nextActivity = new Intent(MainActivity.this, MainActivity2.class);
Bundle bundle = new Bundle();
bundle.putInt("VAL", 2);
nextActivity.putExtras(bundle);
startActivity(nextActivity);
}
});
//===============================================================
Button3 = findViewById(R.id.Button3);
Button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Button 3
Intent nextActivity = new Intent(MainActivity.this, MainActivity2.class);
Bundle bundle = new Bundle();
bundle.putInt("VAL", 3);
nextActivity.putExtras(bundle);
startActivity(nextActivity);
}
});
//===============================================================
Button4 = findViewById(R.id.Button4);
Button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Button 4
Intent nextActivity = new Intent(MainActivity.this, MainActivity2.class);
Bundle bundle = new Bundle();
bundle.putInt("VAL", 4);
nextActivity.putExtras(bundle);
startActivity(nextActivity);
}
});
//===============================================================

}
}

Step 4: Create a New Empty Activity with language as Java :-Open ->res -> layout ->activity_main2.xml and add following code:

In this step we open an XML file and add the code :-

<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity2">

<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>

Step 5: Open -> package – > MainActivity2.Java and add the following code.

In this step we open an Java file and add the code :-
package com.rsmdeveloper.visibility;
//===============================================================
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;
//===============================================================
public class MainActivity2 extends AppCompatActivity {
//===============================================================
LinearLayout Activity1, Activity2, Activity3, Activity4;
//===============================================================
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//===============================================================
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);
}//===============================================================

}
}

Output:

Now run the App and you will see main topics and sub-topics listed.....

Watch Video




Post a Comment

Previous Post Next Post

যোগাযোগ ফর্ম