Opening the activity in another Tab on Click - android

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);
}
});

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;
}
});
}

changing activity on sliding the screen

i have three activities in my application .
first one is the default activity when i slide right to left second activity should be open with its layout and when i slide left to right third one should open with its layout
i want to change the activities on sliding not only background or layout..
give me appropriate way to do this.
i m using this tab view but this is not giving any slider functionality
public class TabDemo extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_host);
TabHost tabHost=getTabHost();
// no need to call TabHost.Setup()
//First Tab
TabSpec spec1=tabHost.newTabSpec("activity 1");
spec1.setIndicator("activity 1");
Intent in1=new Intent(this, activity 1);
spec1.setContent(in1);
TabSpec spec2=tabHost.newTabSpec("activity 2");
spec2.setIndicator("activity 2");
Intent in2=new Intent(this,activity 2);
spec2.setContent(in2);
Toast toast = Toast.makeText(getApplicationContext(), "Welcome in Resident center",Toast.LENGTH_SHORT);
toast.show();
TabSpec spec3=tabHost.newTabSpec("activity 3");
spec3.setIndicator("activity 3");
Intent in3=new Intent(this,activity 3);
spec3.setContent(in3);
tabHost.addTab(spec1);
tabHost.addTab(spec2);
tabHost.addTab(spec3);
tabHost.setCurrentTab(1);
}
}
Simplest way to create an App with Swiping functionality is in my opinion to create a new Android Application via Eclipse Wizard with following options selected:
Create Activity -> BlankActivity
Navigation Type = Tabs + Swipe
This will create an Application with (i think) 3 pages you can swipe through.
Now you can check the created files/code and change whatever you want/need to.
And as mentioned in the comments to your answer, this isn't possible with Activites, you need to use Fragments!

android tab activity go to previous tab

I have an activity with five tabs. Everything looks okay when I go from tab 1 to tab 2 or tab 3. How can I go back programatically from tab 2 to tab 1?
Intent myIntent = new Intent(this, Tab1.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myIntent);
This is not working properly because it starts activity 1 without any tab.
When going from tab 1 to tab 2 I can both see tab 1 and tab 2 (current tab activated). But when going from tab 2 to tab 1, both tab 1 and tab 2 disappear from the activity. What could cause this?
This will surely help you.
TabHost tabHost = (TabHost) getParent().findViewById(android.R.id.tabhost);
tabHost.setCurrentTab(1);
OR you can refer to this link
How to programmatically switch tabs using buttonclick in Android
Thanks :)
just use finish() method
public void onClick(View v)
{
finish();
startActivity(new Intent(Activity2.this, Activity1.class));
}
I don't know about the Intent.FLAG_ACTIVITY_CLEAR_TOP, never needed that, but the mentioned effect of loosing your tabs is produced by calling startActivity() from your TabHost, not one of your tabs. If that's the case, move the call there and your tabs should stay.
I have a similar situation but seems none of the answers help. so, I post my solution here:
// tab selection history, each tab has a tag which is a string
private List<String> tabIdHistory = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstance) {
super.onCreate(savedInstanceState);
// this layout contains TabHost and TabWidget
setContentView(R.layout.activity_main);
TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
tabIdHistory.remove(tabId); // ensure uniqueness
tabIdHistory.add(tabId);
}
});
// continue your tab initialisation, such as
// tabHost.addTab(tabHost.newTabSpec(TAG)
// .setContent(...).setIndicator(...));
}
#Override
public void onBackPressed() {
if (tabIdHistory.size() > 1) {
// pop the current last item, we want the second last
tabIdHistory.remove(tabIdHistory.size() - 1);
tabHost.setCurrentTabByTag(tabIdHistory.get(tabIdHistory.size() - 1));
} else {
super.onBackPressed();
}
}
If use select tab#1, tab#3, tab#2, tab#1, then the back stack is "3, 2, 1" and app will exit to main screen if user press back button three times. If you want to keep full history, comment out this line:
tabIdHistory.remove(tabId);

Android, Inside a tabhost how to add a button and a listview undernetath?

I want to create something like this
|Button|
item 1
item 2
item 3
.
.
.Items on a listview
I already have a tabhost with 3 tabs, so i don't want to change my main.xml because the button will appear on every tab! i want my first tab to show a calendar (this one is done, i'm not sure if its ok but it has be done), the second tab will show something diferrent and the last one the button and the item list underneath.
I donnot paste any code because everything i've done comes from android tutorials, so i don't want to ask someone to give me an already written code, just to guide me through what do i have to read and where to look to achieve that!
Thanks in advance!
Well this is what I've done so far
this is my main class
`public class HourPayActivity 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 //setContentView(R.layout.emptab); Activity for the tab (to be reused)
intent = new Intent().setClass(this, MonthsActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("Months").setIndicator("",
res.getDrawable(R.drawable.ic_tab_months))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, EmployersActivity.class);
spec = tabHost.newTabSpec("Employers").setIndicator("",
res.getDrawable(R.drawable.ic_tab_employers))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, PaymentsActivity.class);
spec = tabHost.newTabSpec("Payments").setIndicator("",
res.getDrawable(R.drawable.ic_tab_payments))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
`
And this is the tab content that i want to show up
public class EmployersActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView employersList = getListView();
String[] employers = getResources().getStringArray(R.array.employers_list);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, employers));
employersList.setAdapter(getListAdapter());
employersList.setTextFilterEnabled(true);
employersList.setOnItemClickListener(new OnItemClickListener() {
//#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_LONG).show();
}
});
setContentView(employersList);
}
What you do is; You follow the android tutorial, which tells you to create an class that extends TabActivity if I remember correctly.
In this activity you load intents via
TabHost.TabSpec spec = tabHost.newTabSpec([title]);
....
Intent content = new Intent(this, activityToLoad.class);
spec.setContent(content);
tabHost.addTab(spec);
What this does is, it loads an activity in a tab.
This in turn means that you can use an activity like normal, you can use custom layouts with setContentView(R.layout.yourlayout); in the onCreate() method. You can call your components, attach a custom list adapter to your list and so on. For the 3 tabs create 3 different activities. Based on personal knowledge its the easiest way to maintain your tabs.
What you put in these layout is upto you. But those activities you put in the tabs are "normal" activities like you are (probably) familiar with.
To explain your view:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/btnId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/yourtext"
/>
<ListView
android:id="#android:id/android:list"
android:layout_height="fill_content"
android:layout_width="match_parent"
/>
</LinearLayout>
This will place a button at the top with a listview underneath it.
The id of the listview is android:list because in my activity i extend listActivity, which expects a listview with that id.

TabHost , TabSpec - onclick implementation

i am working with TabHost , TabSpec .i want to do some actions(like onClick) when TabSpec is clicked. has anyone tried this i am able to go to another activity with "home.setIndicator("Home")...." is it possible
Use http://developer.android.com/reference/android/widget/TabHost.OnTabChangeListener.html
Example
mTabHost.setOnTabChangedListener(new OnTabChangeListener(){
#Override
public void onTabChanged(String tabId) {
if(TAB_1.equals(tabId)) {
//Change first image
}
if(TAB_2.equals(tabId)) {
//chnage second image ...so on
}
}});
If you follow this tutorial, you'll see that it's possible to change the tab image (to show that the tab is being selected or not) by defining XML file in res/drawable/

Categories

Resources