Problems with implementing custom actionbar android

141 views Asked by At

I've created a custom action bar layout below. I want to implement this for one of my activities (the aim is to have it for all the activities but I'm testing with one for now). I use the following code to do this:

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_all_bets);
        getSupportActionBar().setDisplayShowHomeEnabled(false);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        LayoutInflater mInflater = LayoutInflater.from(this);

        View customView=getLayoutInflater().inflate(R.layout.action_bar, null);
        getSupportActionBar().setCustomView(customView);
        getSupportActionBar().setDisplayShowCustomEnabled(true);
}

However, instead of getting the actionbar, I get a mashup of things looking like this which doesn't even fill the full width of the screen. enter image description here

action_bar.xml

<?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="120dp"
    android:weightSum="100"
    android:background="#27044A"
    android:orientation="vertical">


    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_weight="15"
        android:layout_height="wrap_content">


        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_weight="30"
            android:layout_height="match_parent">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:textAlignment="center"
                android:textColor="#d35400"
                android:textStyle="bold"
                android:text="User"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:textColor="#d35400"
                android:layout_gravity="center_vertical"
                android:text="[email protected]"/>

        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_weight="25"
            android:layout_width="wrap_content"
            android:layout_height="match_parent">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:textAlignment="center"
                android:textColor="#d35400"
                android:textStyle="bold"
                android:text="Coins"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:textColor="#d35400"
                android:text="60000"/>
        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_weight="25"
            android:layout_height="match_parent">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:textAlignment="center"
                android:textStyle="bold"
                android:textColor="#d35400"
                android:text="Winnings"/>
            <TextView
                android:layout_width="match_parent"
                android:textColor="#d35400"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:text="35000"/>
        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_weight="20"
            android:layout_height="match_parent">

            <Button
                android:layout_width="match_parent"
                android:layout_height="33dp"
                android:text="Logout"
                android:textColor="#ffd35400"
                android:textSize="13sp"/>
        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="85"
        android:orientation="horizontal"
        android:weightSum="100">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="20"
            android:background="@drawable/selector"
            android:orientation="vertical"
            android:weightSum="100">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/football50"
                android:layout_weight="93"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="7"
                android:textStyle="bold"
                android:textColor="#d35400"
                android:text= "Bet Now!"
                android:gravity="center"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="20"
            android:background="@drawable/selector"
            android:orientation="vertical"
            android:weightSum="100">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src = "@drawable/chip50"
                android:layout_weight="93"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text = "My Bets"
                android:textStyle="bold"
                android:gravity="center"
                android:textColor="#d35400"
                android:layout_weight="7"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="20"
            android:background="@drawable/selector"
            android:orientation="vertical"
            android:weightSum="100">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/clover50"
                android:layout_weight="93"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text = "Coin Frenzy"
                android:textStyle="bold"
                android:gravity="center"
                android:textColor="#d35400"
                android:layout_weight="7"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:background="@drawable/selector"
            android:layout_weight="20"
            android:orientation="vertical"
            android:weightSum="100">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/trophy50"
                android:layout_weight="93"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text = "Rankings"
                android:textStyle="bold"
                android:gravity="center"
                android:textColor="#d35400"
                android:layout_weight="7"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="20"
            android:orientation="vertical"
            android:background="@drawable/selector"
            android:weightSum="100">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/moneybag50"
                android:layout_weight="93"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Shop"
                android:textStyle="bold"
                android:textColor="#d35400"
                android:gravity="center"
                android:layout_weight="7"/>
        </LinearLayout>

    </LinearLayout>
</LinearLayout>

enter image description here Intended look

2

There are 2 answers

8
adao7000 On BEST ANSWER

What do you mean by it not filling the width of the screen? The mashup is because you're trying to squeeze so many nested LinearLayouts and things together into one ActionBar. The three dots on the right is the overflow menu (which you can disable).

Check out the Android design guidelines for proper uses of the Toolbar (since ActionBar is deprecated). I think that you have more of a design issue than a programming issue.

1
saurabh1489 On

You are trying to add lot of views in the action bar.Usually the action bar layout is simple and clean.Mostly the action bar purpose is to show important updates and navigational views. If you still need this GUI, I think you can discard this action bar GUI and instead create reusable custom layout and place it on top of every activity.