I have had a big trouble since several hours;
I have a recyclerview with several elements in each item. For one of them (lv_container), I can decrease its height but not increase it.
See the screenshot:
the xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/dashboard_part_medical_file_locked_title"
android:textSize="18sp"
android:textStyle="bold" />
<RelativeLayout
android:id="#+id/main_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/row_dashboard_color"
android:orientation="horizontal"
android:padding="10dp">
<ImageView
android:id="#+id/dashboard_part_image"
android:layout_width="80dp"
android:layout_height="95dp"
android:layout_alignParentEnd="true"
android:paddingBottom="20dp"
android:paddingEnd="10dp"
android:paddingStart="10dp"
android:paddingTop="20dp"
android:src="#drawable/dossier_medical" />
<RelativeLayout
android:id="#+id/lv_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/dashboard_part_image"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/dashboard_part_image"
android:layout_toStartOf="#+id/dashboard_part_image"
android:background="#android:color/white"
android:padding="10dp">
<ImageView
android:id="#+id/sup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_sup" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_toStartOf="#id/sup"
android:text="#string/dashboard_part_medical_file_inside" />
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
and the code of the viewholder
DisplayMetrics metrics = pContext.getResources().getDisplayMetrics();
RelativeLayout.LayoutParams lvContainerParams = (RelativeLayout.LayoutParams) lvContainer.getLayoutParams();
lvContainerParams.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
lvContainerParams.height = (int) (40 * metrics.density + 20 * metrics.density);
lvContainer.setLayoutParams(lvContainerParams);
ViewGroup.LayoutParams mainContainerParams = mainContainer.getLayoutParams();
mainContainerParams.height = (int) (lvContainerParams.height + 20 * metrics.density);
mainContainer.setLayoutParams(mainContainerParams);
ViewGroup.LayoutParams params = itemView.getLayoutParams();
params.height = (int) (mainContainerParams.height + 50 * metrics.density);
itemView.setLayoutParams(params);
When I change the height of lvContainer (the white container), the height of mainContainer is well modified (as you can see with the blue container), but the white container always remains fixed. However, in debug, heights are well calculated.
I tried to call lvContainer.invalidate() and/or lvContainer.requestLayout(), but nothing changed.
What's the error? Why the height of item and mainContainer can be easily modified, but not this of lvContainer?
I need your help
Thanks
Try removing this line
android:layout_alignBottom="#+id/dashboard_part_image"
Related
I am using the below function to set the height of my ListView dynamically based on the number of items in the ListView.
public boolean setListViewHeightBasedOnItems(ListView listView, String select) {
MyListAdapter adapter = (MyListAdapter) listView.getAdapter();
if (adapter != null) {
int numberOfItems = adapter.getCount();
// Get total height of all items.
int totalItemsHeight = 0;
for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
View item = adapter.getView(itemPos, null, listView);
float px = 300 * (listView.getResources().getDisplayMetrics().density);
item.measure(View.MeasureSpec.makeMeasureSpec((int) px, View.MeasureSpec.AT_MOST), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
totalItemsHeight += item.getMeasuredHeight();
}
// Get total height of all item dividers.
int totalDividersHeight = listView.getDividerHeight() *
(numberOfItems - 1);
// Get padding
int totalPadding = listView.getPaddingTop() + listView.getPaddingBottom();
// Set list height.
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalItemsHeight + totalDividersHeight + totalPadding; // +50
listView.setLayoutParams(params);
listView.requestLayout();
return true;
} else {
return false;
}
}
The above function works well on the device with there screen density mentioned below along with the OS.
Moto E3 Power (Android 6.0, 720 x 1280 pixels, 5.0 inch screen).
Lenovo A6010 (Android 5.0.2, 720 x 1280 pixels, 5.0 inch screen).
Samsung Tablet SM T113 (Android 4.4.4, 600 x 1024 pixels, 7.0 inch
screen).
But, When I run the same on the Device with Android Nougat (7.0, 7.1.1), I get empty spaces below the last item of my ListView. That is, the height is set more than required.
The below screen shot explains what I am facing.
The Devices which are giving this issue are listed below:-
Moto E4 Plus (Android - 7.1.1, 720 x 1280 pixels, 5.5 inch screen).
Moto E5 Plus (Android - 7.0, 1080 x 1920 pixels, 5.2 inch screen).
Below is my XMl declaration of the ListView.
<ListView
android:id="#+id/lv_menu_details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/dp10"
android:layout_marginRight="#dimen/dp10"
android:divider="#color/transparent" />
Can anyone help me to resolve the issue I am facing.
Thanks in advance.
I Got the solution to the issue I was facing.
For Me the solution lied in the item list of the adapter i was using for my List View.
I was using LinearLayout with weight and to the widgets inside the LinearLayout I was giving weightsum along with some margin to the Views.
Now as the entire view was divided by weightsum views the additional margin was creating those white space at the bottom of my ListView Attributes as shown in the question posted. I replace the LinearLayout with the RelativeLayout and removed the weight attribute from the ViewGroup and weightSum attributes from my View.
And it has worked for me.
For those who are facing the same issue I am posting the xml file below for the reference:
My Old item_list.xml file Used for ListView item UI:
<?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/white"
android:orientation="horizontal"
android:paddingLeft="#dimen/dp10"
android:paddingTop="#dimen/dp10"
android:paddingBottom="#dimen/dp10"
android:paddingRight="#dimen/dp5"
android:weightSum="5">
<CheckBox
android:id="#+id/iv_checkbox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:button="#drawable/selector_checkbox_menu_detail_item"
android:textColor="#color/white"
android:textSize="#dimen/sp18" />
<TextView
android:id="#+id/tv_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2.3"
android:text="dsfdsfds"
android:textColor="#color/black"
android:textSize="#dimen/sp15" />
<TextView
android:id="#+id/tv_price"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:text="dsfdsfds"
android:textColor="#color/black"
android:layout_marginRight="#dimen/dp10"
android:textSize="#dimen/sp15" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/layout_add_sub"
android:layout_weight="1.5"
android:orientation="horizontal"
android:weightSum="3">
<ImageView
android:id="#+id/iv_subtract"
android:layout_width="#dimen/dp30"
android:layout_height="#dimen/dp25"
android:layout_weight="1"
android:background="#drawable/button_shape"
android:padding="#dimen/dp5"
android:src="#drawable/minus" />
<TextView
android:id="#+id/tv_count"
android:layout_width="#dimen/dp30"
android:layout_height="#dimen/dp25"
android:layout_gravity="center"
android:layout_weight="1"
android:background="#drawable/button_shape"
android:gravity="center"
android:text="1"
android:textColor="#color/black" />
<ImageView
android:id="#+id/iv_add"
android:layout_width="#dimen/dp30"
android:layout_height="#dimen/dp25"
android:layout_marginRight="#dimen/dp5"
android:layout_weight="1"
android:background="#drawable/button_shape"
android:padding="#dimen/dp5"
android:src="#drawable/add" />
</LinearLayout>
</LinearLayout>
My list_item.xml file where I replaced the LinearLayout with RelativeLayout and without using the weight and weightSum Attributes:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/app_background_color"
android:orientation="horizontal"
android:paddingTop="#dimen/dp10"
android:paddingBottom="#dimen/dp10">
<CheckBox
android:id="#+id/iv_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/dp5"
style="#style/CheckBoxStyle"
android:layout_marginRight="#dimen/dp5"
android:textColor="#color/app_text_color_primary"
android:textSize="#dimen/sp18" />
<TextView
android:id="#+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/iv_checkbox"
android:layout_toLeftOf="#+id/liLayRightPanel"
android:text="dsfdsfd"
android:layout_centerVertical="true"
android:layout_marginRight="#dimen/dp10"
android:textColor="#color/app_text_color_primary"
android:textSize="#dimen/sp15" />
<LinearLayout
android:id="#+id/liLayRightPanel"
android:orientation="horizontal"
android:layout_alignParentRight="true"
android:layout_marginRight="#dimen/dp5"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dsfdsfd"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#color/app_text_color_primary"
android:layout_marginRight="#dimen/dp10"
android:textSize="#dimen/sp15" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/layout_add_sub"
android:layout_weight="3"
android:orientation="horizontal">
<ImageView
android:id="#+id/iv_subtract"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/circle_theme_bkgnd"
android:padding="#dimen/dp5"
android:src="#drawable/minus" />
<TextView
android:id="#+id/tv_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/dp5"
android:layout_marginRight="#dimen/dp5"
android:textSize="#dimen/sp16"
android:textStyle="bold"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="0"
android:textColor="#color/app_text_color_primary" />
<ImageView
android:id="#+id/iv_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/circle_theme_bkgnd"
android:padding="#dimen/dp5"
android:src="#drawable/add" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Hope My Answer can help those who are facing the same issue...
i would like to know why my fragment layout has different dimensions when I run the app on different devices. I thought using dp was enough, but now I think i've missed something.
For istance, on my Nexus 6P it looks fine, but in the AVD Emulator's Nexus 4 it looks smaller
Layout code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="#+id/deleteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|left"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:background="#color/colorPrimaryLight"
android:text="#string/delete_button" />
<Button
android:id="#+id/moveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:layout_toRightOf="#id/deleteButton"
android:background="#color/colorPrimaryLight"
android:text="#string/move_button" />
<Button
android:id="#+id/copyButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/deleteButton"
android:layout_gravity="center"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:background="#color/colorPrimaryLight"
android:text="#string/copy_button" />
<Button
android:id="#+id/renameButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/moveButton"
android:layout_gravity="center"
android:layout_margin="15dp"
android:layout_toRightOf="#id/copyButton"
android:background="#color/colorPrimaryLight"
android:text="#string/rename_button" />
</RelativeLayout>
EDIT: I forgot to write the code where I set the dialogFragment size.
/**
* Setting Dialog size
*/
Window window = getDialog().getWindow();
Point size = new Point();
// Store dimensions of the screen in `size`
Display display = window.getWindowManager().getDefaultDisplay();
display.getSize(size);
window.setLayout((int) (size.y * 0.40), (int) (size.x * 0.60));
window.setGravity(Gravity.CENTER);
Most of your elements width and height seems to wrap_content, so i would check the Font configured in your emulator by default, if its size is smaller, the whole content will be small.
I have the following ListView-item:
Currently everything works as intended and is placed where it should, except for the width of the TextView (2).
Since I use a RelativeLayout with some wrap_contents, I use a onGlobalLayoutListener so I can access the MeasuredWidths and Heights the moment the View is done loading and rendering. With the TextView2 however I get some weird results when I debug.
The first time onGlobalLayout is called, measuredWidth of the TextView2 is what it should be (375 px). The second time however, it's 48 px (same as the Height) and when I look at the fields of the TextView2 it says: mMeasuredHeight: 375; mMeasuredWidth: 48 :S
My layout is at the bottom of this post. My onGlobalLayoutListener is:
if(view.getViewTreeObserver().isAlive()){
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
// This will be called once the layout is finished, prior to displaying it
// So we can change some widths and heights based on other View-Elements that are filled now
// (We couldn't do this in the XML itself since they weren't filled yet and we didn't knew the sizes yet.)
#Override
public void onGlobalLayout() {
if(holder != null){
...
// Change the height of the ProductName-TextView to match the Image and leave the width as is
int height = h.imageView.getMeasuredHeight();
int width = h.tvName.getMeasuredWidth();
* h.tvName.setLayoutParams(new RelativeLayout.LayoutParams(h.imageView.getMeasuredHeight(), h.tvName.getMeasuredWidth()));
...
}
// Since we don't want onGlobalLayout to continue forever, we remove the Listener here again.
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
}
The * is a breakpoint. The first time width is 48 and height is 375. The second time width is 48 and height is 48, and if I look at the mMeasuredHeight and mMeasuredWidth fields in my holder.textView2, they are h=375 and w=48 :S
Here is my layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/item_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants">
<LinearLayout
android:id="#+id/left_ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentLeft="true"
android:layout_marginTop="#dimen/default_margin"
android:layout_marginLeft="#dimen/default_margin"
android:layout_marginBottom="#dimen/default_margin">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="#layout/transparent_background"
android:contentDescription="#string/checkbox_content_description"
android:src="#drawable/checkbox_unchecked" />
<Space
android:id="#+id/filler_space_image"
android:layout_width="1dp"
android:layout_height="1dp"
android:visibility="gone" />
</LinearLayout>
<TextView
android:id="#+id/tv_product_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/left_ll"
android:layout_toLeftOf="#+id/right_ll"
android:ellipsize="end"
android:singleLine="true"
android:gravity="center_vertical"
android:layout_marginTop="#dimen/default_margin"
android:layout_marginLeft="#dimen/default_margin"
android:layout_marginBottom="#dimen/default_margin" />
<EditText
android:id="#+id/et_result_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tv_product_name"
android:layout_toRightOf="#id/left_ll"
android:inputType="number"
android:layout_marginLeft="#dimen/default_margin"
android:layout_marginBottom="#dimen/default_margin"
android:visibility="gone" />
<AutoCompleteTextView
android:id="#+id/actv_result_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/right_ll"
android:layout_toRightOf="#id/et_result_amount"
android:layout_below="#+id/tv_product_name"
android:ellipsize="end"
android:inputType="text"
android:singleLine="true"
android:visibility="gone" />
<TextView
android:id="#+id/tv_tags"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/et_result_amount"
android:layout_toRightOf="#id/left_ll"
android:text="#string/tags"
android:gravity="center"
android:layout_marginLeft="#dimen/default_margin"
android:layout_marginBottom="#dimen/default_margin"
android:visibility="gone" />
<Spinner
android:id="#+id/sp_tags"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/actv_result_name"
android:layout_toRightOf="#id/tv_tags"
android:layout_toLeftOf="#id/right_ll"
android:layout_marginLeft="#dimen/default_margin"
android:layout_marginBottom="#dimen/default_margin"
android:visibility="gone" />
<LinearLayout
android:id="#id/right_ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:orientation="vertical"
android:layout_margin="#dimen/default_margin">
<TextView
android:id="#+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical" />
<Space
android:id="#+id/filler_space_price"
android:layout_width="1dp"
android:layout_height="1dp"
android:visibility="gone" />
<ImageButton
android:id="#+id/btn_tags"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#android:drawable/ic_menu_manage"
android:contentDescription="#string/button_tags_content_description"
android:background="#layout/transparent_background"
android:visibility="gone" />
</LinearLayout>
</RelativeLayout>
Ok, it was (after I figured it out) pretty obvious.. Instead of
h.tvName.setLayoutParams(new RelativeLayout.LayoutParams(
h.imageView.getMeasuredHeight(), h.tvName.getMeasuredWidth()));
I now use
h.tvName.setLayoutParams(new RelativeLayout.LayoutParams(
h.tvName.getMeasuredWidth(), hh.imageView.getMeasuredHeight()));
Difference you ask? I used LayoutParams(height, width) instead of LayoutParams(width, height)...
I've got a ListActivity with a custom Adapter. This adapter uses the following list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/item_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/ll1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/default_margin"
android:contentDescription="#string/checkbox_content_description"
android:src="#drawable/checkbox_unchecked"
android:background="#layout/transparent_background"
android:focusableInTouchMode="false"
android:adjustViewBounds="true"
android:clickable="false"
android:focusable="false" />
<TextView
android:id="#+id/tv_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/default_margin"
android:layout_marginTop="#dimen/default_margin"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:textIsSelectable="false" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:layout_marginRight="#dimen/default_margin"
android:layout_marginTop="#dimen/default_margin">
<TextView
android:id="#+id/tv_product_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="end"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:textIsSelectable="false" />
</LinearLayout>
<TextView
android:id="#+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/default_margin"
android:layout_marginTop="#dimen/default_margin"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:textIsSelectable="false" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll2"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_below="#id/ll1">
<Space
android:id="#+id/filler_space"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/default_margin"
android:layout_marginRight="#dimen/default_margin"
android:layout_marginBottom="#dimen/default_margin"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false" />
<EditText
android:id="#+id/et_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/default_margin"
android:layout_marginBottom="#dimen/default_margin"
android:inputType="number" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:layout_marginRight="#dimen/default_margin"
android:layout_marginBottom="#dimen/default_margin"
android:padding="0dp">
<AutoCompleteTextView
android:id="#+id/actv_search_product"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:singleLine="true"
android:ellipsize="end"
android:inputType="text" />
</LinearLayout>
<EditText
android:id="#+id/et_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/default_margin"
android:layout_marginBottom="#dimen/default_margin"
android:inputType="numberDecimal"
android:digits="0123456789.,-" />
</LinearLayout>
</RelativeLayout>
In the Adapter's getView-method I've added the following piece of code:
// Change the width of the Filler-Space to match the Image
// and leave the height as is
Space space = (Space) view.findViewById(R.id.filler_space);
space.setLayoutParams(new LinearLayout.LayoutParams(
imageView.getMeasuredWidth(), space.getMeasuredHeight()));
This gives the following result at first:
When I change the Visibility of my second LinearLayout (ll2) to VISIBLE, it gives the following result:
What I want instead is:
As it seems the Space-View width isn't changed at all.. While I know for fact that the getView methods are successfully called thanks to Log-messages and other things I do in the getView method.
Does anyone know what I did wrong?
PS: When I use View instead of Space I have the same result. Never used Space before, so I thought it might have to do something with that.
Solution thanks to Demand's option 4:
// Since we can't access Measured widths and heights before the View is completely loaded,
// we set up an Observer that will be called once the ListItem's layout is ready
ViewTreeObserver observer = view.getViewTreeObserver();
if(observer.isAlive()){
// In order to access the view in the onGlobalLayout, it needs to be final, so we create a final copy here
final View v = view;
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
// This will be called once the layout is finished, prior to displaying it
#Override
public void onGlobalLayout() {
ImageView imageView = (ImageView) v.findViewById(R.id.image);
Space space = (Space) v.findViewById(R.id.filler_space);
// Change the width of the Filler-Space to match the Image and leave the height as is
space.setLayoutParams(new LinearLayout.LayoutParams(imageView.getMeasuredWidth(), space.getMeasuredHeight()));
}
});
}
You set width and height if your space to wrap_content. It's mean that space will have width as their children, but there is no children and you have width = 0. It's not about space, it's about layout measuring in android.
When you call your code:
Space space = (Space) view.findViewById(R.id.filler_space);
space.setLayoutParams(new LinearLayout.LayoutParams(
imageView.getMeasuredWidth(), space.getMeasuredHeight()));
Your imageView havn't measured yet and return width = 0. It will measured later before drawing. In getView() in adapter you only create views, but measuring, layout and drawing will be later.
You have several ways to fix it:
set width of your space in dp, instead of wrap_content.
using relative layout instead of three linear layouts.
Use TableLayout.
add GlobalTreeObserver and getMeasuredWidth() at right time.
Post your runnable to view's handler to get width after drawing.
I think the best ways is 2 and 3, because 4 and 5 will cause measuring several times.
I have to align horizontally in the center|bottom of the activity two imageviews and I have to resize the imageviews in a screen width percentage (ex. 10% of screen width).
If I don't apply the resize, I have two imageviews in the center|bottom, but too big;
if I apply the resize, I have only the first in the center; the other one is disappeared!
I've tried to use Linearlayouts (orientation = horizontal) and Tablelayout...no results!
This is my activity XML when I'used the Tablelayout:
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginTop="48dp" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imgLogoCosoft"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/logo_cosoft" />
<ImageView
android:id="#+id/imgLogoFeelife"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/logo_feelife" />
</TableRow>
</TableLayout>
This is my activity XML when I've used the Linearlayout:
<LinearLayout
android:id="#+id/linearSplash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:paddingTop="20dp"
android:layout_alignParentBottom="true">
<ImageView
android:id="#+id/imgLogoCosoft"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/logo_cosoft" />
<ImageView
android:id="#+id/imgLogoFeelife"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/logo_feelife" />
</LinearLayout>
This is the Java code when I've used the Linearlayout:
int DisplayWidth = Funzioni.ScreenWidth (SplashScreen.this);
int DisplayHeight = Funzioni.ScreenHeight(SplashScreen.this);
imgLogoCosoft = (ImageView)findViewById(R.id.imgLogoCosoft);
LayoutParams params_imgLogoCosoft = new LayoutParams(DisplayWidth,(DisplayHeight/100)*10);
imgLogoCosoft.setLayoutParams(params_imgLogoCosoft);
imgLogoFeelife = (ImageView)findViewById(R.id.imgLogoFeelife);
LayoutParams params_imgLogoFeelife = new LayoutParams(DisplayWidth,(DisplayHeight/100)*10);
imgLogoFeelife.setLayoutParams(params_imgLogoFeelife);
Suggestions?
Thanks in advance!
Ok, I'll answer to myself.
For the tho images to place and resize at the bottom of the activity, instead of
LayoutParams params_imgLogoCosoft = new LayoutParams(DisplayWidth,(DisplayHeight/100)*10);
imgLogoCosoft.setLayoutParams(params_imgLogoCosoft);
I've used
imgLogoCosoft.getLayoutParams().width = DisplayWidth/10;
This is the XML of the Activity:
<ImageView
android:id="#+id/imgSfondoSplash"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="#drawable/back_splash" />
<ProgressBar
android:id="#+id/pgbSplash"
style="?android:attr/progressBarStyleLarge"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RelativeLayout
android:id="#+id/linearLayout1"
android:layout_below="#+id/imgSfondoSplash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"">
<ImageView
android:id="#+id/imgLogoCosoft"
android:layout_alignParentBottom="true"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/logo_cosoft" />
<ImageView
android:id="#+id/imgLogoFeelife"
android:layout_alignParentBottom="true"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/logo_feelife" />
</RelativeLayout>
And this is the code of the activity:
imgSfondoSplash = (ImageView)findViewById(R.id.imgSfondoSplash);
RelativeLayout.LayoutParams params_imgSfondoSplash = new RelativeLayout.LayoutParams(DisplayWidth,(DisplayHeight/100)*90);
imgSfondoSplash.setLayoutParams(params_imgSfondoSplash);
imgLogoCosoft = (ImageView)findViewById(R.id.imgLogoCosoft);
RelativeLayout.LayoutParams lp_imgLogoCosoft=new RelativeLayout.LayoutParams((DisplayWidth / 10), (DisplayWidth / 10));
lp_imgLogoCosoft.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
imgLogoCosoft.setLayoutParams(lp_imgLogoCosoft);
imgLogoFeelife = (ImageView)findViewById(R.id.imgLogoFeelife);
RelativeLayout.LayoutParams lp_imgLogoFeelife=new RelativeLayout.LayoutParams((DisplayWidth / 10), (DisplayWidth / 10));
lp_imgLogoFeelife.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
lp_imgLogoFeelife.addRule(RelativeLayout.RIGHT_OF, R.id.imgLogoCosoft);
imgLogoFeelife.setLayoutParams(lp_imgLogoFeelife);
I hope this could be useful for other developers.