user1282637
user1282637

Reputation: 1867

How to make a row in a ListView adjust to its content in Android app

Ok, so right now I have a ListView with a set height.

good

but when the content inside of a certain row of that ListView grows...the row doesn't

bad

as you can see on the second image, content is cut off on the first row

Is there any way I can make a single row expand when there isn't enough space?

Activity Code:

public class MainActivity extends ActionBarActivity {

ArrayList<Location> arrayOfLocations;
LocationAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);

    // Construct the data source
    arrayOfLocations = new ArrayList<Location>();

    // Create the adapter to convert the array to views
    adapter = new LocationAdapter(this, arrayOfLocations);

    adapter.add(new Location(R.drawable.ic_launcher, "Fruit Stand",
            "We have the freshest fruit in the whole world!", "2 miles",
            "8-5 mon-sat\nclosed sun"));

    //getData();

    // Attach the adapter to a ListView
    ListView listView = (ListView) findViewById(R.id.listView1);

    View header = (View) getLayoutInflater().inflate(
            R.layout.listview_header, null);
    listView.addHeaderView(header);

    listView.setAdapter(adapter);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

XML Layout for a row

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="140dip" >

<ImageView
    android:id="@+id/imgIcon"
    android:layout_width="90sp"
    android:layout_height="120sp" />

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_toRightOf="@id/imgIcon"
    android:orientation="vertical"
    android:paddingLeft="10sp" >

    <TextView
        android:id="@+id/tvName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="25sp" />

    <TextView
        android:id="@+id/tvDetails"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/tvDistance"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/tvHours"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="15sp" />
</LinearLayout>

</RelativeLayout>

Thank you so much!

Upvotes: 0

Views: 416

Answers (1)

HatemTmi
HatemTmi

Reputation: 1078

May be you should update your row height to be "wrap_content" instead of 140dip

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="140dip" >
    ...
</RelativeLayout>

Upvotes: 5

Related Questions