I am trying to add a custom divider between my tabs. but it doesn't work. here is my java code:
String tabschedule = getString(R.string.myschedule);
mTabHost = getTabHost();
intent = new Intent(this, Schedule.class);
spec = mTabHost
.newTabSpec("Schedule")
.setIndicator(tabschedule,
this.getResources().getDrawable(R.drawable.schedule))
.setContent(intent);
mTabHost.addTab(spec);
String filteration = getString(R.string.filteration);
intent = new Intent(this, Filteration.class);
spec = mTabHost
.newTabSpec("Filteration")
.setIndicator(filteration,
this.getResources().getDrawable(R.drawable.filteration))
.setContent(intent);
mTabHost.addTab(spec);
String history = getString(R.string.history);
intent = new Intent(this, MyHistory.class);
spec = mTabHost
.newTabSpec("History")
.setIndicator(history,
this.getResources().getDrawable(R.drawable.historylay))
.setContent(intent);
mTabHost.addTab(spec);
and here is my xml code:
<style name="tabtheme" parent="#android:style/Theme.Light.NoTitleBar">
<item name="android:actionBarTabBarStyle">#style/customTabBar</item>
</style>
<style name="customTabBar" parent="#style/Widget.AppCompat.ActionBar.TabBar">
<item name="android:showDividers">middle</item>
<!-- give your divider here -->
<item name="android:actionBarDivider">#drawable/tabdivider</item>
<item name="android:dividerPadding">0dp</item>
</style>
This is my screen shot
Any suggestion will be appreciated, thanks in advance.
Related
Here is my activity in which i set fragment tabs:
private FragmentTabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bottom_tabs);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
Bundle b = new Bundle();
b.putString("key", "Simple");
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),this.getResources().getDrawable(R.drawable.iconxml)
Fragment1.class, b);
iconxml.xml file
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="#drawable/ic_launcher" />
<item android:drawable="#drawable/icontab" />
my app crashes with this.. plz help
![exceptions][logcat outout]
Try to change this:
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple",getResources().getDrawable(R.drawable.iconxml)
Fragment1.class, b);
Replace this to
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),this.getResources().getDrawable(R.drawable.iconxml) Fragment1.class, b);
this
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple",this.getResources().getDrawable(R.drawable.iconxml)) Fragment1.class, b);
See there TabHost.TabSpec
http://wptrafficanalyzer.in/blog/creating-navigation-tabs-using-tabhost-and-fragments-in-android/
i tried modyfing above example and finally solved my problem :)
i have made a simple tabhost demo for learning purpose,i have made it successfully and running it as well,But my problem is images which i have put in drawable should come as mentioned in selectors but its not working..its not even showing images..!my code is:
main.java
package com.example.tabhostdemo;
import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.view.Menu;
import android.widget.TabHost;
public class TabHostActivity extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_host);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Reusable 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, HomeActivity.class);
spec = tabHost.newTabSpec("home")
.setIndicator("HOME", res.getDrawable(R.drawable.home))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, AboutActivity.class);
spec = tabHost.newTabSpec("about")
.setIndicator("ABOUT", res.getDrawable(R.drawable.about))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, ContactActivity.class);
spec = tabHost
.newTabSpec("contact")
.setIndicator("CONTACT",
res.getDrawable(R.drawable.contact))
.setContent(intent);
tabHost.addTab(spec);
//set tab which one you want open first time 0 or 1 or 2
tabHost.setCurrentTab(0);
}
}
selectors
*home.xml*
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected -->
<item android:drawable="#drawable/home1"
android:state_selected="true" />
<!-- When not selected-->
<item android:drawable="#drawable/home2" />
</selector>
contact
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected -->
<item android:drawable="#drawable/contact1"
android:state_selected="true" />
<!-- When not selected-->
<item android:drawable="#drawable/contact2" />
</selector>
about.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected -->
<item android:drawable="#drawable/about1"
android:state_selected="true" />
<!-- When not selected-->
<item android:drawable="#drawable/about2" />
</selector>
You can try this way.
spec.setIndicator("HOME", setImageResource(R.drawable.home))
It will work great.
Either you have to used this example.
Really great.
I have put together a very simple Tab Host demo in Android for learning purposes. It's documented here: http://www.androidhub4you.com/2013/04/android-tabactivity-tab-layout-demo-tab.html
I followed the steps as indicated but it's not working. Please help me. My code is:
TabhostActivity.java
package com.example.tabhostdemo;
import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.view.Menu;
import android.widget.TabHost;
public class TabHostActivity extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_host);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Reusable 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, HomeActivity.class);
spec = tabHost.newTabSpec("home")
.setIndicator("HOME", res.getDrawable(R.drawable.home1))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, AboutActivity.class);
spec = tabHost.newTabSpec("about")
.setIndicator("ABOUT", res.getDrawable(R.drawable.about1))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, ContactActivity.class);
spec = tabHost
.newTabSpec("contact")
.setIndicator("CONTACT",
res.getDrawable(R.drawable.contact1))
.setContent(intent);
tabHost.addTab(spec);
//set tab which one you want open first time 0 or 1 or 2
tabHost.setCurrentTab(0);
}
}
/Drawable/home.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected -->
<item android:drawable="#drawable/home1"
android:state_selected="true" />
<!-- When not selected-->
<item android:drawable="#drawable/home2" />
</selector>
/Drawable/contact.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected -->
<item android:drawable="#drawable/contact1"
android:state_selected="true" />
<!-- When not selected-->
<item android:drawable="#drawable/contact2" />
</selector>
/Drawable/about.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected -->
<item android:drawable="#drawable/about1"
android:state_selected="true" />
<!-- When not selected-->
<item android:drawable="#drawable/about2" />
</selector>
Tab activity been deprecated.Switch over to Fragments.
http://wptrafficanalyzer.in/blog/adding-navigation-tabs-containing-listview-to-action-bar-in-android/
I'm setting a image hover in my Tab Host using xml file. But its not working for Me. Please take a look at my codes below to see if I forgotten something or if there is something wrong in my codes.
Main Activity Class
public class MainActivity extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabHost = getTabHost();
// Tab for Photos
TabSpec photospec = tabHost.newTabSpec("Photos");
// setting Title and Icon for the Tab
photospec.setIndicator("Photos", getResources().getDrawable(R.drawable.icon_photos_tab));
Intent photosIntent = new Intent(this, PhotosActivity.class);
photospec.setContent(photosIntent);
// Tab for Songs
TabSpec songspec = tabHost.newTabSpec("Songs");
songspec.setIndicator("Songs", getResources().getDrawable(R.drawable.icon_songs_tab));
Intent songsIntent = new Intent(this, SongsActivity.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, 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
}
}
My XML file for Hover
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- When selected, use grey -->
<item android:drawable="#drawable/photohover"
android:state_selected="true" />
<!-- When not selected, use white-->
<item android:drawable="#drawable/photo" />
</selector>
i checked your code it is working fine. I don't know what's your problem. Please check this link. It will helps you to understand the Tabwidget concept. http://www.rdcworld-android.blogspot.in/2011/11/tabwidget-in-android-advance.html
Let me know still you are facing a problem with tabwiget, with clear information.
Problem Solved. I just change getResources() to context.getResources() and it works fine.
try this :-
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- selected state -->
<item android:drawable="#drawable/normal_pressed"
android:state_selected="true"
android:state_pressed="false" />
<!-- unselected state (default) -->
<item android:drawable="#drawable/normal" />
</selector>
Hi i'm trying to change the color of my tab widget but when i run the app it doesnt change does anyone know why?
heres my code where i try to change the color of my tabhost both unselected and selected
public static void setTabColor(TabHost tabhost) {
for(int i=0;i<tabhost.getTabWidget().getChildCount();i++)
{
tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#666666")); //unselected
}
tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.parseColor("#FFFFFF")); // selected
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tablayout);
checkInternet();
checkPreferences();
TabHost tabHost = getTabHost();
// setTabColor(tabHost);
// Tab for Photos
TabSpec resultsspec = tabHost.newTabSpec("Results");
// setting Title and Icon for the Tab
resultsspec.setIndicator("Results", getResources().getDrawable(R.drawable.miresults_tab));
Intent photosIntent = new Intent(this, MIResults.class);
resultsspec.setContent(photosIntent);
// setTabColor(tabHost);
// Tab for Songs
TabSpec Fixturesspec = tabHost.newTabSpec("Fixtures");
Fixturesspec.setIndicator("Fixtures", getResources().getDrawable(R.drawable.mifixtures_tab));
Intent songsIntent = new Intent(this, MIFixtures.class);
Fixturesspec.setContent(songsIntent);
TabSpec tablespec = tabHost.newTabSpec("Table");
tablespec.setIndicator("Table", getResources().getDrawable(R.drawable.mitable_tab));
Intent videosIntent = new Intent(this, MITable.class);
tablespec.setContent(videosIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(resultsspec);
tabHost.addTab(Fixturesspec);
tabHost.addTab(tablespec);
}
You'll need to create a drawable selector, similar to:
mytab.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="#drawable/tab_unselected_v4" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="#drawable/tab_selected_v4" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="#drawable/tab_focus" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="#drawable/tab_focus" />
<!-- Pressed -->
<item android:state_pressed="true" android:drawable="#drawable/tab_press" />
</selector>
to handle each of the states of the tab. Each of the drawables, like tab_focus, is a 9-patch png.
You'll also need to override the tab layout to reference your drawable resource. I'd use a style with a parent that references the android default, then override the background property to reference your "drawable/mytab".