I'm trying to make a list of LinearLayout become VISIBLE at a click on a "header" LinearLayout.
<LinearLayout android:id="#+id/sample_title" ...>
<TextView ... />
</LinearLayout>
<LinearLayout
android:id="#+id/sample_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:duplicateParentState="true"
android:orientation="horizontal"
android:visibility="visible" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="left"
android:text="Sample text 1"
android:textColor="#color/white" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:duplicateParentState="true"
android:orientation="horizontal"
android:visibility="visible" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="left"
android:text="Sample text 2"
android:textColor="#color/white" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
</LinearLayout>
Programmatically:
mSampleTitle = (LinearLayout) mView.findViewById(R.id.sample_title);
mSampleTitle.setOnClickListener(this);
mSampleContent = (LinearLayout) mView.findViewById(R.id.sample_content);
[...]
public void onClick(View v) {
if (v == mSampleTitle) {
mSampleContent.setVisibility(mSampleContent.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
}
}
It works fine when there is only one TextView to show, but as soon as there is more, only the first one becomes visible and all the rest is just blank space.
Thank you
My bad, stupid error.
I forgot the android:orientation="vertical". The code changing the VISIBILITY works fine.
<LinearLayout
android:id="#+id/sample_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" >
<LinearLayout
android:id="#+id/sample_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone" >
I still find weird the fact that they were so much blank space below that LinearLayout horizontal...
Related
Hi I have made layout with couple of ImageView and TextView elements due to reduction of code duplication.
I have occurred to two problems:
I need to update these elements inside of layout using data binding, but I don't know how can I access to them in order to update images and texts?
The visibility of layout should be bind from VM object(default visibility is GONE) but it doesn't work. Even if I update VM setter for visibility, the layout is till visible on the screen.
The code:
included layout:
<?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">
<LinearLayout
android:id="#+id/details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#FF0000">
<ImageView
android:id="#+id/image_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/text_details"
android:text="details"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/favorites"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#00FF00">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="details"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/record"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#0000FF">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="details"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/remind_me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#FF0000">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="details"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/return_to_begin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#00FF00">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="details"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
activity layout(this is a snippet, the whole xml is too large)
<include layout="#layout/manu_layout"
android:layout_above="#+id/info"
android:layout_alignParentRight="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:visibility="#{vm.infoMenu}"/>
viewModel method for visibility:
public void setInfoMenu() {
if(menuVisibility == View.VISIBLE){
menuVisibility = View.GONE;
setMediaControlView(false);
}else {
menuVisibility=View.VISIBLE;
setMediaControlView(true);
}
notifyPropertyChanged(BR.infoMenu);
}
#Bindable
public int getInfoMenu(){
return menuVisibility;
}
Please let me know if you need any additional code snippet.
Can you please help me to solve these problems?
Thanks,
Hogar
Make sure you have <layout> as a root tag of your xml file if you want to use DataBinding.
You can have a id of your <include> and acceess controls of included layout via it. For example you have a id as 'included' of your <include> then you can access it as binding.included.details or binding.included.favourite
Currently working with listview which contains edittext in each row.
The row item xml is below
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/result_solution_list_row_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/demo_product_background"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/artist_name_textview"
android:layout_width="#dimen/twohundred"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/home_dark_blue"
android:textSize="#dimen/list_text_size"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="#+id/contentLinearLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:descendantFocusability="afterDescendants"
android:orientation="vertical" >
</LinearLayout>
<TextView
android:id="#+id/add_product"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_margin="2dp"
android:textColor="#color/home_dark_blue"
android:textSize="#dimen/list_text_size"
android:textStyle="bold" />
<ImageView
android:id="#+id/img_camera"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_margin="2dp"
android:background="#drawable/ic_pre_camera"
android:contentDescription="#string/app_name" />
<ImageView
android:id="#+id/drag_handle"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_gravity="center_vertical"
android:layout_margin="2dp"
android:background="#drawable/drag_icon"
android:contentDescription="#string/app_name" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="#color/cus_container" >
</LinearLayout>
In this xml, LinearLayout - contentLinearLayout can be inflated a view dynamically.
The inflating view (xml) contains the EditText views.
The following is the inflated xml .
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingLeft="#dimen/five"
android:paddingRight="#dimen/five"
>
<TextView
android:id="#+id/artist_name_textview"
android:layout_width="#dimen/hundreadfifty"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/home_dark_blue" />
<LinearLayout
android:layout_width="#dimen/home_hundred"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/home_five"
android:layout_marginRight="#dimen/home_five"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/txt_name_quan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:singleLine="true"
android:text="QUANTITY"
android:textSize="#dimen/list_text_size"
android:textColor="#color/home_dark_blue" />
<EditText
android:id="#+id/txt_quan"
android:layout_width="#dimen/home_eighty"
android:layout_height="match_parent"
android:background="#drawable/edt_blue_background"
android:inputType="numberDecimal"
android:singleLine="true"
android:textColor="#color/home_dark_blue" />
</LinearLayout>
<LinearLayout
android:layout_width="#dimen/nav_drawer_uf_image_width"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/txt_name_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:singleLine="true"
android:text="PRICE"
android:textStyle="bold"
android:textSize="#dimen/list_text_size"
android:textColor="#color/home_dark_blue" />
<EditText
android:id="#+id/txt_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/edt_blue_background"
android:inputType="numberDecimal"
android:singleLine="true"
android:textColor="#color/home_dark_blue" />
</LinearLayout>
My problem is , I want both events. 1. ListView's OnItemClickListener 2. EditText should be editable.
I have tried the solutions posted in below links
Trying to catch a click on android ListView item: android:descendantFocusability="blocksDescendants" not working
Android EditText in ListView - keyboard
Focusable EditText inside ListView
http://androidtechnicalblog.blogspot.in/2014/04/quick-trick-of-week-edittext-inside.html
Most of the solutions stressed on android:descendantFocusability attribute.I have tried with different values and set to the listview and listview's root layout alone.
Still the EditTexts are not editable.
Any solutions ?
I have done this with the Imageview earlier.
At that time i put the click listner of Imageview at the Adapter.
I am not sure but may be this will help you.
set this at your adapter
viewHolder.txt_price.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Your Code
}
});
your ListView click event
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// your code
}
});
Not Sure might be help You. Good Luck
I have a CustomListView, when I longClick on a row, the layout of that row changes to a different listRow Layout. So far, everything is working, however, the layout of the new view is compressed. Here are my xml files:
search_result_layout.xml --> this is the original xml file used by the customListView Adapter.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/search_result_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:orientation="horizontal"
android:padding="1dip" >
<TextView
android:id="#+id/dialogItemName"
android:layout_width="0dp"
android:layout_weight="0.54"
android:layout_marginRight="5dp"
android:layout_height="match_parent"
android:text="Item Name"
android:gravity="center"
android:textSize="10pt" />
<TextView
android:id="#+id/price"
android:layout_width="0dp"
android:layout_weight="0.14"
android:layout_height="match_parent"
android:text="Price"
android:gravity="center"
android:textSize="10pt" />
<TextView
android:id="#+id/discount"
android:layout_width="0dp"
android:layout_weight="0.10"
android:layout_height="match_parent"
android:text="Discount"
android:gravity="center"
android:textSize="10pt" />
<TextView
android:id="#+id/qtyInput"
android:layout_width="0dp"
android:layout_weight="0.14"
android:layout_height="match_parent"
android:text="qtyInput"
android:gravity="center"
android:textSize="10pt" />
<TextView
android:id="#+id/discInput"
android:layout_width="0dp"
android:layout_weight="0.11"
android:layout_height="match_parent"
android:text="discInput"
android:gravity="center"
android:textSize="10pt" />
</LinearLayout>
Here is my search_result_inflate.xml file
<?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="wrap_content"
android:minHeight="50dp"
android:orientation="horizontal"
android:padding="1dip" >
<TextView
android:id="#+id/dialogItemName"
android:layout_width="0dp"
android:layout_weight="0.54"
android:layout_marginRight="5dp"
android:layout_height="match_parent"
android:text="Item Name"
android:gravity="center"
android:textSize="10pt" />
<TextView
android:id="#+id/price"
android:layout_width="0dp"
android:layout_weight="0.14"
android:layout_height="match_parent"
android:text="Price"
android:gravity="center"
android:textSize="10pt" />
<TextView
android:id="#+id/discount"
android:layout_width="0dp"
android:layout_weight="0.10"
android:layout_height="match_parent"
android:text="Discount"
android:gravity="center"
android:textSize="10pt" />
<EditText
android:id="#+id/qtyInput"
android:layout_width="0dp"
android:layout_weight="0.14"
android:layout_height="match_parent"
android:hint="qtyInput"
android:inputType="number"
android:gravity="center"
android:textSize="10pt" />
<EditText
android:id="#+id/discInput"
android:layout_width="0dp"
android:layout_weight="0.11"
android:layout_height="match_parent"
android:hint="discInput"
android:inputType="number|date"
android:gravity="center"
android:textSize="10pt" />
</LinearLayout>
The two files are almost identical, only that the last two TextViews become EditTexts instead so the user can input the values he or she wants.
Also, kindly notice that I placed weight in my textViews and EditTexts, so that the spaces are maximized when the user shifts the view from portrait to landscape.
resultListView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
TextView textViewQuantity = (TextView)view.findViewById(R.id.qtyInput);
TextView textViewDiscountReq = (TextView)view.findViewById(R.id.discInput);
TextView textViewName = (TextView)view.findViewById(R.id.dialogItemName);
TextView textViewPrice = (TextView)view.findViewById(R.id.price);
TextView textViewDiscount = (TextView)view.findViewById(R.id.discount);
textViewQuantity.setVisibility(View.GONE);
textViewDiscountReq.setVisibility(View.GONE);
textViewName.setVisibility(View.GONE);
textViewPrice.setVisibility(View.GONE);
textViewDiscount.setVisibility(View.GONE);
LinearLayout ll_inflate = (LinearLayout)view.findViewById(R.id.search_result_layout);
Log.d("Angelo", "original width = " + ll_inflate.getWidth());
View child = getLayoutInflater().inflate(R.layout.search_result_inflate, null);
TextView newText = (TextView)child.findViewById(R.id.qtyInput);
newText.setText(textViewQuantity.getText().toString());
EditText enter_txt = (EditText)child.findViewById(R.id.qtyInput);
ll_inflate.addView(child);
Log.d("Angelo", "not original width = " + ll_inflate.getWidth());
return false;
}
});
When the I longClick an item, the layout changes. However, the TextViews and the EditTexts are all next to one another, as if the weight property was ignored. At first I thought that the width might have changed before and after the view was changed, but the Log told me it had 1136 width (I'm using a 2nd Gen Nexus 7). Now I'm blank, the width of the parent didn't change but the weight value was ignored. How do I fix this? Would assigning the weight programmatically a viable solution?
Also, follow up question, I only want the last 2 TextViews to change to EditTexts. So what I try to do to achieve that effect is that from the original layout, I pull the values of the first 3 fields (Item Name, Price, Discount) by doing textView.getText().toString() and get the textviews from the new layout and use setText() but so far, it did not work, any suggestions?
UPDATE:
Upon reading this link, I think that the Weight property of the TextViews inside the LinearLayout is being ignored when I use the LinearLayout as my ListView Row View. Nesting another Linear Layout did not help much either.
I placed another LinearLayout inside as such:
<?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="wrap_content"
android:minHeight="50dp"
android:orientation="horizontal"
android:padding="1dip" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="1dip" >
<TextView
android:id="#+id/dialogItemName"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:layout_weight="0.70"
android:gravity="center"
android:text="Item Name"
android:textSize="23sp" />
<TextView
android:id="#+id/price"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.16"
android:gravity="center"
android:text="Price"
android:textSize="23sp" />
<TextView
android:id="#+id/discount"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.12"
android:gravity="center"
android:text="Discount"
android:textSize="23sp" />
<EditText
android:id="#+id/qtyInput"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.16"
android:gravity="center"
android:hint="qtyInput"
android:inputType="number"
android:textSize="23sp" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/discInput"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.13"
android:gravity="center"
android:hint="discInput"
android:inputType="number|date"
android:textSize="23sp" />
</LinearLayout>
</LinearLayout>
Any ideas?
Call instead:
ll_inflate.addView(child, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
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.
I have pretty simple dialog layout and added scrollview because dialog can be quite long and not fit into the screen
When I initially run the dialog (it fits the screen) everything is fine, then I change device orientation to landscape, dialog does not fit to screen anymore, scrolling appears, but several top elements (up to #+id/addr_label) become invisible though some empty space appears between Title and first visible element exist. The most confusing thing is- when I change device back to portrait view- these elements are still invisible.
UPDATE: Just checked the case when phone is initially in landscape mode- in this case all elements are shown correctly. when I change it back to portrait- the elements disappear. seems to be a "change orientation" bug
What can be a problem?
Layout and screenshots are below (Android is 2.3.5)
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root22" android:orientation="vertical"
android:layout_width="wrap_content" android:layout_height="fill_parent"
android:padding="0dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:padding="10dp">
<TextView android:id="#+id/details_ssid"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text=""
android:layout_marginLeft="5dip"/>
<TextView android:id="#+id/details_bssid"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text=""
android:layout_weight="1"
android:layout_marginLeft="5dip"/>
<TextView android:id="#+id/latitude"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text=""
android:layout_weight="1"
android:layout_marginLeft="5dip"/>
<TextView android:id="#+id/longitude"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text=""
android:layout_weight="1"
android:layout_marginLeft="5dip"/>
<TextView android:id="#+id/addr_label"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="#string/address_label"
android:layout_weight="1"
android:layout_marginLeft="5dip"/>
<EditText android:id="#+id/address"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text=""
android:layout_weight="1"
android:layout_marginLeft="5dip"/>
<TextView android:id="#+id/descr_label"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="#string/description_label"
android:layout_weight="1"
android:layout_marginLeft="5dip"/>
<EditText android:id="#+id/description"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text=""
android:layout_weight="1"
android:layout_marginLeft="5dip"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root" android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:padding="10dp">
<Button
android:id="#+id/yes"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginTop="10dip"
android:layout_marginRight="10dip"
android:text= "#string/Yes"
android:visibility="visible"
/>
<Button
android:id="#+id/no"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginTop="10dip"
android:layout_marginRight="10dip"
android:text= "#string/No"
android:visibility="visible"
/>
</LinearLayout>
</LinearLayout>
</ScrollView>
Code is pretty boring:
onCreateDialog in Activity which sets layout and
onPrepareDialog whic sets values to all these fields
onCreateDialog
case CONFIRM_SHARE_AP:
{
final Dialog dialog = new Dialog(this){
public void onBackPressed()
{
dismissDialog(CONFIRM_SHARE_AP);
}
};
dialog.setContentView(R.layout.ap_add_confirmation);
dialog.setTitle(R.string.confirm_share);
Button cancel = (Button) dialog.findViewById(R.id.no);
cancel.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
dismissDialog(CONFIRM_SHARE_AP);
}
});
return dialog;
}
onPrepareDialog
has nothing specific at all
http://img525.imageshack.us/img525/4642/devicev1.png
http://img13.imageshack.us/img13/985/devicev2.png
Found a root of the prob.
This is because during orientation change activity is restarted (by default) and therefore dialogs are recreated.