onClick event in android tabwidget - android

i am working with tabview. i have 3 tabs in my tabwidget view. Below is my MainActivity.java
public class MainActivity extends Activity {
TabHost host;
Intent intent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res = getResources();
host = (TabHost) findViewById (R.id.tabhost);
host.setup();
TabSpec tspecMovies = host.newTabSpec("tag1");
// tspecMovies.setContent(R.id.tab1);
tspecMovies.setIndicator("Movies", res.getDrawable(R.drawable.movie_icon));
host.addTab(tspecMovies);
TabSpec tspecTv = host.newTabSpec("tag2");
// tspecTv.setContent(R.id.tab2);
tspecTv.setIndicator("TV", res.getDrawable(R.drawable.tv_icon));
host.addTab(tspecTv);
TabSpec tspecEvents = host.newTabSpec("tag3");
// tspecEvents.setContent(R.id.tab3);
tspecEvents.setIndicator("Events", res.getDrawable(R.drawable.event_icon));
host.addTab(tspecEvents);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
I have 3 different Activities for each tabs which contains the tab contents. I want to know how do i call these activities on onClick event when the tab is clicked.

find below code for that.
host.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
if(getString(R.string.tab1).equals(tabId)){
//your activity 1
// R.string.tab1 is the Id given to tab using values/string.xml file
}else if(getString(R.string.tab2).equals(tabId)){
// your activity 2
}
}
}

You have to set intent to each tab..
use this code
// Android tab //activity which you want to call
Intent intentAndroid = new Intent().setClass(this, AndroidActivity.class);
TabSpec tabSpecAndroid = tabHost
.newTabSpec("Android")
.setIndicator("", ressources.getDrawable(R.drawable.icon_android_config))
.setContent(intentAndroid);
For more info Complete Tutorial

You have to pass tabspect with intent
TabSpec tspecMovies = host.newTabSpec("tag1");
// tspecMovies.setContent(R.id.tab1);
tspecMovies.setIndicator("Movies", res.getDrawable(R.drawable.movie_icon));
host.addTab(tspecMovies)
Intent moviesintent=new Intent(this, MoviesActivity.class);
moviesintent.setContent(videosIntent);
You can refer this tutorial.
I hope You can learn very well.

Related

Custom TabView with Intent in Android

I want to create Tabview like below in Image. I tried it but can't get proper view.
Here is My Code.. It's not give exact view that I want. So, How can I customized it.
public class BandInfo extends TabActivity implements OnTabChangeListener {
TabHost tabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.band_info);
tabHost = getTabHost();
tabHost.getTabWidget().setBackgroundResource(R.drawable.footerbar);
// Tab for Bio
TabSpec bioSpec = tabHost.newTabSpec("Bio");
// Setting Title for Tab
bioSpec.setIndicator("Bio");
Intent bioIntent = new Intent(this,Bio.class);
bioSpec.setContent(bioIntent);
// Tab for Upcoing Shows
TabSpec upcomingShowSpec = tabHost.newTabSpec("Upcoming Shows");
upcomingShowSpec.setIndicator("Upcoming Shows");
Intent upcomingShowIntent = new Intent(this, UpcomingShow.class);
upcomingShowSpec.setContent(upcomingShowIntent);
// Tab for Band Members
TabSpec bandMemberSpec = tabHost.newTabSpec("Band Members");
bandMemberSpec.setIndicator("Band Members");
Intent bandMemberIntent = new Intent(this, BandMembers.class);
bandMemberSpec.setContent(bandMemberIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(bioSpec); // Adding Bio Tab
tabHost.addTab(upcomingShowSpec); // Adding Upcoming Show Tab
tabHost.addTab(bandMemberSpec); // Adding Band Members Tab
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.footerbar);
}
tabHost.getTabWidget().setDividerDrawable(R.drawable.footer_saprater);
}
#Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.footerbar);
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(R.drawable.bandmember_active);
}
Please Help me with some code...
Thanks in Advance..
This is an output Screen I get.
Heyy i found this. Try this. you'll surely make it.
http://code.google.com/p/android-custom-tabs/
For setting width,
tabHost.getTabWidget().getChildAt(0).getLayoutParams().width =(int) 30;
Also check this link: http://adanware.blogspot.in/2012/04/android-custom-tab-layouts-just-using.html and http://www.speakingcode.com/2011/12/01/creating-custom-views-for-tabs-in-an-android-activity-using-tabhost-and-tabwidget/

android tabhost tab and view not matching

I have two tabs in my tabwidget and each tab display a list view.
ay
Initially my first tab is displayed as selected but below list correspond to second tab.
Once i click the tabs, i am getting correct display.
private static final String LIST_TAB_TAG1 = "UpcomingEvents";
private static final String LIST_TAB_TAG2 = "PastEvents";
tabHost.addTab(tabHost.newTabSpec(LIST_TAB_TAG1)
.setIndicator(LIST_TAB_TAG1)
.setContent(new TabContentFactory() {
public View createTabContent(String arg) {
return listView1;
}
}));
tabHost.addTab(tabHost.newTabSpec(LIST_TAB_TAG2)
.setIndicator(LIST_TAB_TAG2)
.setContent(new TabContentFactory() {
public View createTabContent(String arg) {
return listView2;
}
}));
tabHost.setCurrentTab(0);
LIST_TAB_TAG1 is highlighted when this sctivity is launched but the list displayed is listview2. This problem is only when activity starts. Upon clicking tabs its working fine
Can please help me in fixing this. Thanks for your time
I generally use the code given in docs which works fine for me ,try this one .
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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 Activity for the tab (to be reused)
intent = new Intent().setClass(this, UpcomingEvents.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("UpcomingEvents").setIndicator("UpcomingEvents",getResources().getDrawable(R.drawable.homebutton1)).setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this,PastEvents.class);
spec = tabHost.newTabSpec("PastEvents").setIndicator("PastEvents",
getResources().getDrawable(R.drawable.bank_transaction1))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}

Fire action on other activity within TabHost? (Android)

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 to change title of a Tab in TabActivity

I have TabActivity with tabs on bottom which is composed of several different activities like this:
public class HomeScreen extends TabActivity {
private TabHost mTabHost;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
try{
setContentView(R.layout.hometabs);
Resources res = getResources(); // Resource object to get Drawables
mTabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Reusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
String strName;
// Create an Intent to launch an Activity for the tab (to be reused)
strName=Central.Res.getString(R.string.tab_Books);
intent = new Intent().setClass(this, BookList.class);
spec = mTabHost.newTabSpec(strName).setIndicator("",
res.getDrawable(R.drawable.ic_menu_database))
.setContent(intent);
mTabHost.addTab(spec);
// Create an Intent to launch an Activity for the tab (to be reused)
strName=Central.Res.getString(R.string.tab_Questions);
intent = new Intent().setClass(this, QuestionsList.class);
intent.putExtra("NoteType", UserDataSQLHelper.NOTE_TYPE_QUEST );
spec = mTabHost.newTabSpec(strName).setIndicator("",
res.getDrawable(R.drawable.ic_menu_dialog))
.setContent(intent);
mTabHost.addTab(spec);
My problem is that I have the same window title for every tab. This window title is set to application name.
I need to be able to change this title to the name of the tab.
I am trying to do it by calling setTitle("MyTitle"); in onCreate() of corresponding activity and by overriding TabActivity onChildTitleChanged:
public void onChildTitleChanged(Activity childActivity, CharSequence title)
{
View tabView = mTabHost.getCurrentTabView();
int idTitle = android.R.id.title;
TextView tvTitle = (TextView) tabView.findViewById(idTitle);
tvTitle.setText(title);
}
But the result I get isn't what I need - it sets text to tab under the tab icon.
Could you please help me?
try with following code,
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
Log.i("Tab id", ""+tabId);
setTitle(tabId);
}
});
setIndicator (CharSequence label, Drawable icon).
Use this function and the label will be shown as a title.

How to allow user to Add and Delete Tabs in an android application

I am developing an application that uses tabs with each tab being linked to a webpage that the user will be able to see and interact with using webview. What I am having trouble with is implementing a add and delete command that the user will be able to use to delete a tab if desired or add a tab with a url of their choice that works just like the others.
Below is my code.
Here is the main java file that all other files use:
public class UniversityofColorado extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost host=getTabHost();
host.addTab(host.newTabSpec("one")
.setIndicator("Google")
.setContent(new Intent(this, Hello.class)));
host.addTab(host.newTabSpec("two")
.setIndicator("Colorado Main Site")
.setContent(new Intent(this, ColoradoMainSiteBrowser.class)));
host.addTab(host.newTabSpec("three")
.setIndicator("CULearn")
.setContent(new Intent(this, CULearnBrowser.class)));
host.addTab(host.newTabSpec("four")
.setIndicator("CULink")
.setContent(new Intent(this, CULinkBrowser.class)));
host.addTab(host.newTabSpec("five")
.setIndicator("MyCUInfo")
.setContent(new Intent(this, MyCUInfoBrowser.class)));
host.addTab(host.newTabSpec("six")
.setIndicator("Campus Map")
.setContent(new Intent(this, CampusBrowser.class)));
host.addTab(host.newTabSpec("Seven")
.setIndicator("Notes")
.setContent(new Intent(this, Notepadv3.class)));
}
// Inflates menu when "menu Key" is pressed
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
}
Then I have each webpage defined in a separate java file that the main file calls
below is one of them:
public class ColoradoMainSiteBrowser extends Activity {
WebView webview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new HelloWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://colorado.edu/");
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
webview.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
I have the menu defined in the main file I just need to construct the methods so the buttons do what they are suppose to do. Any help would be great.
Using code mentioned by Mohammed can be useful, but you may need to add
tabs.setCurrentTab(0);
before calling
tabs.clearAllTabs();
according to issue described at http://code.google.com/p/android/issues/detail?id=2772.
Then your tab probably gets removed, but you may notice an error when trying to switch or add tabs. For me, this problem was fixed after calling
tabs.setCurrentTab(index);
inside the for-loop (after adding the tab).
So you should get:
list.remove(nTabToRemoveIndex);
tabs.setCurrentTab(0); // <== ***FIRST EDIT***
tabs.clearAllTabs();
int nTabIndex = 0; // <== ***SECOND EDIT***
for(TabHost.TabSpec spec : list)
{
tabs.addTab(spec);
tabs.setCurrentTab(nTabIndex++); // <== ***THIRD EDIT***
}
Hope it will help.
You may use this way:
1- add every tabs to list array
2-to remove a spesific tab, first remove it from list and clear all tabs from tabHost and add them again from list.
check this code which has basic idea of how to delete tabs from tabHost dynamically :
// data structure, what I referred to as memory
ArrayList<TabHost.TabSpec> list = new ArrayList<TabHost.TabSpec>();
// when you are adding tabs to tab host
// what you add, you remember
TabHost.TabSpec spec = tabs.newTabSpec("tag1");
spec.setContent(R.id.button);
spec.setIndicator("TabONe");
tabs.addTab(spec);
list.add(spec);
...
// when you want to remove
list.remove(list.size()-1); // remove it from memory
tabs.clearAllTabs(); // clear all tabs from the tabhost
for(TabHost.TabSpec spec : list) // add all that you remember back
tabs.addTab(spec);
resource:
http://www.coderanch.com/t/460859/Android/Mobile/TabHost-Remove-Tab
I hope this will help.
M.

Categories

Resources