1

Looks right in Android Studio, but doesn't work when executed on my phone.
I'm using a simple linear layout (horizontal) with two linear layout (vertical) containers inside, which should each take up 50% of the space of the row in the listview. Here's the code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:weightSum="1">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="0.5">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="001"
                android:id="@+id/pokemonNumber"
                android:layout_marginLeft="5dp" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Bulbasaur"
                android:id="@+id/pokemonName"
                android:layout_marginLeft="10dp" />

        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Grass"
                android:id="@+id/pokemonTypes"
                android:layout_marginLeft="50dp" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="0.5">

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/pokemonImage"
            android:src="#ff65ff4f" />

    </LinearLayout>

</LinearLayout>

I can upload pictures if you need a visual reference, but basically it should just be two linear layouts taking up 50% each of their parent linear layout, horizontally. Any ideas why this isn't working?

EDIT: attached pictures for clarity:

How it looks in Android Studio (correct): https://i.stack.imgur.com/bxhxi.png

How it actually looks (incorrect): https://i.stack.imgur.com/jPXCW.png

EDIT2: this is a row layout for a listview

EDIT3: Code where this layout is used:

public class DexListAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private List<Pokemon> mPokes;
    private String mRowLayout = "pokemon_list_item";
    private Context mContext;

    public DexListAdapter(Context context, List<Pokemon> pokes) {
        mInflater = LayoutInflater.from(context);
        mPokes = pokes;
        mContext = context;
    }

    @Override
    public int getCount() {
        return mPokes.size();
    }

    @Override
    public Object getItem(int position) {
        return mPokes.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        ViewHolder holder;

        if(convertView == null) {
            int resID = mContext.getResources().getIdentifier(mRowLayout, "layout", mContext.getPackageName());
            view = mInflater.inflate(resID, parent, false);

            holder = new ViewHolder();

            holder.number = (TextView)view.findViewById(R.id.pokemonNumber);
            holder.name = (TextView)view.findViewById(R.id.pokemonName);
            holder.types = (TextView)view.findViewById(R.id.pokemonTypes);
            holder.image = (ImageView)view.findViewById(R.id.pokemonImage);

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

        Pokemon poke = mPokes.get(position);
        //Set info from pokemon object to listview item
        holder.number.setText(poke.getStringNumber());
        holder.name.setText(poke.getName());
        holder.types.setText(poke.getTypeOne() + " " + poke.getTypeTwo());

        return view;
    }

    private class ViewHolder {
        public TextView number, name, types;
        public ImageView image;
    }
}

Above code called here (as part of fragment used in navigation drawer activity):

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_pokelist, container, false);
    ListView l1=(ListView)rootView.findViewById(R.id.pokeList);
    l1.setAdapter(new DexListAdapter(c,pokemonList));
    return rootView;
}

EDIT4: Code for listview

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/pokeList" />
</LinearLayout>
math-eww
  • 290
  • 3
  • 13
  • Show the code where you actually use this layout - that's where the problem would be. – dominicoder Jan 21 '15 at 20:48
  • Make the ListView fill the parent, use match_parent or fill_parent. I think the wrap content only makes it as big as the TextViews need it and the weights are not being used. – Gent Ahmeti Jan 21 '15 at 22:13
  • Success! Thank you torque203, I should have realized that, silly mistake on my part. If you post your solution as an answer I will rep and accept the answer. Thanks!! – math-eww Jan 21 '15 at 22:19

2 Answers2

5

The problem was that the ListView had it's width on wrap_content which didn't allow the LinearLayout to use it's weight properties. So the solution is as shown below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/pokeList" />
</LinearLayout>

I also read it in a question here (Can't find the link unfortunately) that it's not good to give a ListView wrap_content as height, always try to be specific, like match_parent or use other constraints so it knows it's height before any children are added.

Gent Ahmeti
  • 1,559
  • 13
  • 17
0

Try replacing:

view = mInflater.inflate(resID, parent, false);

with

view = mInflater.inflate(resID, null, false);

The first says "use my parent to figure out my layout params", the second says "use my layout resource's layout params settings".

dominicoder
  • 9,338
  • 1
  • 26
  • 32