0

I'd like to achieve such a layout, where user got 2 control panels. He is able to switch from first to second by pressing button and vice versa.

Already have tried to use LayoutInflater, however, without success :/

The main reason, why doing it with 2 different layouts is, that buttons will be almost on the same position, so i'd like to prevent all that mess in one layout and create 2 separate control panel layouts.

Here are my layouts:

main.xml

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/control_panel_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="5">


        <!-- Here comes including layouts-->

    </RelativeLayout>

</LinearLayout>

control_panel_1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/control_panel1"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_action1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_action_selector"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:id="@+id/btn_action2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_action_selector2"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/btn_action1"
        android:layout_marginRight="50dp"/>

    <Button
        android:id="@+id/btn_action3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_action_selector3"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/btn_action1"
        android:layout_marginLeft="50dp" />

</RelativeLayout>

control_panel_2.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/control_panel1"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_action3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_action_selector4"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:id="@+id/btn_action4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_action_selector5"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/btn_action3"
        android:layout_marginTop="20dp"
        android:layout_marginRight="60dp"/>

</RelativeLayout>

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.layout_root);
    RelativeLayout controlPanelLayout = (RelativeLayout) findViewById(R.id.control_panel_layout);


    //Inflate first control panel on activity start
    LayoutInflater layoutInflater = (LayoutInflater) 
        this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View controlPanel1 = layoutInflater.inflate(R.layout.control_panel_1.xml);

    controlPanelLayout.addView(controlPanel1)
}

EDIT: enter image description here

As shown in the image, let's say activity starts with Screen1 and once user press Btn1, Screen2 appears...as you can see, only control panel has been switched.

however, it won't inflate that layout at start of application...

Thanks in advance for any suggestions, hints...

ligi
  • 39,001
  • 44
  • 144
  • 244
jpact
  • 1,042
  • 10
  • 23
  • It will be good if you share a screenshot – SweetWisher ツ Oct 18 '14 at 10:24
  • 1
    You could use Fragments for that. It's easy to have a Fragment placeholder and swap the contained Fragment. – Phantômaxx Oct 18 '14 at 10:24
  • Looks like fragments will be the best idea... thank you guys :) – jpact Oct 18 '14 at 10:42
  • @Creck if you go with `Fragment`s also look into [`ViewPager`](http://developer.android.com/reference/android/support/v4/view/ViewPager.html) or [another `ViewPager`](http://stackoverflow.com/questions/18413309/how-to-implement-a-viewpager-with-different-fragments-layouts) instead of relying on buttons, swiping is nicer, and also it gives you an animation when you call `setCurrentItem` on it. – TWiStErRob Oct 18 '14 at 11:00

1 Answers1

0

Inflate your control panel in onCreateView() and when handling button click (to change panel). The code should be somewhat like this:

private void inflateYourPanel(int panelLayoutID, ViewGroup placeholder) {
        placeholder.removeAllViews();

        View view = LayoutInflater.from(mActivity).inflate(panelLayoutID, placeholder, false);

        //find your views and set values and listeners

        placeholder.addView(view);
    }


Edit:
placeholder may be any layout (control_panel_layout) inflated when starting activity etc

Still may be you'd better look at Fragments - it may fit your purpose better and provide more scalability

sberezin
  • 3,266
  • 23
  • 28
  • Thanks for reply dude, looks like at the end i'll use fragments as you guys mentioned. – jpact Oct 18 '14 at 10:41
  • 1
    still the code like I posted works for me - it'll be useful for dynamic content anyway (where lists won't do the job for some reason) – sberezin Oct 18 '14 at 10:49