Mohan Timilsina
Mohan Timilsina

Reputation: 500

Displaying the button at the end of the ListView with Linear Layout

I am a newbie in android application development. I faced a problem regarding the displaying a button at the end of the List View. I am using Linear Layout. The application can show all the list but cannot show the Button. I have also pasted my XML code here. Any help in this regard, will be highly appreciated.

Mohan

main.xml

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

<ImageView 
    android:id="@+id/contact_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />


<Button
    android:id="@+id/btn_New"
    android:width="170dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="20dp"
    android:text="Click"

     />

<TextView  
    android:id="@+id/textView"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_above="@id/btn_New"
    android:text="@string/hello"
    />

<ListView 
    android:id="@+id/contactList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/textView"
    />

</RelativeLayout>

contactlistitem.xml

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout

  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent" android:orientation="horizontal" android:weightSum="1">

    <TextView
        android:id="@+id/txtDisplayName"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textAppearance="?android:attr/textAppearanceMedium" >
</TextView>

    <ImageView android:id="@+id/contact_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />


    <TextView android:textAppearance="?android:attr/textAppearanceLarge" 
    android:id="@+id/textView1" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content"></TextView>




</LinearLayout>

ContactListActivity.java

package com.contactlist;

import android.app.Activity;
import android.graphics.BitmapFactory;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.ListView;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.provider.ContactsContract.CommonDataKinds.Photo;
import android.provider.ContactsContract.Data;

public class ContactListActivity extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ContactList contactList=this.getContacts();
        ArrayAdapter<Contact> adapter=new ContactAdapter(this,contactList.getContacts());
        setListAdapter(adapter);


    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id)
    {
        super.onListItemClick(l, v, position, id);
        Object o=this.getListAdapter().getItem(position);
        Contact c=(Contact)o;
        Toast.makeText(this, c.getDisplayName(), Toast.LENGTH_SHORT).show();
        Toast.makeText(this, c.getId(), Toast.LENGTH_SHORT).show();
    }

    private ContactList getContacts()
    {
        ContactList contactList=new ContactList();
        Uri uri=ContactsContract.Contacts.CONTENT_URI;
        ContentResolver cr=getContentResolver();
        String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
        Cursor cur=cr.query(uri, null, null, null, sortOrder);

        if(cur.getCount() >0)
        {
            String id;
            String img;
            String name;
            while(cur.moveToNext())
            {
                Contact c =new Contact();
                id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                img= cur.getString(cur.getColumnIndex(ContactsContract.Contacts.PHOTO_ID));
                name=cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                final Bitmap photo;
                if(img != null) {
                    photo = queryContactBitmap(img);


                } else {
                    photo = null;
                }


                c.setId(id);


                c.setImage(photo);
                c.setDisplayName(name);
                contactList.addContact(c);


            }
        }
    //  cur.close();
        return contactList;
    }
    private Bitmap queryContactBitmap(String photoId) {
        final Cursor photo = managedQuery(
                Data.CONTENT_URI,
                new String[] {Photo.PHOTO},     // column where the blob is stored
                Data._ID + "=?",                // select row by id
                new String[]{photoId},          // filter by the given photoId
                null);


        final Bitmap photoBitmap;
        if(photo.moveToFirst()) {
            byte[] photoBlob = photo.getBlob(
                    photo.getColumnIndex(Photo.PHOTO));
            photoBitmap = BitmapFactory.decodeByteArray(
                    photoBlob, 0, photoBlob.length);
        } else {
            photoBitmap = null;
        }
        photo.close();

        return photoBitmap;


    }
}

ContactList.java

package com.contactlist;

import java.util.ArrayList;
import java.util.List;

public class ContactList {
    private List<Contact> _contacts=new ArrayList<Contact>();
    public List<Contact> getContacts(){return _contacts;}

    public void addContact(Contact contact){ this._contacts.add(contact);}

}

ContactAdapter.java

package com.contactlist;

import java.util.List;


import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ContactAdapter extends ArrayAdapter<Contact> {

    private final List<Contact> _contacts;
    private final Activity _context;

    public ContactAdapter(Activity context, List<Contact> contacts)
    {
        super(context,R.layout.main,contacts);
        this._contacts=contacts;
        this._context=context;
    }
    static class ViewHolder {
        protected TextView text;
        private Contact  _contact;
        public ImageView imageview;
        protected void setContact(Contact contact)
        {
            text.setText(contact.getDisplayName());
            imageview.setImageBitmap(contact.getImage());
            _contact=contact;
        }
        protected Contact getContact() {return _contact;}
    }
    @Override
    public Contact getItem(int position)
    {
        return _contacts.get(position);
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {

        View view=null;


        if(convertView==null)
        {
            LayoutInflater inflater=_context.getLayoutInflater();
            view=inflater.inflate(R.layout.contactlistitem, null);
            final ViewHolder viewHolder=new ViewHolder();
            viewHolder.text=(TextView)view.findViewById(R.id.txtDisplayName);
            viewHolder.imageview =(ImageView)view.findViewById(R.id.contact_image);
            viewHolder.setContact(_contacts.get(position));
            view.setTag(viewHolder);

        }

        else 
        {
            view = convertView;
        }

        return view;
    }
}

Contact.java

package com.contactlist;

import java.util.ArrayList;

import android.R.string;
import android.graphics.Bitmap;

public class Contact {
    private String _id;
    private String _displayName;
    private Bitmap _img;

    public String getId(){return _id;}
    public String getDisplayName(){return _displayName;}
    public Bitmap getImage(){return _img;}
    public void setId(String id){_id=id;}
    public void setDisplayName(String displayName){_displayName=displayName;}
    public void setImage(Bitmap img){_img=img;}
}

Upvotes: 0

Views: 2932

Answers (3)

0gravity
0gravity

Reputation: 2762

Try this and see if it works:

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

<ImageView 
    android:id="@+id/contact_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />


<Button
    android:id="@+id/btn_New"
    android:width="170dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="20dp"
    android:text="Click"

     />

<TextView  
    android:id="@+id/textView"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_above="@id/btn_New"
    android:text="@string/hello"
    />

<ListView 
    android:id="@+id/contactList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/textView"
    />

</RelativeLayout>

MainActivity class:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);       
    setContentView(R.layout.main);

    ListView listView = (ListView) findViewById(R.id.contactList);

    String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
              "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
              "Linux", "OS/2" };

    // First parameter - Context
    // Second parameter - Layout for the row
    // Third parameter - ID of the TextView to which the data is written
    // Forth - the Array of data
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1, values);

    // Assign adapter to ListView
    listView.setAdapter(adapter);
  }

enter image description here

Now when you put things inside the listView it should not "push" the button or the textView down.

Upvotes: 1

tae jun Kim
tae jun Kim

Reputation: 176

android:layout_weight="1"

delete it.

if you are not express 'android:layout_weightSum' on over level that cotains some components are express 'android:layout_weight' but also component that contain 'android:layout_weight="1"' code is exactly change size to fullscreen.

(i'm sorry, i can't speak english very well....)

  1. android:layout_weight="1" is wrong
  2. android:layout_weight="1" delete it
  3. Or use android:layout_weightSum first.

Upvotes: 0

Newts
Newts

Reputation: 1372

Firstly you have to set a linear layout in a main relative layout and set its gravity bottom

<RelativeLayout
android:width = "fill..."
android....>

<LinearLayout
android:gravity="bottom"
android:id="@+id/linear1"  
android:layout_alignParentBottom="true">
<Button/>

</LinearLayout>


<ListView
android:layout_above="@id/linear"/>

Upvotes: 0

Related Questions