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.
Related
What i want to do : I want to make exactly like below :
i managed to do something like below :
Code:
ISSUE : i want to following things :
i want to add divider the same in above picture .
i want to remove the space between the icon and text . Also want my icon in centre .
Following is code :
Tab.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white"
>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="#android:color/white"
/>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:showDividers="middle"
android:background="#color/button_color"
android:tabStripEnabled="false"
/>
</LinearLayout>
</TabHost>
TabActivity.class
public class MainActivity extends TabActivity {
protected static Context context;
TabHost tabHost;
SharedPreferences mpref;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
// TabHost tabHost = getTabHost();
tabHost = getTabHost();
SharedPreferences preferences2 = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
String u_id1 = preferences2.getString("Userid","");
Bean b = new Bean();
b.setDeviceid(u_id1);
// tabHost.getTabWidget().getResources().getDrawable(R.drawable.tab);
// Tab for Photos
TabSpec photospec = tabHost.newTabSpec("Favourite");
photospec.setIndicator("Favorites", getResources().getDrawable(R.drawable.tab_favourites));
Intent photosIntent = new Intent(this, Favourite.class);
photospec.setContent(photosIntent);
// Tab for Songs
TabSpec songspec = tabHost.newTabSpec("Contacts");
// setting Title and Icon for the Tab
songspec.setIndicator("Contacts", getResources().getDrawable(R.drawable.tab_contacts));
Intent songsIntent = new Intent(this, Contacts.class);
songspec.setContent(songsIntent);
// Tab for Videos
TabSpec videospec = tabHost.newTabSpec("Meeting");
videospec.setIndicator("Meeting",getResources().getDrawable(R.drawable.tab_meeting));
Intent videosIntent = new Intent(this, Meeting.class);
videospec.setContent(videosIntent);
// Tab for Future SMS
TabSpec futuresms = tabHost.newTabSpec("Map");
futuresms.setIndicator("Map",getResources().getDrawable(R.drawable.tab_map));
Intent futureIntent = new Intent(this, Map.class);
futuresms.setContent(futureIntent);
// Tab for Future SMS
TabSpec setting = tabHost.newTabSpec("Setting");
setting.setIndicator("Setting",getResources().getDrawable(R.drawable.tab_setting));
Intent settingintent = new Intent(this, Setting.class);
setting.setContent(settingintent);
tabHost.getTabWidget().setDividerDrawable(R.color.dividerColor);
// Adding all TabSpec to TabHost
tabHost.addTab(photospec); // Adding photos tab
tabHost.addTab(songspec); // Adding songs tab
tabHost.addTab(videospec); // Adding videos tab
tabHost.addTab(futuresms);
tabHost.addTab(setting);
// tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tab_favourites);
// tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tab_contacts);
// tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.tab_meeting);
// tabHost.getTabWidget().getChildAt(3).setBackgroundResource(R.drawable.tab_map);
// tabHost.getTabWidget().getChildAt(4).setBackgroundResource(R.drawable.tab_setting);
int k = tabHost.getTabWidget().getTabCount();
for (int j=0;j<tabHost.getTabWidget().getChildCount();j++){
TextView textView = (TextView)tabHost.getTabWidget().getChildAt(j).findViewById(android.R.id.title);
textView.setTextSize(10);
}
for (int i = 0; i <tabHost.getTabWidget().getChildCount(); i++) {
tabHost.getTabWidget().getChildTabViewAt(i).setBackgroundColor(getResources().getColor(R.color.button_color));
}
// ((TextView)tabHost.getChildAt(2).findViewById(android.R.id.title)).setTextSize(10);
// ((TextView)tabHost.getChildAt(3).findViewById(android.R.id.title)).setTextSize(10);
// ((TextView)tabHost.getChildAt(4).findViewById(android.R.id.title)).setTextSize(10);
// tv.setTextSize(20);
tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tab_click);
int in = Integer.parseInt(tabid);
tabHost.setCurrentTab(in);
}
}
Please help me to achieve this either by some other approach or some
snippet .
I was create android application with tab and TabHost but users cant switch between tabs via swipe.
I use TabActivity for this app. Users most click on tabs for switch between tabs or activities.
MainActivity.Java :
public class MainActivity extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources ressources = getResources();
TabHost tabHost = getTabHost();
//Home Intent
Intent intentHome = new Intent().setClass(this, HomeActivity.class);
TabSpec tabSpedHome = tabHost
.newTabSpec("Home")
.setIndicator("",ressources.getDrawable(R.drawable.icon_home_config))
.setContent(intentHome);
//Search Intent
Intent intentSearch = new Intent().setClass(this, SearchActivity.class);
TabSpec tabSpedSearch = tabHost
.newTabSpec("Search")
.setIndicator("",ressources.getDrawable(R.drawable.icon_search_config))
.setContent(intentSearch);
//Archive Intent
Intent intentArchive = new Intent().setClass(this, ArchiveActivity.class);
TabSpec tabSpedArchive = tabHost
.newTabSpec("Archive")
.setIndicator("",ressources.getDrawable(R.drawable.icon_archive_config))
.setContent(intentArchive);
//Download Intent
Intent intentDownload = new Intent().setClass(this, DownloadActivity.class);
TabSpec tabSpedDownload = tabHost
.newTabSpec("Download")
.setIndicator("",ressources.getDrawable(R.drawable.icon_download_config))
.setContent(intentDownload);
//Conctactus Intent
Intent intentConctactus = new Intent().setClass(this, ContactusActivity.class);
TabSpec tabSpedConctactus = tabHost
.newTabSpec("Conctact us")
.setIndicator("",ressources.getDrawable(R.drawable.icon_contactus_config))
.setContent(intentConctactus);
//AddAllTab
tabHost.addTab(tabSpedHome);
tabHost.addTab(tabSpedSearch);
tabHost.addTab(tabSpedArchive);
tabHost.addTab(tabSpedDownload);
tabHost.addTab(tabSpedConctactus);
//Set Default Tab
tabHost.setCurrentTab(0);
}
}
And activity_main.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">
<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="5dp" />
</LinearLayout>
</TabHost>
TabActivity has been deprecated for over three years. Please use another tab solution. Particularly if you want tab contents to be able to be swiped, use ViewPager for the tab contents and a tabbed indicator (e.g., PagerTabStrip) for the tabs.
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...
}
}
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);
}
});