I am trying to add an icon to a tab in android and it isnt working.
here is part of my code, im not sure what is wrong
th = (TabHost) findViewById(R.id.thbodyview);
th.setup();
TabSpec specs = th.newTabSpec("tag1");
specs.setContent(R.id.Front);
specs.setIndicator("Front",getResources().getDrawable(R.drawable.tab_one));
th.addTab(specs);
When I run the app the tab just says "Front" and there is no icon, if someone could fix it that would be great.
Thanks
I know this question was asked over a year ago, but I shall provide an answer anyway, as it might be helpful to others wanting to know how this is possible.
The following code will enable you to retrieve the TextView and ImageView that form the title / heading of each tab (defined in the standard layout/tab_indicator.xml layout file):
TabHost tabHost = (TabHost) activity.findViewById(android.R.id.tabhost);
View currentTab = tabHost.getTabWidget().getChildAt(areaIndex);
or to get the view of the current tab that's in view:
View currentTab = tabHost.getCurrentTabView();
Then to actually get the Text and Image views:
TextView tabTitleField = (TextView) currentTab.findViewById(android.R.id.title);
ImageView tabTitleIcon = (ImageView) currentTab.findViewById(android.R.id.icon);
tabTitleIcon.setImageResource([specify your drawable resource here]);
Padding can be programmatically added to the ImageView using tabTitleIcon.setPadding(0, 0, 10, 0);
to add spacing between the title and the icon.
Related
I am using tabhost. I have two tabs in that. I can able to set border for tabwidget. But I don't know how to set border for each tab.
I tried to get current tab and convert the view as textview and had
set border but It was looking like view is wrapped,so the border
encloses the tab name with no gap .I don't know how to add padding
to make it as match_parent.
Here is my code,
int tab = TabHostWindow.getCurrentTab();
TextView tv1 = (TextView) TabHostWindow.getTabWidget().getChildAt(tab).findViewById(android.R.id.title);
tv1.setTextColor(Color.WHITE);
tv1.setBackgroundDrawable(getResources().getDrawable(R.drawable.tabdes));
Guide me please, Thanks in advance.
I'm hoping someone can help. I am looking to change the indicator colour of my tab widget.
I had used the action bar tabs and had them styled correctly, however, I am using a nav drawer and require it to open over the tabs so I have changed to a tabhost/tabwidget.
I have followed this link:
Customizing tab indicator images in android?
Here is the strange part, I can change the tab colour, the divider colour, but when I try to change the tabStripLeft and the tabStrip right it won't work. I also tried to remove the strip below the indicator but that wouldn't work
android:background="#color/tab"
android:divider="#color/appPink"
android:tabStripLeft="#color/appPink"
android:tabStripRight="#color/appPink"
android:tabStripEnabled = "false"
I have tried drawables for the indicator too in case you may think this may be the problem. Does anyone have any ideas on this?
Here is how I've set them up in my activity:
private FragmentTabHost mTabHost;
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.container);
mTabHost.addTab(mTabHost.newTabSpec("home").setIndicator("Home"), PlaceholderFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("favourites").setIndicator("Settings"), settings_fragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("settings").setIndicator("Favourites"), mark_list_fragment.class, null);
If anyone has any thoughts I'd appreciate hearing them.
I got this image in a tab but it's too small. how can I scale it up or make it fill the tab
final TabHost tabHost = getTabHost(); //edit: added this line
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("Discussion",
getResources().getDrawable(R.drawable.forum))
.setContent(new Intent(this, MyActivity.class)));
Set a LinearLayout as a child of the TabHost and add your image to that LinearLayout
You won't be able to with the method you're using. The drawable is mean't to be a small icon. If you want something custom, you'll have to use setIndicator(View) instead. I'd image you'd create an ImageView set to use the drawable you want and pass that in. If you wanted the text "Discussion" to still be there, you'd had to create a layout with an ImageView and TextView and use that.
This is how I want the tabs to look:
http://img14.imageshack.us/img14/5696/tabort.png
This is how they look using tabHost:
http://img684.imageshack.us/img684/1030/tabort2.png
So I want to remove the border around the images. Instead, I want to have the greyish background image behind the tabs. Can anyone please help me with this (I'm new to Android)?
Here is some relevant code:
// Create an Intent to launch an Activity for the tab
intent = new Intent().setClass(this, WashActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("washer").setIndicator("",
res.getDrawable(R.drawable.wash_tab))
.setContent(intent);
tabHost.addTab(spec);
You can use Buttons positioned side-by-side in Relative Layout with custom background images instead of TabView.
set custom view ( imageview or image with text in ur case ) using setView() instead setIndicator() will work for you .
TabHost is deprecated now . so better to use fragment with compatibility package .
Anyone know how facebook did this:
From what I know we cannot change the height of tabhost. I'm guessing that they laid the "Frank Cho" view over the tabhost to give it the appearance of being shorter but I may be wrong. Anyone know what's going on?
You actually can have custom looking tab widgets. You need to set the tab indicator to some custom layout (with your drawables) and you should be good to go.
Here's an semi-example:
final TabHost host = getTabHost();
final TextView indicator = (TextView) getLayoutInflater().inflate(
R.layout.tab_indicator,
getTabWidget(), false);
indicator.setText("Tab title");
host.addTab(host.newTabSpec("The tab tag")
.setIndicator(indicator)
.setContent([put your content here]));
}
Where the tab_indicator layout can look like this:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tab_label"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:textSize="14sp"
android:textStyle="bold"
android:minHeight="38dp"
android:background="#drawable/minitab" />
The minitab drawable is a drawable item selector (so you need to have an image for selected, default, pressed, and unselected). The facebook app used some white drawable image for the default tab and the blue gradient drawable for the unselected tabs.
Check out the Google IO Schedule app for a full working example: http://code.google.com/p/iosched/ (and specifically the TrackDetailActivity.java)
Somebody else had what I think is the correct answer, but deleted it for some reason...
The height in question is not that of the TabHost, but of the TabWidget.
Try using the version of setIndicator() that takes a View instead of just a String or String plus drawable resource. While I have not played with this yet, my understanding is that it solves this problem nicely.
Be careful, though, that you don't wind up with tabs that are too tough to tap.