Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Drawing a line/border in toolbar layout?

I have the requirement that the appBar should look like this:

enter image description here

The background is simply transparent.

I use this layout to get an appBar (details hidden):

<android.support.v7.widget.Toolbar>
  <TextView>
  <ImageView>
</android.support.v7.widget.Toolbar>

Simply adding a view with height=1dp below the ImageView with layout_gravity="bottom won't work.

Is it even possible to draw a line or a border in the ToolbarLayout?

like image 954
McOzD Avatar asked Aug 26 '15 07:08

McOzD


People also ask

How to add border to the top and bottom of Android view?

Android Apps/Applications Mobile Development This example demonstrates how to add a border to the top and bottom of an Android View. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/drawable/border_top_bottom.xml

How do I add a border to a linearlayout in Android?

Drag and drop a LinearLayout onto the screen and set the border background ( android:background="@drawable/customborder" ). In this example the existing single TextView is moved onto the LinearLayout.

How do I draw a line in Android?

This example demonstrates how do I draw a line in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Let's try to run your application.

How to add a border to the top and bottom?

How to add a border to the top and bottom of an Android View? This example demonstrates how to add a border to the top and bottom of an Android View . Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.


2 Answers

Using RelativeLayout like this:

<android.support.v7.widget.Toolbar>
    <RelativeLayout>
         <TextView>
         <ImageView>
         <View  android:layout_width="match_parent" 
                android:layout_height="1dp"
                android:layout_below="@+id/button_menu"/>
    </RelativeLayout>
</android.support.v7.widget.Toolbar>
like image 131
Norutan Avatar answered Sep 21 '22 14:09

Norutan


Wrap the Toolbar in an AppBarLayout (which extends LinearLayout) and add the line as a view there:

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    ...>

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        .../>

    <View android:layout_width="match_parent"
          android:layout_height="1dp"
          android:background="..."
          />

</android.support.design.widget.AppBarLayout>

With AppBarLayout there will be a default "key line" on Android 21 and up and I had to turn it off in code with:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    findViewById (R.id.app_bar).setOutlineProvider (null);
}
like image 40
dkneller Avatar answered Sep 20 '22 14:09

dkneller