Attach Custom Toolbar in Your Android Mobile Application (Android Java) - The Quickest Way

 

Attach Custom Toolbar in Your Android Application - The Quickest Way

<Code Snippet for my personal reference>

If anyone else finds useful, it is their credit and kindness.

1. Add a LinearLayout to you activity where you want to show the toolbar

<LinearLayout
android:id="@+id/toolbarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:orientation="vertical">

<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />

</LinearLayout>

2. Add another layout toolbar under layout directory
    <?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="40dp"
app:titleTextAppearance="@style/Toolbar.TitleText"
app:titleTextColor="@color/colorPrimary">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal">

<ImageView
android:id="@+id/iv_backbutton"
android:layout_width="38dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:layout_weight="3"
android:src="@drawable/backbutton" />

<!-- android:fontFamily="@font/bungasai" -->
<TextView
android:id="@+id/textView_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textColor="@color/theme_color_five"
android:textSize="18sp"
android:visibility="visible" />

<Spinner
android:id="@+id/spinner_language"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="240dp"
android:layout_marginEnd="10dp"
android:layout_weight="6"
android:visibility="gone" />
</LinearLayout>
</androidx.appcompat.widget.Toolbar>

Add following code snippet in Themes.xml
<style name="Toolbar.TitleText" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
<item name="android:textSize">24sp</item>
<item name="android:textStyle">bold</item>
<item name="android:layout_gravity">center_horizontal</item>
<item name="android:fontFamily">@font/yesteryear_regular</item>
</style>

And finally, Add Code Snippet to MainActivity
// Define Variable
Toolbar toolbar;
toolbar = findViewById(R.id.toolbar);
/** Toolbar **/
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);

Also, do not forget to set 
android:layout_marginTop="50dp"
in the very next View that comes after toolbar.

Now run the App, you get few errors. Add few graphics, color elements to fix those missing element issues one after another.

You are done.

Comments

Popular posts from this blog

How to Add Animation to Android App in JAVA

Get Android App Version No and Display in a Text Field

How to Send Crash Report to Email / Mobile Device in Android Java