0

For some reason the android:layout_height="200sp" attribute does not have any effect. The preview gets drawn correctly in Android Studio but if I run my App the list items are not changing their size according to android:layout_height="200sp". I am using this RelativeLayout as a list item for a ListView.

Any suggestions?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="120dip"
    android:background="#ffffff"
    >

    <ImageView
        android:id="@+id/icon"
        android:src="@drawable/ic_launcher"

        android:layout_width="50dip"
        android:layout_height="50dip"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:layout_gravity="center"

        />

</RelativeLayout>

@Edit:

I've made a simpler example. I would assume that the list items would be 120dip in height and got an icon on their left side with 50x50 dip in size. The reality is that the list items have the same height as the ImageView for some reason.

public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder holder;
    if(convertView == null){
        convertView = mInflater.inflate(R.layout.listview_nearbylist_item, null);
        holder = new ViewHolder();
        //holder.text1 = (TextView) convertView.findViewById(R.id.text1);
        //holder.text2 = (TextView) convertView.findViewById(R.id.text2);
        //holder.text3 = (TextView) convertView.findViewById(R.id.text3);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    //holder.text1.setText(items.get(position).getRestaurantName());
    //holder.text2.setText(items.get(position).getRestaurantGenre());
    //holder.text3.setText("08:00 - 18:00 Uhr");

    return convertView;
}
Stefan Falk
  • 23,898
  • 50
  • 191
  • 378
  • quick note: you should rather be using `dp` instead of `sp` for layouts unless you are abolutely fine with sideeffects this would bring in certain user configurations. – Marcin Orlowski Apr 14 '15 at 23:42
  • @MarcinOrlowski Thanks for the hint! There are just so many units .. :D – Stefan Falk Apr 14 '15 at 23:43
  • "the list items are not changing their size" -- this implies that you are using this `RelativeLayout` for rows in a `ListView` or `RecyclerView`. If so, post the code where you are inflating the layout. – CommonsWare Apr 15 '15 at 00:00
  • Do you mean where I fill the with the `ListItems` with the data to display? – Stefan Falk Apr 15 '15 at 00:05
  • "Do you mean where I fill the with the ListItems with the data to display?" -- I mean where you inflate the layout. You, or a superclass, uses a `LayoutInflater` and an `inflate()` call on it to convert the XML into a corresponding tree of widgets. Depending on how that is done, given a `RelativeLayout` as your row root container, you can get incorrect results. BTW, please use @ addressing on comments to ensure that they show up in our inbox, as I'm not going to be monitoring this question. – CommonsWare Apr 15 '15 at 00:12
  • @CommonsWare Okay, I've added this code that used to do this part. But since I've updated my `.xml` code I'm not sure if this will help. Right now I'm trying to find out why the list items have the height of the `ImageView`. – Stefan Falk Apr 15 '15 at 00:16

3 Answers3

1
convertView = mInflater.inflate(R.layout.listview_nearbylist_item, null);

You have two problems here:

  1. I don't know where mInflater comes from, but please get it from getLayoutInflater() called on your activity, not any other source. Otherwise, themes won't work.

  2. Use inflate(R.layout.listview_nearbylist_item, parent, false), which may fix your height problem, and if nothing else will avoid other problems with rows based on a RelativeLayout.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I am not sure what your code would do but it crashes either if I pass `convertView` which is a `View` or if I pass `parent` which would be a `ViewGroup`. – Stefan Falk Apr 15 '15 at 00:26
  • @StefanFalk: You are correct, in that it is supposed to be `parent` -- my apologies for my error. However, that should work just fine. Even simpler, if this is an `ArrayAdapter`, is just to chain to the superclass and let it handle the inflation. – CommonsWare Apr 15 '15 at 00:30
  • 2
    @StefanFalk: BTW, [this epic Dave Smith blog post](http://possiblemobile.com/2013/05/layout-inflation-as-intended/) covers both of these issues. In particular, it shows how not using the `inflate()` method as I described (at least, the corrected version) gives the exact problem that you are experiencing. – CommonsWare Apr 15 '15 at 00:33
  • Oha.. I have no idea why it is working now all of a sudden but I tried your `inflate(...)` two or three times and it didn't work. Now that I restated AS it works all of a sudden.. but maybe I'm just tired. It's 2.35am here. You answer is correct! Thank you and good night! :) – Stefan Falk Apr 15 '15 at 00:35
  • Thank you! +1 for the extra mile :) – Stefan Falk Apr 15 '15 at 01:19
0

You should only use sp for sizing fonts. For everything else, you need to be using dip. Please refer to What is the difference between "px", "dp", "dip" and "sp" on Android? for more information on this.

With regards to your question, replace the container size with dip. Only use sp with the textSize attribute. You should not have a problem.

EDIT: I also noticed some logical errors in your layout. Your fourth child widget (TextView) is set to match the size of the parent widget - this will overlap everything else in the layout. Are you sure this is what you want? This can be done technically but I'm not sure if that is what you intended.

Also the first child widget (the image view) is supposed to align both to the top and the bottom of the parent. As you can see, for the given dimensions, this is not possile. Something will have to change there.

Community
  • 1
  • 1
ucsunil
  • 7,378
  • 1
  • 27
  • 32
  • Thank you. Another user told me to use `dp`. Guess I'll have to read your link there. I've updated my question. I tried to set `300dip` .. AS displays it "*correctly*" but the live application still doesn't do a thing. I've added some padding to the `TextView` items but that is not how I intended to do it. – Stefan Falk Apr 14 '15 at 23:52
0

Try this instead

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="200dip"
android:padding="6sp"
android:background="#ffffff">
Ahmed Abidi
  • 1,047
  • 12
  • 24