android - inflating layout -



android - inflating layout -

i'd accomplish such layout, user got 2 command panels. able switch first sec pressing button , vice versa.

already have tried utilize layoutinflater, however, without success :/

the main reason, why doing 2 different layouts is, buttons on same position, i'd prevent mess in 1 layout , create 2 separate command panel layouts.

here 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 command 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:

as shown in image, let's activity starts screen1 , 1 time user press btn1, screen2 appears...as can see, command panel has been switched.

however, won't inflate layout @ start of application...

thanks in advance suggestions, hints...

inflate command panel in oncreateview() , when handling button click (to alter panel). code should this:

private void inflateyourpanel(int panellayoutid, viewgroup placeholder) { placeholder.removeallviews(); view view = layoutinflater.from(mactivity).inflate(panellayoutid, placeholder, false); //find views , set values , listeners placeholder.addview(view); }

edit: placeholder may layout (control_panel_layout) inflated when starting activity etc

still may you'd improve @ fragments - may fit purpose improve , provide more scalability

android android-layout android-activity layout-inflater android-inflate

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -