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);
}
});
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.
Hy everyone
i am using TabHost to create TabBar in android....
public class TabBarActivity extends TabActivity implements OnTabChangeListener
{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, CustomizedListView.class);
spec = tabHost.newTabSpec("Clubs").setIndicator("Clubs", getResources().getDrawable(R.drawable.clubs_icon))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Events.class);
spec = tabHost.newTabSpec("Events").setIndicator("Events", getResources().getDrawable(R.drawable.events_icon))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Maps.class);
spec = tabHost.newTabSpec("Maps").setIndicator("Maps", getResources().getDrawable(R.drawable.maps_icon))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Settings.class);
spec = tabHost.newTabSpec("Settings").setIndicator("Settings", getResources().getDrawable(R.drawable.settings_icon))
.setContent(intent);
tabHost.addTab(spec);
getTabHost().setOnTabChangedListener(this);
}
#Override
public void onTabChanged(String tabId)
{
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Inside the new tab ", Toast.LENGTH_SHORT).show();
//getTabHost().setVisibility(View.VISIBLE);
}
}
**tab.xml**
<?xml version="1.0" encoding="utf-8"?>
<TabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="#android:id/tabs">
</TabWidget>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#android:id/tabcontent">
</FrameLayout>
</RelativeLayout>
</TabHost>
but the problem is that whenever the contents of the tab grow e.g displaying dynamic content in listview against a tab then the TabHost becomes INVISIBLE..
any help in this regard will be highly appreciated....
Insted of using TabActivity use normal Activity. and then create TabHost with the tabspec based on your requirement. and add that tabHost as your first component in the screen/Activity.
My tabhost tools.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tabtools);
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
// TabDados
intent = new Intent().setClass(this, ToolDadosTubuCirc.class);
spec = tabHost.newTabSpec("dados")
.setIndicator("Dados", res.getDrawable(R.drawable.icondados))
.setContent(intent);
tabHost.addTab(spec);
// TabLegenda
intent = new Intent().setClass(this, ToolLegendaTubuCirc.class);
spec = tabHost
.newTabSpec("legenda")
.setIndicator("Legenda",
res.getDrawable(R.drawable.iconlegenda))
.setContent(intent);
tabHost.addTab(spec);
// TabCalcular
intent = new Intent().setClass(this, ToolCalcularTubuCirc.class);
spec = tabHost
.newTabSpec("calcular")
.setIndicator("Calcular",
res.getDrawable(R.drawable.iconcalcular))
.setContent(intent);
tabHost.addTab(spec);
// TabCorrente
tabHost.setCurrentTab(0);
}}
Each intent calls an activity.
and within each "file. java" commands have to perform the calculations.
Within "dados.java" is where I get the information of the User.
When they click on the tab "calcular" must verify that all data has been completed.
I guess I need a "onclicklistener" for each tab, like a button, right??
How? how to create an event to run code when a tab"Dados" tab"Legenda" tab"calcular" is clicked?
Attention: I need to check which tab was clicked from the file "ToolDadosCircular.java."
Edit:
tabtools.xml code:
<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_above="#+layout/rowLog"
android:layout_below="#+layout/rowLine" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
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>
Try
tabHost.getChildAt(yourtab).setOnClickListener(new View.onClickListener()
{
#Override public void onClick(View view){
//your code
}
}
Edit: I'm guessing your class is like this
public class ToolDadosTubuCirc extends Activity{
#Override public void onCreate(Bundle bundle){
//code...
TabHost tabHost = (TabHost) getParent().findViewById(yourTabHostId);
//do what you want with tabHost
//code...
}
}
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);
how can i Reduce Tab height and align text on top?
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
public class MessagesTabs extends TabActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.messages_tabs);
//Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, GetMessages.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("inbox").setIndicator("Inbox")
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, GetConversations.class);
spec = tabHost.newTabSpec("conversation").setIndicator("Conversation")
.setContent(intent);
tabHost.addTab(spec);
/*
intent = new Intent().setClass(this, SongsActivity.class);
spec = tabHost.newTabSpec("songs").setIndicator("Songs",
res.getDrawable(R.drawable.ic_tab_songs))
.setContent(intent);
tabHost.addTab(spec);*/
tabHost.setCurrentTab(0);
}
}
xmlfile
<?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"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<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"
/>
</LinearLayout>
</TabHost>
just try this for increasing the height of the tabs :
tabHost.getTabWidget().getChildAt(index).getLayoutParams().height =(int) height;
for text aligning try this in your xml layout :
android:gravity="top" or android:layout_gravity="top"
use this code. It may work for you:
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
tabHost.getTabWidget().getChildAt(i).getLayoutParams().height /= 2;
}
You need to create a custom layout for your tab, inflate it and use setIndicator