Refreshing Android Tabs without Intent - android

Please how can I refresh the content of Tab 2 (tag 2,Live blog) without adding intent.
TabHost th = (TabHost)findViewById(R.id.tabhost1);
th.setup();
TabHost.TabSpec spec = th.newTabSpec("tag1")
.setIndicator("Testimonies", getResources()
.getDrawable(R.drawable.icon_testimonies_tab))
.setContent(R.id.tab1);
th.addTab(spec);
spec = th.newTabSpec("tag2")
.setIndicator("Live Blog", getResources()
.getDrawable(R.drawable.icon_liveblog_tab))
.setContent(R.id.tab2);
th.addTab(spec);
spec = th.newTabSpec("tag3")
.setIndicator("Streaming Settings", getResources()
.getDrawable(R.drawable.icon_streamingsetting_tab))
.setContent(R.id.tab3);
th.addTab(spec);
th.setCurrentTab(1);
th.getCurrentView().setVisibility(View.VISIBLE);

We "see" each other again, #meduteniopeyemi ! I worked hard trying to solve the issue you are talking about. And the aproach that I used to do it was:
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId) {
//YOUR CODE checking if "tabId" is the tab you want to REFRESH
}
});
It worked so far for me. It just allows you to do any refreshing without problems.

Related

Opening OptionsMenu on the touch of a tab of the TabActivity

I have a tabActivity where I am adding tabs on runtime. So I think this is the usual code to do that:
_tabSpec = TabHost.newTabSpec("More");
_tabSpec.setIndicator("", Resources.GetDrawable(Resources.Drawable.myIcon).SetContent(intent);
TabHost.AddTab(_tabSpec);
Now the thing is, I have defined an options menu and I want to pop that up when the user clicks on the 'More' tab. I don't know how to do that. I tried not setting a content on that tab and simply use the OpenOptionsMenu() to pop it, but it doesn't seem to work.
Any clue how to achieve that?
P.S.: This is a C# code written in Xamarin. It might not look like the native java-android code, but its almost the same.
Alright, here's an answer to what I was trying to do.
Motive: Add a 'More' tab to the existing TabActivity. When user clicks it, open some kind of a PopUpWindow or ContextMenu, etc.
Steps to do that:
1) Create a tabSpec and add that tab to the TabHost as shown in the question.
2) Now you need to take in this last added tab as a View type variable. Do this by..
View v = TabWidget.GetChildAt(index)
Remember index of tabs starts from 0
3) Now on the onCreate() method of your main activity (one that holds the TabActivity) add an onTouchListener() (I use C#, so I added v.Click+=myFunction()) and write your code of the PopupWindow or ContextMenu or whatever you want to do there.
private String lastTab = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TabHost tabHost = getTabHost();
TabHost.TabSpec tabSpec;
tabSpec = tabHost.newTabSpec("tab1");
tabSpec.setIndicator("Tab 1");
tabSpec.setContent(new Intent(this, OneActivity.class));
tabHost.addTab(tabSpec);
tabSpec = tabHost.newTabSpec("tab2");
tabSpec.setIndicator("Tab 2");
tabSpec.setContent(new Intent(this, TwoActivity.class));
tabHost.addTab(tabSpec);
tabSpec = tabHost.newTabSpec("more");
tabSpec.setIndicator("More");
tabSpec.setContent(new Intent(this, OneActivity.class));
tabHost.addTab(tabSpec);
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
if (tabId.equalsIgnoreCase("more")){
openOptionsMenu();
tabHost.setCurrentTabByTag(lastTab);
}
else lastTab = tabId;
}
});
}

Custom Tab Content

I made custom tabs using this tutorial: http://joshclemm.com/blog/?p=136
I have them completely customized and looking nice, but now I don't know how to add content to the tabs. I don't even know where to start with the way this code is written, any help? Thanks.
This is the code that I think sets the content:
private void setupTab(final View view, final String tag) {
View tabview = createTabView(mTabHost.getContext(), tag);
TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() {
public View createTabContent(String tag) {return view;}
});
mTabHost.addTab(setContent);
}
I don't know what this is doing with "TabContentFactory"
I too followed the same blog and was facing problem, while adding the Intent's. So, finally I fixed it myself. What I did is,
1.) public class CustomTabActivity extends Activity here I changed it to extend TabActivity
2.) Just add the content using Intent
Intent intent = new Intent().setClass(this, Hello.class);
TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(intent);
That's it and it worked. Hope this works for you too.
Try following the tutorial given on the android website. Combined with the tutorial you used, it should give you completely what you want.
Specifically, to add content to to each tab you add and activity to each tab like so:
spec = tabHost.newTabSpec("artists").setIndicator("Artists",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
Look at this:
private void addActivityTab(int labelResId, int iconResId, Intent intent) {
String tabLabel = getString(labelResId);
View indicator = View.inflate(this, R.layout.simple_tab_spec, null);
ImageView icon = (ImageView) indicator.findViewById(R.id.simple_tab_spec_icon);
icon.setImageResource(iconResId);
TabSpec tabSpec = tabHost.newTabSpec(tabLabel).setIndicator(indicator).setContent(intent);
tabHost.addTab(tabSpec); }
The first argument is the title of your tab item, the second argument is the background image of your tab item.
You should create an intent object to set the parameters and the target activity.

Android: Tab switch - How to update display when switching between tabs?

The app I am building has a tabhost with three tabs. The goal is to update the display in the new tab when the switch takes place. I have the following tab change listener set up in the main activity that built the tab host.
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
public void onTabChanged(String tabId) {
//Now what?
}
});
The question is, How do I take the tabId given and use it to call a method in that tab?
Edit
To clarify a bit: when you create an Intent for a new tab you specify an Activity Class, an object of which, presumably, is created to handle setup and management of that tab.
intent = new Intent().setClass(this, Setup.class);
spec = tabHost.newTabSpec("setup").setIndicator("",
res.getDrawable(R.drawable.tab_setup))
.setContent(intent);
tabHost.addTab(spec);
What I am looking for with this question is how to get a reference to that object? In the example above, the Setup class is instantiated to handle the “Setup Tab”.
To restate my question now: How do I, from the OnTabChangeListener, call a method in the Setup class?
Try to use
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
public void onTabChanged(String tabId) {
//Now what?
Object.refreshDrawableState();
}
});
Where Object = any display Object.
As it turns out the solution to the problem is closer to home. Rather than attaching code to the TabHost, the solution involves getting into the Life-Cycle of the activity by overriding the onPause and onResume methods.
#Override
public void onResume() {
super.onResume();
// Update your UI here.
}

How to get tab click in Android?

I have created two tabs in my Activity using the below code. This is working perfectly fine. When I click on the first tab I see its contents and the other one shows its content when clicked on it.
How ever I want to set a variable value to true or false based on the Tab selected. But I dont know how to get the tab click for this tab. Can you please help me on this.
The code :
tabHost.setup();
TabSpec ts = tabHost.newTabSpec("Tab1");
ts.setIndicator("", getResources().getDrawable(R.drawable.tab1_content));
ts.setContent(R.id.tab1Layout);
tabHost.addTab(ts);
TabSpec ts1 = tabHost.newTabSpec("Tab2");
ts1.setIndicator("", getResources().getDrawable(R.drawable.tab2_content));
ts1.setContent(R.id.tab2Layout);
tabHost.addTab(ts1);
Add onTabchangedListener to the tabhost and using selectedTab Value manage whatsoever you want to manage.
selectedTab value will be = 0 for first tab and rest so on
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
#Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
int selectedTab = tabHost.getCurrentTab() // selected
}
});
Hope it helps :)

Opening the activity in another Tab on Click

I am using TabHost in my application and I want to open a webPage when I click on the Item from the List of one Tab into the another Tab.
I mean the list is in one Tab and when I click on an item from the list, I want to open the WebPage in to another Tab.
Can I doe this? If yes than please tell me how?
Thanks,
david
In the assumption that you've got your tabs setup like this :
LocalActivityManager localActivityManager = new LocalActivityManager(this, false);
tabHost.setup(localActivityManager);
TabSpec spec = tabHost.newTabSpec("tab1").setIndicator("My List").setContent(R.id.layout_tab1);
tabHost.addTab(spec);
spec = tabHost.newTabSpec("tab2").setIndicator("My Browser").setContent(R.id.layout_tab2);
tabHost.addTab(spec);
In your action (a button click in the snippet below), just set the current tab on your tabhost.
btnSearch.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
tabHost.setCurrentTab(1);
}
});

Categories

Resources