Ali pishkari
Ali pishkari

Reputation: 542

using <include> in xamarin android dynamically

I have a layout in xamarin with two parts: header and body depends on a property I have to change the header part. I created 2 layouts to use as header part : header1 and header2 I used tag in my xamarin android layout to add my header layout to main layout.

<include
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        layout="@layout/header1" />

but I can not change the layout property to header2 dynamically?

Upvotes: 4

Views: 2005

Answers (2)

Ali pishkari
Ali pishkari

Reputation: 542

LayoutInflater is the best solution :

LayoutInflater inflater = LayoutInflater.From(this);
 View  inflatedLayout = inflater.Inflate(Resource.Layout.yourlayput, null);                  
 LinearLayout ll =  this.Window.FindViewById<LinearLayout>(Resource.Id.container);
ll.AddView(inflatedLayout);

the function is very good!

Upvotes: 2

William Barbosa
William Barbosa

Reputation: 5005

I suggest using a ViewSwitcher, since you only need to switch between two layouts:

.axml

<ViewSwitcher
    android:id="@+id/headerSwitcher"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Your two headers go here -->

</ViewSwitcher>

Create a private field on your Activity/Fragment and initialize it in your OnCreate method:

private Switcher _headerSwitcher;

//Inside your OnCreate
_headerSwitcher = FindViewById<ViewSwitcher>(Resource.Id.headerSwitcher);

Then you can use _headerSwitcher.ShowNext() or _headerSwitcher.ShowPrevious() to change between your headers

Upvotes: 2

Related Questions