Display a listView on mapView - android

I want to display a listView on mapView as in below image of Google maps app.
Initially only map is visible. When user clicks on a tab I want this listView to appear on mapView.
But the problem is that using below layout does not display maps(gray screen is visible). However, listView is displayed.
Layout :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pickMatchesLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TabHost
android:id="#+id/tabHost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/map_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<include layout="#layout/listView" >
</include>
</RelativeLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</RelativeLayout>
Method to set up tabs :
// Set up tabs
private void setUpTabs() {
// Get TabHost
tabHost = (TabHost) findViewById(R.id.tabHost);
tabHost.setup();
// Create tabs
TabSpec sourceOnlyState = tabHost.newTabSpec(SOURCE_ONLY_STATE);
sourceOnlyState.setContent(R.id.map);
sourceOnlyState.setIndicator("Source Only");
TabSpec allState = tabHost.newTabSpec(ALL_STATE);
allState.setContent(R.id.map);
allState.setIndicator("All");
TabSpec mapTypes = tabHost.newTabSpec(MAP_FEATURES);
mapTypes.setContent(R.id.map_layout);
mapTypes.setIndicator("Map Types");
// Add tabs in TabHost
tabHost.addTab(sourceOnlyState);
tabHost.addTab(allState);
tabHost.addTab(mapTypes);
// Set tab change listener
tabHost.setOnTabChangedListener(this);
}

OnClick of tab start new activity and do all the code related to listview in that activity and set theme for activity is
android:theme="#android:style/Theme.Holo.Dialog.NoActionBar"

Related

Conditional use of tabbed activity

I am trying to create an app that starts with an activity A with a "custom" layout. From that activity it is possible to enter activity B with 3 tabs.
I succeed at creating activity with tabs. The problem is that tabs shows up only if that activity is also launcher activity. If it is not, the tabs do not shows up.
At first time i tried to create tabs with TabLayout. Now i tried to create tabs with TabHost.
Can someone pleas help?
onCreate method in main activity where tabs are "created".
public class GlavnaStran extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_glavna_stran);
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
TabHost.TabSpec tab1 = tabHost.newTabSpec("Search");
TabHost.TabSpec tab2 = tabHost.newTabSpec("Lyric");
TabHost.TabSpec tab3 = tabHost.newTabSpec("Playlist");
//tab1.setIndicator("Tab1");
tab1.setIndicator("Search");
tab1.setContent(new Intent(this, SearchTab.class));
//tab2.setIndicator("Tab2");
tab2.setIndicator("Lyric");
tab2.setContent(new Intent(this, LyricTab.class));
//tab3.setIndicator("Tab3");
tab3.setIndicator("Playlist");
tab3.setContent(new Intent(this, PlaylistTab.class));
tabHost.addTab(tab1);
tabHost.addTab(tab2);
tabHost.addTab(tab3);
}
}
For each tab i was created separate activity which looks like that:
public class SearchTab extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_tab);
}
}
The xml part of the project is created by importing TabHost container in design view.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.example.admin.besedilatakt_v5.GlavnaStran">
<TabHost
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#android:id/tabhost">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="#+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
In the XML files of tab's activity is nothing special except TextView widget to display some random text.

Using Listview to show posts in Tab layout

i'm using Listview on Tab Layout in my android app to load data from network. Tha tabs are working properly and i can switch between my tabs easily without any error but when the listview is populated with data from server the tabs go away and i've a simple listview with data. I can't see my other tabs anymore.
PS The data is loading just fine without any problem.
Here is the Home java code for home_layout
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_layout);
TabHost tabHost = getTabHost();
TabHost.TabSpec myprofiletab = tabHost.newTabSpec("Profile");
myprofiletab.setIndicator("Profile",getResources().getDrawable(R.drawable.myprofile_icon));
Intent myprofileintent = new Intent(this,MyProfile.class);
myprofiletab.setContent(myprofileintent);
TabHost.TabSpec newsfeedtab = tabHost.newTabSpec("NewsFeed");
newsfeedtab.setIndicator("Feed",getResources().getDrawable(R.drawable.newsfeed_icon));
Intent feed = new Intent(this,NewsFeed.class);
newsfeedtab.setContent(feed);
tabHost.addTab(newsfeedtab);
tabHost.addTab(myprofiletab);
}
Here is the home_layout
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:layout_height="fill_parent">
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</RelativeLayout>
Solved my problem by adding one line to my FrameLayout
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#android:id/tabs" />

How to update TabSpec content (ex. add textView)

I am very new to Android so go easy on me. I have implemented an activity that makes use of the TabSpec to make 2 tabs. I can get them running with content loading from the xml layouts.
My question is how can I add/change content of one of the tabs? Lets use adding a textview as an example. How would I do this?
//set up tabs
TabHost tabs=(TabHost)findViewById(R.id.tabhost);
tabs.setup();
//indicate setting for first tab
TabHost.TabSpec spec=tabs.newTabSpec("tag1");
spec.setContent(R.id.tab1);
spec.setIndicator("Templates");
tabs.addTab(spec);
//indicate setting second tab
spec=tabs.newTabSpec("tag2");
spec.setContent(R.id.tab2);
spec.setIndicator("Stat Sheets");
tabs.addTab(spec);
Here is my XML
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
/*I want to add content here at runtime*/
</LinearLayout>
<Button android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="A semi-random button"
/>
</FrameLayout>
Turns out TabSpec approach is a little dated. I turned to properly implementing fragments which turned out to be much more concise and straightforward. I followed this tutorial specifically which really helped.
http://www.androidbegin.com/tutorial/implementing-fragment-tabs-in-android/

Get instance of child/sub TabHost from the parent TabHost

I've created an activity which contains TabHost. In one of the tabspec there is another TabHost (like a sub-TabHost).
By default the visibility of this subTabHost is gone, & is only visible when the second parent tabspec is selected.
Now when the second tab is selected, I want to get the instance of the subTabHost inside java code in TabSpec variable.
Thank You
Layout:
Parent TabHost Layout
<TabHost
android:id="#android:id/tabhost"
android:visibility="gone" >
...
<TabWidget
android:id="#android:id/tabs" />
<FrameLayout
android:id="#android:id/tabcontent" >
<include
android:id="#+id/abc"
layout="#layout/abc"
android:visibility="gone" />
<include
android:id="#+id/subtab2"
layout="#layout/subtab2" <--! sub tab -->
android:visibility="gone" />
...
Inside layout of subtab2
<!-- want to get this's tabhost instance in code -->
<TabHost
android:id="#android:id/tabhost"
android:visibility="visible" >
<LinearLayout
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs" />
<FrameLayout
android:id="#android:id/tabcontent" >
<include
android:id="#+id/xyz"
layout="#layout/xyz"
android:visibility="gone" />
.....
Java Code
TabHost parent = mTabHost = (TabHost) findViewById(android.R.id.tabhost);
// How to code below
(if subtab2 is visible)
Tabhost subTabHost = ??
As you used the same ids for the TabHosts you can't use findViewById(android.R.id.tabhost) in the main layout but you can use it in the content panel:
// when the second tab is selected
FrameLayout content = (FrameLayout) findViewById(android.R.id.tabcontent); // main tabs content
TabHost subtabs = (TabHost)content.findViewById(android.R.id.tabhost);

tabHost in android gives Unfortunately,app has stopped error

I want two tabs in my application which are in other activity which I access from my main activity from a button.
My Activity which will control the tabs has the code as below
public class Myclass extends TabActivity
{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myclass);
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
TabSpec firstSpec = tabHost.newTabSpec("First");
firstSpec.setIndicator("first Tab",null);
Intent firstintent = new Intent(this,Tab1.class);
firstSpec.setContent(firstintent);
TabSpec secondSpec = tabHost.newTabSpec("Second");
secondSpec.setIndicator("first Tab",null);
Intent secondintent = new Intent(this,Tab2.class);
secondSpec.setContent(secondintent);
tabHost.addTab(firstSpec);
tabHost.addTab(secondSpec);
}
}
And the XML which has the tabHost content has the code
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/tabHost"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</FrameLayout>
</LinearLayout>
</TabHost>
So the app is compiling and loading up fine in my device and able to access other activites. But when I try to access the button which should display the activity above mentioned "Myclass" activity. The application exits and I get a screen displaying "Unfortunately,app has stopped".
I have entered the activities in the AndroidManifest.xml also.
The TabActivity shows a cancel mark on the word and says 'android.app.TabActivity' is deprecated.
What does this mean exactly?
Can any one suggest me what is going wrong with the program.
I changed the code as given in the link http://developer.android.com/reference/android/app/TabActivity.html to the code below Code
public class MyClass extends FragmentActivity
{
private FragmentTabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myclass);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
Tab1.class, null);
mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
Tab2.class, null);
}
}
And the XML CODE to
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="#android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<FrameLayout
android:id="#+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
But still no effect, the same problem of application exiting upon clicking of the button to start the activity Myclass continues.
Can anyone suggest a solution?
you should change
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
with
TabHost tabHost = (TabHost)findViewById(R.id.tabHost);
try this:
make changes in your XML
Change Tab host id
android:id="#+id/tabHost" to android:id="#android:id/tabhost"
it should be like this :
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#android:id/tabhost
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true">

Categories

Resources