Reduce Tab height and align text on top? - android

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

Related

how to set tabhost on top of the content

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.

Android - create vertical tab at left using android 3.0

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);

Make Tab a scrollbar and show only 3 out of 6 tabs

Im trying to set my tabs as a scrollbar. I have 6 tabs but want to show 3 in the UI. How do I set the scrollbar view in this format? If it even possible to set it like this?
Tab Bar is based on androids developer page.
Thanks
<?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">
<ScrollView android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
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>
</ScrollView>
Java file
package developers.tab;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
public class AndroidDevelopersTabActivity extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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, WorkoutActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("workouts").setIndicator("Workout",
res.getDrawable(R.drawable.ic_tab_workout))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, ExcerciseActivity.class);
spec = tabHost.newTabSpec("excercises").setIndicator("Excercises",
res.getDrawable(R.drawable.ic_tab_albums))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, ProgramsActivity.class);
spec = tabHost.newTabSpec("programs").setIndicator("Programs",
res.getDrawable(R.drawable.ic_tab_songs))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, LogActivity.class);
spec = tabHost.newTabSpec("mealplans").setIndicator("Meal Plans",
res.getDrawable(R.drawable.ic_tab_songs))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, BodyMActivity.class);
spec = tabHost.newTabSpec("bodymeasurements").setIndicator("Body Measurements",
res.getDrawable(R.drawable.ic_tab_songs))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, MealPlanActivity.class);
spec = tabHost.newTabSpec("mealplan").setIndicator("Meal Plan",
res.getDrawable(R.drawable.ic_tab_songs))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
}
}
Why couldn't you just make the visiblity of unwanted tabs "GONE"?
You can make the side tabs GONE or VISIBLE when scrolling from side to side.
By view.setVisibility=GONE; or view.setVisibility=VISIBLE;
If you do viewA GONE, it will take no place. All other views behave themselves as if viewA didn't exist. Listen to scrolling and when there is place for showing right tabs, show them, and hide the left ones. And vice versa.

TabLayout Android, customize background color

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);
}
});

How to create custom api for tabhost and tabwidget?

I have created scrollable tab bar, using tabhost, tabwidget, and horizontalscrollbar which is layout_gravity is bottom. Now, i want to create custom API for that, so any one can use the api change the textsize, height, width etc..,according their reqiurment.
Tabbar.java
package com.tabbar.project;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TabHost;
public class Tabbar extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources();
// TabHost will have Tabs
TabHost tabHost = getTabHost();
/* TabSpec used to create a new tab.
* By using TabSpec only we can able to setContent to the tab.
* By using TabSpec setIndicator() we can set name to tab.
* TabSpec setContent() is used to set content for a particular tab.*/
//adding tabbar items
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, FirstActivity.class);
spec = tabHost.newTabSpec("first").setIndicator("Contact", res.getDrawable(R.drawable.contact_img)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, SecondActivity.class);
spec = tabHost.newTabSpec("second").setIndicator("Dial", res.getDrawable(R.drawable.dial_img)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, ThirdActivity.class);
spec = tabHost.newTabSpec("third").setIndicator("Movie", res.getDrawable(R.drawable.movie_img)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, FourthActivity.class);
spec = tabHost.newTabSpec("fourth").setIndicator("gellary", res.getDrawable(R.drawable.gellary_img)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, FifthActivity.class);
spec = tabHost.newTabSpec("fifth").setIndicator("Opera", res.getDrawable(R.drawable.opera_img)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, SixthActivity.class);
spec = tabHost.newTabSpec("sixth").setIndicator("Contacts", res.getDrawable(R.drawable.contact_img)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, SeventhActivity.class);
spec = tabHost.newTabSpec("seventh").setIndicator("Dial", res.getDrawable(R.drawable.dial_img)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, EightActivity.class);
spec = tabHost.newTabSpec("eight").setIndicator("Movie", res.getDrawable(R.drawable.movie_img)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, NinthActivity.class);
spec = tabHost.newTabSpec("ninth").setIndicator("Gellary", res.getDrawable(R.drawable.gellary_img)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, TenthActivity.class);
spec = tabHost.newTabSpec("tenth").setIndicator("Opera", res.getDrawable(R.drawable.opera_img)).setContent(intent);
tabHost.addTab(spec);
// set the width of tab
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++){
tabHost.getTabWidget().getChildAt(i).getLayoutParams().width = 64;
}
//provide a method/function for setting height
// set the Height of tab
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++){
tabHost.getTabWidget().getChildAt(i).getLayoutParams().height = 60;
}
// set the background color of tab (#50000000-transparent,#7392B5)
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++){
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#50000000"));
}
//tabHost.getTabWidget().getChildAt(0).;
tabHost.setCurrentTab(0);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp"
android:background="#drawable/zero"
>
<TabHost android:id="#android:id/tabhost" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="visible">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:scrollbars="none">
<TabWidget android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#android:id/tabs"
android:layout_gravity="bottom"
android:tabStripEnabled="false"
/>
</HorizontalScrollView>
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#android:id/tabcontent"/>
</TabHost>
</LinearLayout>
Firstly, decide what list of features you want to support. It seems like you have some ideas already. Then create public methods in your Tabbar class that allow people to customize it.
An idea could be
public void setTabHeight(int dip) {
//Code to set tab height goes here
}
Then people can have access to your Tabber and the functions you want them to have.
The amount of functionality and the generality of Tabbar's use will be up to your imagination and design skills.

Categories

Resources