Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 860
Ollie C Avatar asked Apr 16 '12 16:04

Ollie C


1 Answers

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>
like image 184
irio Avatar answered Nov 13 '22 12:11

irio