How to add icon at the top of action bar text in android

I am new to android. I am trying to implement one file with three tabs, each tab contains one icon and tab name. I managed to place the icon and text on each tab, but, unfortunately, the icon goes to the left side of the text (tab name) in the bookmark. I want a place icon at the top of the text instead of the left side. Please find a snippet of my code and please help me find a solution. Thanks in advance,

private void setActionBar() { ActionBar bar = getActionBar(); bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); bar.setDisplayShowHomeEnabled(false); bar.setDisplayShowTitleEnabled(false); ActionBar.Tab tabA = bar.newTab().setText("TabA"); tabA.setIcon(R.drawable.iconA); ActionBar.Tab tabB = bar.newTab().setText("TabB"); tabB.setIcon(R.drawable.iconB); ActionBar.Tab tabC = bar.newTab().setText("TabC"); tabC.setIcon(R.drawable.iconC); } 
+4
source share
1 answer

You can use a custom view to determine how you want to display your tabs.

  • define a custom layout where you have an image above the text
  • In your activity, inflate the presentation and set values ​​for your image and your text.
  • set custom view for tab

Here is an example:

CUSTOMS PLAN

 <?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:gravity="center_horizontal" android:orientation="vertical" > <ImageView android:id="@+id/tabIcon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:paddingTop="2dp" /> <TextView android:id="@+id/tabText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:textColor="#FFFFFF" /> </LinearLayout> 

INFLATE VIEW

 View tabView = activity.getLayoutInflater().inflate(R.layout.actiobar_tab, null); TextView tabText = (TextView) tabView.findViewById(R.id.tabText); tabText.setText(R.String.sometext); ImageView tabImage = (ImageView) tabView.findViewById(R.id.tabIcon); tabImage.setImageDrawable(activity.getResources().getDrawable(R.drawable.someimage)); 

INSTALL CUSTOMS VIEW FOR PROVIDED TABLE

 Tab tab = actionBar.newTab().setCustomView(tabView) 
+5
source

All Articles