How to set border of each tab in tabhost - android

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.

Related

Why is TextView not aligned as coded?

The text view tv_title is always displayed above the activity even though when I've set bottom margin 0dp.
Here is the screenshot:
Also when bottom margin is 10 dp, the text view goes further up:
I hope I understood the problem, since the question isn't clear (I don't understand what's the outcome you want) and you didn't add your whole xml code.
tv_title and tv_desc have 120dp space between them because tv_desc has marginTop 120dp. When you add marginBottom = 10dp to tv_title, the space equals to 130dp.
In RelativeLayout, if you want to display tv_title beneath tv_desc, add layout_below field to tv_title.
And it would be better for you to see some RelativeLayout examples.
May be you are using noactionbar theme so it does't appear . but if you want using without actionbar than just use :
android:layout_marginTop="?attr/actionBarSize"
this may help.
or Use :
remove android:layout_above="#+id/tv_desc" from tv_title textview. and add android:layout_below="#+id/tv_title" in tv_desc textview will help you.

Change the padding of TextView in TabLayout

I want to change the padding on the textview in the TabLayout. I have a tab that's called Subcategories. This is breaking line wrapping into two lines. Is there a way to do this?
There are answers only concerning the old TabHost view, I am using the TabLayout used by Google's Material Design library using android.support.design.widget.TabLayout.
For TabHost: How to add padding to a tabs label?
The TextView is not padded out, but the layout surrounding the textview is, so change the LinearLayout's padding:
int tabIndex = 0;
LinearLayout layout = ((LinearLayout)((LinearLayout)mTabLayout.getChildAt(0)).getChildAt(tabIndex));
layout.setPadding(0, 0, 0, 0);

How to change customized text color of selected tab by programmatic in android?

I am using tab widget for my application. My code is here main.xml http://pastie.org/8491814 and tab_layout.xml http://pastie.org/8491816. In this main.xml I created separate linear layout for my tab text.I can able to change my tab image while selecting tab using by selector. Now i want to change my text view color while selecting tab. My TabActivity http://pastie.org/8491823. How can i chnage my text view color of tab? Can any body help me? Thanks in advance.
try this :
for (int i = 0;i<mTabHost.getTabWidget().getChildCount(); i++) {
mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tab);
TextView tv = (TextView) mTabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
tv.setTextColor(Color.parseColor("#000000"));
}
TextView t = (TextView) mTabHost.getCurrentTabView().findViewById(android.R.id.title); //for Selected Tab
t.setTextColor(Color.parseColor("#FFFFFF"));
You can change the color of tab text and background of tab

How can I make a tab image larger?

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.

how to change the tab widget size in TabHost

In my application i use the tabhost in linear layout, tabhost contains 4 tabs. The size of the tabwidget is very very high and it shows title very small. Can any one help me how to reduce the size of tabwidget.
I would insist to create custom Tabs using setIndicator(View). So, create a TextView of your desired size and set it to the TabWidget. Here is an Tutorial for the same with Example.
You can just inflate a TextView and set it to the TabWidget using
View view = LayoutInflater.from(context).inflate(R.layout.your_custom_tab-layout,
null);
TextView tv = (TextView) view.findViewById(R.id.my_text_view);
TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tv).
setContent(intent);
mTabHost.addTab(setContent);
I'm not clear on exactly what you're looking to do here, but I'll make a guess at it.
If you don't want space to be taken up by graphics (in other words, have text only) you can set the height and graviy of the widget to effectively remove the space that is used by the graphic, making for a much more compact tab label (vertically).
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:gravity="bottom" />

Categories

Resources