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.
Related
I have a Linear layout then programatically I'm adding some spinners and buttons and so on, but I have xml button Wrap content (width) and then on java I add spinner (or anything else) and it goes below this view even if both views are wrap content:
progBar = new ProgressBar(this);
pBarToca = new ProgressBar(this);
pBarToca.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
linToca = (LinearLayout) findViewById(R.id.tetoca);
linToca.addView(pBarToca);
and it's placed under the button of xml:
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" android:layout_marginTop="6dp"
android:id="#+id/tetoca">
<TextView style="#style/StylePartida"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/te_toca_jugar" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#A7E9A9" android:onClick="callJugar"
android:text="#string/jugar" />
</LinearLayout>
edit!!!!!!
I want textview on first line then on next line button + progressbar (for example)
You have android:orientation=vertical so the Views will be laid out starting at the top and going down.
If you want them to all be next to each other, remove that from your xml since the default orientation for a LinearLayout is horizontal. If you do this, you will obviously need to change the android:width to wrap_content for your TextView or else it will take up the entire screen.
After your comment, a RelativeLayout would work best here.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
style="#style/StylePartida"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/te_toca_jugar"
android:id="#+id/tvID" /> // give it an id
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:id="#+id/tetoca"
android:layout_below="#/id=tvID"> // place it below the TV
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#A7E9A9" android:onClick="callJugar"
android:text="#string/jugar" />
</LinearLayout>
</RelativeLayout>
Note the changes in the comments. Now when you add your progressbar to the LL, it should be next to the Button. You may need some changes but this should give you approximately what you want.
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:id="#+id/tetoca">
<TextView style="#style/StylePartida"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/te_toca_jugar"
android:text="#string/te_toca_jugar" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#A7E9A9" android:onClick="callJugar"
android:text="#string/jugar" />
</LinearLayout>
In your textView you are matching the parent
android:layout_width="match_parent"
This will cause the textview to take up the entire width of the parent view.
android:layout_width="wrap_content"
and
android:orientation="horizontal"
android:orientation="vertical" will cause the elements to be stacked.
If you are using "horizontal" it's important not to have a child element with width matching parent.
EDIT:
After OPs change to question:
I have used a textview, two buttons and listview to give you an idea of how you can format it. There are many ways to achieve the same thing, this is one suggestion.
The internal linearlayout has a horizontal orientation (by default).
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="te_toca_jugar"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#A7E9A9"
android:text="jugar"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#A7E9A9"
android:text="jugar2"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lv">
</ListView>
</LinearLayout>
</LinearLayout>
I need to have like several listviews on a scrollview which Android doesn't allow me to do so I decided to make it manually to simply inflate some listrows on a vertical LinearLayout, but everything goes not the right way
private void fillData(final ViewGroup parent, final ArrayList<Person> array ){
for (int i=0;i<array.size();i++){
final View view = lf.inflate(R.layout.personal_listrow, parent,true);
parent.setBackgroundColor(getActivity().getResources().getColor(R.color.background_light_darker));
view.setBackgroundColor(getActivity().getResources().getColor(R.color.background_night_lighter));
TextView tvName=(TextView) view.findViewById(R.id.tvName);
TextView tvStatus=(TextView) view.findViewById(R.id.tvStatus);
TextView tvGender=(TextView) view.findViewById(R.id.tvGender);
//fill textviews with info, set the onClickListeners and so on
}
So what I get - the view.setBackgroundColor paints parent as well .
parent is a vertical LinearLayout, view is horizontal LinearLayout - it's strange that I don't paint inflated element only so please explain me why
The next thing - after I add something to array - 1st element changes and all the rest is with unchanged text. However, I walked through the code with debugger - it passes every element and sets text.
The clicklisteners works on the 1st element only..
Would you plz explain
here's vertical , parent layout - nothing special:
<LinearLayout
android:id="#+id/rodll"
android:layout_marginTop="16dp"
android:layout_marginBottom="8dp"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
the inflateable listrow is nothing special also :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvStatus"
android:layout_width="0dp"
android:layout_weight=".3"
android:maxLines="1"
android:layout_height="wrap_content"
android:text="tvSt" />
<TextView
android:id="#+id/tvName"
android:layout_weight="1"
android:layout_width="0dp"
android:maxLines="1"
android:layout_height="wrap_content"
android:text="tvName" />
<TextView
android:id="#+id/tvGender"
android:layout_weight=".1"
android:layout_width="0dp"
android:maxLines="1"
android:layout_height="wrap_content"
android:text="tvGender" />
<com.andexert.library.RippleView
android:id="#+id/removeRipple"
xmlns:ripple="http://schemas.android.com/apk/res-auto"
android:layout_marginLeft="30dp"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginRight="7.5dip"
android:padding="3dp"
ripple:rv_alpha="255"
ripple:rv_zoom="true"
ripple:rv_zoomScale="0.8"
ripple:rv_type="rectangle">
<ImageView
android:id="#+id/remove_icon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/remove" />
</com.andexert.library.RippleView>
<com.andexert.library.RippleView
android:id="#+id/addRipple"
xmlns:ripple="http://schemas.android.com/apk/res-auto"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginRight="7.5dip"
android:padding="3dp"
ripple:rv_alpha="255"
ripple:rv_zoom="true"
ripple:rv_zoomScale="0.8"
ripple:rv_type="rectangle">
<ImageView
android:id="#+id/add_icon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/add" />
</com.andexert.library.RippleView>
</LinearLayout>
So. what's the problem and how to solve it?
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'm programatically adding views into my layout inside a for loop.
The xml file of the layout I'm adding is like this one:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout_consulta_item_list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView_consulta_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/LinearLayout_dados_consulta"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/LinearLayout_dados_consulta"
android:layout_centerHorizontal="true"
android:layout_centerInParent="false"
android:adjustViewBounds="true"
android:maxWidth="70dp"
android:padding="10dp"
android:scaleType="centerInside"
android:src="#drawable/estrela_off" />
<LinearLayout
android:id="#+id/LinearLayout_dados_consulta"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_toLeftOf="#+id/imageView_consulta_action"
android:background="#660000"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="#string/origem"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#ffaaaa"
android:textSize="10sp" />
<TextView
android:id="#+id/textView_origem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="YYY"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#eeeeee" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:text="#string/data_ida"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#ffaaaa"
android:textSize="10sp" />
<TextView
android:id="#+id/textView_data_ida"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2014-05-05"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#eeeeee"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
In my main_activity, I call the constructor:
for (int i=0;i<resultList.length-1;i++){
RelativeLayout viewToLoad = (RelativeLayout)LayoutInflater.from(this).inflate(R.layout.consulta_item, null);
ll.addView(viewToLoad);
ImageView ImgConsulta_action = (ImageView)findViewById(R.id.imageView_consulta_action);
LinearLayout dados_consulta = (LinearLayout)findViewById(R.id.LinearLayout_dados_consulta);
dados_consulta.setOnLongClickListener(...);
ImgConsulta_action.setOnLongClickListener(...);
Well, I keep setting SetText for all the views in the layout and so on.
But, in order to make the setOnClickListener to work for each view, I must set a id to the views inside the for loop:
dados_consulta.setId(4000+i);
ImgConsulta_action.setId(5000+i); //(*lastline)
The weird thing is happening is that:
If I comment this last line, the layout is inserted correctly with the imageview showing a star as it is suposed to do and the listener at "dados_consulta" works fine.
But if I remove the comment at this last line, the star dissapear from the screen.
Can anybody help me to understand what is going on?
I had an layout params referenced to the ID of the layout elements at the original view.
Since I have to change the ID dynamically while I'm adding this layout several times into my activity, I also need to reset the layoutparams to the new Ids.
So, I added this lines:
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)ImgConsulta_action.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_TOP, dados_consulta.getId());
params.addRule(RelativeLayout.ALIGN_BOTTOM, dados_consulta.getId());
ImgConsulta_action.setLayoutParams(params);
RelativeLayout.LayoutParams paramsDadosConsulta = (RelativeLayout.LayoutParams)dados_consulta.getLayoutParams();
paramsDadosConsulta.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
paramsDadosConsulta.addRule(RelativeLayout.LEFT_OF, ImgConsulta_action.getId());
dados_consulta.setLayoutParams(paramsDadosConsulta);
Now it's working perfectly.
Notice that the problem is not about the layout disappearing it was about a layout get in front of the other one. (making this other one invisible)
Here's the layout (below). I'm trying to reposition the location of the checkbox; move it to the right of the view. android:gravity and android:layout_gravity seem to have no effect. Any explanation? This LinearLayout is a child of a Relative Layout.
<LinearLayout
android:id="#+id/deviceLinear"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="#id/view1" >
<ImageView
android:contentDescription="#string/devices_icon"
android:id="#+id/devices_img"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.15"
android:layout_gravity="center"
android:src="#drawable/hardware_phone" />
<TextView
android:id="#+id/devices"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.43"
android:text="#string/devices"
android:textSize="20sp" />
<CheckBox
android:id="#+id/check_devices"
android:button="#drawable/custom_checkbox"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="0.42"
android:onClick="onCheckboxClicked" />
</LinearLayout>
I assume that you want the CheckBox to be on the right side of the LinearLayout. Since you have given it a layout_weight it will always take up some fixed percentage of the width regardless of its size as you can see by that blue bounding box.
Try wrapping the checkbox in another LinearLayout that has android:orientation='horizontal'. Give the 0.42 weight to the wrapping LinearLayout and then set the LinearLayout's android:gravity to be right. That should keep your spacing while moving the checkbox to the far right.
Something like this:
<LinearLayout
android:orientation="horizontal"
android:layout_width="0dp"
android:layout_weight="0.42"
android:layout_height="wrap_content"
android:gravity="right" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
You might also want to consider a RelativeLayout which enables you to position Views based on where other views are.
You are giving layout_weight="0.42" to your checkbox, that means that it will be measured with a width based on the LinearLayout's width. By this approach you will never manage to put it on the right.
The best way to achieve your goal is to use a RelativeLayout. Here is my list item layout with a checkbox on the right. Sorry about the merge tag but it is merged inside a RelativeLayout with
android:layout_width="match_parent"
android:layout_height="wrap_content"
attributes
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView android:id="#+id/imgChannelIcon"
android:layout_alignParentLeft="true"
style="?muListItemImage60x60" />
<CheckBox android:id="#+id/chkIsChecked"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_alignTop="#+id/imgChannelIcon"
android:layout_alignParentRight="true"
android:layout_alignBottom="#+id/imgChannelIcon"
android:layout_marginLeft="#dimen/list_item_checkbox_margin_left"
android:layout_marginRight="#dimen/list_item_checkbox_margin_right"
android:layout_marginTop="#dimen/list_item_checkbox_margin_top"
android:layout_marginBottom="#dimen/list_item_checkbox_margin_bottom"
android:focusable="false"
android:focusableInTouchMode="false" />
<TextView android:id="#+id/lblChannelTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/imgChannelIcon"
android:layout_alignTop="#+id/imgChannelIcon"
android:layout_toLeftOf="#+id/chkIsChecked"
android:layout_alignWithParentIfMissing="true"
android:includeFontPadding="false"
android:textStyle="bold"
android:text="#string/channel_item_dummy_title"
style="?muListItemTextBig" />
<TextView android:id="#+id/lblChannelSubtitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/imgChannelIcon"
android:layout_below="#+id/lblChannelTitle"
android:layout_toLeftOf="#+id/chkIsChecked"
android:layout_alignWithParentIfMissing="true"
android:includeFontPadding="false"
android:textStyle="normal"
android:text="#string/channel_item_dummy_subtitle"
style="?muListItemTextNormal" />
<TextView android:id="#+id/lblChannelCategory"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_toRightOf="#+id/imgChannelIcon"
android:layout_below="#+id/lblChannelSubtitle"
android:layout_alignBottom="#+id/imgChannelIcon"
android:layout_toLeftOf="#+id/chkIsChecked"
android:layout_alignWithParentIfMissing="true"
android:includeFontPadding="false"
android:paddingLeft="3dp"
android:gravity="center_vertical|left"
android:textStyle="italic"
android:text="#string/channel_item_dummy_category"
style="?muListItemTextNormal_Inverse" />
<TextView android:id="#+id/lblChannelUpdate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="#+id/imgChannelIcon"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:includeFontPadding="false"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:textStyle="italic"
android:text="#string/channel_item_dummy_update"
style="?muListItemTextTiny" />
</merge>
As you can see I first place an image with layout_alignParentLeft="true" then the checkbox with layout_alignParentRight="true" and finally I arrange the other components based on those two. From your image I can see that you can use your devices_img as the left component (but you will have to give it a fixed height and maybe some margins in order to became the Layout's height) and your check box as your right.
Keep in mind that in a RelativeLayout with wrap_content as height you cannot use the AlignParentBottom attribute.
Hope this helps...