A simple TabHost in android - java

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>

Related

Android Studio - How to find EditText in tab1 within tabhost inflated on View layout?

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).

Having problems with the Fragment Tab Host - It doesn't work

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>

Inflating a LinearLayout dynamically - Android

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>

android tabs repeated content

I've set up a tab host and defined 5 tabs both in the xml file and the corresponding java class file. I added some fields for tab 1 and a test field for tab 2 but all I see in each tab is the repeated content from tab 1. Has anyone come across this before? code is pasted below.
Thanks
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inspection);
TabHost tabHost = (TabHost)findViewById(R.id.tabHost);
tabHost.setup();
TabHost.TabSpec tabSpec = tabHost.newTabSpec("Part1");
tabSpec.setContent(R.id.tabPart1);
tabSpec.setIndicator("1");
tabHost.addTab(tabSpec);
tabHost.newTabSpec("Part2");
tabSpec.setContent(R.id.tabPart2);
tabSpec.setIndicator("2");
tabHost.addTab(tabSpec);
tabHost.newTabSpec("Part3");
tabSpec.setContent(R.id.tabPart3);
tabSpec.setIndicator("3");
tabHost.addTab(tabSpec);
tabHost.newTabSpec("Part4");
tabSpec.setContent(R.id.tabPart4);
tabSpec.setIndicator("4");
tabHost.addTab(tabSpec);
tabHost.newTabSpec("Part5");
tabSpec.setContent(R.id.tabPart5);
tabSpec.setIndicator("5");
tabHost.addTab(tabSpec);
}
and the xml code:
<TabHost
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/tabHost"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="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">
<LinearLayout
android:id="#+id/tabPart1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Id"
android:layout_marginLeft="5sp"
android:layout_marginTop="10sp"
android:layout_marginRight="5sp"
android:hint="Global Unique ID" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobNum"
android:hint="Job Number"
android:layout_marginLeft="5sp"
android:layout_marginTop="10sp"
android:layout_marginRight="5sp" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Required?"
android:id="#+id/req"
android:layout_marginLeft="5sp"
android:layout_marginTop="10sp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/material"
android:layout_marginLeft="5sp"
android:layout_marginTop="10sp"
android:layout_marginRight="5sp"
android:hint="Material" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/shape"
android:layout_marginLeft="5sp"
android:layout_marginTop="10sp"
android:layout_marginRight="5sp"
android:hint="Shape" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/size"
android:hint="Size (mm)"
android:layout_marginLeft="5sp"
android:layout_marginTop="10sp"
android:layout_marginRight="5sp" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5sp"
android:layout_marginTop="10sp"
android:id="#+id/LvlCollectGp">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Collect Now?"
android:id="#+id/collectNow"
android:hint="Collect Now?" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Collect Later?"
android:id="#+id/collectLtr" />
</RadioGroup>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/lvl"
android:hint="Level (m)"
android:layout_marginLeft="5sp"
android:layout_marginTop="10sp"
android:layout_marginRight="5sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/tabPart2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/test"
android:hint="Test" />
</LinearLayout>
<LinearLayout
android:id="#+id/tabPart3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"></LinearLayout>
<LinearLayout
android:id="#+id/tabPart4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"></LinearLayout>
<LinearLayout
android:id="#+id/tabPart5"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"></LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
try this code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inspection);
TabHost tabHost = (TabHost)findViewById(R.id.tabHost);
tabHost.setup();
TabHost.TabSpec tabSpec1 = tabHost.newTabSpec("Part1");
tabSpec1.setContent(R.id.tabPart1);
tabSpec1.setIndicator("1");
tabHost1.addTab(tabSpec1);
TabHost.TabSpec tabSpec2 = tabHost.newTabSpec("Part2");
tabSpec2.setContent(R.id.tabPart2);
tabSpec2.setIndicator("2");
tabHost2.addTab(tabSpec2);
TabHost.TabSpec tabSpec3 = tabHost.newTabSpec("Part3");
tabSpec3.setContent(R.id.tabPart3);
tabSpec3.setIndicator("3");
tabHost3.addTab(tabSpec3);
TabHost.TabSpec tabSpec4 = tabHost.newTabSpec("Part4");
tabSpec4.setContent(R.id.tabPart4);
tabSpec4.setIndicator("4");
tabHost4.addTab(tabSpec4);
TabHost.TabSpec tabSpec5 = tabHost.newTabSpec("Part5");
tabSpec5.setContent(R.id.tabPart5);
tabSpec5.setIndicator("5");
tabHost5.addTab(tabSpec5);
}

android-wants to put Button above listview

I am using linear layout inside that one button and one list view..
list-view is adding its item dynamically at the run-time..
so at that time button goes behind list-view and not visible and list-view fit in whole layout.
I want to put Button above the List view and it must also stay after loading list-view
Thanks in Advance
my .xml file is:
<?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"
android:background="#000000">
<Button
android:id="#+id/btn_searchNewDevices"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:layout_gravity="left"
android:background="#000000"
android:textColor="#FFFFFF"
android:text="Search New Devices"
/>
<ListView
android:id="#+id/lst_add_newDevices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="horizontal" >
</ListView>
</LinearLayout>
here it is search new device button.
click on that button device search for Bluetooth and add into listview
so this is thing happen in my application..
I think even this works.Give a try
edited...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relative"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/btn_searchNewDevices"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginTop="5dip"
android:text="Search New Devices"
android:textColor="#000000" />
<ListView
android:id="#+id/lst_add_newDevices"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/btn_searchNewDevices"
android:scrollbars="horizontal">
</ListView>
</RelativeLayout>
and the activity is as follows .
public class MainActivity extends Activity implements OnClickListener {
ArrayAdapter<String> adapter;
private Button mBTNSearch;
private static ListView mLV_list;
ArrayList<String> devicesList;
SparseBooleanArray checked;
RelativeLayout layout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mBTNSearch = (Button) findViewById(R.id.btn_searchNewDevices);
devicesList = new ArrayList<String>();
mLV_list = (ListView) findViewById(R.id.lst_add_newDevices);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, devicesList);
mBTNSearch.setOnClickListener(this);
mLV_list.setAdapter(adapter);
}
public void onClick(View v) {
adapter.add("new Device");
adapter.notifyDataSetChanged();
}
}
The output capture is as below
Try using android:layout_weight="1" in the listview's height to that you make sure your button will always be seen in the screen.
You should try:
<?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:background="#000000"
android:orientation="vertical" >
<Button
android:id="#+id/btn_searchNewDevices"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginTop="5dip"
android:background="#000000"
android:text="Search New Devices"
android:textColor="#FFFFFF" />
<ListView
android:id="#+id/lst_add_newDevices"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scrollbars="horizontal" >
</ListView>
</LinearLayout>
Or you can use ListView's addHeaderView() method. Here is a tutorial for that.
Try this code for your Layout.
<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" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</ListView>
</LinearLayout>
Try putting your listview inside a scrollview.
<Button
android:id="#+id/btn_searchNewDevices"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginTop="5dip"
android:background="#000000"
android:text="Search New Devices"
android:textColor="#FFFFFF" />
<ScrollView
android:id="#+id/scroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="7dip"
android:scrollbars="none" >
<ListView
android:id="#+id/lst_add_newDevices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="horizontal" >
</ListView>
</ScrollView>
It might work. I haven't tested, so not sure.

Categories

Resources