0

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;}
}
Mohan Timilsina
  • 490
  • 6
  • 25
  • What are you doing with the image view on your main.xml? – 0gravity Aug 10 '12 at 05:07
  • I tried to extract the image from the android phone contact list that is why I use that image view tags, but even after deleting it and re running the code the output is the same :-) – Mohan Timilsina Aug 10 '12 at 07:05
  • I tested your main.xml and I do see a button In the graphical layout. Is the problem when you add things to your listview that you can't see the button? – 0gravity Aug 10 '12 at 15:53
  • Yes, you are right. When the list view is loaded with contact names and image the button is not appearing at the end. Do you have any idea how to fix this ? – Mohan Timilsina Aug 11 '12 at 04:56
  • Take a look at my answer, it might help. – 0gravity Aug 11 '12 at 05:08

3 Answers3

1

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.

0gravity
  • 2,682
  • 4
  • 24
  • 33
  • Ops, sorry about that. I updated my code, so now It should not give you that error. I don't know if that might fix the button problem so give it a try. – 0gravity Aug 13 '12 at 05:05
  • When I tried with your provided XML code, XML parser complained at '' and ''. So, I removed it and try to run then I still get no change in my layout, Though I see the button in the Graphical Layout. I have attached the layout image in this [link]http://img859.imageshack.us/img859/598/screenshot1bs.png. Any idea about this issue :-) – Mohan Timilsina Aug 13 '12 at 05:12
  • no don't remove it. I updated my code...paste that code "as is" and see if that works. – 0gravity Aug 13 '12 at 05:15
  • I tried with updated version but still the result is the same. – Mohan Timilsina Aug 13 '12 at 06:02
  • I have updated the answer to put my code from the onCreate in the main activity. I have tested that code with the main.xml that I updated and it works fine. The button does not get push down. – 0gravity Aug 13 '12 at 06:20
  • Since your example works perfectly fine but I don't know why it is not happening in my applications.For your reference I have also included all my java files and my xml files in my questions, hope to hear your suggestions. – Mohan Timilsina Aug 13 '12 at 07:57
0

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"/>
Newts
  • 1,354
  • 14
  • 23
0

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.
tae jun Kim
  • 176
  • 1
  • 1
  • 10