Nikhil Kumar
Nikhil Kumar

Reputation: 141

How to show two layouts in a single class file using android studio

I have a mainactivity and a button inside it so that once i click it, i should show two layouts in a single activity.

My main file:

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void settings(View view){
    Intent intent = new Intent(getApplicationContext(),
                                     Settings_activity.class);
    startActivity(intent);
}

Settings_activity Class file where i have to access the Profile and
notification layouts:

public class Settings_activity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    "I need help here to write the required code"
}

activity main file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp" >
        <Button
            android:id="@+id/settings"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="settings"
            android:text="Settings"/>


    </LinearLayout>

    </LinearLayout> 

Profile Layout:

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Family"
    android:background="@color/highlighted_text_material_dark"
    android:id="@+id/btnfamily"
    android:layout_gravity="center_horizontal"/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Friends"
    android:background="#FFFFFF"
    android:id="@+id/btnfriends"
    android:layout_gravity="center_horizontal" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Health"
    android:id="@+id/btnhealth"
    android:layout_gravity="center_horizontal" />
</LinearLayout>

Notification Layout:

<?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"
android:background="#454545">
<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hii..."
    android:textSize="30dp"
    android:layout_marginTop="50dp"
    android:layout_marginLeft="30dp"
    android:textColor="#FFFFFF"/>
<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="My first notification...."
    android:textSize="30dp"
    android:layout_marginLeft="30dp"
    android:layout_marginTop="50dp"
    android:textColor="#FFFFFF"/>
</LinearLayout>

Upvotes: 0

Views: 1100

Answers (1)

Vesko
Vesko

Reputation: 3760

Best way to do this is to include 2 Fragments in your Activity when the button is clicked. You can read more about Fragments HERE.

Another option would be to inflate both layouts with a LayoutInflater and just add them to your parent with parent.addView(), but I'd suggest you use the first option.

Or third (an easiest) option would be to include the two layouts you want to display in your main layout file, but add them with android:visibility="gone". Then on the button click, you just display them via notificationsLayout.setVisibility(View.VISIBLE).

Upvotes: 1

Related Questions