Created
July 8, 2014 10:36
-
-
Save nishitpatel/1b434985e1136f2f4600 to your computer and use it in GitHub Desktop.
Display List View in custom Dialog in android.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
xmlns:ads="http://schemas.android.com/apk/res-auto" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:background="#dddddd" > | |
<TextView | |
android:id="@+id/textViewTitle" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_margin="10dp" | |
android:text="@string/contact_list" | |
android:textColor="#000000" | |
android:textSize="18sp" | |
android:textStyle="bold" /> | |
<ImageButton | |
android:id="@+id/imageButtonClose" | |
android:layout_width="32dp" | |
android:layout_height="32dp" | |
android:layout_alignParentRight="true" | |
android:layout_margin="5dp" | |
android:background="@drawable/ic_action_cancel" | |
android:contentDescription="@string/detele" /> | |
<RelativeLayout | |
android:id="@+id/relativeLine" | |
android:layout_width="match_parent" | |
android:layout_height="1dp" | |
android:layout_below="@+id/textViewTitle" | |
android:background="#047AFF" > | |
</RelativeLayout> | |
<EditText | |
android:id="@+id/edittext_search" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_below="@+id/relativeLine" | |
android:layout_margin="10dp" | |
android:hint="@string/search_contact" | |
android:inputType="text" | |
android:paddingBottom="10dp" | |
android:paddingLeft="20dp" | |
android:paddingTop="10dp" | |
android:textColor="#000000" | |
android:textSize="16sp" /> | |
<ListView | |
android:id="@+id/listViewContact" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_below="@+id/edittext_search" > | |
</ListView> | |
</RelativeLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
xmlns:app="http://schemas.android.com/apk/res/com.vs2.scheduledsms" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_gravity="center_vertical" | |
android:background="#dddddd" | |
android:orientation="horizontal" | |
android:padding="10dp" > | |
<!-- i used circularimageview library you can use simple image here --> | |
<com.mikhaellopez.circularimageview.CircularImageView | |
android:id="@+id/image_contact" | |
android:layout_width="60dp" | |
android:layout_height="60dp" | |
android:layout_gravity="center_vertical" | |
android:contentDescription="@string/app_name" | |
app:border_color="#EEEEEE" | |
app:border_width="2dp" | |
app:shadow="true" /> | |
<LinearLayout | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center_vertical" | |
android:layout_marginLeft="5dp" | |
android:orientation="vertical" > | |
<TextView | |
android:id="@+id/textViewName" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="" | |
android:textStyle="bold" /> | |
<TextView | |
android:id="@+id/textViewNumber" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="5dp" | |
android:text="/> | |
</LinearLayout> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vs2.sheduledsms.adpaters; | |
import java.util.ArrayList; | |
import android.annotation.SuppressLint; | |
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.BaseAdapter; | |
import android.widget.Filter; | |
import android.widget.Filterable; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
import com.mikhaellopez.circularimageview.CircularImageView; | |
import com.vs2.scheduledsms.R; | |
import com.vs2.sheduledsms.dialogs.ContactListDialog; | |
import com.vs2.sheduledsms.objects.ContactPicker; | |
import com.vs2.sheduledsms.utilities.Logcat; | |
import com.vs2.sheduledsms.utilities.Utility; | |
@SuppressLint("DefaultLocale") | |
public class ContactListAdapter extends BaseAdapter implements Filterable { | |
ArrayList<ContactPicker> mContactList; | |
private ArrayList<ContactPicker> DisplayContactList; | |
LayoutInflater mInflater; | |
Context mContext; | |
public ContactListAdapter(Context context, | |
ArrayList<ContactPicker> contactlist) { | |
// TODO Auto-generated constructor stub | |
mContext = context; | |
mInflater = LayoutInflater.from(mContext); | |
mContactList = contactlist; | |
this.DisplayContactList = contactlist; | |
} | |
@Override | |
public int getCount() { | |
// TODO Auto-generated method stub | |
return mContactList.size(); | |
} | |
@Override | |
public Object getItem(int position) { | |
// TODO Auto-generated method stub | |
return mContactList.get(position); | |
} | |
@Override | |
public long getItemId(int position) { | |
// TODO Auto-generated method stub | |
return position; | |
} | |
@Override | |
public View getView(final int position, View convertView, ViewGroup parent) { | |
// TODO Auto-generated method stub | |
final ViewHolder holder; | |
if (convertView == null) { | |
convertView = mInflater.inflate(R.layout.contact_list_item, null); | |
holder = new ViewHolder(); | |
holder.textName = (TextView) convertView | |
.findViewById(R.id.textViewName); | |
holder.textNumber = (TextView) convertView | |
.findViewById(R.id.textViewNumber); | |
holder.imageViewPic = (CircularImageView) convertView | |
.findViewById(R.id.image_contact); | |
convertView.setTag(holder); | |
} else { | |
holder = (ViewHolder) convertView.getTag(); | |
} | |
holder.textName.setText("" + mContactList.get(position).getName()); | |
holder.textNumber.setText("" + mContactList.get(position).getNumber()); | |
Bitmap bitmap = Utility.getByteContactPhoto(mContactList.get(position) | |
.getId(), mContext); | |
if (bitmap != null) { | |
holder.imageViewPic.setImageBitmap(bitmap); | |
} else { | |
holder.imageViewPic.setImageResource(R.drawable.ic_launcher); | |
} | |
return convertView; | |
} | |
public class ViewHolder { | |
TextView textName, textNumber; | |
CircularImageView imageViewPic; | |
} | |
@SuppressLint("DefaultLocale") | |
@Override | |
public Filter getFilter() { | |
// TODO Auto-generated method stub | |
Filter filter = new Filter() { | |
@SuppressWarnings("unchecked") | |
@Override | |
protected void publishResults(CharSequence constraint, | |
FilterResults results) { | |
// TODO Auto-generated method stub | |
Logcat.e("PublishResults Constraint", | |
"" + constraint.toString()); | |
mContactList = (ArrayList<ContactPicker>) results.values; | |
ContactListDialog.mContactPickerList = mContactList; | |
notifyDataSetChanged(); | |
} | |
@SuppressLint("DefaultLocale") | |
@Override | |
protected FilterResults performFiltering(CharSequence constraint) { | |
// TODO Auto-generated method stub | |
FilterResults results = new FilterResults(); | |
ArrayList<ContactPicker> FilteredArrList = new ArrayList<ContactPicker>(); | |
mContactList = new ArrayList<ContactPicker>(DisplayContactList); | |
if (constraint == null || constraint.length() == 0) { | |
// set the Original result to return | |
results.count = mContactList.size(); | |
results.values = mContactList; | |
} else { | |
constraint = constraint.toString().toLowerCase(); | |
for (int i = 0; i < mContactList.size(); i++) { | |
String data = mContactList.get(i).getName(); | |
if (data.toLowerCase() | |
.startsWith(constraint.toString())) { | |
FilteredArrList.add(mContactList.get(i)); | |
} else if (data.toLowerCase().contains( | |
constraint.toString())) { | |
FilteredArrList.add(mContactList.get(i)); | |
} | |
} | |
// set the Filtered result to return | |
results.count = FilteredArrList.size(); | |
results.values = FilteredArrList; | |
} | |
return results; | |
} | |
}; | |
return filter; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vs2.sheduledsms.dialogs; | |
import java.util.ArrayList; | |
import android.app.Activity; | |
import android.app.Dialog; | |
import android.app.ProgressDialog; | |
import android.os.AsyncTask; | |
import android.os.Bundle; | |
import android.support.v4.app.FragmentManager; | |
import android.text.Editable; | |
import android.text.TextWatcher; | |
import android.view.View; | |
import android.view.Window; | |
import android.widget.AdapterView; | |
import android.widget.AdapterView.OnItemClickListener; | |
import android.widget.EditText; | |
import android.widget.ImageButton; | |
import android.widget.ListView; | |
import com.vs2.scheduledsms.R; | |
import com.vs2.sheduledsms.adpaters.ContactListAdapter; | |
import com.vs2.sheduledsms.database.DatabaseFunctions; | |
import com.vs2.sheduledsms.interfaces.onSelectContact; | |
import com.vs2.sheduledsms.objects.ContactPicker; | |
public class ContactListDialog extends Dialog implements | |
android.view.View.OnClickListener { | |
public Activity activity; | |
public Dialog dailog; | |
public ListView listContact; | |
public ImageButton imageButtonExit; | |
public EditText mEditTextSearch; | |
public static onSelectContact contactListener; | |
public static ArrayList<ContactPicker> mContactPickerList; | |
public FragmentManager mFragmentManager; | |
public ContactListAdapter adapter; | |
public ContactListDialog(Activity activity) { | |
super(activity); | |
// TODO Auto-generated constructor stub | |
this.activity = activity; | |
} | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
requestWindowFeature(Window.FEATURE_NO_TITLE); | |
setContentView(R.layout.contact_list_dialog); | |
try { | |
DatabaseFunctions.openDB(activity); | |
} catch (Exception e) { | |
// TODO: handle exception | |
} | |
new getContactList().execute(); | |
listContact = (ListView) findViewById(R.id.listViewContact); | |
imageButtonExit = (ImageButton) findViewById(R.id.imageButtonClose); | |
mEditTextSearch = (EditText) findViewById(R.id.edittext_search); | |
imageButtonExit.setOnClickListener(this); | |
listContact.setOnItemClickListener(new OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> parent, View view, | |
int position, long id) { | |
// TODO Auto-generated method stub | |
contactListener.getSelectedContact(mContactPickerList | |
.get(position)); | |
dismiss(); | |
} | |
}); | |
mEditTextSearch.addTextChangedListener(new TextWatcher() { | |
@Override | |
public void onTextChanged(CharSequence s, int start, int before, | |
int count) { | |
// TODO Auto-generated method stub | |
adapter.getFilter().filter(s.toString()); | |
} | |
@Override | |
public void beforeTextChanged(CharSequence s, int start, int count, | |
int after) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void afterTextChanged(Editable s) { | |
// TODO Auto-generated method stub | |
} | |
}); | |
} | |
@Override | |
public void onAttachedToWindow() { | |
// TODO Auto-generated method stub | |
contactListener = (onSelectContact) activity; | |
super.onAttachedToWindow(); | |
} | |
public void onClick(View v) { | |
// TODO Auto-generated method stub | |
if (v.getId() == R.id.imageButtonClose) { | |
dismiss(); | |
} | |
} | |
public void loadContactList() { | |
adapter = new ContactListAdapter(activity, mContactPickerList); | |
listContact.setAdapter(adapter); | |
} | |
public class getContactList extends | |
AsyncTask<Void, Void, ArrayList<ContactPicker>> { | |
ProgressDialog pd; | |
@Override | |
protected void onPreExecute() { | |
// TODO Auto-generated method stub | |
pd = new ProgressDialog(activity); | |
pd.setMessage("Please wait, getting data"); | |
pd.setCancelable(false); | |
pd.setCanceledOnTouchOutside(false); | |
pd.show(); | |
super.onPreExecute(); | |
} | |
@Override | |
protected ArrayList<ContactPicker> doInBackground(Void... params) { | |
// TODO Auto-generated method stub | |
try { | |
return getYourListData(); | |
} catch (Exception e) { | |
// TODO: handle exception | |
return null; | |
} | |
} | |
@Override | |
protected void onPostExecute(ArrayList<ContactPicker> result) { | |
// TODO Auto-generated method stub | |
try { | |
pd.dismiss(); | |
if (result != null && result.size() > 0) { | |
mContactPickerList = result; | |
loadContactList(); | |
} else { | |
/*Log.e("","length:"+mContactPickerList.size()); | |
mEditTextSearch.setVisibility(View.GONE); | |
ToastMsg.showError(getOwnerActivity(), "There are no contects in phone", ToastMsg.GRAVITY_CENTER);*/ | |
contactListener.getSelectedContact(null); | |
} | |
//Log.e("","length:"+mContactPickerList.size()); | |
} catch (Exception e) { | |
// TODO: handle exception | |
} | |
super.onPostExecute(result); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vs2.sheduledsms.interfaces; | |
import com.vs2.sheduledsms.objects.ContactPicker; | |
public interface onSelectContact { | |
public void getSelectedContact(ContactPicker contact); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment