I have created a tab in my app. Now the thing is that when I set the background color of my tab to white it's working but only grey color underline still appears so can anyone tell me how can I remove that line?
I am sending my code and snapshot of where the underline arise.
Code for XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal"
android:background="#FFFFFF">
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp"/>
</LinearLayout>
</TabHost>
</LinearLayout>
Code for java:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources();
TabHost MainTabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, ContactListForm.class);
spec = MainTabHost.newTabSpec("Contacts").setIndicator("Contacts",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
MainTabHost.addTab(spec);
intent = new Intent().setClass(this, CallDialerForm.class);
spec = MainTabHost.newTabSpec("Call").setIndicator("Call",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
MainTabHost.addTab(spec);
intent = new Intent().setClass(this, MyInfoForm.class);
spec = MainTabHost.newTabSpec("My Info").setIndicator("MyInfo",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
MainTabHost.addTab(spec);
MainTabHost.setCurrentTab(0);
for (int j = 0; j < MainTabHost.getTabWidget().getChildCount(); j++)
{
MainTabHost.getTabWidget().getChildAt(j).setBackgroundColor(Color.parseColor("#FFFFFF"));
}
}
Snapshot:
Use this in your code where you setup tabs. Mind you it is supported only from SDK 8 onwards.
if(7 < Build.VERSION.SDK_INT){
tabHost.getTabWidget().setStripEnabled(false);
}
I was also same type of problem but i have removed it using the fallowing blow given code
tabHost = getTabHost(); // The activity TabHost
tabHost.setOnTabChangedListener(this);
Resources res = getResources(); // Resource object to get Drawables
tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Reusable TabSpec for each tab
TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
TabSpec secondTabSpec = tabHost.newTabSpec("tid2");
TabSpec thirdTabSpec = tabHost.newTabSpec("tid3");
TabSpec fourthTabSpec = tabHost.newTabSpec("tid4");
TabSpec fifthTabSpec = tabHost.newTabSpec("tid5");
viewCache[0] = LayoutInflater.from(this).inflate(R.layout.tabs1, null);
viewCache[1] = LayoutInflater.from(this).inflate(R.layout.tabs1, null);
viewCache[2] = LayoutInflater.from(this).inflate(R.layout.tabs1, null);
viewCache[3] = LayoutInflater.from(this).inflate(R.layout.tabs1, null);
viewCache[4] = LayoutInflater.from(this).inflate(R.layout.tabs1, null);
firstTabSpec.setIndicator(viewCache[0]);
secondTabSpec.setIndicator(viewCache[1]);
thirdTabSpec.setIndicator(viewCache[2]);
fourthTabSpec.setIndicator(viewCache[3]);
fifthTabSpec.setIndicator(viewCache[4]);
firstTabSpec.setContent(new Intent(this, HomeTabActivityGroup.class));
secondTabSpec
.setContent(new Intent(this, ProfileTabActivityGroup.class));
thirdTabSpec.setContent(new Intent(this,
NotificationTabActivityGroup.class));
fourthTabSpec.setContent(new Intent(this,
FavoritesTabActivityGroup.class));
fifthTabSpec
.setContent(new Intent(this, MoreTabActivityGroupNew.class));
tabHost.addTab(firstTabSpec);
tabHost.addTab(secondTabSpec);
tabHost.addTab(thirdTabSpec);
tabHost.addTab(fourthTabSpec);
tabHost.addTab(fifthTabSpec);
currentTabvalue = tabHost.getCurrentTab();
C2DMessaging.register(TennisAppActivity.mContext,
"racquetester#gmail.com");
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
// tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#7392B5"));
switch (i) {
case 0:
tabHost.getTabWidget().getChildAt(i)
.setBackgroundResource(R.drawable.home);
break;
case 1:
tabHost.getTabWidget().getChildAt(i)
.setBackgroundResource(R.drawable.profile);
break;
case 2:
tabHost.getTabWidget().getChildAt(i)
.setBackgroundResource(R.drawable.notifications);
break;
case 3:
tabHost.getTabWidget().getChildAt(i)
.setBackgroundResource(R.drawable.fav);
break;
case 4:
tabHost.getTabWidget().getChildAt(i)
.setBackgroundResource(R.drawable.more);
break;
}
}
//***************************************************
it is the xml file use for it.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="wrap_content" android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android" android:gravity="center">
<ImageView android:id="#+id/ImageView01" android:layout_width="wrap_content" android:layout_height="50dip"></ImageView>
I hope it is help full to you.
Related
This is my MainActivity.java:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Resources ressources = getResources();
TabHost tabHost = getTabHost();
registerReceiver(myBroadcastReceiver1, new IntentFilter(
MYBROADCAST_INTENTFILTER));
Intent intentFriends = new Intent().setClass(this,
TabFriendsActivity.class).addFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP);
TabSpec tabFriends = tabHost
.newTabSpec("Friends")
.setIndicator("",
ressources.getDrawable(R.drawable.btn_tab_friends))
.setContent(intentFriends);
// Talks tab
Intent intentTalks = new Intent()
.setClass(this, TabTalksActivity.class).addFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP);
TabSpec tabTalks = tabHost
.newTabSpec("Talks")
.setIndicator("",
ressources.getDrawable(R.drawable.btn_tab_talk))
.setContent(intentTalks);
// Add friends tab
Intent intentAddFriends = new Intent().setClass(this,
TabAddFriendsActivity.class).addFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP);
TabSpec tabAddFriends = tabHost
.newTabSpec("Add friends")
.setIndicator("",
ressources.getDrawable(R.drawable.btn_tab_addfriend))
.setContent(intentAddFriends);
// Other tab
Intent intentOther = new Intent()
.setClass(this, TabOtherActivity.class).addFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP);
TabSpec tabSpecOther = tabHost
.newTabSpec("Other")
.setIndicator("",
ressources.getDrawable(R.drawable.btn_tab_other))
.setContent(intentOther);
// add all tabs
tabHost.addTab(tabFriends);
tabHost.addTab(tabTalks);
tabHost.addTab(tabAddFriends);
tabHost.addTab(tabSpecOther);
// set Windows tab as default (zero based)
tabHost.setCurrentTab(0);
TabWidget tabs = (TabWidget) findViewById(android.R.id.tabs);
badge = new BadgeView(this, tabs, 1);
badge.setText("new");
}
I want to show text "new" in tabTalks when I got message from GCM by BroadcastReceiver. I try to debug and got information of "intentChatting" that means BroadcastReceiver is working, but text "new" not show in tabTalks icon.
private BroadcastReceiver myBroadcastReceiver1 = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String intentChatting = intent.getExtras().get("Intent").toString();
if (intentChatting != null) {
pushBadge(intentChatting);
}
}
};
private void pushBadge(String intentChating) {
badge.show();
}
And this is my main_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#f2f2f2">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="462dp"
android:layout_weight="0.07">
</FrameLayout>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000" />
</LinearLayout>
</TabHost>
So, what wrong with my code. I want to show badge in tabhost when receiver message from GCMby BroadcastReceiver. But it's not work!
Any help much appreciated! Thanks.
We have a strange problem concerning a TabHost-widget in Android. How can we fix the pixel-Line error (shown on the image below):
Thanks for your help.
I also have Same problem before some days. But now got solution with changing it to images.
By Default First Tab is selected and when the Tab Getting change the all tab get changed automatically based on the Tab Selection.
See below Code:
public class MainTabActivity extends TabActivity {
TabHost mTabHost;
TabWidget mTabWidget;
private TextView title;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_tab_layout);
//Resources res = getResources();
mTabHost = getTabHost();
TabHost.TabSpec spec;
mTabWidget = mTabHost.getTabWidget();
Intent intent;
intent = new Intent().setClass(MainTabActivity.this, TodaysDealsGroupActivity.class);
spec = mTabHost.newTabSpec(getResources().getString(R.string.tab_todays_deal)).setIndicator(getResources().getString(R.string.tab_todays_deal), getResources().getDrawable(R.drawable.tab_loyalshop_selector)).setContent(intent);
mTabHost.addTab(spec);
intent = new Intent().setClass(MainTabActivity.this, BuddiesGroupActivity.class);
spec = mTabHost.newTabSpec(getResources().getString(R.string.tab_buddies)).setIndicator(getResources().getString(R.string.tab_buddies), getResources().getDrawable(R.drawable.tab_buddies_selector)).setContent(intent);
mTabHost.addTab(spec);
intent = new Intent().setClass(MainTabActivity.this, SearchGroupActivity.class);
spec = mTabHost.newTabSpec(getResources().getString(R.string.tab_search)).setIndicator(getResources().getString(R.string.tab_search), getResources().getDrawable(R.drawable.tab_search_selector)).setContent(intent);
mTabHost.addTab(spec);
intent = new Intent().setClass(MainTabActivity.this, ProfileGroupActivity.class);
spec = mTabHost.newTabSpec(getResources().getString(R.string.tab_profile)).setIndicator(getResources().getString(R.string.tab_profile), getResources().getDrawable(R.drawable.tab_profile_selector)).setContent(intent);
mTabHost.addTab(spec);
intent = new Intent().setClass(MainTabActivity.this, NotificationsGroupActivity.class);
spec = mTabHost.newTabSpec(getResources().getString(R.string.tab_notifications)).setIndicator(getResources().getString(R.string.tab_notifications), getResources().getDrawable(R.drawable.tab_notification_selector)).setContent(intent);
mTabHost.addTab(spec);
for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
mTabHost.getTabWidget().getChildAt(i).setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_black_bg));
title = (TextView) mTabWidget.getChildAt(i).findViewById(android.R.id.title);
title.setTextColor(Color.WHITE);
title.setTextSize(10);
}
// check if App starts from the Notification click or not
if(getIntent().hasExtra("notification")){
// for the current tab selection
mTabHost.getTabWidget().getChildAt(4).setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_blue_bg));
mTabHost.setCurrentTab(4);
title = (TextView) mTabWidget.getChildAt(4).findViewById(android.R.id.title);
}else{
// for the current tab selection
mTabHost.getTabWidget().getChildAt(0).setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_blue_bg));
mTabHost.setCurrentTab(0);
title = (TextView) mTabWidget.getChildAt(0).findViewById(android.R.id.title);
}
title.setTextColor(Color.BLACK);
title.setTextSize(10);
// listener for the tab changed
mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
mTabHost.getTabWidget().getChildAt(i).setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_black_bg));
title = (TextView) mTabWidget.getChildAt(i).findViewById(android.R.id.title);
title.setTextColor(Color.WHITE);
title.setTextSize(10);
}
int tab = mTabHost.getCurrentTab();
mTabHost.getTabWidget().getChildAt(tab).setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_blue_bg));
title = (TextView) mTabWidget.getChildAt(tab).findViewById(android.R.id.title);
title.setTextColor(Color.BLACK);
title.setTextSize(10);
}
});
}
}
And here is the XMl file main_tab_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom|center_horizontal"
android:tabStripEnabled="false" >
</TabWidget>
</LinearLayout>
</TabHost>
</LinearLayout>
Just Replace with your images and See the Result.
Hope this will help you a lot as it help me.
Enjoy Coding...
I have three tabs and a custom divider, however a custom xml (customtabview.xml) is used for all five tabs.
Tabs.java
tabHost = getTabHost();
TabHost.TabSpec spec;
LinearLayout v = (LinearLayout)(getLayoutInflater().inflate(R.layout.customtabview, null));
//FIRST TAB
((TextView)v.findViewById(R.id.title)).setText("FIRST");
((TextView)v.findViewById(R.id.title)).setCompoundDrawablesWithIntrinsicBounds(0,R.drawable .tab_back,0,0 );
spec = tabHost.newTabSpec("first").setIndicator(v).setContent(R.id.tab_first_info);
tabHost.addTab(spec);
//SECOND TAB
((TextView)v.findViewById(R.id.title)).setText("SECOND");
((TextView)v.findViewById(R.id.title)).setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.tab_back,0,0 );
spec = tabHost.newTabSpec("second").setIndicator(v).setContent(R.id.tab_second_info);
tabHost.addTab(spec);
//THIRD TAB
((TextView)v.findViewById(R.id.title)).setText("THIRD");
((TextView)v.findViewById(R.id.title)).setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.tab_back,0,0 );
spec = tabHost.newTabSpec("third").setIndicator(v).setContent(R.id.tab_third_info);
tabHost.addTab(spec);
customtabview.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"
android:gravity="center" >
<TextView
android:id="#+id/title"
android:drawablePadding="4dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
I would like to create vertical tab on left. I had done a lot of research from the internet to find the solution. However, when i try to implement the suggested solution, I still failed to get the result that I want. Below is my code, i wish someone can help me to solve my problem. Very much appreciate if someone here can help me.
main.xml
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="#android:id/tabhost">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="#android:id/tabs"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_weight="0">
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_height="match_parent"
android:layout_width="0dip"
android:layout_weight="1"/>
</LinearLayout>
</TabHost>
Java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources();
TabHost tabHost = getTabHost();
Configuration cfg = res.getConfiguration();
boolean hor = cfg.orientation == Configuration.ORIENTATION_LANDSCAPE;
if(hor)
{
TabWidget tw = tabHost.getTabWidget();
tw.setOrientation(LinearLayout.VERTICAL);
}
TabHost.TabSpec spec;
Intent intent;
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, ArtistsActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("artists").setIndicator("Artists",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, AlbumsActivity.class);
spec = tabHost.newTabSpec("albums").setIndicator("Albums",
res.getDrawable(R.drawable.ic_tab_album))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, SongsActivity.class);
spec = tabHost.newTabSpec("songs").setIndicator("Songs",
res.getDrawable(R.drawable.ic_tab_song))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
do like below in your application.
getTabWidget().setOrientation(LinearLayout.VERTICAL);
in android tutorial there isn't a way for customize tab background. my app is official-tutorial based..can anyone help me with a tutorial or code for backgroundcolor white?
this is my code:
LauncherActivity.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.launcher);
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, MyActivity.class);
spec = tabHost.newTabSpec("myactivity").setIndicator("Activity")
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, search.class);
spec = tabHost.newTabSpec("search").setIndicator("Search"
)
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, MyActivity3.class);
spec = tabHost.newTabSpec("songs").setIndicator("Songs"
)
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
Launcher.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="[url]http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="0dp" />
</LinearLayout>
</TabHost>
If you mean to change background color of tabs,then here is the code:
for(int i=0;i<tabhost.getTabWidget().getChildCount();i++)
{
tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.WHITE); //unselected tab
}
for selected tab,
tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.GRAY); // selected tab
You can put above lines into a method (say setTabColors()) and call it:
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String arg0) {
setTabColors(tabHost);
}
});
Actually i have created a tab in my app.here there are three tabs..but thing is that i am not able to manage style in the tabsan morevere i want size of one tab larger than the others two i am sending my code u plese check it..
XML CODE:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp"
/>
</LinearLayout>
Java Code:
setContentView(R.layout.main);
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, ContactActivity.class);
spec = tabHost.newTabSpec("Contacts").setIndicator("Contacts",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, CallActivity.class);
spec = tabHost.newTabSpec("Call").setIndicator("Call",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, MyInfoActivity.class);
spec = tabHost.newTabSpec("My Info").setIndicator("MyInfo",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
Here the code for changing the size of tab
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
TabHost mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("tab_test1")
.setIndicator((""),getResources().getDrawable(R.drawable.mzl_05))
.setContent(new Intent(this, NearBy.class)));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2")
.setIndicator((""),getResources().getDrawable(R.drawable.mzl_08))
.setContent(new Intent(this, SearchBy.class)));
mTabHost.setCurrentTab(0);
mTabHost.getTabWidget().getChildAt(0).setLayoutParams(new
LinearLayout.LayoutParams((width/2)-2,50));
mTabHost.getTabWidget().getChildAt(1).setLayoutParams(new
LinearLayout.LayoutParams((width/2)-2,50));