Shabeeralimsn
Shabeeralimsn

Reputation: 825

listview vertical height is not coming properly

i am working on a App which deals with Contact. i need a custom activity for openening a contact from Contact list which should show all the details.

here, i need to display a label as "mobile"/"work"/"Home" and corresponding numbers.

Expected format:- enter image description here

my problem here is, Listview height is not coming properly.i tried below code in xml.

open_contact.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffff">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:orientation="vertical"
        android:gravity="center">

        <ImageView
            android:id="@+id/imgView_open_contact"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_gravity="center_vertical|center_horizontal"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="8dp"

            />
        </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="vertical"
        android:gravity="center">

        <TextView
            android:id="@+id/txtView_name_open_contact"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:textSize="20sp"
            android:textStyle="bold"
            android:textColor="#0d0d0d"
            android:maxLines="1"
            />

        <TextView
            android:id="@+id/txtView_company_open_contact"
            android:layout_width="wrap_content"
            android:layout_height="20dp"
            android:textSize="15sp"
            android:maxLines="1"
            android:orientation="vertical"
            android:textColor="#403f3f"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/listView_showAllNumbers"
            android:background="#ffff"
            android:scrollbars="horizontal"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="30pt"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="15pt"
                android:layout_marginStart="16dp"
                android:textColor="#0e0e0e"
                android:textStyle="bold"
                android:id="@+id/txtView_label"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="15pt"
                android:layout_marginStart="16dp"
                android:textColor="#1d34c9"
                android:orientation="vertical"
                android:id="@+id/txtView_label_value"/>

        </LinearLayout>


    </LinearLayout>

</LinearLayout>

relevant java code to populate data

openContactActivity.java

.........

@Override
protected void onPostExecute(String r) {
    customAdapOpenContact cAdap;

    if(mob_index > 0)
    {
        int i = 0;
        //listOpenContact s = new listOpenContact("Mobile", number_mob[0]);
        listOpenContact s = new listOpenContact("Mobile");
        listItem.add(s);
        cAdap = new customAdapOpenContact(getBaseContext(), listItem);
        list_showAllNumbers.setAdapter(cAdap);
        while(i < mob_index){
            Log.e("DEBUG: ", "NUMBER: " + number_mob[i]);
            listOpenContact n = new listOpenContact(number_mob[i]);
            listItem.add(n);
            cAdap = new customAdapOpenContact(getApplicationContext(), listItem);
            list_showAllNumbers.setAdapter(cAdap);
            i++;
        }
    }

.......

listOpenContact.java

public class listOpenContact {

    String label;
//    String label_content;
    //Constructor for Single row
    listOpenContact (String label) {
        this.label = label;
        //this.label_content = label_content;
    }
}

customAdapOpenContact.java

public class customAdapOpenContact extends BaseAdapter {

    private Context c;
    private ArrayList<listOpenContact> list;


    customAdapOpenContact(Context context, ArrayList<listOpenContact> list){
        c = context;
        this.list = list;
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row =  inflater.inflate(R.layout.open_contact, null, false);

        TextView label   = (TextView) row.findViewById(R.id.txtView_label);
        TextView label_content = (TextView) row.findViewById(R.id.txtView_label_value);

        listOpenContact listOpenContact = list.get(position);
    if(listOpenContact.label == "Mobile" || listOpenContact.label == "Work" || listOpenContact.label == "Home")
        label.setText(listOpenContact.label);
    else
        label_content.setText(listOpenContact.label);

        Log.e("DEBUG: ", "Size: " + listOpenContact.label.length());

        return row;
    }
}

output coming as : enter image description here

can somebody help me to undrestand what is wrong in my xml? or please suggest new code to get the expected layout (listview part)

Upvotes: 0

Views: 48

Answers (3)

Muthukrishnan Rajendran
Muthukrishnan Rajendran

Reputation: 11622

Then main problem is you gave single layout for both your activity and your list item, and you gave dimension to each item and you didnt set default value, so in your adapter it will try to create your view and it will set only the mobile number part so remaing item are empty so its looking like empty space.

To check this try to give some value in your layout it will appear in each item, to achive that you should create 2 seperate layout for your activity and the list item like below

  1. Check your root layout of open_contact if its not wrap_content change to android:layout_height="wrap_content"

  2. You are adding adapter into loop change that like below,

Add adapter to list outside loop

@Override
protected void onPostExecute(String r) {
    customAdapOpenContact cAdap;

    if(mob_index > 0) {
        int i = 0;
        listOpenContact s = new listOpenContact("Mobile");
        listItem.add(s);

        while(i < mob_index){
            Log.e("DEBUG: ", "NUMBER: " + number_mob[i]);
            listOpenContact n = new listOpenContact(number_mob[i]);
            listItem.add(n);

            i++;
        }
        cAdap = new customAdapOpenContact(getBaseContext(), listItem);
        list_showAllNumbers.setAdapter(cAdap);
    }
}

Create new layout list_item.xml

<?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="wrap_content">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:textColor="#0e0e0e"
        android:textStyle="bold"
        android:id="@+id/txtView_label"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:textColor="#1d34c9"
        android:orientation="vertical"
        android:id="@+id/txtView_label_value"/>
</LinearLayout>

give this layout to your adapter

View row =  inflater.inflate(R.layout.list_item, null, false);

And change your activity layout like 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"
              android:background="#ffff">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:orientation="vertical"
        android:gravity="center">

        <ImageView
            android:id="@+id/imgView_open_contact"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_gravity="center_vertical|center_horizontal"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="8dp"

            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="vertical"
        android:gravity="center">

        <TextView
            android:id="@+id/txtView_name_open_contact"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:textSize="20sp"
            android:textStyle="bold"
            android:textColor="#0d0d0d"
            android:maxLines="1"
            />

        <TextView
            android:id="@+id/txtView_company_open_contact"
            android:layout_width="wrap_content"
            android:layout_height="20dp"
            android:textSize="15sp"
            android:maxLines="1"
            android:orientation="vertical"
            android:textColor="#403f3f"/>

    </LinearLayout>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listView_showAllNumbers"
        android:background="#ffff"
        android:scrollbars="horizontal"/>

</LinearLayout>

Upvotes: 1

E_K
E_K

Reputation: 2249

Try removing the height of the TextView s android:id="@+id/txtView_name_open_contact" and android:id="@+id/txtView_company_open_contact" to wrap_content (You can use padding_top and padding_bottom for some space around the textViews also remove the orientation for theandroid:id="@+id/txtView_company_open_contact" TextView

Upvotes: 0

Satan Pandeya
Satan Pandeya

Reputation: 3815

In your layout structure, your root layout has:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="#ffff">

Here you are creating the height as match_parent. This is why your each item has shown like that. Change the height wrap_content.

Upvotes: 0

Related Questions