I have an activity with dialog theme. Its layout has 3 parts
HEADER
BODY (HAS ListView)
OK BUTTON
This has to be the structure of my layout always. If I use following layout xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dip" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/iv_av_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dip"
android:layout_marginTop="2dip"
android:paddingLeft="6dip"
android:src="#drawable/tablet_ic_logo" />
<RelativeLayout
android:id="#+id/rl_av_found"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/iv_av_logo"
android:layout_marginBottom="5dip"
android:paddingLeft="16dip" >
<TextView
android:id="#+id/tv_av_found"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/orange"
android:textSize="22dp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_av_recommendation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tv_av_found"
android:layout_marginBottom="8dip"
android:textColor="#color/white"
android:textSize="13dp"
android:visibility="gone" />
</RelativeLayout>
<LinearLayout
android:id="#+id/ll_av_body"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/rl_av_found"
android:background="#drawable/tablet_dialog_glow" >
<ListView
android:id="#+id/lv_av_threats_found"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:divider="#drawable/tablet_dialog_separator" >
</ListView>
</LinearLayout>
<RelativeLayout
android:id="#+id/rl_button_bar"
android:layout_width="wrap_content"
android:layout_height="70dip"
android:layout_below="#id/ll_av_body" >
<Button
android:id="#+id/btn_av_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="onClick"
android:text="#string/btn_ok" />
<ImageView
android:id="#+id/iv_separator"
android:layout_width="match_parent"
android:layout_height="1dip"
android:layout_alignParentTop="true"
android:background="#drawable/tablet_dialog_footer" />
</RelativeLayout>
</RelativeLayout>
It gives me what I want. But if the list grows and I have say 100 items, OK button goes out of the picture.
And if I use this layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dip" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/iv_av_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dip"
android:layout_marginTop="2dip"
android:paddingLeft="6dip"
android:src="#drawable/tablet_ic_logo" />
<RelativeLayout
android:id="#+id/rl_av_found"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/iv_av_logo"
android:layout_marginBottom="5dip"
android:paddingLeft="16dip" >
<TextView
android:id="#+id/tv_av_found"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/orange"
android:textSize="22dp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_av_recommendation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tv_av_found"
android:layout_marginBottom="8dip"
android:textColor="#color/white"
android:textSize="13dp"
android:visibility="gone" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/rl_button_bar"
android:layout_width="wrap_content"
android:layout_height="70dip"
android:layout_alignParentBottom="true" >
<Button
android:id="#+id/btn_av_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="onClick"
android:text="#string/btn_ok" />
<ImageView
android:id="#+id/iv_separator"
android:layout_width="match_parent"
android:layout_height="1dip"
android:layout_alignParentTop="true"
android:background="#drawable/tablet_dialog_footer" />
</RelativeLayout>
<LinearLayout
android:id="#+id/ll_av_threats"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/rl_button_bar"
android:layout_below="#id/rl_av_found"
android:background="#drawable/tablet_dialog_glow" >
<ListView
android:id="#+id/lv_av_found"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:divider="#drawable/tablet_dialog_separator" >
</ListView>
</LinearLayout>
</RelativeLayout>
The OK button is always displayed at the bottom of the screen and the layout's height takes full screen even if I have just one item in the ListView.
How should I structure my xml so as to have its height wrapped according to the content and OK button always visible independent of the number of items in the ListView?
By Default you should have the params as,
Header - Align Top
Body(ListView) - Below header,
Button - below body(ListView)
Now at runtime,
You should get the height of the list view and if it goes beyond the screen, and if it does, then Set the layout params of button to alignParentBottom and layout params of list view to be below header and above button.
Use may use this in vice versa.
To get the height of the list view. Use this
public int getTotalListViewHeight(ListView lv, BaseAdapter ba) {
int listviewElementsheight = 0;
for (int i = 0; i < ba.getCount(); i++) {
View view = ba.getView(i, null, lv);
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
listviewElementsheight += view.getMeasuredHeight();
// for Width use view.getMeasuredWidth()
}
return listviewElementsheight;
}
Here the adapter is listviews adapter.
also will checking this, add the buttons height to the total height.
Related
I have been "fighting" for two days trying to find a solution to this design but apparently it is impossible.
Scenario:
The View (activity) with 4 LinearLayouts and below must have a container (I have been trying with ScrollView + LinearLayouts) with 2 ListViews (with a TextView title for each one), and yet, these ListViews will vary according to some choices made in another ListView coming from a Dialog.
Firstly, when nothing is added yet the View is exactly as should be, but when I add, let's say 3 items, then the disaster shows up.
The first ListView becomes huge (in height size) and the other one goes down there, out of view.
I have tried lots of combinations of layouts but no success so far, tried even resize the ListView via java code after adding items to its adapter.
Util.setListViewHeightBasedOnItems(listViewCaixasSelecionadas);
The initial view - this is OK:
Dialog to choose some items to add to ListView adapter:
Three items add to the ListView (see the huge space below the last item):
View of the second ListView, which now I must scroll a lot to see it:
Print from eclipse project:
Here is my layout xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutTerceiraTela"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<include
android:id="#+id/include1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
layout="#layout/include_toolbar" />
<include
android:id="#+id/include2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/include1"
layout="#layout/include_user_emp" />
<LinearLayout
android:id="#+id/layoutBotoes"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/include2"
android:orientation="horizontal" >
<Button
android:id="#+id/btnSalvarParte3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:text="Salvar" />
<Button
android:id="#+id/btnAdicionarCaixa"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+Caixa" />
<Button
android:id="#+id/btnAdicionarProduto"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+Produto" />
</LinearLayout>
<LinearLayout
android:id="#+id/layoutTituloTela"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#id/layoutBotoes"
android:layout_marginTop="2dp"
android:orientation="vertical" >
<TextView
android:id="#+id/textViewNomeProcedimento"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/actionbar_background"
android:padding="3dp"
android:text="Nome do Procedimento"
android:textColor="#color/branco"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
<include
android:id="#+id/ProgressBarTela3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/layoutTituloTela"
layout="#layout/include_progress_overlay" />
<!-- Scroll com as duas listas -->
<ScrollView
android:id="#+id/containerCaixasProdutos"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/viewBottomWindow"
android:layout_below="#id/layoutTituloTela"
android:fillViewport="true" >
<LinearLayout
android:id="#+id/layoutScrollCaixasProdutos"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip" >
<TextView
android:id="#+id/textViewTituloDialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/primary_dark"
android:gravity="center_vertical"
android:padding="10dp"
android:text="Caixas"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/branco"
android:textStyle="bold" />
<ListView
android:id="#+id/listViewCaixas"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:background="#B29090" >
</ListView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#color/primary_dark"
android:gravity="center_vertical"
android:padding="10dp"
android:text="Produtos (Opcionais)"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/branco"
android:textStyle="bold" />
<ListView
android:id="#+id/listViewProdutosAdicionados"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:background="#4A9C67" >
</ListView>
</LinearLayout>
</LinearLayout>
</ScrollView>
<View
android:id="#+id/viewBottomWindow"
android:layout_width="fill_parent"
android:layout_height="10dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:background="#drawable/btn_cab_done_focused_csa" />
I really would appreciate any idea how to solve this issue, absolutely annoying.
Use this to measure height
public void setDynamicListViewHeight(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = 0;
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
May be adding weights to the views may solve your problem. Let me know ur problem solved or not.
Edit: Stupid user error, I had malformed XML.
--
I have an activity with three fragments inside it. In onResume() of one fragment, I am trying to grab views from another fragment using activity.findViewById(), but that method is returning null for any of the views from one particular fragment. Views in either of the other two fragments are fine.
If I dump the entire view tree using getRootView() and the following quickly-hacked-together code, I see that the views in question are indeed part of the view tree, but have an id of -1.
private void dumpAllViews(View v, int indent)
{
// Java doesn't support variable width format code -- "%*c"
// Fake it with string concatenation. Lame!
Log.d(tag, String.format("%" + (indent * 4 + 1) + "c%s %d", ' ', v.toString(), v.getId()));
if (v instanceof ViewGroup)
{
ViewGroup vg = (ViewGroup)v;
for (int i = 0; i < vg.getChildCount(); i++)
{
dumpAllViews(vg.getChildAt(i), indent + 1);
}
}
}
Why would all the views in that fragment have id -1?
Edit: For the curious, here's the view tree that gets dumped out. The problematic views are the ones inside of the second block, the view with id 2131165209.
com.android.internal.policy.impl.PhoneWindow$DecorView#4057e118 -1
android.widget.FrameLayout#4057e4f0 16908290
android.widget.RelativeLayout#4057eee8 -1
android.widget.FrameLayout#4057f2e0 2131165208
android.support.v4.app.NoSaveStateFrameLayout#40594dd0 -1
android.widget.RelativeLayout#405933e0 -1
android.widget.ImageView#405937d0 -1
android.widget.Button#40593b30 2131165223
android.widget.Button#40594260 2131165224
android.widget.TextView#40594820 -1
android.widget.FrameLayout#4057f5b0 2131165209
android.support.v4.app.NoSaveStateFrameLayout#405972e0 -1
android.widget.RelativeLayout#40595588 -1
android.widget.ImageView#40595978 -1
android.widget.Button#40595cd8 -1
android.widget.Button#40596448 -1
android.widget.Button#40596b78 -1
android.widget.TextView#4057f8c8 2131165210
android.widget.FrameLayout#405805a8 2131165211
android.support.v4.app.NoSaveStateFrameLayout#40592c20 -1
android.widget.RelativeLayout#4058e1c0 -1
android.view.View#4058e5a8 2131165213
com.mycompany.MyCustomWidget#4058e7b8 -1
android.widget.TextView#40592158 2131165210
android.widget.TextView#405926c0 2131165259
android.widget.RelativeLayout#40580878 2131165212
android.view.View#40580cc8 2131165213
android.widget.FrameLayout#40580e50 2131165214
android.widget.FrameLayout#40581120 2131165215
android.widget.LinearLayout#40581440 2131165216
android.widget.FrameLayout#405816c8 2131165217
android.widget.FrameLayout#40581930 2131165218
android.widget.RelativeLayout#40581d00 -1
android.widget.Button#40582878 2131165261
android.widget.LinearLayout#40583ac0 -1
android.widget.Button#40583d48 2131165262
android.widget.Button#405846e0 2131165263
android.widget.Button#40585078 2131165264
android.widget.Button#40585a10 2131165265
android.widget.LinearLayout#405863a8 -1
android.widget.Button#40586630 2131165266
android.widget.Button#40586e50 2131165267
android.widget.Button#40587670 2131165268
android.widget.Button#40587e90 2131165269
Edit 2: Here's all of the relevant XML. First the "fragment container" layout, then the layouts of the individual fragments. Everything after the "Center, halved" comment is unused at this time, but included for completeness.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!--
Total contents:
Top bar
Bottom bar
Middle area (full screen)
Middle area (half/half, tablet only)
Middle area (40/60, tablet only)
-->
<!-- Note to self:
Why are the placeholder FrameLayouts and not just Views?
Once I get this bootstrapped I should try switching them
to Views and see if that works just as well.
-->
<!-- Top -->
<FrameLayout
android:id="#+id/top_fragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
/>
<!-- Bottom -->
<FrameLayout
android:id="#+id/bottom_fragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
<TextView
android:text="This space unintentionally left blank"
android:textColor="#FFF"
android:textSize="18sp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<!-- Center, full -->
<FrameLayout
android:id="#+id/center_fragment_full"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/top_fragment"
android:layout_above="#id/bottom_fragment"
/>
<!-- Center, halved -->
<RelativeLayout
android:id="#+id/center_fragment_half_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/top_fragment"
android:layout_above="#id/bottom_fragment"
>
<View android:id="#+id/keystone"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_height="0dip"
android:layout_width="0dip"
android:visibility="invisible"
/>
<FrameLayout
android:id="#+id/center_fragment_half_left"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_toLeftOf="#id/keystone"
/>
<FrameLayout
android:id="#+id/center_fragment_half_right"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_toRightOf="#id/keystone"
/>
</RelativeLayout>
<!-- Center, uneven split (roughly 40/60) -->
<LinearLayout
android:id="#+id/center_fragment_uneven_split_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/top_fragment"
android:layout_above="#id/bottom_fragment"
android:orientation="horizontal"
>
<FrameLayout
android:id="#+id/center_fragment_uneven_split_left"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_toLeftOf="#id/keystone"
android:layout_weight="2"
/>
<FrameLayout
android:id="#+id/center_fragment_uneven_split_right"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_toRightOf="#id/keystone"
android:layout_weight="3"
/>
</LinearLayout>
<include layout="#layout/slide_out_menus"/>
</RelativeLayout>
Here's the layout that gets substituted in for the top bar; its IDs are fine:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:layout_height="#dimen/bars"
android:layout_width="fill_parent"
android:background="#drawable/rectangle_1"
android:layout_alignParentTop="true"
/>
<Button android:id="#+id/button3"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:background="#drawable/button_3"
android:layout_marginTop="5dip"
android:layout_marginLeft="4dip"
/>
<Button android:id="#+id/button4"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:background="#drawable/button_4"
android:layout_marginTop="5dip"
android:layout_marginRight="4dip"
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="#drawable/top_bar1"
android:text="Title text"
style="#style/style_2"
android:gravity="center_horizontal|center_vertical"
android:layout_marginTop="4dip"
android:layout_marginLeft="4dip"
android:layout_marginRight="4dip"
android:layout_toRightOf="#id/button3"
android:layout_toLeftOf="#id/button4"
android:layout_alignParentTop="true"
android:scaleType="fitXY"
/>
</RelativeLayout>
Here's the layout that gets substituted in the bottom bar; its IDs are broken:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:layout_height="#dimen/bars"
android:layout_width="fill_parent"
android:background="#drawable/rectangle_1"
/>
<Button android="#+id/button1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/button_1"
android:layout_alignParentLeft="true"
android:layout_marginLeft="3dip"
android:layout_marginTop="5dip"
style="#style/style_1"
android:text="foobar"
/>
<Button android="#+id/button2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/button_2"
android:layout_alignParentRight="true"
android:layout_marginRight="3dip"
android:layout_marginTop="5dip"
style="#style/style_1"
android:text="#string/menu"
/>
<Button android="#+id/button5"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/button_5"
android:layout_centerHorizontal="true"
style="#style/style_1"
android:textSize="14dip"
android:layout_marginTop="7dip"
/>
</RelativeLayout>
And finally, here's the layout that I currently have substituted in the middle; its IDs are fine.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<View android:id="#+id/keystone"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_height="0dip"
android:layout_width="0dip"
android:visibility="invisible"
/>
<com.mycompany.MyCustomWidget
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
<TextView
android:id="#+id/current_location_label"
android:text="Current Location"
android:textColor="#FFF"
android:textSize="13sp"
android:layout_centerHorizontal="true"
android:layout_above="#id/keystone"
android:paddingBottom="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/current_location_data"
android:text="City\nSTATE"
android:textColor="#ACF"
android:textSize="22sp"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_below="#id/keystone"
android:paddingTop="0dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
I spotted the problem, and it was very much stupid user error, as expected.
android="#+id/button2"
should have been
android:id="#+id/button2"
Whoops! Thanks all, and sorry for the stupid question.
I want to create a layout in which two bars , one at top , another at bottom will be there, and one listview between these two bars, But question here is , between the top and bottom bar, i need to show list of items, where both top and bottom bar will be there in their place, only listview will be scrollable.
below is my xml layout , sorry for the lengthy question, Any suggestion will be appreciated. Thanks
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
style="#android:style/ButtonBar"
>
<ImageButton
android:id="#+id/IBADDNEW"
android:layout_width="62dp"
android:layout_height="62dp"
android:src="#drawable/ad_user_l" />
<EditText
android:id="#+id/ETSEARCHTEXT"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" >
<requestFocus />
</EditText>
<ImageButton
android:id="#+id/IBSEARCH"
android:layout_width="62dp"
android:layout_height="62dp"
android:src="#drawable/search_64" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:fillViewport="true"
>
<LinearLayout
android:id="#+id/LLforList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity ="center_vertical"
android:orientation="horizontal" >
<ImageView
android:id="#+id/IVimageIcon"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#drawable/ic_farmer" />
<TextView
android:id="#+id/TVFLNAME"
android:layout_width="match_parent"
android:layout_height="80dp"
android:text="Some Name"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</ScrollView>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/blankbarbottom"
android:layout_alignParentBottom="true" >
<LinearLayout
android:id="#+id/FLlayoutLIst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="horizontal" >
<ImageView
android:id="#+id/btnMainMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/mainmenu_on" />
<ImageView
android:id="#+id/btnSettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/settings_off" />
<ImageButton
android:id="#+id/btnExit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/exit_off" />
</LinearLayout>
</RelativeLayout>
Use a ListView for your List. It's scrollable so you don't need a ScrollView. Refer to http://developer.android.com/resources/tutorials/views/hello-listview.html on how to use a simple ListView with an Adapter.
And you should also use a TabHost for your Tabs. Have a look at http://developer.android.com/resources/tutorials/views/hello-tabwidget.html
You can create a Layout that you want to add as the Header and Footer of the ListView and then you can add them to the ListView,
View header = getLayoutInflater().inflate(R.layout.header, null);
View footer = getLayoutInflater().inflate(R.layout.footer, null);
ListView listView = getListView();
listView.addHeaderView(header);
listView.addFooterView(footer);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice,
android.R.id.text1, names));
ithink you can use listView.addheadview and listView.addrootview
edit:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_alignParentTop="true"
android:layout_height="36dip"
android:orientation="horizontal"
android:background="#drawable/cloth_xixian_bg"
android:id="#+id/headclothesroom">
<Button android:id="#+id/redianroom"
android:layout_width="100dip"
android:layout_height="36dip"
android:layout_marginLeft="40dip"
android:background="#null"
android:textSize="18sp"
android:textColor="#drawable/color_wenzi_bg"
android:text="热点排行"/>
<Button android:id="#+id/liangyiroom"
android:layout_width="100dip"
android:layout_height="36dip"
android:textColor="#drawable/color_wenzi_bg"
android:textSize="18sp"
android:background="#null"
android:layout_marginLeft="25dip"
android:text="我的靓衣间" />
<Button android:id="#+id/preButton"
android:layout_width="90dip"
android:layout_height="46dip"
android:layout_alignParentLeft="true"
android:layout_marginLeft="9dip"
android:text="前页"
android:layout_marginTop="1dip"
android:background="#drawable/btn_default"
android:textColor="#FFFFFF"
/>
<Button android:id="#+id/midchoth"
android:layout_width="90dip"
android:layout_height="46dip"
android:layout_marginLeft="12dip"
android:background="#drawable/btn_default"
android:layout_marginTop="1dip"
android:textColor="#FFFFFF"
android:visibility="invisible"
android:text="继续"
/>
<Button android:id="#+id/backbu"
android:layout_width="90dip"
android:layout_height="46dip"
android:layout_marginLeft="12dip"
android:background="#drawable/btn_default"
android:layout_marginTop="1dip"
android:textColor="#FFFFFF"
android:text="后页"
/>
</LinearLayout>
You need to Inflate the xml for ListView items. Create new xml say inflate.xml with the following code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="5dp" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:src="#drawable/icon" >
</ImageView>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" >
</TextView>
</LinearLayout>
Now in your activity class extend it with ListActivity, and do this
Create string array say "items" with multiple "First L Name"s as given in white screen like
String[] items = {"First L Name","First L Name","First L Name"};
Then,
setContentView(R.layout.main);
setListAdapter(new NewListAdapter(this));
Extend NewListAdapter class with any Adapter like ArrayAdapter and do this
class NewListAdapter extends ArrayAdapter {
Activity context;
NewListAdapter(Activity context) {
super(context, R.layout.inflate,items);
this.context=context;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View row = inflater.inflate(R.layout.inflate, null);
TextView lblName = (TextView) row.findViewById(R.id.textView1);
lblName.setText(items[position]);
return (row);
}
}
Now run your code and see the magic...
I did it my self here is the exact solution for the cause,
in layour i have added one listView
<ListView
android:id="#+id/LVfarmerList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
in place of
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:fillViewport="true"
>
<LinearLayout
android:id="#+id/LLforList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity ="center_vertical"
android:orientation="horizontal" >
<ImageView
android:id="#+id/IVimageIcon"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#drawable/ic_farmer" />
<TextView
android:id="#+id/TVFLNAME"
android:layout_width="match_parent"
android:layout_height="80dp"
android:text="Some Name"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</ScrollView>
and created one more layout which is
<LinearLayout
android:id="#+id/LLforList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity ="center_vertical"
android:orientation="horizontal" >
<ImageView
android:id="#+id/IVimageIcon"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#drawable/ic_farmer" />
<TextView
android:id="#+id/TVFLNAME"
android:layout_width="match_parent"
android:layout_height="80dp"
android:text="Some Name"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
now created one Activity(not listActivity)
in which i showed the layout, which have listview between top and bottom bar.
and created a customAdapter by using another layout, which which have one imageview and textview.
by listview.setAdapter , added this adapter into list view, and it worked for me.
I'm currently developing an Android app and I've got an issue concerning layout. In my XML layout file, I create a relative layout (fill_parent for width and wrap_content for height) which contains a list View (or possibly an expendable List View - fill_parent for height and width). In the corresponding activity onCreate method, I just set the adapter corresponding to my list and attach it to my list. The problem is that when I run my app the relative layout only has a size of 93dip and so it hides my list which has a size from 67 to 210dip when I would like the relative layout height to stick to the list current size (The size will never change through action in the activity just possibly when creating or resuming the activity). Would anyone have an idea of a solution to fix that ?
Some code as asked :
<LinearLayout android:id="#+id/content"
android:layout_width="fill_parent"
android:background="#drawable/row"
android:layout_below="#+id/header"
android:layout_height="wrap_content">
<ListView android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/listrow"/>
</LinearLayout>
And in the java file (where snapRecords is my object lists):
m_panel = (RelativeLayout)findViewById(R.id.lastFareRecordPanel);
ListView lv = (ListView)m_panel.findViewById(R.id.list);
final QuickSnapBookmarksAdapter adapter = new QuickSnapBookmarksAdapter(snapRecords, getApplicationContext(), this, lv);
lv.setAdapter(adapter);
Many thanks in advance for your help !
Benja
I had a similiar situation.When I changed parent layout into LinearLayout, it is working.Here is the code:
<?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:background="#FFFFFF"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/LinearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageButton
android:id="#+id/prev"
android:layout_width="106dip"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="#null"
android:onClick="prev"
android:paddingTop="8dip"
android:src="#drawable/previous" >
</ImageButton>
<ImageButton
android:id="#+id/next"
android:layout_width="106dip"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="#null"
android:onClick="next"
android:paddingTop="8dip"
android:src="#drawable/next" >
</ImageButton>
<ImageButton
android:id="#+id/Logout"
android:layout_width="106dip"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="#null"
android:onClick="logout"
android:paddingTop="8dip"
android:src="#drawable/logout1"
android:text="#string/Logout" >
</ImageButton>
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="330dip" >
</ListView>
<TextView
android:id="#android:id/empty"
android:layout_width="fill_parent"
android:layout_height="338dip"
android:text="#string/EmptyList"
android:textSize="35dip" />
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageButton
android:id="#+id/prev1"
android:layout_width="106dip"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="#null"
android:onClick="prev"
android:src="#drawable/previous" >
</ImageButton>
<ImageButton
android:id="#+id/next1"
android:layout_width="106dip"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="#null"
android:onClick="next"
android:src="#drawable/next" >
</ImageButton>
<ImageButton
android:id="#+id/Logout"
android:layout_width="106dip"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="#null"
android:onClick="logout"
android:src="#drawable/logout1"
android:text="#string/Logout" >
</ImageButton>
</LinearLayout>
</LinearLayout>
had make layout for showing the content in listview and a button below on list view for displaying more information on list view ,so first i am showing only 4 rows in list view and when i next time click on button 4 rows again added in list so now my button goes down in screen so how to make whole layout scroll able so that i can go to button via scrolling my layout is..
listplacehold.xml is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#1A77BD"
>
<ListView
android:id="#+id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawSelectorOnTop="false" />
<View
android:id="#+id/horizontolline"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:layout_below="#id/android:list"
android:background="#000000"
/>
<Button
android:text="Load More..."
android:textColor="#color/white"
android:id="#+id/keywordsearchbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/horizontolline"
android:drawableLeft="#drawable/load_more_off"
android:gravity="left|center_vertical"
android:background="#1A77BD"
/>
</RelativeLayout>
and for list item layout is: listitem.xml is
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView android:id="#+id/img"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<!-- Location where barrio items will be displayed -->
<TextView
android:id="#+id/reportid"
android:layout_marginLeft="70dip"
android:text="reporet id"
android:textColor="#color/white"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<TextView android:id="#+id/status"
android:textColor="#color/white"
android:text="status"
android:layout_marginTop="40dip"
android:layout_marginLeft="200dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="#+id/title"
android:text="title"
android:layout_marginLeft="150dip"
android:textColor="#color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/date"
android:text="dateeeeeee"
android:layout_marginLeft="70dip"
android:layout_marginTop="40dip"
android:textColor="#color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
items are dynamically loadein list view when button click
i think that scroll will be set on above xml means on "listplacehold.xml" but how?,when i am doing it is set to for half screen,means it is not displaying in whole view... Thanks for help in advance.
don't use the ScrollView with the combination of the ListView. Instead make your layout like this.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#1A77BD" >
<Button
android:id="#+id/lodmore"
android:text="Load More..."
android:textColor="#color/white"
android:id="#+id/keywordsearchbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true""
android:drawableLeft="#drawable/load_more_off"
android:gravity="left|center_vertical"
android:background="#1A77BD"/>
<View
android:id="#+id/horizontolline"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:layout_above="#id/loadmore"
android:background="#000000"/>
<ListView
android:id="#+id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_alignParentTop="true"
android:layout_above="#id/horizontolline/>
</RelativeLayout>
put scrollview over your root layout and remember only one layout node is allowed under scrollview.....
refer this doc:
http://developer.android.com/reference/android/widget/ScrollView.html