Ollie C
Ollie C

Reputation: 28509

Align ActionBar Navigation Tab icons above the tab text?

The app has an ActionBar, and the ActionBar has Navigation Tabs, each containing text. I need to add icons to the tabs, in addition to the text.

I have added the icons using ActionBar.Tab.setIcon(drawable) successfully, but the icon shows to the left of the tab text.

How do I move the tab icon so it is ABOVE the navigation tab text?

(I'm using ActionBarSherlock, but figure the solution to this will work across native and ABS implementations)

Upvotes: 10

Views: 11037

Answers (3)

Sujiz
Sujiz

Reputation: 1740

its very simple just add android:drawableTop="@drawable/your_image" to your Textview and remove the ImageView

<?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:orientation="vertical" >

<TextView
    android:id="@+id/tab_to_opine_title"
    android:layout_width="wrap_content"
    android:drawableTop="@drawable/your_image" 
    android:layout_height="wrap_content" />

</LinearLayout>

Upvotes: 0

irio
irio

Reputation: 1401

You can set a custom view to each tab.

https://gist.github.com/3167287

PropositionListActivity.java

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockActivity;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ImageView;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.NavUtils;

public class PropositionListActivity extends SherlockActivity implements ActionBar.TabListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 

        setContentView(R.layout.proposition_list);

        getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        createTab(R.layout.tab_to_opine, R.id.tab_to_opine_title, "Vou Opinar");
        createTab(R.layout.tab_opined, R.id.tab_opined_title, "Já Opinei");
        createTab(R.layout.tab_feedback, R.id.tab_feedback_title, "Feedback");
    }

    public void createTab(int view, int titleView, String title) {
        ActionBar.Tab tab = getSupportActionBar().newTab();
        tab.setTabListener(this);
        tab.setCustomView(view);
        getSupportActionBar().addTab(tab);

        ((TextView) findViewById(titleView)).setText(title);
    }
}

tab_to_opine.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="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/tab_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/tab_to_opine"
        android:background="@android:color/transparent" />

    <TextView
        android:id="@+id/tab_to_opine_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

Upvotes: 12

chuckliddell0
chuckliddell0

Reputation: 2061

In the styles.xml file you can add an <item name="android:drawableTop">@drawable/your_image.</item> This will work if you want all of the images to be the same across the top. I don't quite know how to do this for tabs that use different images yet unfortunately.

Upvotes: 1

Related Questions