Making Custom TabHost and TabWidget in Android - android

I want to make a TabHost like that image . where i can use my own image as background of TabWidget. :
and want to remove default background (showing by a red arrow in this image). Code for doing this.`
<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">
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_weight="1"/>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="#drawable/navbar_glass"/>
</LinearLayout>
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
//tabHost.getTabWidget().setStripEnabled(false);
tabHost.getTabWidget().setFocusable(false);
// Tab for Photos
TabSpec photospec = tabHost.newTabSpec("Chats");
photospec.setIndicator("Chats", getResources().getDrawable(R.drawable.chat_button));
Intent photosIntent = new Intent(this, PhotosActivity.class);
photospec.setContent(photosIntent);
TabSpec photospec2 = tabHost.newTabSpec("Friends");
photospec2.setIndicator("Friends", getResources().getDrawable(R.drawable.friend_button));
Intent photosIntent2 = new Intent(this, PhotosActivity.class);
photospec2.setContent(photosIntent2);
// Tab for Songs
TabSpec songspec = tabHost.newTabSpec("Library");
// setting Title and Icon for the Tab
songspec.setIndicator("Library", getResources().getDrawable(R.drawable.library_button));
Intent songsIntent = new Intent(this, SongsActivity.class);
songspec.setContent(songsIntent);
// Tab for Videos
TabSpec videospec = tabHost.newTabSpec("Settings");
videospec.setIndicator("Settings", getResources().getDrawable(R.drawable.setting_button));
Intent videosIntent = new Intent(this, VideosActivity.class);
videospec.setContent(videosIntent);
// 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(photospec2);
}
`

Related

tabwidget Issue

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 .

Android: Showing Tabs at the bottom

I am trying to show the tabs at the bottom of the screen. I have the following code and it is showing tabs at the bottom in design preview and emulator but when I test the code on real device tabs are showing at the top. Can't understand why. I already tried the answers on stackoverflow and other sites like: but no use
Android: Tabs at the BOTTOM
How to show tabs At Bottom rather then at top?
Android having tabs at the bottom of screen?
This is the 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">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_weight="1"/>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:layout_marginBottom="-4dp"
android:gravity="center_vertical|center_horizontal|bottom" />
</LinearLayout>
And this is my activity code:
public class AndroidTabLayoutActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
// Tab for Photos
TabHost.TabSpec photospec = tabHost.newTabSpec("");
// setting Title and Icon for the Tab
photospec.setIndicator("", getResources().getDrawable(R.drawable.icon_photos_tab));
Intent photosIntent = new Intent(this, PhotosActivity.class);
photospec.setContent(photosIntent);
// Tab for Songs
TabHost.TabSpec songspec = tabHost.newTabSpec("");
songspec.setIndicator("", getResources().getDrawable(R.drawable.icon_songs_tab));
Intent songsIntent = new Intent(this, SongsActivity.class);
songspec.setContent(songsIntent);
//Tab for Lists
TabHost.TabSpec listspec = tabHost.newTabSpec("");
listspec.setIndicator("", getResources().getDrawable(R.drawable.icon_lists_tab));
Intent listIntent = new Intent(this, ListsActivity.class);
listspec.setContent(listIntent);
// Tab for Videos
TabHost.TabSpec videospec = tabHost.newTabSpec("");
videospec.setIndicator("", getResources().getDrawable(R.drawable.icon_videos_tab));
Intent videosIntent = new Intent(this, VideosActivity.class);
videospec.setContent(videosIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(photospec); // Adding photos tab
tabHost.addTab(songspec); // Adding songs tab
tabHost.addTab(listspec); //Adding lists tab
tabHost.addTab(videospec); // Adding videos tab
// Set drawable images to tab
tabHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.parseColor("#1D2023"));
tabHost.getTabWidget().getChildAt(2).setBackgroundColor(Color.parseColor("#1D2023"));
tabHost.getTabWidget().getChildAt(3).setBackgroundColor(Color.parseColor("#1D2023"));
// Set Tab1 as Default tab and change image
tabHost.getTabWidget().setCurrentTab(0);
tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#1D2023"));
}
}
any help will be greatly appreciated ...

Switch between tabs via swipe in android

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.

How to show Badge in TabHost

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.

Android tabs changing between activities

I have an application with 3 tabs. I have made the xml files to describe the layout and it works perfectly. But I'm stuck at this point. My tabs are A B C. I want when I click on tab A, the tabs B C to be replaced by another tab D. Are there any solutions for this?
<!-- language: java -->
public class AndroidTabLayoutActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
TabHost tabHost2 = getTabHost();
// Tab for Photos
TabSpec photospec = tabHost.newTabSpec("Photos");
photospec.setIndicator("Photos", getResources().getDrawable(R.drawable.icon_photos_tab));
Intent photosIntent = new Intent(this, LoginActivity.class);
photospec.setContent(photosIntent);
// Tab for Songs
TabSpec songspec = tabHost.newTabSpec("Songs");
// setting Title and Icon for the Tab
songspec.setIndicator("Songs", getResources().getDrawable(R.drawable.icon_songs_tab));
Intent songsIntent = new Intent(this, SettingsActivity.class);
songspec.setContent(songsIntent);
// Tab for Videos
TabSpec videospec = tabHost.newTabSpec("Videos");
videospec.setIndicator("Videos", getResources().getDrawable(R.drawable.icon_videos_tab));
Intent videosIntent = new Intent(this, AboutActivity.class);
videospec.setContent(videosIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(photospec); // Adding photos tab
tabHost.addTab(songspec); // Adding songs tab
tabHost.addTab(videospec); // Adding videos tab
}
}
<!-- language: 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">
<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>

Categories

Resources