Set Title in Tab childs - android

How can I change a title from tab child?
I tried a simple setTitle(...) but it won't work.
(from the parent tab activity, it does, however...)
thanks,
Ori

Actually I got it to work this way:
In the TabActvitiy, override:
onChildTitleChanged().
This method is called when a child activity invoking setTitle(). from there you can change the title of the screen.

There is no API to modify the tabs once created -- sorry!

I know that if you're adding tabs dynamically you can use ts.setIndicator("MY TAB!");
...
like so:
myTabHost = (TabHost)this.findViewById(R.id.th_set_menu_tabhost);
myTabHost.setup();
ls1 = new ListView(Main_screen.this);
TabSpec ts1 = myTabHost.newTabSpec("TAB_TAG_1");
ts1.setIndicator("Tab1");
ts1.setContent(new TabHost.TabContentFactory(){
public View createTabContent(String tag)
{
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Main_screen.this,android.R.layout.simple_list_item_1,new String[]{"item1","item2","item3"});
ls1.setAdapter(adapter);
return ls1;
}
});
myTabHost.addTab(ts1);
here's the xml if you need it
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="64dip" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="64dip">
<ListView
android:id = "#+id/danhsach"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>

Related

Android layout: buttons at bottom similar to wechat

For my app, I am trying to put buttons at the bottom of all activities similar to WeChat app as can be seen in below image:
Now one way I thought of is simply adding <ImageButton>s at bottom of each activities but I don't think WeChat does that because you can swipe between activities and there is no onCreate triggered, activities are always there that are simply switched when swiped or touched on specific button.
I did research and found out one can use split action bar or even ViewFlipper. Action bar splitting is out of question since button will be moved back to action bar for example in landscape mode since I want to have buttons always appear at bottom like WeChat. Another option I saw was Tabbed interface but I saw with it, it can appear below actionbar unlike WeChat where even actionbar is changed based on activity.
Question: Does anyone know what does WeChat use for this functionality so I can implement the same ?
here is code main class
public class TabSample extends TabActivity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setTabs();
}
private void setTabs() {
addTab("Wechat", R.drawable.tab_home, ShowList.class);
addTab("Social", R.drawable.tab_search, UploadVideo.class);
addTab("Contact Us", R.drawable.tab_home, Contactus.class);
addTab("Setting", R.drawable.tab_search, DoNotDo.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 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:background="#ffffff"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_weight="0" />
</LinearLayout>
</TabHost>
One Idea is create BaseActivity, in which include the general design you prefer throughout the App. In other activities, extend BaseActivity. You can also extend Activity class in some activities where no need to show the buttons, such as LoginActivity.

Open child activity in Tab host android

My Tab_Bar.class define define Tabs
How I can open child activity
I am using only one single Tab_bar.class for Tab Host
public class Tab_Bar extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
setTabs() ;
}
void setTabs()
{
addTab("My Profile", R.drawable.home_normal, MyProfile.class);
addTab("Search", R.drawable.search_normal, JobSearch.class);
addTab("Saved Jobs", R.drawable.starred, Saved_jobs.class);
addTab("Job Alert", R.drawable.job_match, JobAlert.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);
tabHost.setCurrentTab(1);
}
}
I am using these xml file
TabIndicator.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dip"
android:layout_height="55dip"
android:layout_weight="1"
android:orientation="vertical"
android:background="#drawable/tab_indicator"
android:padding="5dp">
<ImageView android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="#drawable/icon"
/>
<TextView android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
style="?android:attr/tabWidgetStyle"
android:textSize="13sp"
/>
and Tab.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:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="fill_parent" android:layout_height="0dip"
android:layout_weight="1" />
<TabWidget android:id="#android:id/tabs"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="0" />
</LinearLayout>
</TabHost>
I am using Tab_bar.class and these xml file for Tab host but i can't have any idea about open
tab host child activity .
I am new in android.
Please Help me, How i can open child activity
Any Help is Appreciated
And I am really sorry about my bad English
This is not eaxctly what you need, but might help. I used this setup to create dynamic tabs and then do different things with them.
protected void onCreate(Bundle savedInstanceState) {
...
final TabHost Tabs = (TabHost) findViewById(android.R.id.tabhost);
Tabs.setup();
int count;
for (count =0;count < 2;count++){
...
final int passedTabId = count;
NewTab.setContent(new TabHost.TabContentFactory()
{
public View createTabContent(String tag)
{
...
RelativeLayout layout = new RelativeLayout(getApplicationContext);
android.widget.RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(params);
layout.setId(some ID);
layout.setBackgroundResource(R.drawable.room_background);
TextView dynText = new TextView(getApplicationContext);
...
layout.addView(dynText);
return layout;
// You can set onClickListeners, etc here and then assign them some functions you need
// You can also create different layouts for every tab according to the passedTabId
}
});
Tabs.addTab(NewTab);
}
}
Thing is, you cant just simply set another activity to run in each tab. You need to set some functions you need to the objects you create in that tab, but the main activity remains the same. Good luck :)
Firstly try using Fragment for creating tabs.
Feature which you are using is deprecated by now.
I have posted complete post over Dynamically changing the fragments inside a fragment tab host?
Please check that out and let me know if you have any concern.
Simply fallow each of defined steps one by one.
CONCEPT
I am putting simple example for clarifying you that exactly how this fragment works.
Take simple scenario of your bed. Your bed consist of several things such as pillow, bed sheet etc.
Now consider a case that your bed sheet is dirty. So what you will do by now. You will simply replace that sheet with a new one and then you will have a tight sleep :)
Similarly when you need to change your UI... then this fragment is replaced with a new one.
That's it. Now you are good to go with my post.
Thanks!

How to return result from Tabs to my Activity

I am developing an application using android 2.1. In one Page, upper part have two textviews and lower part have tablayout having two tabs.
My XML code is like this...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<include android:id="#+id/content" layout="#layout/content_table"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tabHost" android:layout_below="#+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<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="wrap_content" />
</LinearLayout>
</TabHost>
</RelativeLayout>
And activity class is like...
public class PageActivity extends ActivityGroup {
LogoBarActivity logoBar;
TabHost mTabHost;
TextView text1;
TextView text2;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.page);
text1 = findViewById(R.id.textview1);
text2 = findViewById(R.id.textview2);
mTabHost = (TabHost) findViewById(R.id.tabHost);
mTabHost.setup(this.getLocalActivityManager());
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, Type1Activity.class);
spec = mTabHost.newTabSpec("type1").setIndicator("Type1").setContent(intent);
mTabHost.addTab(spec);
intent = new Intent().setClass(this, Type2Activity.class);
spec = mTabHost.newTabSpec("type2").setIndicator("Type2").setContent(intent);
mTabHost.addTab(spec);
mTabHost.setCurrentTab(0);
mTabHost.getTabWidget().getChildAt(0).getLayoutParams().height /= 2;
mTabHost.getTabWidget().getChildAt(1).getLayoutParams().height /= 2;
}
}
Explanation: When user comes this page, he can click on any tabs either on Type1 or Type2. Both Tabs have ListView (Type1Activity & Type2Activity are ListView ).
When user click on any tab he will get ListView in Tabs. User can select any item from listview and that item will set in text1 and text2 variable of PageActivity class respectively.
Question: How can I return result from tab activity to my PageActivity when user select any item from list view of any Tab.
You can get the position from the ListView in the tabs, use that position to get corresponding data in the onClickListener.
Set this data in the TextViews. I am assuming ListViews' OnClickListener has reference to the TextViews

How can i change the background color for tabs in android?

i have working with tabs. i want to change the background color or theme for tab. when i click on the tab appears in default color as grey but i want to display that tab in custom color.
here my code:
<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:orientation="vertical"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-30dp"
android:background="#FF0000"
/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</FrameLayout>
</LinearLayout>
</TabHost>
class file is
public class AndroidtabActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// View title = getWindow().findViewById(android.R.id.title);
// View titleBar = (View) title.getParent();
// titleBar.setBackgroundResource(R.drawable.top_bar2);
Resources res = getResources();
TabHost tabHost = getTabHost();
//tabHost = (TabHost) findViewById(R.id.tabhost);
tabHost.setup();
//tabHost.setBackgroundResource(R.drawable.pink);
TabHost.TabSpec spec;
//tabHost.getTabWidget().setDividerDrawable(R.drawable.top_bar);
Intent in;
in = new Intent().setClass(this, MainActivity.class);
spec=tabHost.newTabSpec("calc").setIndicator("calculate").setContent(in);
tabHost.addTab(spec);
in = new Intent().setClass(this, TutorialZoomActivity1.class);
spec=tabHost.newTabSpec("help").setIndicator("help").setContent(in);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
// spec1.setIndicator("Tab 1",getResources().getDrawable(R.drawable.flash));
}
}
please tell me how can i change the background color for tabs.
Maybe you should create your custom tab.
Let say.. create another custom_tab.xml (linearlayout and textview)
in the linearlayout, set android:background to another xml file in the drawable (#drawable/tab_selector)
Create new xml file in the drawable just mention above. ex. tab_selector.xml. in this file, you can set state (focused, pressed, selected etc) good luck
You can change tab background color by
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
tabHost.getTabWidget().getChildAt(i).
setBackgroundResource(R.drawable.hans_layout_nblue);
}

TabHost not showing contents in the beginning

I have a TabActivity with two tabs. the layout of the activity is as follows:
<?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">
<ListView android:id="#+id/list"
android:layout_width="fill_parent" android:layout_height="fill_parent" />
</FrameLayout>
</LinearLayout>
</TabHost>
The layout consists of a ListView which is populated accordingly in setOnTabChangedListener(). no problem with filling the list and showing it.
My problem is that the list view is not shown when the activity starts, although I find it by ID and populate it; it is only populated after I change the tabs.
and the code in the onCreate(..) looks like this:
l = (ListView) findViewById(R.id.list);
l.setAdapter(new CommentsAdapter(this, (JSONArray) obj));
TabSpec spec;
spec = tabHost.newTabSpec("0").setIndicator("0",
res.getDrawable(R.drawable.tab_recent)).setContent(
R.id.list);
tabHost.addTab(spec);
spec = tabHost.newTabSpec("1").setIndicator("1",
res.getDrawable(R.drawable.tab_pop)).setContent(
R.id.list);
tabHost.addTab(spec);
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
int tab = Integer.valueOf(tabId);
showDialog(WAIT_DIALOG);
switch (tab) {
// recent
case 0:
//refill the list
break;
// popular
case 1:
//refill the list
break;
}
}
});
tabHost.setCurrentTab(0);
any hints?
I had a similar issue. What I noticed is that if you do
tabHost.setCurrentTab(0);
then onTabChanged is not triggered. BUT, if you do
tabHost.setCurrentTab(1);
then it is, the list shows up and all it's fine. As per why this happens, it remains a bit of a mistery to me.
Use this cheat to start your app on the first tab:
in onCreate(), after you've created the tabs call first:
tabHost.setCurrentTab(1);
Then call:
tabHost.setCurrentTab(0);
You can try use different layout, say
spec = tabHost.newTabSpec("0").setIndicator("0",
res.getDrawable(R.drawable.tab_recent)).setContent(
R.id.list1);
tabHost.addTab(spec);
spec = tabHost.newTabSpec("1").setIndicator("1",
res.getDrawable(R.drawable.tab_pop)).setContent(
R.id.list2);
tabHost.addTab(spec);
you just do another activity in which list view is present & put the below one
Intent i1 = new Intent(tabs.this,listDisplay.class);
spec=tabHost.newTabSpec("0").setIndicator("0",res.getDrawable(R.drawable. tab_recent)).setContent(i1);
Also put the tabHost.setCurrentTab(0);
before ontabChangeListener Hope this might help..
hope this might solve the problem..

Categories

Resources