Custom TabWidget Android Tab Indicator - android

Okay, so i am making an android app that has tabs, now my problem is that the tab widget isn't uniform across the diffrent android versions or devices.
I want to make it to be the same on any android this is my tab activity
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class Cook extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cook_layout);
TabHost tabHost = getTabHost();
// Tab for Snacks
TabSpec snackspec = tabHost.newTabSpec("Snacks");
// setting Title and Icon for the Tab
snackspec.setIndicator("Snacks", getResources().getDrawable(R.drawable.cook_icon_tab_snacks));
Intent snacksIntent = new Intent(this, Cook_tab_snacks.class);
snackspec.setContent(snacksIntent);
// Tab for Mains
TabSpec mainspec = tabHost.newTabSpec("Mains");
mainspec.setIndicator("Mains", getResources().getDrawable(R.drawable.cook_icon_tab_snacks));
Intent mainsIntent = new Intent(this, Cook_tab_mains.class);
mainspec.setContent(mainsIntent);
// Tab for Desserts
TabSpec dessertspec = tabHost.newTabSpec("Desserts");
dessertspec.setIndicator("Desserts", getResources().getDrawable(R.drawable.cook_icon_tab_snacks));
Intent dessertsIntent = new Intent(this, Cook_tab_desserts.class);
dessertspec.setContent(dessertsIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(snackspec); // Adding snacks tab
tabHost.addTab(mainspec); // Adding mains tab
tabHost.addTab(dessertspec); // Adding desserts tab
}
}
I also have my XML layout :
<?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:background="#drawable/gradient_bg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
I made a new indicator xml which is like the main android tab indicator v4
I followed and searched alot of blogs , i couldn't find my answer ...
I really want to make the android tabs uniform across all android versions and to make the colors nice , since orange and yellow dont really fit with the color theme in my app
Help please!!!!
I cant seem to find a way to fix it...
Cheers

okay i found a solution.
here is the code:
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class Cook extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cook_layout);
TabHost tabHost = getTabHost();
// Tab for Snacks
TabSpec snackspec = tabHost.newTabSpec("Snacks");
// setting Title and Icon for the Tab
snackspec.setIndicator(makeTabIndicator(getResources().getDrawable(R.drawable.cook_icon_tab_snacks)));
Intent snacksIntent = new Intent(this, Cook_tab_snacks.class);
snackspec.setContent(snacksIntent);
// Tab for Mains
TabSpec mainspec = tabHost.newTabSpec("Mains");
mainspec.setIndicator(makeTabIndicator( getResources().getDrawable(R.drawable.cook_icon_tab_snacks)));
Intent mainsIntent = new Intent(this, Cook_tab_mains.class);
mainspec.setContent(mainsIntent);
// Tab for Desserts
TabSpec dessertspec = tabHost.newTabSpec("Desserts");
dessertspec.setIndicator(makeTabIndicator( getResources().getDrawable(R.drawable.cook_icon_tab_snacks)));
Intent dessertsIntent = new Intent(this, Cook_tab_desserts.class);
dessertspec.setContent(dessertsIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(snackspec); // Adding snacks tab
tabHost.addTab(mainspec); // Adding mains tab
tabHost.addTab(dessertspec); // Adding desserts tab
}
//making the tab view:
private View makeTabIndicator(Drawable drawable){
ImageView Tabimage = new ImageView(this);
LayoutParams LP = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT,1);
LP.setMargins(1, 0, 1, 0);
Tabimage.setLayoutParams(LP);
Tabimage.setImageDrawable(drawable);
Tabimage.setBackgroundResource(R.drawable.tabview);
return Tabimage;
}}
I dont know weather or not i need the cook_layout anymore i'll see if i can remove it or leave it later... right now i just want to get it all working and later i'll come round by for a clean and tiding up
hope that helps you guys out there that stumble upon this question! cheers

Related

deprecated TabActivity in android no errors but the application unfortunately stopped when clicking button

PS. i am new in android and java programming.
i have this problem in my application it crashes when i clicked on my intent button which handles tab activity (the deprecated one), there are no errors with some warnings but i can't seem to figure out what i am doing wrong.
here is the code.
InformationActivity.class
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class InformationActivity extends TabActivity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_information);
// create the TabHost that will contain the Tabs
TabHost tabHost = (TabHost)findViewById(R.id.tabhost);
TabSpec tab1 = tabHost.newTabSpec("First Tab");
TabSpec tab2 = tabHost.newTabSpec("Second Tab");
// Set the Tab name and Activity
// that will be opened when particular Tab will be selected
tab1.setIndicator("Tab1");
tab1.setContent(new Intent(this,OfflineTab.class));
tab2.setIndicator("Tab2");
tab2.setContent(new Intent(this,OnlineTab.class));
/** Add the tabs to the TabHost to display. */
tabHost.addTab(tab1);
tabHost.addTab(tab2);
}
}
OfflineTab.class
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TextView;
public class OfflineTab extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView tv=new TextView(this);
tv.setTextSize(25);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setText("Contains R.E.D. e - Kit Offline module");
setContentView(tv);
}
}
OnlineTab.class
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TextView;
public class OnlineTab extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView tv=new TextView(this);
tv.setTextSize(25);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setText("This Is Tab2 Activity");
setContentView(tv);
}
}
activity_information.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/tabhost">
<LinearLayout
android:id="#+id/LinearLayout01"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TabWidget
android:id="#android:id/tabs"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</FrameLayout>
</LinearLayout>
</TabHost>
The log said:
java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
So just don't define a new id for tabhost:
activity_information.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:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
</LinearLayout>
</TabHost>
onCraete() of InformationActivity
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_information);
// create the TabHost that will contain the Tabs
TabHost tabHost = getTabHost(); // <- get tabhost by this
TabSpec tab1 = tabHost.newTabSpec("First Tab");
TabSpec tab2 = tabHost.newTabSpec("Second Tab");
// Set the Tab name and Activity
// that will be opened when particular Tab will be selected
tab1.setIndicator("Tab1");
tab1.setContent(new Intent(this,OfflineTab.class));
tab2.setIndicator("Tab2");
tab2.setContent(new Intent(this,OnlineTab.class));
/** Add the tabs to the TabHost to display. */
tabHost.addTab(tab1);
tabHost.addTab(tab2);
}
And it worked!

Android app using TabHost with Multiple Maps causing issue

I am using TabHost in my application and I need to show 2 maps in 2 different tabs. I have been able to integrate maps but the problem is when I move from one tab to another the map hangs/stucks and does not respond i.e. maps only works on one of the screen
Below is xml and class for my main layout which has TabHost
<?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="#ff0000" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff0000"
>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#f00"
android:foregroundGravity="top"
android:layout_above="#android:id/tabs"
/>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#fff"
android:tabStripEnabled="false"
>
</TabWidget>
</RelativeLayout>
</TabHost>
package com.cotechnica.alps;
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;
import com.ankitkedia.alps.R;
public class Main extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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, Map1.class);
spec = tabHost.newTabSpec("home").setIndicator("Map1",
getResources().getDrawable(android.R.drawable.star_on)).setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, Map2.class);
spec = tabHost.newTabSpec("rescue").setIndicator("Map2",
getResources().getDrawable(android.R.drawable.star_big_off)
).setContent(intent);
tabHost.addTab(spec);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
I have seen many people are facing same issue but there is no proper solution available for it.
Any help would be appreciated, I can sent the sample project source if required
Regards,
Ankit
This guy has a great solution
http://www.ankitkedia.com/2013/07/07/android-google-map-v2-use-multiple-maps-in-application/

How to locate drawable folder in Android

According to this http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
developer page says, create a XML file which should holds the icons to indicate state of resource. But by default a new project is created, this Drawable folder does not exist but i will be having 4 diff folder indicate different resolution images.
I forcefully created a folder named Drawable and place the XML file and used in my application, but unfortunately my application throws exception saying resource is NULL. How to overcome this issue?
Where do we have to place the XML file?, should we make individual XML file and place in different resource folders? kindly advice me
Here is the image of folder structure.
If i use the ic_tab_artist, my app crashes. here is the full source
package simple.tab.proj;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TextView;
public class SimpleTabActivity 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(); // 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, 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, ArtistsActivity.class);
spec = tabHost.newTabSpec("albums").setIndicator("Albums",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, ArtistsActivity.class);
spec = tabHost.newTabSpec("songs").setIndicator("Songs",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
}
public static class ArtistsActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("This is the Artists tab");
setContentView(textview);
//setContentView(R.layout.main);
}
}
}
This is my Main.xml file
<?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>
Here is my ic_artist_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- When selected, use grey -->
<item android:drawable="#drawable/ic_tab_artists_grey"
android:state_selected="true" />
<!-- When not selected, use white-->
<item android:drawable="#drawable/ic_tab_artists_white" />
</animation-list>
I forcefully created a folder named Drawable and place the XML file and used in my application, but unfortunately my application throws exception saying resource is NULL. How to overcome this issue?
Android is case-sensitive. The directory needs to be res/drawable/, not Drawable.
should we make individual XML file and place in different resource folders?
One XML file in res/drawable/ should suffice, though the individual drawable resources pointed to by your StateListDrawable may need different versions for different densities.

Getting a False return from listview

I am getting a false return when implementing a listview within a framelayout/tab layout. For the life of me I can't seem to figure out why I am getting a false return on my app when implementing a listview. The code works on a external project but I cant seem to intergrate it into this one. I will list the main activity groups used within the project and the xml file for the page that the string is meant to appear on. Sorry its alot of code but this has been driving me insane over the last few days cause it should be relatively simple.
The xml layout files are held in framelayout coding within the main.xml(which is not shown) so have I got the ID wrong when referencing? Any help to solve this problem would be amazing as I know its a really simple solution that Im just missing. Thanks guys.
If I havnt explained it well a picturee of the error is below
View the page here http://i41.tinypic.com/do04ee.png
Their are no log errors.
Main Activity
(Please note I have removed code to make it more relavant and clear and to show the links from activity to activity)
package workout.fitty;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
public class WorkoutFittyActivity 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(); // 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
// More
**intent = new Intent().setClass(this, MoreActivity.class);
spec = tabHost.newTabSpec("songs").setIndicator("More",
res.getDrawable(R.drawable.tab_more))
.setContent(intent);
tabHost.addTab(spec);**
tabHost.setCurrentTab(1);
}
}
MoreActivity
package workout.fitty;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class MoreActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.more_layout);
Intent Intent = new Intent(this, MyListActivity.class);
}
}
MyListActivity
package workout.fitty;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MyListActivity extends ListActivity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
String[] values = new String[] { "BMI", "Body Measurements", "Logs",
"Feedback", "How To Use", "About" };
// Use your own layout
**ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.more_layout, R.id.label, values);
setListAdapter(adapter);**
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
}
}
And the XML file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/icon"
android:layout_width="22px"
android:layout_height="22px"
android:layout_marginLeft="4px"
android:layout_marginRight="10px"
android:layout_marginTop="4px"
android:src="#drawable/ic_launcher" >
</ImageView>
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label"
android:textSize="20px" >
</TextView>
</LinearLayout>
Main XML File with container and framelayout
<?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>
change your:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.more_layout, R.id.label, values);
in
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.more_layout, values);

Selection problem in tabs

I am making an application where i need tabs placed at the bottom of the screen .
I have managed to get to implement the code of tabs but i can't get them to be placed in the bottom of the screen.
Also i don't want any of the tabs to be selected i.e. i just want four tabs to be present at the bottom of the screen and above the tabs there are some image views and buttons.
I have linked the tabs to respective activites but i don't want the tab bar to be functional until the user wants to go and click it.
Till then i just want a normal gui screen with tabs at the bottom with none of them pre selected.
package com.tabs;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
public class HelloTabWidget 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, 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_grey)).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_artists_grey)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, SongsActivity.class);
spec = tabHost.newTabSpec("songs").setIndicator("Songs",res.getDrawable(R.drawable.ic_tab_artists_grey)).setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
}
}
---------- To place tab at the bottom add the below code
<TabHost android:id="#android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff"><RelativeLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"><FrameLayout android:id="#android:id/tabcontent" android:background="#drawable/bg" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_above="#android:id/tabs"/><TabWidget android:id="#android:id/tabs" android:background="#drawable/tab_bar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:cacheColorHint="#fff" android:layout_alignParentBottom="true" android:textStyle="bold"/></RelativeLayout></TabHost>
##
and if yu dont want any tabs to be selected remove
tabHost.setCurrentTab(2);

Categories

Resources