Hey all, I've setup a tab layout so that each tab is a custom bitmap of mine. This is the main java file:
public class HelloTabWidget1 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 tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, ArtistsActivity.class);
ImageView imgView1 = new ImageView(this);
BitmapDrawable bmd1 = new BitmapDrawable();
bmd1 = (BitmapDrawable) res.getDrawable(R.drawable.blue);
imgView1.setImageDrawable(bmd1);
spec = tabHost.newTabSpec("firsttab").setIndicator(imgView1)
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, AlbumsActivity.class);
ImageView imgView2 = new ImageView(this);
BitmapDrawable bmd2 = new BitmapDrawable();
bmd2 = (BitmapDrawable) res.getDrawable(R.drawable.pink);
imgView2.setImageDrawable(bmd2);
spec = tabHost.newTabSpec("secondtab").setIndicator(imgView2)
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, SongsActivity.class);
ImageView imgView3 = new ImageView(this);
BitmapDrawable bmd3 = new BitmapDrawable();
bmd3 = (BitmapDrawable) res.getDrawable(R.drawable.purple);
imgView3.setImageDrawable(bmd3);
spec = tabHost.newTabSpec("thirdtab").setIndicator(imgView3)
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Forthtab.class);
ImageView imgView4 = new ImageView(this);
BitmapDrawable bmd4 = new BitmapDrawable();
bmd4 = (BitmapDrawable) res.getDrawable(R.drawable.red);
imgView4.setImageDrawable(bmd4);
spec = tabHost.newTabSpec("forthtab").setIndicator(imgView4)
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
I'm able to get any bitmap I want as a tab, the problem is my original bitmaps were too small to see, once I increased them in size the tabs end up in the middle of the screen with a huge blank black space above it. When I click a black space it changes tabs accordingly. Is there a way to position it back to the very top and make each bitmap fill a tab?
I was able to find a temporary solution. I basically kept the code exactly the same, but played around with my image files. I changed my bitmap pictures to png files, but the important thing is the guideline here looks like the Android system has an extremely sensitive system for tab icons. As I said this is a temporary solution, I plan to play with it more.
The only thing I need help with now is having the tabs connected, using png icons at Google's recommended setting of 48 x 48px works, but the tab icons aren't touching. Looking for a solution to have them touching so they're right next to each other.
Related
I want to add background images for Tabhost which i already added in selector xml. But not sure how to add resource while initiating Tab. Below is tab :
Code to add :
getResources().getDrawable(R.drawable.tabicon)
/* Tabs */
Bundle bundle = getIntent().getExtras();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
// First Activity
intent = new Intent().setClass(this, InfoListView.class);
spec = tabHost.newTabSpec("some_things").setIndicator("Info").setContent(intent);
tabHost.addTab(spec);
// Second Activity
intent = new Intent().setClass(this, LogListView.class);
spec = tabHost.newTabSpec("top_things").setIndicator("Sync Log").setContent(intent);
tabHost.addTab(spec);
tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = 95;
tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = 95;
/* Tabs ends */
Create a TextView , set text to that textView, set background drawable to it, and set this textview as indicator
Somewhat like this:
TextView mTv = new TextView(mContext);
mTv.setText(/*Your-text*/);
mTv.setBackgroundDrawable(mContext.getResources.getDrawable(/*id-of-your-image*/));
spec = tabHost.newTabSpec("top_things").setIndicator(mTv).setContent(intent);
tabHost.addTab(spec);
After your tabhost is intilize with indicator and other things add background to it like this
tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tabicon); //fro first tab
tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tabicon); //for second tab
I'm already built custom tabs only with images and now i need to change this tabs images when user switch from Arabic\English button from another activity and
here is my code to get tabs from tab activity
mainTabs = ((TabActivityMy) getParent()).getTabHost();
mainTabs.getTabWidget().setDividerDrawable(null);
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = mainTabs.newTabSpec(null);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, ((TabActivityMy) getParent()).getTabHost().getTabWidget(), false);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
mainTabs.addTab(spec);
when i do this app add new tabs i know this code is do that but i just need to change images for all current tabs
One way would be to remove all the views from tabhost first:
mainTabs = ((TabActivityMy) getParent()).getTabHost();
mainTabs.removeAllViews()
mainTabs.getTabWidget().setDividerDrawable(null);
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = mainTabs.newTabSpec(null);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, ((TabActivityMy) getParent()).getTabHost().getTabWidget(), false);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
mainTabs.addTab(spec);
Try removing allviews from TabWidget:
mainTabs.getTabWidget().removeAllViews();
There is function:
mainTabs.clearAllTabs();
This should work in your case
You can check the value of your language switch and set the image resources based on it..
for example:
if(lamguage==english)
your_imageView.setImageResource(R.drawable.your_english_image);
else
your_imageView.setImageResource(R.drawable.your_arabic_image);
I want to create layout with TabHost and I want to create activity for each tab to do some calculation. In addition i want this to work on all android >=Froyo. I searched for the solution everywhere which was not clear and conclusive. So if anybody could help me with this it could be a great help.
Nice question really helpful for others,
Use below code to work with Tabs and to call activities by click on a particular tab:
public class TabSample extends TabActivity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabmain);
setTabs() ;
}
private void setTabs()
{
addTab("Tab1", R.drawable.tab1,Home.class);
addTab("Tab2", R.drawable.tab2,AboutUs.class);
addTab("Tab3", R.drawable.tab3,Services.class);
addTab("Tab4", R.drawable.tab4,Contact.class);
}
private void addTab(String labelId, int drawableId, Class<?> c)
{
TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText(labelId);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
}
Here is the whole implementation of TabHost which works in all versions of android. see my answer in given link
tabs and images with some devices
I have a tabhost created by
this.tabHost = getTabHost();
// Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch the first Activity for the tab (to be reused)
intent = new Intent().setClass(this, FirstGroup.class);
// Initialize a TabSpec for the first tab and add it to the TabHost
spec1 = tabHost.newTabSpec("FirstGroup").setIndicator("Regionlar",
getResources().getDrawable(R.drawable.region2)) // Replace null with R.drawable.your_icon to set tab icon
.setContent(intent);
tabHost.addTab(spec1);
And I want to change label of tabhost programmatically: "Regionlar" to "newMenuTabbar". I couldn't find any example. thanks for attention.
Edit:
I want to change second tabitem's label from "Mənzərələr"=> "secondTabitem"
intent = new Intent().setClass(this, FirstGroup.class);
// Initialize a TabSpec for the first tab and add it to the TabHost
spec1 = tabHost.newTabSpec("FirstGroup").setIndicator("Regionlar",
getResources().getDrawable(R.drawable.region2)) // Replace null with R.drawable.your_icon to set tab icon
.setContent(intent);
tabHost.addTab(spec1);
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, SecondActivityGroup.class);
spec2 = tabHost.newTabSpec("SecondActivityGroup").setIndicator("Mənzərələr",
getResources().getDrawable(R.drawable.img_gallery_icon)) // Replace null with R.drawable.your_icon to set tab icon
.setContent(intent);
tabHost.addTab(spec2);
Try this:
final TextView label = (TextView) tabHost.getTabWidget().findViewById(android.R.id.title);
label .setText(YOUR NEW LABEL);
Hope it will help.
b.i 's code can only change the first tab's title.
I think this is a more straight way to get the thing done:
((TextView) mTabHost.getTabWidget().getChildTabViewAt(position)
.findViewById(android.R.id.title))
.setText(yourTitle);
where position is the position of the tab and yourTitle is the title you wish to set for the tab. If you want to change the text for the current tab, then instead of replacing position by getCurrentTab, you can just replace getTabWidget().getChildTabViewAt(position)
by getCurrentTabView()
Whoever wrote this should have defined a setTabText(int position, String text) method, otherwise who would know they have a text view id'ed android.R.id.title? Or if they already had, please enlighten me.
Try this code
public class SlideMainActivity extends TabActivity {
public static RelativeLayout headerLayout;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main_silde_tab);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.hd_header);
setTabs();
}
private void setTabs() {
addTab("FirstGroup", R.drawable.tab_home, FirstGroup.class);
addTab("Regionlar", R.drawable.tab_search, Regionlar.class);
}
private void addTab(String labelId, int drawableId, Class<?> c) {
TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);
View tabIndicator = LayoutInflater.from(this).inflate(
R.layout.tab_indicator, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText(labelId);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
}
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.