How to create custom api for tabhost and tabwidget? - android

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.

Related

Problems with android TabHost

I am doing an app, and on this one I want to use tabs.
This is the Activity where I manage my tabs:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Resources res = getResources();
tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, WUL4Bus.class);
spec = tabHost.newTabSpec("mapa");
spec.setIndicator("Mapa",res.getDrawable(R.drawable.ic_tab_mapa));
spec.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Esquematico.class);
spec = tabHost.newTabSpec("esquemático");
spec.setIndicator("Esquemático",res.getDrawable(R.drawable.ic_tab_listado));
spec.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, ListaTickets.class);
spec = tabHost.newTabSpec("tickets");
spec.setIndicator("Tickets",res.getDrawable(R.drawable.ic_tab_bono));
spec.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, ListaFavoritos.class);
spec = tabHost.newTabSpec("favoritos");
spec.setIndicator("Favoritos",res.getDrawable(R.drawable.ic_tab_directo));
spec.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, About.class);
spec = tabHost.newTabSpec("acerca de");
spec.setIndicator("Acerca de",res.getDrawable(R.drawable.ic_tab_info));
spec.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
instancia = this;
}
This is the layout :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</HorizontalScrollView>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
My first problem is that, when I run the app, it is properly working, but it doesn´t show the icons. Without the HorizontalScrollView there was no problems, but now, it is not showing the icons.
The other problem is that I would like my app to show only one tab per screen(just like Google Play does) and not several tabs.
Thanks a lot
we add like this every time we add only same value just change like this
intent1 = new Intent().setClass(this, Esquematico.class);
spec1 = tabHost.newTabSpec("esquemático");
spec.setIndicator("Esquemático",res.getDrawable(R.drawable.ic_tab_listado));
spec.setContent(intent1);
tabHost.addTab(spec1);
intent2 = new Intent().setClass(this, ListaTickets.class);
spec2 = tabHost.newTabSpec("tickets");
spec.setIndicator("Tickets",res.getDrawable(R.drawable.ic_tab_bono));
spec.setContent(intent2);
tabHost.addTab(spec2);
intent3 = new Intent().setClass(this, ListaFavoritos.class);
spec3 = tabHost.newTabSpec("favoritos");
spec.setIndicator("Favoritos",res.getDrawable(R.drawable.ic_tab_directo));
spec.setContent(intent3);
tabHost.addTab(spec3);........
...............................................
see THIS example....

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.

Reduce Tab height and align text on top?

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

How to change size of tabs

Actually i have created a tab in my app.here there are three tabs..but thing is that i am not able to manage style in the tabsan morevere i want size of one tab larger than the others two i am sending my code u plese check it..
XML CODE:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal"
>
<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>
Java Code:
setContentView(R.layout.main);
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, ContactActivity.class);
spec = tabHost.newTabSpec("Contacts").setIndicator("Contacts",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, CallActivity.class);
spec = tabHost.newTabSpec("Call").setIndicator("Call",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, MyInfoActivity.class);
spec = tabHost.newTabSpec("My Info").setIndicator("MyInfo",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
Here the code for changing the size of tab
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
TabHost mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("tab_test1")
.setIndicator((""),getResources().getDrawable(R.drawable.mzl_05))
.setContent(new Intent(this, NearBy.class)));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2")
.setIndicator((""),getResources().getDrawable(R.drawable.mzl_08))
.setContent(new Intent(this, SearchBy.class)));
mTabHost.setCurrentTab(0);
mTabHost.getTabWidget().getChildAt(0).setLayoutParams(new
LinearLayout.LayoutParams((width/2)-2,50));
mTabHost.getTabWidget().getChildAt(1).setLayoutParams(new
LinearLayout.LayoutParams((width/2)-2,50));

Categories

Resources