Increasing the tab size? - android

I have these 4 tabs as below. The problem is whenever I increase the tabs the tab size gets smaller. Is there any solution when they can simply be scrolled horizontally and also have the same size.
public class MainActivity 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 Maths
TabSpec mathspec = tabHost.newTabSpec("Maths");
mathspec.setIndicator("Maths");
Intent mathIntent = new Intent(this, MathActivity.class);
mathspec.setContent(mathIntent);
// Tab for Units
TabSpec unitsspec = tabHost.newTabSpec("Units");
// setting Title and Icon for the Tab
unitsspec.setIndicator("Units");
Intent unitsIntent = new Intent(this, UnitsActivity.class);
unitsspec.setContent(unitsIntent);
// Tab for Physics
TabSpec physicsspec = tabHost.newTabSpec("Physics");
// setting Title and Icon for the Tab
physicsspec.setIndicator("Physics");
Intent physicsIntent = new Intent(this, PhysicsActivity.class);
physicsspec.setContent(physicsIntent);
// Tab for Chemistry
TabSpec chemistryspec = tabHost.newTabSpec("Chemistry");
// setting Title and Icon for the Tab
chemistryspec.setIndicator("Chemistry");
Intent chemistryIntent = new Intent(this, ChemistryActivity.class);
chemistryspec.setContent(chemistryIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(mathspec); // Adding maths tab
tabHost.addTab(unitsspec); // Adding units tab
tabHost.addTab(physicsspec); // Adding physics tab
tabHost.addTab(chemistryspec); // Adding chemistry tab
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
tv.setTextSize(10);
}
}
}

Switch over to Fragments. You will never face such an issue.
See this tutorial.
http://wptrafficanalyzer.in/blog/implement-swiping-between-tabs-with-viewpager-in-action-bar-using-sherlock-library/
Moreover tab activity gets deprecated.

Related

Change Tab color depending on which tab is selected

I have an app with a MainActivity that extends TabActivity (i know it's deprecated but too much to do to change whole app).
So in my app I use tabhost to create 3 tabs like this:
TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
TabSpec secondTabSpec = tabHost.newTabSpec("tid2");
TabSpec thirdTabSpec = tabHost.newTabSpec("tid3");
firstTabSpec.setIndicator("tab1").setContent(
new Intent(this, tab1.class));
secondTabSpec.setIndicator("tab2").setContent(
new Intent(this, tab2.class));
thirdTabSpec.setIndicator("tab3").setContent(
new Intent(this, tab3.class));
/* Add tabSpec to the TabHost to display. */
tabHost.addTab(firstTabSpec);
tabHost.addTab(secondTabSpec);
tabHost.addTab(thirdTabSpec);
//Changing the tabs text color on the tabs
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
tv.setTextColor(Color.parseColor("#ffffff"));
}
// remove divider
tabHost.getTabWidget().setDividerDrawable(null);
tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#90a4ae"));
tabHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.parseColor("#607d8b"));
tabHost.getTabWidget().getChildAt(2).setBackgroundColor(Color.parseColor("#607d8b"));
So my code creates the 3 tabs that link to 3 different activities and sets the color of the tabs. First tab has at first load a different color than the other two.
I want the color of the tabs to change depending on which one is selected.
So when I press the second tab i want the first to get #607d8b color and the second to get #90a4ae.
Same for the third one.
Tried to implement a OnTabChangeListener but couldn't get it to work.
Tried to to this:
tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#90a4ae"));
tabHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.parseColor("#607d8b"));
tabHost.getTabWidget().getChildAt(2).setBackgroundColor(Color.parseColor("#607d8b"));
with changed colors inside each loaded tab activity but I get error that it can't resolve tabhost (as it should, since it's defined in MainActivity.
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#54C4C6")); // unselected
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#114C5A")); // selected
}
});

Custom TabView with Intent in Android

I want to create Tabview like below in Image. I tried it but can't get proper view.
Here is My Code.. It's not give exact view that I want. So, How can I customized it.
public class BandInfo extends TabActivity implements OnTabChangeListener {
TabHost tabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.band_info);
tabHost = getTabHost();
tabHost.getTabWidget().setBackgroundResource(R.drawable.footerbar);
// Tab for Bio
TabSpec bioSpec = tabHost.newTabSpec("Bio");
// Setting Title for Tab
bioSpec.setIndicator("Bio");
Intent bioIntent = new Intent(this,Bio.class);
bioSpec.setContent(bioIntent);
// Tab for Upcoing Shows
TabSpec upcomingShowSpec = tabHost.newTabSpec("Upcoming Shows");
upcomingShowSpec.setIndicator("Upcoming Shows");
Intent upcomingShowIntent = new Intent(this, UpcomingShow.class);
upcomingShowSpec.setContent(upcomingShowIntent);
// Tab for Band Members
TabSpec bandMemberSpec = tabHost.newTabSpec("Band Members");
bandMemberSpec.setIndicator("Band Members");
Intent bandMemberIntent = new Intent(this, BandMembers.class);
bandMemberSpec.setContent(bandMemberIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(bioSpec); // Adding Bio Tab
tabHost.addTab(upcomingShowSpec); // Adding Upcoming Show Tab
tabHost.addTab(bandMemberSpec); // Adding Band Members Tab
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.footerbar);
}
tabHost.getTabWidget().setDividerDrawable(R.drawable.footer_saprater);
}
#Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.footerbar);
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(R.drawable.bandmember_active);
}
Please Help me with some code...
Thanks in Advance..
This is an output Screen I get.
Heyy i found this. Try this. you'll surely make it.
http://code.google.com/p/android-custom-tabs/
For setting width,
tabHost.getTabWidget().getChildAt(0).getLayoutParams().width =(int) 30;
Also check this link: http://adanware.blogspot.in/2012/04/android-custom-tab-layouts-just-using.html and http://www.speakingcode.com/2011/12/01/creating-custom-views-for-tabs-in-an-android-activity-using-tabhost-and-tabwidget/

android tabhost tab and view not matching

I have two tabs in my tabwidget and each tab display a list view.
ay
Initially my first tab is displayed as selected but below list correspond to second tab.
Once i click the tabs, i am getting correct display.
private static final String LIST_TAB_TAG1 = "UpcomingEvents";
private static final String LIST_TAB_TAG2 = "PastEvents";
tabHost.addTab(tabHost.newTabSpec(LIST_TAB_TAG1)
.setIndicator(LIST_TAB_TAG1)
.setContent(new TabContentFactory() {
public View createTabContent(String arg) {
return listView1;
}
}));
tabHost.addTab(tabHost.newTabSpec(LIST_TAB_TAG2)
.setIndicator(LIST_TAB_TAG2)
.setContent(new TabContentFactory() {
public View createTabContent(String arg) {
return listView2;
}
}));
tabHost.setCurrentTab(0);
LIST_TAB_TAG1 is highlighted when this sctivity is launched but the list displayed is listview2. This problem is only when activity starts. Upon clicking tabs its working fine
Can please help me in fixing this. Thanks for your time
I generally use the code given in docs which works fine for me ,try this one .
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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, UpcomingEvents.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("UpcomingEvents").setIndicator("UpcomingEvents",getResources().getDrawable(R.drawable.homebutton1)).setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this,PastEvents.class);
spec = tabHost.newTabSpec("PastEvents").setIndicator("PastEvents",
getResources().getDrawable(R.drawable.bank_transaction1))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}

How to change title of a Tab in TabActivity

I have TabActivity with tabs on bottom which is composed of several different activities like this:
public class HomeScreen extends TabActivity {
private TabHost mTabHost;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
try{
setContentView(R.layout.hometabs);
Resources res = getResources(); // Resource object to get Drawables
mTabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Reusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
String strName;
// Create an Intent to launch an Activity for the tab (to be reused)
strName=Central.Res.getString(R.string.tab_Books);
intent = new Intent().setClass(this, BookList.class);
spec = mTabHost.newTabSpec(strName).setIndicator("",
res.getDrawable(R.drawable.ic_menu_database))
.setContent(intent);
mTabHost.addTab(spec);
// Create an Intent to launch an Activity for the tab (to be reused)
strName=Central.Res.getString(R.string.tab_Questions);
intent = new Intent().setClass(this, QuestionsList.class);
intent.putExtra("NoteType", UserDataSQLHelper.NOTE_TYPE_QUEST );
spec = mTabHost.newTabSpec(strName).setIndicator("",
res.getDrawable(R.drawable.ic_menu_dialog))
.setContent(intent);
mTabHost.addTab(spec);
My problem is that I have the same window title for every tab. This window title is set to application name.
I need to be able to change this title to the name of the tab.
I am trying to do it by calling setTitle("MyTitle"); in onCreate() of corresponding activity and by overriding TabActivity onChildTitleChanged:
public void onChildTitleChanged(Activity childActivity, CharSequence title)
{
View tabView = mTabHost.getCurrentTabView();
int idTitle = android.R.id.title;
TextView tvTitle = (TextView) tabView.findViewById(idTitle);
tvTitle.setText(title);
}
But the result I get isn't what I need - it sets text to tab under the tab icon.
Could you please help me?
try with following code,
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
Log.i("Tab id", ""+tabId);
setTitle(tabId);
}
});
setIndicator (CharSequence label, Drawable icon).
Use this function and the label will be shown as a title.

Android Reusing SurfaceViews from a single layout xml file on two tabs of a tabactivity

I have a layout.xml file with a surfaceview and various buttons.
This is loaded (setContentView) into two instantiations of a class. Each instance is an activity on a different tab (within another class which is a tabactiviy).
The problem: if I start the app with tab1 (setCurrentTab) as the opening tab it (tab1) can draw on the surface. If I set tab2 as the opening tab, it can draw on the surface. BUT switching between the tabs when running, only the initial tab can draw on the surface. It's as if the initial tab has a lock on the surface.
I've tried making the surfaceview invisible (setvisibility) on the inactive tab during pause and resume but this just covers (I think) the other tab's copy of the surfaceview.
The question: can two tabs share a single layout's surfaceview? How can a 'resumed' tab get control from the 'paused' tab?
PS I have rewritten, torn apart, tried reloading (rebuilt in several ways this application over 3 weeks and I get the feeling that there's something about shared layout that I'm missing.
I've cut this cade back to the following and I still have can't get the non-default tab to redraw it's view on moving to its tab (even though some parts, such as a background colour, are updated correctly. The code is below.
I would be extremely grateful for ay advice on getting this to work. It should show "Mode 1" or "Mode 2" depending on the selected tab but, only the tab selected in tabHost.setCurrentTab(1); ever shows the string BUT the red or blue border does update.
// THIS IS THE ENTRY POINT ////////////////////////////////////////
public class TabTest extends TabActivity
{
Intent intent;
Bundle params;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
// TEMPLATE
intent = new Intent().setClass(this, TabContent.class);
params = new Bundle();
params.putString("TABNAME", "Mode 1");
intent.putExtras(params);
spec = tabHost.newTabSpec("Template").setIndicator("template",
res.getDrawable(R.drawable.tab_pane))
.setContent(intent);
tabHost.addTab(spec);
// ATTEMPT
intent = new Intent().setClass(this, TabContent2.class);
params = new Bundle();
params.putString("TABNAME", "Mode 2");
intent.putExtras(params);
spec = tabHost.newTabSpec("Attempt").setIndicator("attempt",
res.getDrawable(R.drawable.tab_pane))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(1);
}
}
// Tab content one ////////////////////////////////////////
public class TabContent extends Activity
{
ScreenWrite s=null;
SurfaceView screen_1;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.content);
Bundle params = this.getIntent().getExtras();
String name = params.getString("TABNAME");
screen_1=(SurfaceView)findViewById(R.id.screen_1);
s=new ScreenWrite((Context)this, screen_1, name);
}
}
// Tab content two ////////////////////////////////////////
public class TabContent2 extends Activity
{
ScreenWrite s=null;
SurfaceView screen_2;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.content2);
Bundle params = this.getIntent().getExtras();
String name = params.getString("TABNAME");
screen_2=(SurfaceView)findViewById(R.id.screen_2);
s=new ScreenWrite((Context)this, screen_2, name);
}
}
// The content layouts ////////////////////////////////////////
// and ////////////////////////////////////////
////////////////////////////////////////
PS the layouts were truncated from the submission but are essentially just holders for a background colour and surfaceview. Can supply if not clear
Using SurfaceView is a bit tricky, you might try manually setting e.g:
screen_2.setVisibility(View.GONE)
When it loses focus e.g. via an onTabChangedListener.

Categories

Resources