What I am trying to do is to create three tabs in my main xml file. and then load up the fragments based on the tabs which has been pressed.
I have having problems with the Fragment Tab Host. I have this XML which has ;
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#369742"
android:gravity="start|end"
android:longClickable="true">
<FragmentTabHost
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/tabHost"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:clickable="false">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:clickable="false"
android:background="#5DAD68">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:clickable="false"
android:background="#5DAD68"
android:weightSum="1">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:background="#5DAD68"
android:clickable="false"
android:measureWithLargestChild="true"
android:orientation="horizontal"
android:layout_weight="1.04"
android:longClickable="false"
android:paddingTop="10dp"
android:paddingLeft="30dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"></TabWidget>
<ImageButton
android:layout_width="62dp"
android:layout_height="40dp"
android:id="#+id/horsePointer"
android:backgroundTintMode="src_atop"
android:src="#drawable/ic_horse"
android:layout_toRightOf="#android:id/tabs"
android:background="#5DAD68"
android:layout_gravity="center_vertical" />
</LinearLayout>
<FrameLayout
android:id="#+id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="false">
<LinearLayout
android:id="#+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:clickable="false"
android:background="#FFFFFF"
android:weightSum="1">
</LinearLayout>
<LinearLayout
android:id="#+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:clickable="true"
android:weightSum="1"
android:background="#FFFFFF"
android:layout_gravity="bottom">
</LinearLayout>
<LinearLayout
android:id="#+id/tab3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:clickable="false"
android:background="#FFFFFF">
</LinearLayout>
</FrameLayout>
</LinearLayout>
</FragmentTabHost>
In My Main activity I have got this code ;
public class MainActivity extends ActionBarActivity {
private FragmentTabHost tabHost;
private TextView click;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_tab_main);
// Refactor at your will.
tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), R.id.tabcontent);
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("", getResources().getDrawable(R.drawable.ic_tab1)),
tab1Fragment.class, null);
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("", getResources().getDrawable(R.drawable.tab2)),
tab2Fragment.class, null);
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("", getResources().getDrawable(R.drawable.ic_tab3)),
tab3Fragment.class, null);
}
When I run this program it says "Unfortunately, Example Project has stopped."
This is the Logcat I get ;
Logcat
You have used a TabHost in your XML layout and a FragmentTabHost in your Activity code. The two classes are different, and you can use only one of them at a time, not both together.
Your XML layout needs to be defined like this:
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
Related
I have a List View thatis retrived from a php that get's it from MySql. However,when a retice the data that does not match the simple adater that I formated. Here is my code:
''HashMap map = new HashMap();
map.put(TAG_VALOR, valorIntent);
map.put(TAG_COMIDA , Scomida);
map.put(TAG_TABACO , Stabaco);
map.put(TAG_BEBIDAS , Sbebidas);
map.put(TAG_SABADO , Ssabado);
map.put(TAG_DOMINGO , Sdomingo);
map.put(TAG_HIGIROUP , Shigiroup);
map.put(TAG_MULTIBANCO , Smultibanco);
map.put(TAG_OUTROS , Soutros);
productsList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
ListAdapter adapter = new SimpleAdapter(
Relatorio.this, productsList,
R.layout.relatorio_details, new String[] { TAG_COMIDA,TAG_TABACO,TAG_BEBIDAS,TAG_SABADO
,TAG_DOMINGO,TAG_HIGIROUP, TAG_MULTIBANCO, TAG_OUTROS},
new int[] { R.id.comidaRel, R.id.tabacoRel,R.id.bebidasRel, R.id.sabadoRel
, R.id.domingoRel, R.id.higroupasRel, R.id.multibancoRel, R.id.outrosRel});
setListAdapter(adapter);
Log.d("Multibanco", TAG_MULTIBANCO);
}
});'
and this is the relatorio_details wrapped in LinearLayout vertical:
'<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Comida"/>
<TextView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:id="#+id/comidaRel"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Tabaco"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="#+id/tabacoRel"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Multibanco"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="#+id/multibancoRel"
android:text="0.00"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Bebidas"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="#+id/bebidasRel"
android:text="0.00"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Sábado"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="#+id/sabadoRel"
android:text="0.00"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Domingo"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="#+id/domingoRel"
android:text="0.00"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Higiene e Roupas"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="#+id/higroupasRel"
android:text="0.00"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1"
android:id="#+id/outros_rel">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Outros"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="#+id/outrosRel"
android:text="0.00"/>
</LinearLayout>
The data shows but does not respect the relatorio_details Layout
You need to make a custom adapter class for your list view nad a custom layout that is a simple xml layout file after that you need to set the adapter of your list view
ListView.setAdapter(new CustomAdapter(datalist));
Use this link for more help
Custom Adapter for List View
i'm in serious problem. I'm trying to get EditText from tab1 in Tabhost, but it keeps giving me error:
Attempt to invoke virtual method 'android.view.View android.widget.LinearLayout.findViewById(int)' on a null object reference.
I have tried my code in many many different ways without any success!
Here is my Code :
final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View control_layout = inflater.inflate(R.layout.control_layout,null,false);
TabHost tbhst = (TabHost)control_layout.findViewById(R.id.tabHost);
tbhst.setup();
TabHost.TabSpec tb1 = tbhst.newTabSpec("TAB 1");
TabHost.TabSpec tb2 = tbhst.newTabSpec("TAB 2");
TabHost.TabSpec tb3 = tbhst.newTabSpec("TAB 3");
tb1.setContent(R.id.tab1);
tb2.setContent(R.id.tab2);
tb3.setContent(R.id.tab3);
tb1.setIndicator("CMD");tb2.setIndicator("Chat");tb3.setIndicator("Control");tbhst.addTab(tb3);tbhst.addTab(tb2);tbhst.addTab(tb1);
LinearLayout gff = (LinearLayout)tbhst.findViewById(R.id.ggf);
FrameLayout tbs_content = (FrameLayout)gff.findViewById(android.R.id.tabcontent);
LinearLayout tbs1 = (LinearLayout)tbs_content.findViewById(R.id.tab1);
final EditText command_resualt_box = (EditText) tbs1.findViewById(R.id.editText5);
UPDATE :
as #popvitsj said maybe i'm making mistake but i tried to change it in layout/control.xml, but it gives me another different error and here is a Screenshot :
and this the code in layout/control.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/control_q">
<TabHost
android:layout_width="fill_parent"
android:layout_height="410dp"
android:id="#+id/tabHost"
android:layout_gravity="center_horizontal">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="#+id/ggf">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></TabWidget>
<FrameLayout
android:id="#+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"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout"
android:layout_gravity="center_horizontal">
<EditText
android:layout_width="match_parent"
android:layout_height="35dp"
android:id="#+id/editText9"
android:inputType="text"
android:imeOptions="actionDone"
android:background="#android:color/black"
android:textColor="#ffff0c00"
android:layout_marginTop="5dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="300dp"
android:inputType="textMultiLine"
android:ems="10"
android:id="#+id/editText10"
android:enabled="false"
android:background="#android:color/black"
android:textColor="#ffff0c00"
android:autoLink="all"
android:layout_marginTop="5dp"
android:scrollbars="horizontal|vertical"
android:linksClickable="true"
android:editable="false" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:id="#+id/linearLayout2" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" >
<EditText
android:layout_width="260dp"
android:layout_height="40dp"
android:id="#+id/editText11"
android:background="#android:color/black"
android:textColor="#ffff0c00"
android:layout_marginLeft="5dp"
android:layout_marginTop="2.5dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button11"
android:id="#+id/button12"
android:background="#android:color/black"
android:textColor="#ffff0c00"
android:layout_marginLeft="5dp" />
</LinearLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="300dp"
android:enabled="false"
android:inputType="textMultiLine"
android:background="#android:color/black"
android:textColor="#ffff0c00"
android:autoLink="all"
android:ems="10"
android:id="#+id/editText12"
android:layout_gravity="center_horizontal"
android:layout_weight="0.53"
android:layout_marginTop="5dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/tab3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"></LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
NOTE : this code is giving an error you can take look on screenshot above.
FrameLayout tbs_content = (FrameLayout)gff.findViewById(android.R.id.tabcontent);
LinearLayout tbs1 = (LinearLayout)tbs_content.findViewById(R.id.tab1);
The second of these lines gives the NullPointerException. So tbs_content is null, because it fails to load it on the first line.
It looks like you made a mistake specifying the id: android.R.id.tabcontent should most likely be R.id.tabcontent.
android.R is the R-class for the system resources. R is usually your own generated class (unless you import android.R).
I'm trying to inflate a list of views in a LinarLayout directly in the onCreate() method from the activity. However, I'm having a problem somewhere in the code but I really can't see it, I'm turning around this problem for two days, I'm going crazy !
The problem is that when I run the activity, the linear layout is completely empty, there is only the title.
Here is the important part of the code, KeyItems activity :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.keyitems_layout);
initializer();
ll = (LinearLayout) findViewById(R.id.ll);
LayoutInflater inflater = LayoutInflater.from(this);
for (int i = 0; i < list.size(); i++) {
View view = inflater.inflate(R.layout.keyitem_temp, ll, false);
final TextView ind = (TextView) view.findViewById(R.id.ind);
final TextView item = (TextView) view.findViewById(R.id.item);
ind.setText(list.get(i).get("id"));
item.setText(list.get(i).get("item"));
ll.addView(view);
}
}
Here is the keyitems_layout :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/black" >
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/black">
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp" />
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/key_items"
android:textSize="36sp"
android:textColor="#color/white" />
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp" />
<LinearLayout android:id="#+id/ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="32dp"
android:layout_marginRight="32dp"
android:orientation="vertical"
android:background="#color/white" >
</LinearLayout>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:text="caca"
android:padding="32dp" />
</LinearLayout>
</ScrollView>
And finally, my keyitem_temp.xml which I'm inflating :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black"
android:padding="4dp"
android:orientation="horizontal" >
<TextView android:id="#+id/ind"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:textSize="18sp"
android:textColor="#color/blue" />
<TextView android:id="#+id/item"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:textSize="18sp"
android:textColor="#color/white"/>
</LinearLayout>
I have an other activity called ItemsActivity which is the same code with some little differences and it works. Any suggestion? Thanks in advance.
Try using wrap_content for all of your heights except the initial ScrollView:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/black" >
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#color/black">
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp" />
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/key_items"
android:textSize="36sp"
android:textColor="#color/white" />
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp" />
<LinearLayout android:id="#+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="32dp"
android:layout_marginRight="32dp"
android:orientation="vertical"
android:background="#color/white" >
</LinearLayout>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:text="caca"
android:padding="32dp" />
</LinearLayout>
</ScrollView>
and:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black"
android:padding="4dp"
android:orientation="horizontal" >
<TextView android:id="#+id/ind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textSize="18sp"
android:textColor="#color/blue" />
<TextView android:id="#+id/item"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textSize="18sp"
android:textColor="#color/white"/>
</LinearLayout>
Currently I work on a tabhost layout.
In most android app the layout is:
tab1 | tab 2
____________
Tab 1 content
(if I press on tab1)
However, what I would like to achieve is
Tab 1 content
(if I press on tab1)
____________
tab1 | tab 2
Here is the main xml (backbone of app) :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<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" />
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
</LinearLayout>
The tab content layout
<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:background="#31152C"
android:gravity="center"
tools:context=".MonitoringActivity" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/home" />
</LinearLayout>
</ScrollView>
</LinearLayout>
The main class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ctx = this;
gs = (MyApp) getApplication();
tabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
tabHost.addTab(tabHost.newTabSpec("main").setIndicator(""),Home.class, null);
tabHost.addTab(tabHost.newTabSpec("carpark").setIndicator("",getResources().getDrawable(R.drawable.btn_park)), CarPark.class,null);
tabHost.addTab(tabHost.newTabSpec("shop").setIndicator("",getResources().getDrawable(R.drawable.btn_shop)), Shop.class,null);
tabHost.getTabWidget().setDividerDrawable(null);
tabHost.setCurrentTab(0);
}
Thanks for helping
you can add tabwidget to the bottom using the given code..
<?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="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#android:id/tabs" />
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
</TabHost>
Found out the solution using the support fragment tab host:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<android.support.v4.app.FragmentTabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" />
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
Try this way,hope this will help you to solve your problem.
activity_main.xml
<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">
<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"/>
<TabWidget
android:id="#android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
tab1_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#31152C"
android:gravity="center">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="20sp"
android:text="Home"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
tab2_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#31152C"
android:gravity="center">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="20sp"
android:text="Car Park"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
tab3_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#31152C"
android:gravity="center">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="20sp"
android:text="Shop"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
MainActivity.java
public class MainActivity extends FragmentActivity {
private FragmentTabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab 1",getResources().getDrawable(R.drawable.ic_launcher)),
Home.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab 2",getResources().getDrawable(R.drawable.ic_launcher)),
CarPark.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Tab 3", getResources().getDrawable(R.drawable.ic_launcher)),
Shop.class, null);
}
}
Home.java
public class Home extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.tab1_view, container, false);
return V;
}
}
CarPark.java
public class CarPark extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.tab2_view, container, false);
return V;
}
}
Shop.java
public class Shop extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.tab3_view, container, false);
return V;
}
}
I have created a simple tab project in Android
MainActivity.java
public class MainActivity extends TabActivity {
// TabSpec Names
private static final String TAB1 = "Tab1";
private static final String TAB2 = "Tab2";
private static final String TAB3 = "Tab3";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
// Inbox Tab
TabSpec inboxSpec = tabHost.newTabSpec(TAB1);
Intent inboxIntent = new Intent(this, Tab1.class);
inboxSpec.setIndicator(TAB1);
// Tab Content
inboxSpec.setContent(inboxIntent);
// Outbox Tab
TabSpec PriceSpec = tabHost.newTabSpec(TAB2);
Intent PriceIntent = new Intent(this, Tab2.class);
PriceSpec .setIndicator(TAB2);
PriceSpec.setContent(PriceIntent);
// Profile Tab
TabSpec DistanceSpec = tabHost.newTabSpec(TAB3);
Intent DistanceIntent = new Intent(this, Tab3.class);
DistanceSpec .setIndicator(TAB3);
DistanceSpec.setContent(DistanceIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(inboxSpec);
tabHost.addTab(PriceSpec);
tabHost.addTab(DistanceSpec);
//Set the current value tab to default first tab
tabHost.setCurrentTab(0);
//Setting custom height for the tabs
final int height = 45;
tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = height;
tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = height;
tabHost.getTabWidget().getChildAt(2).getLayoutParams().height = height;
}
}
main.xml
<?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"/>
</LinearLayout>
</TabHost>
OUTPUT ::
How can i make a horizontal set of 5 buttons on top of the tab to get something like this in the below figure
What are the code changes i need to make in main.xml ?
<?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"
android:background="#drawable/background" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<include layout="#layout/screen_topbar" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/white" >
</TabWidget>
</FrameLayout>
</LinearLayout>
</TabHost>
And TopBar layout-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/buttonlayout"
android:layout_width="fill_parent"
android:layout_height="53dp"
android:gravity="top"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
Just try this sure it will work..
Add one more LinearLayout define your buttons inside above TabWidget and fixed your FrameLayout height with weight, look below code:
<?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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
</LinearLayout>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/dm_footer_bg"
android:orientation="horizontal" />
<FrameLayout
android:id="#+android:id/realtabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</TabHost>
try this. Replace your main.xml code with the below code. it will work
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal">
<Button android:layout_width="100dp" android:text=" Button 1"
android:layout_height="wrap_content" />
<Button android:layout_width="100dp" android:text=" Button 2"
android:layout_height="wrap_content" />
<Button android:layout_width="100dp" android:text=" Button 3"
android:layout_height="wrap_content" />
<Button android:layout_width="100dp" android:text=" Button 4"
android:layout_height="wrap_content" />
</LinearLayout>
<TabHost
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"/>
</LinearLayout>
</TabHost>
</LinearLayout>