Custom tabs in Android - android

I am trying to change the content of the tab header but am struggling. I can see that it is possible to pass a View to the setIndicator() method. I tried this and got a removeParent() warning which I dont understand. Has anyone managed to do this successfully. If so can you explain how you did it please?
Clive

This problem occurs when you use the same view in different tabs. You must use a different view. This is what I did:
view.setImageResource(R.drawable.videotab);
view1.setImageResource(R.drawable.musictab);
intent = new Intent().setClass(this, CustomList.class);
spec = tabHost.newTabSpec("Video").setIndicator(view)
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, CustomListOne.class);
spec = tabHost.newTabSpec("Music").setIndicator(view1)
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);

You don't have to use different Views. You can simply inflate the same layout again when you need it. That means you inflate your layout, set everything you need on your different Views and then you set it as your indicator. If you need another tab just do it again.

Related

Add icon to tab in android

I am trying to add an icon to a tab in android and it isnt working.
here is part of my code, im not sure what is wrong
th = (TabHost) findViewById(R.id.thbodyview);
th.setup();
TabSpec specs = th.newTabSpec("tag1");
specs.setContent(R.id.Front);
specs.setIndicator("Front",getResources().getDrawable(R.drawable.tab_one));
th.addTab(specs);
When I run the app the tab just says "Front" and there is no icon, if someone could fix it that would be great.
Thanks
I know this question was asked over a year ago, but I shall provide an answer anyway, as it might be helpful to others wanting to know how this is possible.
The following code will enable you to retrieve the TextView and ImageView that form the title / heading of each tab (defined in the standard layout/tab_indicator.xml layout file):
TabHost tabHost = (TabHost) activity.findViewById(android.R.id.tabhost);
View currentTab = tabHost.getTabWidget().getChildAt(areaIndex);
or to get the view of the current tab that's in view:
View currentTab = tabHost.getCurrentTabView();
Then to actually get the Text and Image views:
TextView tabTitleField = (TextView) currentTab.findViewById(android.R.id.title);
ImageView tabTitleIcon = (ImageView) currentTab.findViewById(android.R.id.icon);
tabTitleIcon.setImageResource([specify your drawable resource here]);
Padding can be programmatically added to the ImageView using tabTitleIcon.setPadding(0, 0, 10, 0);
to add spacing between the title and the icon.

How can I make a tab image larger?

I got this image in a tab but it's too small. how can I scale it up or make it fill the tab
final TabHost tabHost = getTabHost(); //edit: added this line
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("Discussion",
getResources().getDrawable(R.drawable.forum))
.setContent(new Intent(this, MyActivity.class)));
Set a LinearLayout as a child of the TabHost and add your image to that LinearLayout
You won't be able to with the method you're using. The drawable is mean't to be a small icon. If you want something custom, you'll have to use setIndicator(View) instead. I'd image you'd create an ImageView set to use the drawable you want and pass that in. If you wanted the text "Discussion" to still be there, you'd had to create a layout with an ImageView and TextView and use that.

Customize tabs android

This is how I want the tabs to look:
http://img14.imageshack.us/img14/5696/tabort.png
This is how they look using tabHost:
http://img684.imageshack.us/img684/1030/tabort2.png
So I want to remove the border around the images. Instead, I want to have the greyish background image behind the tabs. Can anyone please help me with this (I'm new to Android)?
Here is some relevant code:
// Create an Intent to launch an Activity for the tab
intent = new Intent().setClass(this, WashActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("washer").setIndicator("",
res.getDrawable(R.drawable.wash_tab))
.setContent(intent);
tabHost.addTab(spec);
You can use Buttons positioned side-by-side in Relative Layout with custom background images instead of TabView.
set custom view ( imageview or image with text in ur case ) using setView() instead setIndicator() will work for you .
TabHost is deprecated now . so better to use fragment with compatibility package .

Android Change Image

I am trying to develop my first game but i am having trouble, it is something really simple which i am confused about. Basically I need to load 2 images i created in photoshop but if they are clicked, a different image of the same shape will be drawn on top, making it look like its been selected, then if someone clicks on the other image, image one goes back to the initial state and another image for the second one is drawn on top... How can I achieve this?
I am just having trouble on how to load the initial images to the screen and thenbe able to swap them with the others...
Thank you
If I understand your question right, it sounds like using tabs would be a solution.
Smth like this might be a first step to your game
public class MyTab extends TabActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The acWednesdaytivity 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, YourWithImage1.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("tab1").setIndicator("tab1",
res.getDrawable(R.drawable.ic_tab))
.setContent(intent);
tabHost.addTab(spec);
}}
and your ic_tab will look like
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey img-->
<item android:drawable="#drawable/ic_tab_grey"
android:state_selected="true" />
<!-- When not selected, use white img-->
<item android:drawable="#drawable/ic_tab_white" />
</selector>
I am new myself to andorid. Hope this will help somehow.
I'm new to Android too, so not sure if I have the correct answer. But here is what I might try:
Have your activity implement onclicklistener
In the onCreate method, find your ImageView using findViewById
call the ImageView's setOnclickListener method
Handle the onclick event
In the event handler, call the setImageResource of the ImageView to switch the image.
Again, I'm new to Android too so I'm not talking from experience. I hope this helps.
See the following link which I happened to come across:
http://www.higherpass.com/Android/Tutorials/Working-With-Images-In-Android/

How can change my TabHost?

I have a tabhost,
e.g:
final TabHost tabs = getTabHost();
tabs.setup();
TabHost.TabSpec spec = null;
spec = tabs.newTabSpec("search");
spec.setContent(new Intent(this, Search.class));
spec.setIndicator("search");
tabs.addTab(spec);
in this tabhost is a Intent,and in the activity must change to other activity,
question is I hope the other actitivy at same tabhost switch?
Can do this?
I had this problem almost a 3 months ago. you can not replace the activity. Because activities are open in new screens. so you have to use the views and layout to achieve what you are want to do. use the setVisiblity and isShown methods you can hide and release yours view. Hope it make some sense on it.

Categories

Resources