android - How to align icon to the right and text to the left in the App Bar?

Android - How to align icon to the right and text to the left in the App Bar?

To align an icon to the right and text to the left in the App Bar (Toolbar) in Android, you can use a combination of a Toolbar and a RelativeLayout or LinearLayout. Here's a step-by-step guide:

Step 1: Define Your Layout

Create a layout XML file for your Toolbar. Here's an example using a RelativeLayout:

<androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:title="Your Title" app:titleTextColor="@android:color/white"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/toolbar_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:text="Your Title" android:textColor="@android:color/white" android:textSize="20sp" /> <ImageView android:id="@+id/toolbar_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_centerVertical="true" android:src="@drawable/your_icon" /> <!-- Replace with your icon --> </RelativeLayout> </androidx.appcompat.widget.Toolbar> 

Step 2: Set Up Your Activity

In your activity, set the Toolbar as the support action bar:

import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.widget.Toolbar class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val toolbar: Toolbar = findViewById(R.id.toolbar) setSupportActionBar(toolbar) // Optional: If you want to remove the default title supportActionBar?.setDisplayShowTitleEnabled(false) } } 

Explanation

  1. Toolbar: The Toolbar serves as the action bar. Set its background color and title using attributes.

  2. RelativeLayout: Used to position the TextView and ImageView relative to each other.

    • The TextView is aligned to the start (left) of the Toolbar.
    • The ImageView is aligned to the end (right) of the Toolbar.
  3. Icon: Replace @drawable/your_icon with your actual icon resource.

Alternative: Using LinearLayout

If you prefer a LinearLayout, here's how you can do it:

<androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:gravity="center_vertical"> <TextView android:id="@+id/toolbar_title" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Your Title" android:textColor="@android:color/white" android:textSize="20sp" /> <ImageView android:id="@+id/toolbar_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/your_icon" /> </LinearLayout> </androidx.appcompat.widget.Toolbar> 

Summary

  • Use RelativeLayout or LinearLayout inside a Toolbar to align text and icon.
  • Set the Toolbar in your activity to manage the app bar.
  • Customize the title and icon as needed.

This setup will help you achieve the desired layout in your app's toolbar.

Examples

  1. Align icon to the right in App Bar

    • Description: Use a Toolbar with a TextView for the title aligned to the left and an ImageView for the icon aligned to the right.
    • Code:
      <androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:theme="@style/ThemeOverlay.AppCompat.ActionBar"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="start" android:text="Title" android:textColor="@android:color/white" android:textSize="20sp" android:textStyle="bold"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="end" android:src="@drawable/ic_icon" android:contentDescription="@string/icon_desc"/> </androidx.appcompat.widget.Toolbar> 
  2. How to center title in App Bar with icon to the right?

    • Description: Center the title using a RelativeLayout and position the icon to the right.
    • Code:
      <androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Title" android:textColor="@android:color/white" android:textSize="20sp" android:textStyle="bold"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_centerVertical="true" android:src="@drawable/ic_icon" android:contentDescription="@string/icon_desc"/> </RelativeLayout> </androidx.appcompat.widget.Toolbar> 
  3. Add menu icon to the right in Toolbar

    • Description: Use the menu attribute to add an icon to the right in the toolbar.

    • Code:

      <androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:theme="@style/ThemeOverlay.AppCompat.ActionBar" android:menu="@menu/toolbar_menu"> </androidx.appcompat.widget.Toolbar> 
      <!-- res/menu/toolbar_menu.xml --> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/action_icon" android:icon="@drawable/ic_icon" android:title="Icon" android:showAsAction="always"/> </menu> 
  4. How to use ConstraintLayout to align items in Toolbar?

    • Description: Utilize ConstraintLayout for flexible item positioning in a Toolbar.
    • Code:
      <androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary"> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Title" android:textColor="@android:color/white" android:textSize="20sp" android:textStyle="bold" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_icon" android:contentDescription="@string/icon_desc" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent"/> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.appcompat.widget.Toolbar> 
  5. Custom view in Toolbar to align text and icon

    • Description: Create a custom view with a LinearLayout to align text and icon within a Toolbar.
    • Code:
      <androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:gravity="center_vertical"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Title" android:textColor="@android:color/white" android:textSize="20sp" android:textStyle="bold"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_icon" android:contentDescription="@string/icon_desc"/> </LinearLayout> </androidx.appcompat.widget.Toolbar> 
  6. How to position multiple icons to the right in Toolbar?

    • Description: Add multiple icons to the right using the menu attribute.

    • Code:

      <androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:theme="@style/ThemeOverlay.AppCompat.ActionBar" android:menu="@menu/toolbar_menu"> </androidx.appcompat.widget.Toolbar> 
      <!-- res/menu/toolbar_menu.xml --> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/action_icon1" android:icon="@drawable/ic_icon1" android:title="Icon1" android:showAsAction="always"/> <item android:id="@+id/action_icon2" android:icon="@drawable/ic_icon2" android:title="Icon2" android:showAsAction="always"/> </menu> 
  7. Align custom title and icon in Toolbar programmatically

    • Description: Set a custom view programmatically to align title and icon.
    • Code:
      Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); LinearLayout customView = new LinearLayout(this); customView.setOrientation(LinearLayout.HORIZONTAL); customView.setLayoutParams(new Toolbar.LayoutParams( Toolbar.LayoutParams.MATCH_PARENT, Toolbar.LayoutParams.WRAP_CONTENT)); TextView title = new TextView(this); title.setText("Title"); title.setTextColor(Color.WHITE); title.setTextSize(20); title.setTypeface(null, Typeface.BOLD); title.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1)); ImageView icon = new ImageView(this); icon.setImageResource(R.drawable.ic_icon); icon.setContentDescription(getString(R.string.icon_desc)); customView.addView(title); customView.addView(icon); toolbar.addView(customView); 
  8. Align navigation icon to the left and title to the center in Toolbar

    • Description: Use a custom view in the Toolbar to center the title and keep the navigation icon on the left.
    • Code:
      <androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/navigation_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_centerVertical="true" android:src="@drawable/ic_navigation" android:contentDescription="@string/navigation_desc"/> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Title" android:textColor="@android:color/white" android:textSize="20sp" android:textStyle="bold"/> </RelativeLayout> </androidx.appcompat.widget.Toolbar> 
  9. How to use Data Binding to align items in Toolbar?

    • Description: Leverage Data Binding to bind and align title and icon in a Toolbar.
    • Code:
      <layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <variable name="viewModel" type="com.example.ViewModel"/> </data> <androidx.appcompat.widget.Toolbar android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="start" android:text="@{viewModel.title}" android:textColor="@android:color/white" android:textSize="20sp" android:textStyle="bold"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="end" android:src="@{viewModel.icon}" android:contentDescription="@string/icon_desc"/> </androidx.appcompat.widget.Toolbar> </layout> 
  10. Align search icon to the right in App Bar

    • Description: Use the menu attribute to add a search icon to the right in the Toolbar.

    • Code:

      <androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:theme="@style/ThemeOverlay.AppCompat.ActionBar" android:menu="@menu/toolbar_menu"> </androidx.appcompat.widget.Toolbar> 
      <!-- res/menu/toolbar_menu.xml --> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/action_search" android:icon="@drawable/ic_search" android:title="Search" android:showAsAction="always"/> </menu> 

More Tags

language-agnostic formatter uigesturerecognizer wikipedia-api export-to-csv nightwatch.js android-studio-3.1 angularjs-e2e signals-slots nexus3

More Programming Questions

More Electrochemistry Calculators

More Pregnancy Calculators

More Electronics Circuits Calculators

More Fitness-Health Calculators