2dvisio
2dvisio

Reputation: 911

Sidebar in each activity

For an application I am designing we have to deal with some "special" needs of our users. One of these is to create a navigation sidebar that has to appear in almost all the activities.

This navigation bar has to contain always the same three buttons that links to three activities: HOME, INFO, CONFIGURATION.

Each of these three activities can also load others activities that can (or cannot) contain this navigation bar. Each of these buttons has to reset the current activity-stack status bringing to the top the corresponding activity selected by the user.

The navigation bar has to be customisable (made visible/invisible) and I would like also to deactivate some of the buttons.

EDIT: It has to be similar to a Drawer, but the buttons have to be highly customisable (in size and appearance) and it has to be always on (no sliding functions).

What is the best way to achieve that without manually including those buttons in each of my layouts?

Upvotes: 0

Views: 2717

Answers (1)

Michał Z.
Michał Z.

Reputation: 4119

You can create this layout manually but only once - in your ActivityBase class. Other activities can extend this base class.

EDIT:

As I said, I'm improving my answer. So my idea is to create an Activity with menu. And if other Activity needs to have the same menu it can extend this ActivityBase and add it's own layout. Let's look on a simple example.

ActivityBase layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <RelativeLayout
        android:id="@+id/menu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@android:color/black" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="Button" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/button1"
            android:layout_centerHorizontal="true"
            android:text="Button" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/button2"
            android:layout_alignBottom="@+id/button2"
            android:layout_alignParentRight="true"
            android:text="Button" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/menu" >
    </RelativeLayout>

</RelativeLayout>

As you can see I created a simple layout that contains menu bar and a container for layout from Activities that will be extending AcivityBase.

Now ActivityBase:

public class ActivityBase extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
    }


}

It's a simple Activity, but if you want, you can also place here menu events handling if they are the same for all Activities that will be extending this.

And now let's see at SecondActivity's layout:

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

</LinearLayout>

It's a normal layout, nothing special. I put some controls in there just for purpose of this example.

And SecondActivity class:

public class SecondActivity extends MainActivity {


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

        LayoutInflater inflater = getLayoutInflater();

        inflater.inflate(R.layout.second_activity, (ViewGroup) findViewById(R.id.container));
    }


}

It extends ActivityBase and what is important - it does not call setContentView. Instead we are creating LayoutInflater and we are inflating second_activity layout in the container that we created in activity_base layout.

Upvotes: 2

Related Questions