2 Frames/layout in 1 Activity

1.2k views Asked by At

I very a newbie at android programming. I'm here because i want your help. Im designing an activity for my Thesis.

Heres the Image

I want to have an activity that has 2 Frames/Layout. The upper frame have buttons, the lower frame will display something when i click the button on the upper frame.

What would i use to make this happen?

Thank you in advance.

2

There are 2 answers

0
Miki Franko On BEST ANSWER

Here is the complete solution:

step1: create main_layout.xml file into layout folder:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="bottom">

        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button1"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"/>
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button2"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"/>
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="New Text"
        android:id="@+id/textView" />
</LinearLayout>

step2: create MainActivity:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;


public class MainActivity extends Activity {

private View mBtn1;
private View mBtn2;
private TextView mTxt;

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

    mBtn1 = findViewById(R.id.button1);
    mBtn2 = findViewById(R.id.button2);
    mTxt = (TextView)findViewById(R.id.textView);

    mBtn1.setOnClickListener(mBtnClickListener);
    mBtn2.setOnClickListener(mBtnClickListener);

}

View.OnClickListener mBtnClickListener = new View.OnClickListener() {

    /**
     * This is all sync because we want to have only one event at a time
     * @param v
     */
    @Override
    public synchronized void onClick(View v) {

        switch (v.getId()) {
            case R.id.button1:
                mTxt.setText("button1");
                break;
            case R.id.button2:
                mTxt.setText("button2");
                break;

        }
        }
    } ;
    }

step3: add the activty into your AndroidManifest.xml:

<activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
7
atok On

You can split the layout in half using LinearLayout and layout_weight param, like this:

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

    <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"></FrameLayout>

    <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"></FrameLayout>

</LinearLayout>

layout_weight parameter functions as a priority in deciding who gets the space, given the other desciption would cause any of FrameLayouts to fill entire parent. You can find more in documentation.