I am currently trying out the Android 1.5 SDK and I have seen a couple of examples on TabHost.
What I am trying to do is to use a different button on each Tab to do its tasks.
What I tried
was using onClickListiner() and onClick(). I think this is what all the developers use, but I keep getting a null exception on the LogCat every time the button is pressed. Also I have each XML Layout so I call the Tab as : tab.add(...setContent(R.id.firstTabLayout))
firstTabLayout = layout for Button and TextView.
What would be the best way to make a button/TextView work properly under the TabHost?
I'm not entirely sure where your problem is, but this is how I've set up my Tabbed activities before
layout.xml
<TabWidget android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:id="#+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#android:id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:id="#+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#android:id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:id="#+id/tab3"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#android:id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
Tab.java
public class InitialActivity extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
TabHost th = getTabHost();
th.addTab(th.newTabSpec("tab_1").setIndicator("Tab1").setContent(R.id.tab1));
th.addTab(th.newTabSpec("tab_2").setIndicator("Tab2").setContent(R.id.tab2));
th.addTab(th.newTabSpec("tab_3").setIndicator("Tab3").setContent(R.id.tab3));
}
}
Then, you can just use findViewById() on any views within the tabs, or if there are shared names between them, you can do findViewById(R.id.tab1).findViewById(R.id.text)
Related
Basically what I'm trying to do is have half the screen be a tab layout and half an image (or really anything else). It works just fine until I start adding weight.
As is, nothing is displayed. What would cause this problem?
<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"
android:orientation="horizontal"
android:gravity="left"
android:weightSum="2"
android:background="#00025a">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<TabWidget
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#android:id/tabs">
</TabWidget>
<FrameLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:id="#android:id/tabcontent">
</FrameLayout>
<FrameLayout
android:id="#+id/tabcontent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
</LinearLayout>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/test_room_1"/>
</android.support.v4.app.FragmentTabHost>
Without the weight component the tab related stuff displays, but I'm assuming it pushes the image out as it isn't shown.
Any help would be appreciated. :)
UPDATE:
Here's a screenshot as requested: Weighted
And the calling activity:
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.widget.ImageView;
public class sandbox_mode extends FragmentActivity {
ImageView level_background;
private FragmentTabHost mTabHost;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sandbox_mode_play);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.tabcontent);
mTabHost.addTab(mTabHost.newTabSpec("Enemy Tab").setIndicator("Enemies"),
enemy_tab.class, null);
}
}
If you look at the documentation on FragmentTabHost link, you'll see that it's not inherited from LinearLayout, so basically layout_weight is not supported by this container. I would suggest adding additional top LinearLayout for this to work correctly.
<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"
android:orientation="horizontal"
android:gravity="left"
android:weightSum="2"
android:background="#00025a">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<TabWidget
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#android:id/tabs">
</TabWidget>
<FrameLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:id="#android:id/tabcontent">
</FrameLayout>
<FrameLayout
android:id="#+id/tabcontent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
</LinearLayout>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/test_room_1"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
use childFragmentManager instead FragmentManager
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.tabcontent);
If you want to use nested fragments within a native Fragment. use getFragmentManager().
If you want to use nested fragments within a support library Fragment, use getChildFragmentManager().
I follow tutorial found on internet, but it seems that doesn't work...
I get Tab 1 always opened, that is okay, but I don't see TABS menu up...
Here is my code:
Main2Activity:
public class Main2Activity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
// create the TabHost that will contain the Tabs
TabHost tabs=(TabHost)findViewById(R.id.tabhost);
tabs.setup();
TabHost.TabSpec spec=tabs.newTabSpec("tag1");
spec.setContent(R.id.tab1);
spec.setIndicator("Analog Clock");
tabs.addTab(spec);
spec=tabs.newTabSpec("tag2");
spec.setContent(R.id.tab2);
spec.setIndicator("DigitalClock");
tabs.addTab(spec);
spec=tabs.newTabSpec("tag3");
spec.setContent(R.id.tab3);
spec.setIndicator("Button");
tabs.addTab(spec);
}
Content_main2.xml:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
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"/>
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<AnalogClock android:id="#+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<DigitalClock android:id="#+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<Button android:id="#+id/tab3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Do Nothing"/>
</FrameLayout>
</LinearLayout>
</TabHost>
This is how it looks when i run it
I solved it with:
setContentView(R.layout.content_main2);
I was calling activity_main2, but don't know why when I call activity_main2 don't get content_main2.
I have in Activity_main2.xml:
<include layout="#layout/content_main2" />
Does anyone know why?
you can use seperate activity for each tab. as said by "intelliJ". create seperate activity and seperate xml file for each tab and then add your analog digital and button in each activity respectively. say in activity1 add analog and in activity2 add digital and so on.
I am trying to build a simple Android app one of whose activities will have a Google Places AutoCompleteTextView along with a Button and several other components. However, it seems to be the case that the AutoCompleteTextView component does not want to play friendly with anything else. Currently, I can only get one (the AutoCompleteTextView component) or the others rendering at a time. Here are the relevant portions of my code:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text="Please enter your place" >
<requestFocus />
</AutoCompleteTextView>
<Button
android:id="#+id/btnChangeDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Date" />
<TextView
android:id="#+id/lblDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Current Date (M-D-YYYY): "
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
<DatePicker
android:id="#+id/dpResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
list_item.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
GooglePlacesAutocompleteActivity.java
public class GooglePlacesAutocompleteActivity extends Activity implements OnItemClickListener {
// class fields are here ...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setCurrentDateOnView(); // sets up the date picker
addListenerOnButton(); // adds a listener to a Button
// next three lines configure the Google Place autocomplete
AutoCompleteTextView autoCompView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
autoCompView.setAdapter(new GooglePlacesAutocompleteAdapter(this, R.layout.list_item));
autoCompView.setOnItemClickListener(this);
}
// more methods...
}
If you need more information to render an answer here, please feel free to ask me and I will post as soon as I can.
Hi You have a problem of orientation in your LinearLayout. I guess you want your AutocompleteTextView in top of the rest. If that is the case you should probably add a nested LinearLayout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text="Please enter your place" >
<requestFocus />
</AutoCompleteTextView>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
[your other items]
</LinearLayout>
</LinearLayout>
If this is not what you are looking for just change match_parent to wrap_content for thw width of your AutocompleteTextView.
I'm creating a search widget and a searchable activity. I followed the android developer guide and have this so far.
Here is my searchableActivity.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doMySearch(query);
}
}
As mentioned, R.layout.search is creating the error. I don't have a search xml in layouts, and I don't understand what I am supposed to define within search.xml.
You need to have a layout named search.xml in your res/layout folder where you'll show the search results.
I my case I showed the search results in a RecyclerView and here's a sample layout of mine.
<FrameLayout 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="match_parent"
android:layout_height="match_parent"
android:background="#color/color_background_search">
<LinearLayout
android:id="#+id/empty_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:tint="#color/color_primary_light"
android:src="#drawable/ic_action_search" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/empty_search_result"
android:textColor="#color/black"
android:textSize="20sp" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/search_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
You are trying to set the current layout to search.xml and you said yourself that you do not have this file present.
setContentView is setting the layout that you will see when running the app in this instance.
I am working on a android project and I have my four tabs. On one of the tabs, I am trying to insert an edit field box. ANy clues on how to do that?
Here is my main.xml file:
<?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_height="fill_parent">
<LinearLayout
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"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/textview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="year of car" />
<TextView
android:id="#+id/textview2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is another tab" />
<TextView
android:id="#+id/textview3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is a third tab" />
<TextView
android:id="#+id/textview4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is a fourth tab" />
</FrameLayout>
</LinearLayout>
</TabHost>
and here is my .java file:
package com.example.gasfillup;
import android.app.Activity;
import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;
public class GasFillup extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("Car Info").setContent(R.id.textview1));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("Fillup Info").setContent(R.id.textview2));
mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("Gas Milage").setContent(R.id.textview3));
mTabHost.addTab(mTabHost.newTabSpec("tab_test4").setIndicator("Stats").setContent(R.id.textview4));
mTabHost.setCurrentTab(0);
}
}
Any Ideas or help is much appreciated!!
ironmantis7x
Wrap your with a ... so, framelayout, linearlayout, textview, edittext then close the linearlayout.
Well, let me edit that ... create a separate XML file to hold the entire view you want on the tab and then set it with R.layout.tab1 rather than setting tab1 to the textview.
As Bill Mote said, you need to wrap the content of the tab within some sort of container. Here's an example using a simple LinearLayout:
<?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_height="fill_parent">
<LinearLayout
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"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/textview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="year of car" />
<TextView
android:id="#+id/textview2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is another tab" />
<TextView
android:id="#+id/textview3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is a third tab" />
<LinearLayout
android:id="#+id/panel4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textview4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="this is a fourth tab" />
<EditText
android:id="#+id/myeditfield"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
Then change your code behind to:
....
mTabHost.addTab(mTabHost.newTabSpec("tab_test4").setIndicator("Stats").setContent(R.id.panel4));
....
Realistically, this is how you should do all of the tabs (so that you can have more than just text), but you get the idea.