Vishesh Chandra
Vishesh Chandra

Reputation: 7071

Android, How to add icon in TabButton in my code

Here i make 5 Tab buttons that are working proper but now i want to add icon with each Tab, How to add tab icon respect to Tab buttons.... Please guide/help me..

i am using stackOverflow first time please tell if here i missed any steps while posting this code... Thanks in advance

package com.vishesh.soapbox;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.TabSpec;

public class Start extends TabActivity {
private TabHost tabHost;
//Resources res=getResources();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.start);
    //textView for signout from application 
    TextView signout=(TextView)findViewById(R.id.signout);
    signout.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
        finish();   
        }
    });

    // scan button use for read the BarCode through RedLaser
    Button scan=(Button)findViewById(R.id.scan);
    final Intent intent=new Intent(this,RLSample.class);
    scan.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            startActivity(intent);
        }
    });

    //for Tab button

    tabHost=getTabHost();
    setupTabhost();

    Intent intent1=new Intent().setClass(this, SoapBox.class);
    setupTab(new TextView(this),"SoapBox", intent1);
    Intent intent2=new Intent().setClass(this, Profile.class);
    setupTab(new TextView(this),"Profile", intent2);
    Intent intent3=new Intent().setClass(this, Challenges.class);
    setupTab(new TextView(this),"Challenges", intent3);
    Intent intent4=new Intent().setClass(this, Vault.class);
    setupTab(new TextView(this),"Vault", intent4);
    Intent intent5=new Intent().setClass(this, More.class);
    setupTab(new TextView(this),"More", intent5);
    tabHost.setCurrentTab(0);
    }
    private void setupTabhost()
    {
        tabHost=(TabHost)findViewById(android.R.id.tabhost);
        tabHost.setup();
    }
    private void setupTab(final View view, final String tag, Intent intent)
    {
        View tabView=createTabView(tabHost.getContext(),tag);
        TabSpec tabSpec=tabHost.newTabSpec(tag).setIndicator(tabView).setContent(intent);
        tabHost.addTab(tabSpec);
    }
    private static View createTabView(final Context context, final String text)
    {
        View view=LayoutInflater.from(context).inflate(R.layout.start_tabs_bg, null);
        TextView textView=(TextView)view.findViewById(R.id.tabsText);
        textView.setText(text);
        return view;
    }
}

Upvotes: 3

Views: 2533

Answers (1)

Will Tate
Will Tate

Reputation: 33509

Vishesh,

If you look at the example HERE...

spec = tabHost.newTabSpec("albums").setIndicator("Albums",
                      res.getDrawable(R.drawable.ic_tab_albums))
                  .setContent(intent);

The drawable icon is passed in the call to setIndicator(). And you could replace both "Albums" strings with your tag variable.

Although, it currently looks like you are using a custom View for your tabs. You would just have to add an ImageView to the layout and set the drawable to the icon you desire.

Upvotes: 4

Related Questions