2

I am building an app in Android and you can see a sample picture of it. In the red rectangle I have entered some Imagebuttons. When the user clicks one of the imagebuttons, the controls below of it should change. This is like what we have as tabs in a normal desktop application.
But I don't know how to implement that in Android and what kind of technique I should use. Should I use layouts and hide one and show another? Or is there something like a tab technique for layouts I should use?
I have seen that Salesforce has implemented this in it tablet app. So it must be possible. But I have looked almost anywhere and asked around, but couldn't find an answer to it, yet.
I would appreciate for any kind of hint, code sample or else that could show a way to solve that in Xamarin.Android.enter image description here

Code Pope
  • 5,075
  • 8
  • 26
  • 68
  • This is probably implemented with fragments. The highlighted area would be a FrameLayout that has a fragment added to it via a FragmentManager transaction when the button is pressed. I'll make a quick example. – matthewrdev Apr 09 '14 at 11:42

2 Answers2

2

Based on the layout you are showing here, I would assume that this is using a FrameLayout and then Fragments to control the content.

When the user presses the button, the frame layout is repopulated using a transaction. It looks something like this:

Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/buttonOne"
            android:text="One"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/buttonTwo"
            android:text="Two"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
    </LinearLayout>
    <FrameLayout
        android:id="@+id/frameContent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
</LinearLayout>

Activity:

public class MainActivity : Activity
{
    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        SetContentView (Resource.Layout.Main);

        var btnOne = this.FindViewById<Button> (Resource.Id.buttonOne);
        btnOne.Click += (object sender, EventArgs e) => {
            var fragment = new MyFragment(Resource.Layout.FrameLayoutOne);
            FragmentManager
                .BeginTransaction()
                .Replace(Resource.Id.frameContent, fragment)
                .Commit();
        };

        var btnTwo = this.FindViewById<Button> (Resource.Id.buttonTwo);
        btnTwo.Click += (object sender, EventArgs e) => {
            var fragment = new MyFragment(Resource.Layout.FrameLayoutTwo);
            FragmentManager
                .BeginTransaction()
                .Replace(Resource.Id.frameContent, fragment)
                .Commit();
        };
    }
}

Fragment:

public class MyFragment : Fragment
{
    int layoutId;
    public MyFragment(int layoutId)
    {
        this.layoutId = layoutId;
    }

    public override void OnCreate (Bundle savedInstanceState)
    {
        base.OnCreate (savedInstanceState);
    }

    public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        var view = inflater.Inflate (layoutId, container, false);
        return view;
    }
}

For a complete example, see this sample I'm hosting on my github:

FragmentTransactionSample

matthewrdev
  • 11,930
  • 5
  • 52
  • 64
0

First: You are independent of Xamarin, because Xamarin supports almost all native-android-API-classes. So if you find native Android code you should easily be able to translate it into Xamarin/C#.

So what you are looking for is a TabHost: See this tutorial for example:

http://androidituts.com/android-tab-layout-example/

For TabHost in Xamarin Documentation see:

http://docs.xamarin.com/guides/android/user_interface/tab_layout/

I would not recommend creating your custom tabview, but use the existing solutions.

edit:

If you want to embedd a tab-like control in an activity, than you could go for creating a Master-Detail-View as with the following:

  • Create a ListView at the left and a empty Fragment at the right
  • On ListView Item Click you have to load the proper Fragment into the right one
  • Wrap Everything within a Layout or in another Fragment to be portable

This problem has already been discussed in: Android Master Detail Flow Example

You can also create a new Master-Detail-Flow-Project in ADT-Eclipse. Try to understand what the generated code does. You can simply transform it to Xamarin C#.

Another posibility which I don't know in Detail is to use the MVVM Pattern with mvvmcross.

Community
  • 1
  • 1
Peter Ittner
  • 551
  • 2
  • 13
  • 1
    TabHost is not the solution to the issue. TabHost are tabs for the whole activity and they appear in the actionbar. And there are already tabs in the actionbar. But as you can see at the red part of the picture, there are buttons at the center of the page and when the user clicks the buttons, the layout below of it should change. So the list view on the left side and the controls above of the buttons should remain. So there must be different layout(one for Details, one for Task etc.) and they have to be swipped by cicking the image buttons. – CodeLover Apr 08 '14 at 20:25
  • @CodeLover is right. I tried out TabHost and the example in Xamarin. The problem with TabHost is, that it is added at the top of the page and not at the place that I have drawn with the red rectangle. But the bigger and more important problem with TabHost is the fact that when you click on a tab, a whole new activity is loaded. That is definitly not what I am looking for. I just want to change a layout (hide a table layout and show another one in the red rectangle) and the rest should remain unchanged. So please give me a hint how to fix that. – Code Pope Apr 09 '14 at 07:25