I am adding a new tab layout to my app. My app has 4 activities.
Before, the navigation was done with buttons displayed in activities, for example a button in activity 1 make you go to section 4. Each button was starting a new activity with a new intent. To go back user could hit his native device back button.
Example of button:
Button b1= (Button) findViewById(R.id.b1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(this, ActivityTab4.class);
startActivity(i);
}
});
Now, with the tabhost, user goes straight to desire activity, each activity is a child of my tabhost. However, I still need in my layout to keep some buttons that jump directly to a particular activity.
The problem, with these buttons, is that when they start a new activity, the tabhost disappears. I need to keep it at all time.
So how can I use the tabhost normally, but on the top of it also use custom buttons inside the layout of my sections that would keep the tabhost when I hit them?
My tabhost structure is very basic:
public class TabWidget extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.global_tabs);
setTabs() ;
}
private void setTabs() {
addTab("Act1", R.drawable.tab1, ActivityTab1.class);
addTab("Act2", R.drawable.tab2, ActivityTab2.class);
addTab("Act3", R.drawable.tab1, ActivityTab3.class);
addTab("Act4", R.drawable.tab2, ActivityTab4.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);
// SET TITLETAB
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.titleTab);
title.setText(labelId);
// SET IMAGETAB
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}}
Thanks for your help
EDIT: I would rather avoid fragments because it's going to take me a lot of time to implement them and on the top of that to make it compatible with API<11
All right, I've found it, thanks to this answer: setCurrentTab of a tabHost
BAsically in the main tabhost activity, add this
private static Main theInstance;
public static Main getInstance() {
return Main.theInstance;
}
public Main () {
Main.theInstance = this;
}
In the child, you can have a button sending the user to any tab + corresponding activity:
Button b1_3= (Button) findViewById(R.id.b1_3);
b1_3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TabWidget.getInstance().getTabHost().setCurrentTab(3);
}
});
Any reason why you use tabhost and activities instead of Fragments? I think you would find Fragments more suited to what you're trying to do.
As for your question - you can switch the displayed tab with
void setCurrentTab(int index)
void setCurrentTabByTag(String tag)
so there's no need to manually starting an activity on button press, just switch the displayed tab using these methods.
Related
I need to implement tabs in my project.i have a layout, in which i have two tabs and a button. For two tabs,I have two activities and button calling different activity. the thing is I am showing result of button on first tab. i.e tab0 is active on tab0 event and on button click event. And am able to change the tab events using tabHost.setOnTabChangedListener, but now what i further want is, say suppose i click on button so now button view is displaying but again if i click on tab0, tab0 activity should be displayed.
i used onClick but after using this my TabChangeListner not works.
would you suggest solution for my problem.
Thanks.
Below is my code that is working fine :
public class TabLayoutUsingTabChangeEventActivity extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
final TabHost.TabSpec sp1 = tabHost.newTabSpec("TAB1");
TabHost.TabSpec sp2 = tabHost.newTabSpec("TAB2");
//Creating First Tab
Intent intent1 = new Intent(this, Tab1Activity.class);
sp1.setIndicator("TAB1").setContent(intent1);
tabHost.addTab(sp1);
//Creating Second Tab
Intent intent2 = new Intent(this, Tab2Activity.class);
sp2.setIndicator("TAB2").setContent(intent2);
tabHost.addTab(sp2);
//Tab Changed Event
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
#Override
public void onTabChanged(String tabId) {
Log.i("TabId :", tabId);
if(tabId.equals("TAB2")){
Log.i("TAB1", "TAB1 Changed");
Intent intent1 = new Intent().setClass(getApplicationContext(), Tab1Activity.class);
sp1.setIndicator("TAB1").setContent(intent1);
tabHost.setCurrentTab(0);
}
}
});
Button addNewButton = (Button)findViewById(R.id.add_new_ticket_btn);
addNewButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Intent in = new Intent().setClass(getApplicationContext(), AddNewTicketActivity.class);
sp1.setContent(in);
tabHost.setCurrentTab(0);
//startActivity(in);
}
});
}
}
You need to use AvtivityGroup to open new activities in the same tab.
Here is the complete example.
http://ericharlow.blogspot.com/2010/09/experience-multiple-android-activities.html
I am currently in an Android project where our main view is a TabActivity and each tab is a separate Activity. One is a MapActivity and the other two are ordinary Activities.
First note that I think we must have each tab as separate activities, as there is too much code in the separate activities to just have the TabHost switch the content view on a tab change and have all of the code in the same class. Anyways, back to the problem.
One of the tabs include a button, which when pressed should make the TabActivity switch to the MapActivity and animate the map to a specific location.
The tutorial found on http://joshclemm.com/blog/?p=86 shows how to do it if the TabHost contains a mapview and a listview. If an item in the ListView is clicked, the TabHost switches to the mapview and animates to that location (those coordinates). This is exactly what i need to do when the button in the separate activity is pressed.
The MainView.java is created as follows:
public class MainView extends TabActivity implements OnTabChangeListener{
#Override
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
intent = new Intent().setClass(this, MapGUI.class);
spec = tabHost.newTabSpec("map").setIndicator("Map",
res.getDrawable(R.drawable.ic_tab_menu_item))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, MissionView.class);
spec = tabHost.newTabSpec("mission").setIndicator("Mission",
res.getDrawable(R.drawable.ic_tab_menu_item))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, SIPView.class);
spec = tabHost.newTabSpec("call").setIndicator("Call",
res.getDrawable(R.drawable.ic_tab_menu_item))
.setContent(intent);
tabHost.addTab(spec);
The MissionView.java is as follows:
public class MissionView extends Activity implements Observer{
MissionController mc;
private TextView missionheader, missiondescription, missionaddress,
missiontime, missioninjuries;
private Button changedesc, gotoadress;
private String[] mission;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.missiontablayout);
missionheader = (TextView)findViewById(R.id.missionheader2);
missiondescription = (TextView)findViewById(R.id.missiondescription2);
missionaddress = (TextView)findViewById(R.id.missionaddress2);
missiontime = (TextView)findViewById(R.id.missiontime2);
missioninjuries = (TextView)findViewById(R.id.missioninjuries2);
changedesc = (Button)findViewById(R.id.gotoaddress);
changedesc.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// DO SOMETHING HERE?
}
});
mc = new MissionController(MissionView.this);
}
public void update(Observable observable, Object data) {
if(data instanceof String[]){
mission = (String[]) data;
updateView(mission);
}
}
public void updateView(String[] missiontext){
missionheader.setText(missiontext[0]);
missiondescription.setText(missiontext[1]);
missionaddress.setText(missiontext[2]);
missiontime.setText(missiontext[3]);
missioninjuries.setText(missiontext[4]);
}
}
Anyone know how i could achieve this?
Note that the code supplied above has no implementation to actually draw to the actual location, but the question still remains, how do I make a button pressed in one activity to switch tab in the TabHost and fire a change on that tab activity?
changing tabs in a TabHostcan easily be done with setCurrentTab(int)
http://developer.android.com/reference/android/widget/TabHost.html#setCurrentTab(int)
Sending events to other Activities can simply be achieved by sending a broadcast intent and receiving/handling it in the other Activity.
Alternatively you could save static references to all your tab Activities (ugly...) and call methods directly.
Place the below line on button click where you want to switch to the Map activity
((MainView) getParent()).setTabMap();
and in MainView create the following function
public void setTabMap()
{
//as Map activity is your first tab so pass 0 as index
getTabHost().setCurrentTab(0);
}
How could I few layout and require to create in one Tabactivity.
I have try the code below but no luck got error.
tabHost.addTab(tabHost.newTabSpec("Sales Order").setIndicator("Sales Order").setContent(R.layout.frm_txn_so_item_list));
OK let me explain clearly.
I have a code below, as you can see I have 4 tab layout page. Each of it has it own activity class. I have a button which is belong to cls_so_item_list.class, whenever I am try to call it in my cls_so it always return me null value.
So I have come out an idea, to remove all the tab page(item,product,summary,Report) activity classes then create it one standalone class which is cls_so.
My question is how do I put the layout page inside the tabHost.addTab? Thanks
public class cls_so extends TabActivity implements OnClickListener {
protected TabHost tabHost;
int intSalesOrderId;
src_txn_so.cls_so_obj objSalesOrder;
static final String LIST_ID = "list_id";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tabHost = getTabHost();
newTabIntent("Item", null, cls_so_item_list.class);
newTabIntent("Product",
getResources().getDrawable(R.drawable.so_product),
cls_so_prd_list.class);
newTabIntent("Summary",
getResources().getDrawable(R.drawable.so_summary),
cls_so_summary.class);
newTabIntent("Report",
getResources().getDrawable(R.drawable.so_report),
cls_so_summary.class);
Button btnSOLineDiscount = (Button) findViewById(R.id.txn_so_btn_line_discount);
btnSOLineDiscount.setOnClickListener(this);
tabHost.setCurrentTab(0);
}
protected void newTabIntent(String label, Drawable icon, Class<?> pageClass) {
TabSpec tabSpec = tabHost.newTabSpec(label);
tabSpec.setIndicator(label, icon);
Intent SOIntent = new Intent().setClass(this,pageClass);
SOIntent.putExtra(LIST_ID, -1);
tabSpec.setContent(new Intent(this, pageClass));
tabHost.addTab(tabSpec);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
It doesn't work like that. In your main layout, add in the FrameLayout with the id of tabcontent an element <include> that points on that layout.
In your code you'll have to change it to.
tabHost.addTab(tabHost.newTabSpec("Sales Order").setIndicator("Sales Order").setContent(R.id.my_included_layout));
if you're <include> has the id "#+id/my_included_layout"
Here's my problem:
I've have an application with tabs.
in each tab I have a list that list to an other list then to a screen with an image, clickable text etc.
In one word I have Tasks inside tabs.
Question
Despite long seek among forum and tutorials I still can't figure witch is the best pactice to do that : switching activity insde a tab
or changing the view.
Here my code
public class App 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; // Reusable 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, Activity0.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("0").setIndicator("0",
res.getDrawable(R.drawable.ic_tab_0))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Activity1.class);
spec = tabHost.newTabSpec("1").setIndicator("1",
res.getDrawable(R.drawable.ic_tab_1))
.setContent(intent);
tabHost.addTab(spec);
then I have to switch activities (Activity0 -> Activity01) inside tab 0
public class Activity0 extends Activity{
..
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlist);
ListView l1 = (ListView) findViewById(R.id.ListView01);
l1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// Toast.makeText(getBaseContext(), "You clciked "+parentTypes.get(arg2).getLibelle(), Toast.LENGTH_LONG).show();
/*TODO database method */
TypeEvenement parent = parentTypes.get(arg2);
if (parent.getChildren().size()!=0)
{
Intent i = new Intent(TypeParentList.this, TypeChildList.class);
int id= new Long(parentTypes.get(arg2).getId()).intValue();
i.putExtra("typeid", id);
View view = lam.startActivity("TypeChildList",i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)).getDecorView();
setContentView(view);
then go back to this activity from Activity01
public class Activity01 extends Activity{
tv.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(TypeChildList.this, Activity0.class);
LocalActivityManager lam = pa.getLocalActivityManager();
View view2 = lam.startActivity("Activity0",i).getDecorView();
setContentView(view2);
}
});
After go and back I get an error java.lang.IllegalStateException The specified child already has a parent. You must call removeView() on the child's parent first.
I know it comes from my view but I can't figure out how to fix it
So is this the best practice to do multiple activity in one tab ?
Please help needed
If you use a ViewSwitcher or ViewFlipper you can keep the View attached to that and switch back and forth to your hearts desire.
One approach with a tab bar or a common shortcut bar is to have a separate class to handle the OnClickListeners that call startActivity(newIntent) and finish() back to back. I'm not too fond of what the default TabView looks like, so I tend to make a graphical toolbar and use this approach.
Greetings,
I am trying to get the Click - event when clicking on the currently selected tab of my TabActivity. The onTabChangedHandler is only called whenever the tab is changed, not if the currently active Tab is clicked. The debugger tells me i have the onClickListener Registered for the TabWidget within my TabHost.
Am i registering for the wrong View?
Also, I am unable to create a Context Menu for the Tabs, only for its content, is this problem related?
public class TestDroidViewTab extends TabActivity
implements TabContentFactory
, OnTabChangeListener, OnClickListener {
private static final String LOG_KEY = "TEST";
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TabHost tabHost = getTabHost();
TabHost.TabSpec ts = tabHost.newTabSpec("ID_1");
ts.setIndicator("1");
ts.setContent(this);
tabHost.addTab(ts);
ts = tabHost.newTabSpec("ID_2");
ts.setIndicator("2");
ts.setContent(this);
tabHost.addTab(ts);
ts = tabHost.newTabSpec("ID_3");
ts.setIndicator("3");
ts.setContent(this);
tabHost.addTab(ts);
tabHost.setOnClickListener(this);
tabHost.setOnTabChangedListener(this);
}
public void onClick(View v) {
Log.d(LOG_KEY, "OnClick");
}
public void onTabChanged(String tabId) {
Log.d(LOG_KEY, "OnTabChanged");
}
If you want to see that a particular tab is clicked, you need to add your listener to the tab itself, not the TabHost.
The hierarchy of views in a tab implementation is:
TabHost
TabWidget
(tab)
(tab)
FrameLayout
The tabs are added at runtime by calling: tabHost.addTab(tabHost.newTabSpec(""));
You can then get a handle to the individual tabs by calling: getTabWidget().getChildAt(4);
In essence, you are adding your OnClickListener to a child of the TabWidget. You can now pick up the clicks on your individual tab. However, this will override the default behavior which changes the content when a tab is clicked. So, to get your content to change, your OnClickListener will need to do that for you.
Here is a full example, which lets you intercept the click event, and change the content below the tab:
final String myTabTag = "My Tab";
final int myTabIndex = 3;
getTabHost().addTab( getTabHost().newTabSpec(myTabTag) );
getTabWidget().getChildAt(myTabIndex).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (getTabHost().getCurrentTabTag().equals(myTabTag)) {
getTabHost().setCurrentTab(myTabIndex );
}
}
});
use setOnTabChangedListener instead of OnClickListener ;)
static TabHost tabHost;
tabHost = getTabHost();
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String arg0) {
Log.i("******Clickin Tab number ... ", "" + tabHost.getCurrentTab());
}
});
Your clause is wrong, use:
...
if (getTabHost().getCurrentTabTag().equals(myTabTag) == false) {
getTabHost().setCurrentTab(myTabIndex );
}
...
into my code, it shows some errors and ask me to create new methods in
those names like getTabWidget(), getTabHost(), etc. Waiting for your
response.
Try this
tabHost.getTabHost().setCurrentTab(myTabIndex);