Call contacts from a list in Android Studio - android

I have an Android application where I used Linear Layout that shows a list of contacts. Now what I want is that when selecting the contacts, call the indicated number.
For now, all it do is show the contacts, but when I click on call, it does not take any action. I do not know how to make to mark
I tried with setOnClickListener().But Unsuccessful
My code:
public class ContactsAdapter extends RecyclerView.Adapter<ContactsAdapter.ContactViewHolder>{
Dialog myDialog;
private List<ContactModel> contactModelList;
private Context mContext;
public ContactsAdapter(List<ContactModel> contactModelList, Context mContext){
this.contactModelList = contactModelList;
this.mContext = mContext;
}
#Override
public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.single_contact_view, null);
ContactViewHolder contactViewHolder = new ContactViewHolder(view);
return contactViewHolder;
}
#Override
public void onBindViewHolder(ContactViewHolder holder, int position) {
final ContactModel contactModel = contactModelList.get(position);
holder.tvContactName.setText(contactModel.getContactName());
holder.tvPhoneNumber.setText(contactModel.getContactNumber());
holder.contactsRowLV.setOnClickListener(
new View.OnClickListener()
{
#Override
public void onClick(View view) {
Toast.makeText(mContext,""+ contactModel.getContactNumber(),Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return contactModelList.size();
}
public static class ContactViewHolder extends RecyclerView.ViewHolder{
ImageView ivContactImage;
TextView tvContactName;
TextView tvPhoneNumber;
LinearLayout contactsRowLV;
public ContactViewHolder(View itemView) {
super(itemView);
ivContactImage = (ImageView) itemView.findViewById(R.id.ivContactImage);
tvContactName = (TextView) itemView.findViewById(R.id.tvContactName);
tvPhoneNumber = (TextView) itemView.findViewById(R.id.tvPhoneNumber);
contactsRowLV = itemView.findViewById(R.id.contactsRowLV);
}
}
}
Here I show my XML of how I have. Help me please
<LinearLayout
android:id="#+id/contactsRowLV"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/ivContactImage"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_gravity="center"
android:src="#drawable/ic_person"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_vertical">
<TextView
android:id="#+id/tvContactName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:textSize="30dp"
android:textStyle="bold"
android:textColor="#android:color/primary_text_light"
android:text="Name"/>
<TextView
android:id="#+id/tvPhoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:textSize="25dp"
android:textColor="#android:color/primary_text_light"
android:text="Phone"/>
</LinearLayout>
</LinearLayout>

You just need to add following code in setOnClickListener(). This code is used to call particular number.
#Override
public void onBindViewHolder(ContactViewHolder holder, int position) {
final ContactModel contactModel = contactModelList.get(position);
holder.tvContactName.setText(contactModel.getContactName());
holder.tvPhoneNumber.setText(contactModel.getContactNumber());
holder.contactsRowLV.setOnClickListener(
new View.OnClickListener(){
#Override
public void onClick(View view) {
Toast.makeText(mContext,""+ contactModel.getContactNumber(),Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" +contactModel.getContactNumber()));
mContext.startActivity(intent);
}
});
}

Related

How to change textview text on cardview click?

in a cardview i have an imageview, and two textview. I'm having problems trying to figurate out how to change the text of one of the textview when someone click on the cardview....is this even posible?
The card view are controlled by a recyclerview....in total I have to around 20 cardview.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="10dp"
app:cardCornerRadius="5dp"
android:foreground="?android:attr/selectableItemBackground">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<ImageView
android:id="#+id/image1"
android:layout_width="match_parent"
android:layout_height="130dp"
android:background="#color/transparent"
android:padding="0dp"
android:scaleType="fitCenter"
android:src="#drawable/card1" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginTop="5dp"
android:text="Text Here"
android:textColor="#color/red" />
<TextView
android:id="#+id/product"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_gravity="center"
android:gravity="center"
android:text="SUV"
android:textColor="#color/grey_font"
android:textSize="11dp" />
Here is the click of the card view, I get the posistion of the cardview, but don't know how to get the textview inside the cardview.
public void onItemClick(int position) {
Log.e(TAG, "Bien: " + position);
}
public void onBindViewHolder(#NonNull RecyclerViewHolder holder, int position) {
Items currentItem = vReyclerViewList.get(position);
String heading = currentItem.getHeading();
String title = currentItem.getTitlee();
holder.vHeading.setText(heading);
holder.vTitle.setText(title);
}
public class RecyclerViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public TextView product;
public RecyclerViewHolder(View itemView) {
super (itemView);
title = itemView.findViewById(R.id.title);
product = itemView.findViewById(R.id.product);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(vListener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
vListener.onItemClick(position);
}
}
}
});
}
}
You have to override the onBindViewHolder() method then implement as below.
Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
Model model = getItem(position);
holder.bind(model);
}
public class RecyclerViewHolder extends RecyclerView.ViewHolder {
public final TextView title;
public final TextView product;
public RecyclerViewHolder(View itemView) {
super (itemView);
title = itemView.findViewById(R.id.title);
product = itemView.findViewById(R.id.product);
}
public void bind(final Model model){
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
title.setText(model.getText());
}
});
}
}

Recyclerview call contacts Android studio

I have an android app where I have used RecyclerView that shows a list of contacts. Now what I want is that when selecting the contacts, call the indicated number.
For now all it do is show the contacts, but when i click on it it calls, it does not do any action. I do not know how to get it to mark
I tried with onClick() and with button item.But Unsuccessful
My code:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
private static final String TAG = "RecyclerViewAdapter";
Context mContext;
List<Contact> mData;
Dialog myDialog;
Button button;
public RecyclerViewAdapter(Context mContext, List<Contact> mData) {
this.mContext = mContext;
this.mData = mData;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v ;
v = LayoutInflater.from(mContext).inflate(R.layout.single_item_contact,parent,false);
final MyViewHolder vHolder = new MyViewHolder(v);
// Dialog ini
myDialog = new Dialog(mContext);
myDialog.setContentView(R.layout.dialog_contact);
myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
vHolder.item_contact.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(mContext,"Text Click item : "+String.valueOf(vHolder.getAdapterPosition()),Toast.LENGTH_SHORT).show();
TextView dialog_name_tv = (TextView) myDialog.findViewById(R.id.dialog_name_id);
TextView dialog_phone_tv = (TextView) myDialog.findViewById(R.id.dialog_phone_id);
ImageView dialog_contact_img = (ImageView)myDialog.findViewById(R.id.dialog_img);
dialog_name_tv.setText(mData.get(vHolder.getAdapterPosition()).getName());
dialog_phone_tv.setText(mData.get(vHolder.getAdapterPosition()).getPhone());
dialog_contact_img.setImageResource(mData.get(vHolder.getAdapterPosition()).getPhoto());
myDialog.show();
}
});
return vHolder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
holder.tv_name.setText(mData.get(position).getName());
holder.tv_phone.setText(mData.get(position).getPhone());
holder.img.setImageResource(mData.get(position).getPhoto());
}
#Override
public int getItemCount() {
return mData.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
private LinearLayout item_contact;
private TextView tv_name;
private TextView tv_phone;
private ImageView img;
public MyViewHolder(View itemView) {
super(itemView);
item_contact = (LinearLayout) itemView.findViewById(R.id.contact_item_id);
tv_name = (TextView) itemView.findViewById(R.id.name_contact);
tv_phone = (TextView) itemView.findViewById(R.id.phone_contact);
img = (ImageView) itemView.findViewById(R.id.img_contact);
}
}
}
Aquí muestro mi XML de como tengo.
help please
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:background="#232323"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:id="#+id/dialog_name_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:text="Contact Name"
android:textColor="#android:color/white"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:id="#+id/dialog_phone_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp"
android:text="Phone Number"
android:textColor="#android:color/white"
android:textSize="23sp" />
<Button
android:id="#+id/dialog_btn_call"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_marginTop="10dp"
android:background="#android:color/white"
android:drawableLeft="#drawable/dialog_call_black"
android:drawablePadding="20dp"
android:padding="20dp"
android:text="Llamar"
android:textAlignment="textStart"
android:textSize="25dp" />
<Button
android:id="#+id/dialog_btn_msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="#android:color/white"
android:drawableLeft="#drawable/dialog_message_black"
android:drawablePadding="20dp"
android:padding="20dp"
android:text="Mensaje"
android:textAlignment="textStart"
android:textSize="25dp" />
</LinearLayout>
<ImageView
android:id="#+id/dialog_img"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="#drawable/ic_contacts"
android:padding="10dp"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
/>
</RelativeLayout>
You need to put setOnClickListener() into OnBindViewHolder().So far what i understand is you have Recycler View in which on item click you want to open a dialog box dialog_contact and this dialog box contains Contact information. After opening this dialog you want to call that person by clicking call button present in dialog.
#Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
holder.tv_name.setText(mData.get(position).getName());
holder.tv_phone.setText(mData.get(position).getPhone());
holder.img.setImageResource(mData.get(position).getPhoto());
holder.item_contact.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myDialog = new Dialog(mContext);
myDialog.setContentView(R.layout.dialog_contact);
myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
myDialog.show();
TextView dialog_name_tv = (TextView)myDialog.findViewById(R.id.dialog_name_id);
Button dialog_btn_call= (Button )myDialog.findViewById(R.id.dialog_btn_call);
TextView dialog_phone_tv = (TextView) myDialog.findViewById(R.id.dialog_phone_id);
ImageView dialog_contact_img = (ImageView)myDialog.findViewById(R.id.dialog_img);
dialog_name_tv.setText(mData.get(vHolder.getAdapterPosition()).getName());
dialog_phone_tv.setText(mData.get(vHolder.getAdapterPosition()).getPhone());
dialog_contact_img.setImageResource(mData.get(vHolder.getAdapterPosition()).getPhoto());
dialog_btn_call.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Do your code here
myDialog.dismiss();
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" +mData.get(holder.getAdapterPosition()).getPhone()));
mContext.startActivity(intent);
}
});
}
});
}
Edit:-

Using vector images in RecycleView

RecyclerView Adapter Class.
public class TravelListAdapter extends RecyclerView.Adapter<TravelListAdapter.ViewHolder> {
Context mContext;
OnItemClickListener mItemClickListener;
String []names = {"Hotels", "Travel", "Medicine", "Education", "Travel", "Hotels"};
private int[] advertImageList = {R.drawable.hotel, R.drawable.travel, R.drawable.medical, R.drawable.education, R.drawable.travel, R.drawable.hotel};
// 2
public void setOnItemClickListener(OnItemClickListener mItemClickListener) {
this.mItemClickListener = mItemClickListener;
}
public TravelListAdapter(Context context) {
this.mContext = context;
}
// 3
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public LinearLayout placeHolder;
public LinearLayout placeNameHolder;
public TextView placeName;
public ImageView placeImage;
public ViewHolder(View itemView) {
super(itemView);
placeHolder = itemView.findViewById(R.id.mainHolder);
placeName = itemView.findViewById(R.id.placeName);
placeNameHolder = itemView.findViewById(R.id.placeNameHolder);
placeImage = itemView.findViewById(R.id.placeImage);
placeHolder.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (mItemClickListener != null) {
mItemClickListener.onItemClick(itemView, getPosition());
}
}
}
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.category_row_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
// final Place place = new PlaceData().placeList().get(position);
holder.placeName.setText(names[position]);
holder.placeName.setTextColor(R.color.black);
Picasso.with(mContext).load(advertImageList[position]).into(holder.placeImage);
Bitmap photo = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.image2);
Palette.generateAsync(photo, new Palette.PaletteAsyncListener() {
public void onGenerated(Palette palette) {
//int bgColor = palette.getMutedColor(mContext.getResources().getColor(android.R.color.transparent));
holder.placeNameHolder.setBackgroundColor(mContext.getResources().getColor(android.R.color.transparent));
}
});
}
#Override
public int getItemCount() {
return names.length;
}
}`
My Xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/placeCard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:cardBackgroundColor="#android:color/transparent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<android.support.v7.widget.AppCompatImageView
android:id="#+id/placeImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
android:transitionName="tImage"
android:layout_centerInParent="true"
android:tint="#android:color/white"
android:padding="10dp"/>
<!-- Used for the ripple effect on touch -->
<LinearLayout
android:id="#+id/mainHolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:selectableItemBackground"
android:orientation="horizontal" />
<LinearLayout
android:id="#+id/placeNameHolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:transitionName="tNameHolder"
android:padding="5dp"
android:layout_below="#+id/placeImage">
<TextView
android:id="#+id/placeName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_horizontal"
android:textAppearance="?android:attr/textAppearanceListItem"
android:textColor="#android:color/white"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
I am using vector images generated from Android Studio but those images are not displayed in recyclerView.
What should i do? Let me mention that the vector images that i use inside recyclerView are properly displayed if i use them in simple image views.

RecyclerView onClick not working properly?

I am using RecyclerView in my fragment to show images with text in Grid format,the Recycler view grid_item.xml look like following:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="#id/card_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_margin="#dimen/card_margin_grid"
card_view:cardCornerRadius="#dimen/card_album_radius">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#id/thumbnail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="?selectableItemBackgroundBorderless"
android:clickable="true"
android:scaleType="fitCenter" />
<TextView
android:id="#id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/thumbnail"
android:layout_margin="#dimen/album_title_padding"
android:textColor="#color/album_title"
android:textSize="#dimen/album_title" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
I am using adapter to populate data in RecyclerView, the code for this is :
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private ArrayList<Movie> movies;
private Context context;
public DataAdapter(Context context, ArrayList<Movie> movies) {
this.movies = movies;
this.context = context;
}
public void addItems(ArrayList<Movie> movies) {
this.movies.addAll(movies);
}
#Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recycler_view_grid_item, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(DataAdapter.ViewHolder viewHolder, int i) {
viewHolder.title.setText(movies.get(i).getTitle());
Picasso.with(context).load(movies.get(i).getImage_url_medium()).placeholder(R.drawable.placeholder).into(viewHolder.thumbnail);
}
#Override
public int getItemCount() {
return movies.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView title;
private ImageView thumbnail;
public ViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
view.setOnClickListener(this);
}
// Handles the row being being clicked
#Override
public void onClick(View view) {
int position = getAdapterPosition(); // gets item position
if (position != RecyclerView.NO_POSITION) { // Check if an item was deleted, but the user clicked it before the UI removed it
Movie movie = movies.get(position);
// start detail activity
Intent i = new Intent(context, MovieDetail.class);
i.putExtra(Constants.MOVIES_OBJECT, movie);
context.startActivity(i);
}
}
}
}
My problem is click listener is only working on the TextView and not on the image , although I have set click listener on whole view which contains both image and text.Is something wrong with my implementation ?
try setting android:focusableInTouchMode="false" to your imageview.
and remove android:clickable="true" or set it to false
Use viewHolder.itemView like :
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//your action
});
This will enable click action on whole RecyclerView item.
Add android:clickable="true" to the root RelativeLayout.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#id/card_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_margin="#dimen/card_margin_grid"
android:clickable="true"
android:focusableInTouchMode="true"
card_view:cardCornerRadius="#dimen/card_album_radius">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#id/thumbnail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="?selectableItemBackgroundBorderless"
android:clickable="false"
android:focusableInTouchMode="false"
android:scaleType="fitCenter" />
<TextView
android:id="#id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/thumbnail"
android:layout_margin="#dimen/album_title_padding"
android:textColor="#color/album_title"
android:textSize="#dimen/album_title" />
</RelativeLayout>
</android.support.v7.widget.CardView>
public class OffersRecycleViewAdapter extends RecyclerView.Adapter {
private Activity mActivity;
private List<OfferShopModel> offerShopModels = new ArrayList<>();
ArrayList<String> allColors = new ArrayList<String>();
public OffersRecycleViewAdapter(List<OfferShopModel> offerShopModels, Activity activity, ArrayList<String> allColors) {
this.offerShopModels = offerShopModels;
this.mActivity = activity;
}
// String[] allColors = mActivity.getResources().getStringArray(R.array.colors);
#Override
public ItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview_offers, viewGroup, false);
return new ItemViewHolder(v);
}
#TargetApi(Build.VERSION_CODES.M)
#Override
public void onBindViewHolder(ItemViewHolder itemViewHolder, final int position) {
itemViewHolder.mOffer.setText(offerShopModels.get(position).getOffer());
}
#Override
public int getItemCount() {
return offerShopModels.size();
}
class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView mLinearLayoutBack;
TextView mOffer;
ItemViewHolder(View itemView) {
super(itemView);
mOffer = (TextView) itemView.findViewById(R.id.offer);
mOffer.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// call click event
}
}
}
remove android:clickable="true" from your ImageView or change it to android:clickable="false"

Material Design RecyclerView Layout

How do I archive a layout for my RecyclerView that looks like this:
I have tried creating it but it did ever look like in the giudlines.
This is taken form the Material Design Guidlines, but I could not find any xml Layouts, except Sketch and or PSDs.
Are there any ressources directl in xml?
Edit 1: I only need the single list item XML layout
Edit 2: I know how to use & implement a RecyclerView
create a .xml that have what you want inside example:
<?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:background="#drawable/gradient_bg"
android:orientation="horizontal"
android:layout_margin="1dp"
android:padding="1dip" >
<LinearLayout android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="17dip"
android:layout_alignParentLeft="true"
android:layout_marginRight="2dip">
<ImageView
android:id="#+id/gasImagem"
android:contentDescription="cover"
android:layout_width="100dip"
android:layout_height="100dip"
/>
</LinearLayout>
<TextView
android:id="#+id/gasTitulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thumbnail"
android:layout_toRightOf="#+id/thumbnail"
android:textColor="#040404"
android:layout_marginTop="30dp"
android:typeface="sans"
android:textSize="20sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/gasPreco"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#000000"
android:textStyle="bold"
android:layout_marginTop="95dp"
android:layout_toRightOf="#+id/thumbnail"/>
<Button
android:id="#+id/btCarro"
android:layout_width="50dp"
android:layout_height="30dp"
android:background = "#drawable/roundedbutton"
android:layout_marginTop="95dp"
android:layout_marginLeft="310dp"
android:drawableTop="#drawable/shoppingcart"
android:textAlignment="center"
/>
</RelativeLayout>
After this create an adapter example
public class MyAdaptadorRecycler extends RecyclerView.Adapter<MyAdaptadorRecycler.ViewHolder> {
private List<Produto>gasList;
private LayoutInflater layout;
public MyAdaptadorRecycler(Context c,List<Produto>l){
gasList = l;
layout = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = layout.inflate(R.layout.list_row,parent,false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.ivcapa.setImageResource(gasList.get(position).getImagem());
holder.tvtitulo.setText(gasList.get(position).getNome());
holder.tvPreco.setText(String.valueOf(gasList.get(position).getPreco()) + "€");
final int posicao = position;
holder.bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Carrinho: ", Toast.LENGTH_SHORT).show();
}
});
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "Recycle Click", Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return gasList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
protected TextView tvtitulo, tvPreco;
protected ImageView ivcapa;
protected Button bt;
public ViewHolder(View itemView) {
super(itemView);
this.tvtitulo = (TextView) itemView.findViewById(R.id.gasTitulo);
this.ivcapa = (ImageView) itemView.findViewById(R.id.gasImagem);
this.tvPreco = (TextView)itemView.findViewById(R.id.gasPreco);
this.bt = (Button)itemView.findViewById(R.id.btCarro);
}
}
}
Maybe you will need a divider exemple:
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
private final int mVerticalSpaceHeight;
public DividerItemDecoration(int mVerticalSpaceHeight) {
this.mVerticalSpaceHeight = mVerticalSpaceHeight;
}
#Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
RecyclerView.State state) {
outRect.bottom = mVerticalSpaceHeight;
//outRect.left = mVerticalSpaceHeight;
//outRect.right = mVerticalSpaceHeight;
}
}
Then in your mainActivity you need to do this:
LinearLayoutManager llm = new LinearLayoutManager(this);
this.rv.setLayoutManager(llm);
rv.addItemDecoration(new DividerItemDecoration(20));
rv.setHasFixedSize(true);
nr= 1;
this.listaPordutos = new ArrayList<Produto>();
this.adapatadorLivros = new MyAdaptadorRecycler(this, listaPordutos);
rv.setAdapter(this.adapatadorLivros);
This is just my exemples, that I use to create a program
hope this can help you, any doubt just say :)
Refer this official documentation link on how to use RecyclerView.LayoutManager http://developer.android.com/training/material/lists-cards.html

Categories

Resources