Reputation: 1652
I am using a LinearLayout
with various other layouts to act as a menu bar at the bottom of two of my AXML files in Xamarin (Android development atm).
On my main activity I have set click events to switch between two AXML views, both of which include the LinearLayout
menu bar.
The first button click works, and switches the layout, however after that it appears none of my click events work anymore. I have tried re initializing the click events, inside the click events but to no avail.
Please consider the following code:
Main.AXML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/MyButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Main Page" />
<include
layout="@layout/menu_bar" />
</LinearLayout>
Other.AXML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/MyButton2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Other Page" />
<include
layout="@layout/menu_bar" />
</LinearLayout>
MainActivity.cs
using Android.App;
using Android.Widget;
using Android.OS;
namespace Tracker
{
[Activity(Label = "Tracker", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
LinearLayout newsMenuItemLayout = FindViewById<LinearLayout>(Resource.Id.newsMenuItem);
LinearLayout friendsMenuItemLayout = FindViewById<LinearLayout>(Resource.Id.friendsMenuItem);
newsMenuItemLayout.Click += (sender, args) =>
{
SetContentView(Resource.Layout.Other);
};
friendsMenuItemLayout.Click += (sender, args) =>
{
SetContentView(Resource.Layout.Main);
};
}
}
menu_bar.AXML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout03"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="75dp"
android:weightSum="100"
android:layout_alignParentBottom="true">
<LinearLayout
android:orientation="vertical"
android:layout_weight="20"
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/newsMenuItem"
android:background="@drawable/selector"
android:paddingTop="6dp"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">
<ImageView
android:src="@drawable/news_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/imageView1"
android:scaleType="fitCenter"
android:adjustViewBounds="false" />
<TextView
android:text="News"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_weight="20"
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/eventsMenuItem"
android:background="@drawable/selector"
android:paddingTop="6dp"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">
<ImageView
android:src="@drawable/events_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/imageView2"
android:scaleType="fitCenter"
android:adjustViewBounds="false" />
<TextView
android:text="Events"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_weight="20"
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/resultsMenuItem"
android:background="@drawable/selector"
android:paddingTop="6dp"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">
<ImageView
android:src="@drawable/results_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/imageView3"
android:scaleType="fitCenter"
android:adjustViewBounds="false" />
<TextView
android:text="Results"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView3"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_weight="20"
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/friendsMenuItem"
android:background="@drawable/selector"
android:paddingTop="6dp"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">
<ImageView
android:src="@drawable/friends_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/imageView4"
android:scaleType="fitCenter"
android:adjustViewBounds="false" />
<TextView
android:text="Friends"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView4"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_weight="20"
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/moreMenuItem"
android:background="@drawable/selector"
android:paddingTop="6dp"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">
<ImageView
android:src="@drawable/more_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/imageView5"
android:scaleType="fitCenter"
android:adjustViewBounds="false" />
<TextView
android:text="More"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView5"
android:gravity="center_horizontal" />
</LinearLayout>
I assume that as I am reloading my menu_bar, I need to reinitialise my click events, but how would be the best way to go about that?
I did previous load my menu bar as an ActionBar
which I believe is a separate entity to the content view, but as this was impossible to move from the top, I had to rethink the approach.
Any ideas/explanations as to how I can go about this are greatly appreciated.
Upvotes: 1
Views: 1520
Reputation: 61
I'm not entirely sure why this behavior occurs however A solution could be is to include both your layouts in your main layout with one hidden. Switch visibility in the places where you use SetContentView.
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include
android:id="@+id/FirstLayout"
layout="@layout/first" />
<include
android:id="@+id/SecondLayout"
android:visibility="gone"
layout="@layout/second" />
<include
layout="@layout/menu_bar" />
</LinearLayout>
first.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/MyButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Main Page" />
</LinearLayout>
second.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/MyButton2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Main Page" />
</LinearLayout>
Activity
using Android.App;
using Android.Widget;
using Android.OS;
namespace Tracker
{
[Activity(Label = "Tracker", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
LinearLayout newsMenuItemLayout = FindViewById<LinearLayout>(Resource.Id.newsMenuItem);
LinearLayout friendsMenuItemLayout = FindViewById<LinearLayout>(Resource.Id.friendsMenuItem);
LinearLayout firstlayout = FindViewById<LinearLayout>(Resource.Id.FirstLayout);
LinearLayout secondlayout = FindViewById<LinearLayout>(Resource.Id.SecondLayout);
newsMenuItemLayout.Click += (sender, args) =>
{
firstlayout.Visibility = ViewStates.Visible;
secondlayout.Visibility = ViewStates.Gone;
};
friendsMenuItemLayout.Click += (sender, args) =>
{
firstlayout.Visibility = ViewStates.Gone;
secondlayout.Visibility = ViewStates.Visible;
};
}
}
}
Upvotes: 2