I need little bit help related to android tabhost. I have 3 tabs and 3 activities. MainTab activity is "extends from TabActivity" and other 2 activities are "extends from Activity".
In MainTab activity there are two buttons Radio and CheckBox, when user click radio button then i want to display radio button view in tab2. when user click check box i want to display checkbox view. Can some gives me an idea how I can achieve this?? Below is my code.
public class MainTabHost extends TabActivity implements OnTabChangeListener{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.homeui);
TabHost tabHost = getTabHost();
tabHost.setOnTabChangedListener(this);
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, Main.class);
spec = tabHost.newTabSpec("Main Tab").
setIndicator("Main Tab")
.setContent(intent);
spec.setIndicator("", this.getResources().getDrawable(R.drawable.maintabicon) );
tabHost.addTab(spec);
intent = new Intent().setClass(this, TabA.class);
spec = tabHost.newTabSpec("Tab A").
setIndicator("Tab A")
.setContent(intent);
spec.setIndicator("", this.getResources().getDrawable(R.drawable.tabaicon) );
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
#Override
public void onTabChanged(String tabId) {
}
}
You can use SharedPreference for this. So that when you choose radio button you can change your shared value to radio and when you use Check box change value to check box and use the view in your second tab according to the shared value.
Related
I have not used the activityGroup in my project. Now I'm not is a position to implement the whole project using Activity group.
is it nesssory that I must implement the activityGroup class in my project to do so?
If yes then please give links for the basic tutorial of activityGroup implementation.
Here is my MainActvity.java which loads 4 other actvities in 4 tabs.
public class MainActivity extends TabActivity {
TabHost tabHost;
Context context = MainActivity.this;
Button btnGo;
TabSpec spec;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnGo = (Button) findViewById(R.id.btn_GO);
tabHost = getTabHost();
// Android tab
Intent intentHome = new Intent();
intentHome.setClass(this, Home.class);
TabSpec tabSpecHome = tabHost
.newTabSpec("Home")
.setIndicator("Home",
getResources().getDrawable(R.drawable.home))
.setContent(intentHome);
tabHost.addTab(tabSpecHome);
Intent intentNowReading = new Intent().setClass(this, NowReading.class);
TabSpec tabSpecNowReading = tabHost
.newTabSpec("Now Reading")
.setIndicator("Now Reading",
getResources().getDrawable(R.drawable.now_reading))
.setContent(intentNowReading);
tabHost.addTab(tabSpecNowReading);
Intent intentFavourite = new Intent().setClass(this, Favorites.class);
TabSpec tabSpecFavourite = tabHost
.newTabSpec("Favourite")
.setIndicator("Favorites",
getResources().getDrawable(R.drawable.favorites))
.setContent(intentFavourite);
tabHost.addTab(tabSpecFavourite);
Intent intentProfile = new Intent().setClass(this, Profile.class);
TabSpec tabSpecProfile = tabHost
.newTabSpec("Profile")
.setIndicator("Profile",
getResources().getDrawable(R.drawable.profile))
.setContent(intentProfile);
tabHost.addTab(tabSpecProfile);
tabHost.setCurrentTabByTag("Home");
...}
now I want to start the new activity in the Home tab area on the click event of Go button.(See the picture).
please note that I do not want to impelement the ActivityGroup class, How can I do that without this.
New Actvity must load in the HomeTab's area, not on the full screen.
ActvityGroup is a bad idea, this is old, deprecated API, don't use it.
You have to use Fragments API, just create a Fragment and add it to layout using FragmentTransaction, thats all what you need.
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.
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);
}
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);
}
I want to design a page in which i just want to show only one tab. But when i add only one tab, it takes the full width available but i want that tab only takes small space and remaining space is left empty. No other tabs i want to add. Only one tab is displayed all time. Code is following
public class TabViewActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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, FirstTab.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("firstTab").setIndicator("First Tab")
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(1);
Use tabHost.getTabWidget().getChildAt(0).getLayoutParams().width =(int) 30; also set the layout_width="wrap_content"
In your xml hide the tab widget by setting the visibility to gone. and instead use a button. And in the button handler you can use code like
public void tabHandler(View target){
artistButton.setSelected(false);
albumButton.setSelected(false);
if(target.getId() == R.id.artist_id){
tabHost.setCurrentTab(0);
artistButton.setSelected(true);
} else if(target.getId() == R.id.album_id){
tabHost.setCurrentTab(1);
albumButton.setSelected(true);
}
}