Firebaserecyclerview Gravity - android

I'm trying to create a Chat App. It is working really good. But now I'd like to gravity the messages. I don't know how I can set the gravity of the sent messages to the right side. viewHolder.mView.setForegroundGravity(); doesn't work.
public void populateViewHolder(final BlogViewHolder viewHolder, final Blog model, final int position) {
DatabaseReference getname = FirebaseDatabase.getInstance().getReference().child("Users").child(userID).child("Username");
getname.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Grün und weiß setzen
String myname = dataSnapshot.getValue(String.class);
if(myname.equals(model.getUsername())){
LinearLayout linear = (LinearLayout)findViewById(R.id.singleMessageContainer);
linear.setGravity(Gravity.RIGHT);
viewHolder.setNachricht(model.getNachricht());
}else{
viewHolder.setNachricht(model.getNachricht());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
public static class BlogViewHolder extends RecyclerView.ViewHolder{
View mView;
public BlogViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setNachricht(String Nachricht){
TextView post_Nachricht = (TextView)mView.findViewById(R.id.textViewMessage);
post_Nachricht.setText(Nachricht);
}
public void setUsername(String Username){
TextView post_Nachricht = (TextView)mView.findViewById(R.id.kommentareimg);
post_Nachricht.setText(Html.fromHtml("<b>" + Username + ":</b>"));
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/singleMessageContainer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dp">
<LinearLayout
android:id="#+id/warpper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textViewMessage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginBottom="2dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="2dp"
android:text="Message"
android:textSize="14sp"
tools:ignore="HardcodedText" />
</LinearLayout>
</LinearLayout>

In your case you might consider having two TextView in your list item layout. Make the layout such that one layout is aligned to the right and the other is aligned to the left. Now you just have to set the visibility of the left or right layout to GONE or VISIBLE.
So you need to have some other functions in your ViewHolder class.
public static class BlogViewHolder extends RecyclerView.ViewHolder{
View mView;
public BlogViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setNachrichtRight(String Nachricht) {
TextView post_Nachricht_Right = (TextView)mView.findViewById(R.id.textViewMessageRight);
TextView post_Nachricht_Left = (TextView)mView.findViewById(R.id.textViewMessageLeft);
post_NachrichtRight.setText(Nachricht);
// Now set the visibility
post_Nachricht_Right.setVisibility(View.VISIBLE);
post_Nachricht_Left.setVisibility(View.GONE);
}
public void setNachrichtLeft(String Nachricht) {
TextView post_NachrichtLeft = (TextView)mView.findViewById(R.id.textViewMessageLeft);
TextView post_NachrichtRight = (TextView)mView.findViewById(R.id.textViewMessageRight);
post_NachrichtLeft.setText(Nachricht);
// Now set the visibility
post_Nachricht_Right.setVisibility(View.VISIBLE);
post_Nachricht_Left.setVisibility(View.GONE);
}
public void setUsername(String Username){
TextView post_Nachricht = (TextView)mView.findViewById(R.id.kommentareimg);
post_Nachricht.setText(Html.fromHtml("<b>" + Username + ":</b>"));
}
}
So inside the onDataChange function you may consider doing something like this.
if(myname.equals(model.getUsername())){
viewHolder.setNachrichtRight(model.getNachricht());
} else {
viewHolder.setNachrichtLeft(model.getNachricht());
}
Finally, your layout may look something like this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/singleMessageContainer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dp">
<TextView
android:id="#+id/textViewMessageRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:alignParentRight="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="2dp"
android:text="Message"
android:textSize="14sp"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/textViewMessageLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alignParentLeft="true"
android:layout_marginBottom="2dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="2dp"
android:text="Message"
android:textSize="14sp"
tools:ignore="HardcodedText" />
</RelativeLayout>

Related

How to implement EmojiTextView in fragment?

I have an emoji in one of my data in cloud firestore. How do I can make the value readable in my fragment,recycleview? When adding old emoji such as 😭, it shows in my recycler view but when ading new emoji such as "🥺". Is it because of my android keyboard or language didn't update? I hope anyone can solve my problem.
ForumAdapter.java
public ForumHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
android.view.View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardviewforumtitle,parent,false);
EmojiCompat.init(new BundledEmojiCompatConfig(v.getContext()));
return new ForumHolder(v);
}
class ForumHolder extends RecyclerView.ViewHolder{
EmojiTextView textViewTitle;
EmojiTextView textViewDescription;
EmojiTextView timeStamp;
/*
TextView textViewTitle;
TextView textViewDescription;
TextView timeStamp;*/
public ForumHolder(View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.tvTitle);
textViewDescription = itemView.findViewById(R.id.tvDescription);
timeStamp = itemView.findViewById(R.id.tvTimestamp);
textViewTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = getAdapterPosition();
// NO_POSITION to prevent app crash when click -1 index
if(position != RecyclerView.NO_POSITION && listener !=null ){
listener.onItemClick(getSnapshots().getSnapshot(position),position);
}
}
});
}
}
Because I got this error
java.lang.ClassCastException: androidx.appcompat.widget.AppCompatTextView cannot be cast to androidx.emoji.widget.EmojiTextView
cardviewforumtitle.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="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="name"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="description"
android:textSize="15sp" />
<TextView
android:id="#+id/tvTimestamp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="timestamp"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageView
android:id="#+id/profilePic"
android:layout_width="match_parent"
android:layout_height="match_parent"></ImageView>
</LinearLayout>
</LinearLayout>
Try replacing the TextViews with
<androidx.emoji.widget.EmojiTextView
..../>
in the layout.

Clickable on items in RecyclerView

I can't click on items in RecyclerView. For example, as shown in the code, I gave Tittle a click feature, but I can't click right now. In the same way I gave deleteimage a click feature, but I can't click. What do I need to do to make the items clickable? Where is the problem? I can't click on the items in RecyclerView, that's the problem.
XML Source Code:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:orientation="horizontal">
<ImageView
android:id="#+id/productImage"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="#drawable/roomlocation" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginTop="9dp"
android:text="productAdi"
android:textSize="19dp"
android:clickable="true"
android:textStyle="bold" />
<TextView
android:id="#+id/productDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginTop="3dp"
android:text="Cihaza ait eklenmiş odalar"
android:textSize="14dp"
android:textStyle="italic" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="vertical">
<ImageView
android:id="#+id/deleteproduct"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:clickable="true"
android:src="#drawable/ic_delete_yellow" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="stechome.berkeylen.firebasedatabase.RoomsActivity">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
<android.support.v7.widget.RecyclerView
android:id="#+id/rvRooms"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
RecyclerView Class Source:
public class RoomsAdapter extends RecyclerView.Adapter<RoomsAdapter.MyViewHolder> {
DatabaseReference ref = mDatabase.child("0").child("Rooms");
public class MyViewHolder extends RecyclerView.ViewHolder {
public MyViewHolder(View view) {
super(view);
productImage = (ImageView)
itemView.findViewById(R.id.productImage);
title = (TextView) view.findViewById(R.id.title);
deleteImage = (ImageView) view.findViewById(R.id.deleteproduct);
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
String data = snapshot.getValue(String.class);
Rooms.add(data);
title.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = 0;
position = getAdapterPosition();
if (getAdapterPosition() == position){
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference().child("0");
rootRef.child("titles").child("0").child("places").setValue(Rooms.get(position));
}
}
});
}
deleteImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (v == deleteproduct) {
deleteproduct(getLayoutPosition());
}
}
});
}
Set click listeners on recycler items by overriding onBindViewHolder method of adapter:
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.myTextView.setOnClickListener(...);
}
here you can find an example: https://developer.android.com/guide/topics/ui/layout/recyclerview
I had the same problem. After trying many things, the only one that worked was to add an atribute on the XML of the item layout:
onClick = the name of the method / function you want to trigger

How to change the indicator color every different cardview?

I create a cardview design with indicator in the left (Red, Green) but i have problem, how can i change the indicator color depends on the status from database. If the status say "No" then the indicator turn red and if "Yes" the indicator turn green.
This is the xml file :
<?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="wrap_content"
android:orientation="vertical">
<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:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="6dp"
card_view:cardElevation="3dp"
card_view:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.9">
<ImageView
android:layout_marginLeft="22dp"
android:layout_marginTop="14dp"
android:layout_marginBottom="14dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/rectangle"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.1"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_card_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="#+id/txt_order_date"
android:textSize="13sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/txt_customer_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/txt_order_number"
android:textSize="12sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/txt_product_status"
android:textSize="12sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/txt_notes"
android:textSize="12sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="false"
android:layout_weight="1"
android:maxLines="4"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#drawable/layout_bg"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal"
android:layout_below="#+id/layout_card_item"
android:gravity="center"
android:weightSum="1">
<Button
android:id="#+id/download_pdf"
android:textSize="14sp"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:background="#e1e3e8"
android:focusable="false"
android:textColor="#000307"
android:focusableInTouchMode="true"
android:drawableLeft="#drawable/ic_pictogram_download_orange"
android:text=" Download PDF"
/>
<!--<ImageButton-->
<!--android:layout_width="10dp"-->
<!--android:layout_height="10dp"-->
<!--android:src="#mipmap/ic_order_download_pdf"/>-->
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
This is the adapter :
public class OrderListAdapter extends RecyclerView.Adapter<OrderListAdapter.OrderListViewHolder> {
private ArrayList<OrderListModel> itemOrderList;
public OrderListAdapter(ArrayList<OrderListModel> itemOrderList) {
this.itemOrderList = itemOrderList;
}
#Override
public OrderListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.order_list_item, parent, false);
return new OrderListViewHolder(view);
}
#Override
public void onBindViewHolder(OrderListViewHolder orderListViewHolder, int position) {
orderListViewHolder.txtDate.setText(itemOrderList.get(position).getDate());
orderListViewHolder.txtOrderNumber.setText(itemOrderList.get(position).getOrderNumber());
orderListViewHolder.txtCustomerName.setText(itemOrderList.get(position).getCustomerName());
orderListViewHolder.txtProductStatus.setText(itemOrderList.get(position).getProductStatus());
orderListViewHolder.txtNotes.setText(itemOrderList.get(position).getNotes());
}
#Override
public int getItemCount() {
return (itemOrderList != null) ? itemOrderList.size() : 0;
// return itemOrderList.size();
}
public class OrderListViewHolder extends RecyclerView.ViewHolder{
private TextView txtDate, txtOrderNumber, txtCustomerName, txtProductStatus,
txtNotes;
public OrderListViewHolder(View itemView) {
super(itemView);
txtDate = (TextView) itemView.findViewById(R.id.txt_order_date);
txtOrderNumber = (TextView) itemView.findViewById(R.id.txt_order_number);
txtCustomerName = (TextView) itemView.findViewById(R.id.txt_customer_name);
txtProductStatus = (TextView) itemView.findViewById(R.id.txt_product_status);
txtNotes = (TextView) itemView.findViewById(R.id.txt_notes);
}
}
}
This is the main :
public class OrderListFragment extends Fragment {
private RecyclerView recyclerView;
private OrderListAdapter adapter;
private ArrayList<OrderListModel> OrderListArrayList;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_order_list, container, false);
getActivity().setTitle(R.string.title_mn_order_list);
addData();
recyclerView = (RecyclerView) view.findViewById(R.id.orderList_recycler_view);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this.getActivity());
recyclerView.setLayoutManager(layoutManager);
adapter = new OrderListAdapter(OrderListArrayList);
recyclerView.setAdapter(adapter);
return view;
}
void addData(){
OrderListArrayList = new ArrayList<>();
OrderListArrayList.add(new OrderListModel(
"4 Juli 2018 10:54",
"0001",
"Jopa",
"No",
"Notes"));
OrderListArrayList.add(new OrderListModel(
"4 Juli 2018 10:54",
"0001",
"Jopa",
"Yes",
"Notes"));
}
}
Do you have any idea how can i change it?
You must give an id to your ImageView say status
In OrderListViewHolder add in your declarations private ImageView status;
In OrderListViewHolder add status = (ImageView) itemView.findViewById(R.id.status);
In OrderListViewHolder add
if (itemOrderList.get(position).getStatus().equals("Yes")) {
orderListViewHolder.status.setBackgroundColor(0x00ff00);
} else {
orderListViewHolder.status.setBackgroundColor(0xff0000); }
of course you must create the getStatus() method like all the others getSomething()
Assuming, that your ImageView is your indicator bar:
<ImageView
android:id="#+id/ivIndicator" <!-- Add this id -->
android:layout_marginLeft="22dp"
android:layout_marginTop="14dp"
android:layout_marginBottom="14dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/rectangle"/>
Get a reference to it in your OrderListViewHolder:
ivIndicator = (ImageView) itemView.findViewById(R.id.ivIndicator);
And then tint your imageview in onBindViewHolder:
if("Yes".equals(itemOrderList.get(position).getStatus())) {
orderListViewHolder.ivIndicator.setColorFilter(ContextCompat.getColor(activity, android.R.color.green))
else {
orderListViewHolder.ivIndicator.setColorFilter(ContextCompat.getColor(activity, android.R.color.red))
}
Make two drawable files like #drawable/rectangle, set it with different color you want.
if(status.equals("No") {
yourView.setBackground(context.getResources().getDrawable(R.drawable.rectangleWithRed));
}
else if (status.equals("Yes") {
yourView.setBackground(context.getResources().getDrawable(R.drawable.rectangleWithGreen);
}
if you want to change the color according to the status, then on onBindViewHolder of your Recycler Adapter, check for the status and set to desired color.
if(status.equals("Yes")
{
rectangleImageView.setColor(Color.GREEN);
}
else
{
rectangleImageView.setColor(Color.RED);
}
public void onBindViewHolder(OrderListViewHolder orderListViewHolder, int position) {
if (OrderListModel.getProductStatus().equals("Yes"))
rectangleImageView.setColorFilter(getContext().getResources().getColor(R.color.green));
} else {
rectangleImageView.setColorFilter(getContext().getResources().getColor(R.color.red));
}
}

How to delete space when I hide a CardView

How can I delete the space when I hide a CardView? I doing an app with android and Firebase and I show the information from firebase in cardviews, but when the info isn't correct the cardview must disappear. The point is that the cardview disappear, but still uses a space in the layout, I've tried the answers in
setVisibility(GONE) view becomes invisible but still occupies space But it doesn't work for me.
#Override
protected void onStart() {
super.onStart();
firebaseAuth.addAuthStateListener(firebaseAuthListener);
final FirebaseRecyclerAdapter<CursosDB, MainActivity.CursosPViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<CursosDB, MainActivity.CursosPViewHolder>(
CursosDB.class,
R.layout.design_row_cursos,
MainActivity.CursosPViewHolder.class,
myRef)
{
#Override
protected void populateViewHolder(MainActivity.CursosPViewHolder viewHolder, final CursosDB Cmodel, int position)
{
String estado = Cmodel.getSTATUS().toString();
if (estado.equals("OK")){
viewHolder.setPhotoURL(getApplicationContext(), Cmodel.getURI());
viewHolder.setTitle(Cmodel.getTITLE());
viewHolder.setCiudad(Cmodel.getPLACE());
viewHolder.setLevel(Cmodel.getLEVEL());
viewHolder.setDur(Cmodel.getDURACION());
viewHolder.setPrice(Cmodel.getCOSTO());
}else{
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) viewHolder.mView.getLayoutParams();
layoutParams.setMargins(0,0,0,0);
viewHolder.mView.setLayoutParams(layoutParams);
viewHolder.mView.setVisibility(View.INVISIBLE);
viewHolder.mView.setVisibility(View.GONE);
//viewHolder.mView.setVisibility(View.GONE);
//Toast.makeText(getApplicationContext(), "Ningún curso aprobado aún", Toast.LENGTH_SHORT).show();
}
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(getApplicationContext(), Cmodel.getUSERID(), Toast.LENGTH_LONG).show();
//Toast.makeText(getApplicationContext(), Cmodel.getTITLE(), Toast.LENGTH_SHORT).show();
//Toast.makeText(UsersList.this, user_key, Toast.LENGTH_LONG).show();
}
});
}
};
mCourses.setAdapter(firebaseRecyclerAdapter);
}
This is my layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardview_cursos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:onClick="positionAction"
android:padding="4dp"
android:paddingBottom="4dp"
android:paddingEnd="4dp"
android:paddingStart="4dp"
android:paddingTop="4dp"
app:cardElevation="4dp">
<LinearLayout android:id="#+id/layout_to_hide"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/Cimg"
android:layout_width="match_parent"
android:layout_height="165dp"
android:scaleType="center"
app:srcCompat="#drawable/knowit_logo" />
<TextView
android:id="#+id/Ctitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/Cimg"
android:layout_marginLeft="14dp"
android:layout_marginStart="14dp"
android:paddingTop="8dp"
android:text="Titulo"
android:textStyle="bold" />
<TextView
android:id="#+id/Clugar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Ctitle"
android:layout_alignStart="#+id/Ctitle"
android:layout_below="#+id/Ctitle"
android:layout_marginTop="10dp"
android:text="Lugar" />
<TextView
android:id="#+id/textView29"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Clugar"
android:layout_alignStart="#+id/Clugar"
android:layout_below="#+id/Clugar"
android:layout_marginTop="10dp"
android:text="$" />
<TextView
android:id="#+id/Cprice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView29"
android:layout_marginBottom="4dp"
android:layout_toEndOf="#+id/textView29"
android:layout_toRightOf="#+id/textView29"
android:paddingLeft="5dp"
android:text="0.0" />
<TextView
android:id="#+id/Cdur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/Clugar"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="126dp"
android:layout_marginRight="126dp"
android:text="Duración" />
<TextView
android:id="#+id/Cnivel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/Cprice"
android:layout_alignLeft="#+id/Cdur"
android:layout_alignStart="#+id/Cdur"
android:layout_marginBottom="0dp"
android:text="Nivel" />
</RelativeLayout>
</LinearLayout> </android.support.v7.widget.CardView>
This is the mView:
public static class CursosPViewHolder extends RecyclerView.ViewHolder {
View mView;
public CursosPViewHolder(final View itemView) {
super(itemView);
mView = itemView;
}
public void setTitle(String title){
TextView post_title = (TextView)mView.findViewById(R.id.Ctitle);
post_title.setText(title);
}
public void setCiudad (String ciudad){
TextView post_city = (TextView)mView.findViewById(R.id.Clugar);
post_city.setText(ciudad);
}
public void setPhotoURL(Context ctx, String pgotouserurl) {
ImageView post_image =(ImageView)mView.findViewById(R.id.Cimg);
Picasso.with(ctx).load(pgotouserurl).into(post_image);
} }
I hope you can help me.
First the XML code you've posted ils wrong closed,(must finish with a CardView tag)
On addition be sure you're pointing the CardView layout with mView field ans Only use GONE or INVISIBLE,
INVISIBLE change your Panel ( Root view ) structure but GONE don't

Why does Checkbox widget not appear on RecyclerView's CardView?

I have a RecyclerView list populated by CardViews. I tried to add a Checkbox to the left side of the CardView and nothing happens. Only the two TextViews appear. What am I missing here?
card_layout.xml:
...
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/singlecard_view1"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardBackgroundColor="#android:color/white"
card_view:cardCornerRadius="6dp"
android:orientation="horizontal"
android:layout_margin="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:selectableItemBackground" >
<CheckBox
android:id="#+id/chkSelected"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/cardBlankText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_toRightOf="#+id/chkSelected"
android:layout_toEndOf="#+id/chkSelected"
android:textStyle="bold"
android:textColor="#android:color/black"
android:textAppearance="?android:attr/textAppearanceLarge"
android:clickable="true"
android:gravity="center_vertical"
android:textAlignment="center" />
<TextView
android:id="#+id/cardBlankText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_toRightOf="#+id/cardBlankText2"
android:layout_toEndOf="#+id/cardBlankText2"
android:textStyle="bold"
android:textColor="#android:color/black"
android:textAppearance="?android:attr/textAppearanceLarge"
android:clickable="true"
android:gravity="center_vertical"
android:textAlignment="center" />
</RelativeLayout>
</android.support.v7.widget.CardView>
...
DataModel.java:
public class DataModel {
private String todo;
private String note1;
private boolean isSelected;
public String getTodo() {
return todo;
}
public void setTodo(String todo) {
this.todo = todo;
}
public String getNote1() {
return note1;
}
public void setNote1(String note1) {
this.note1 = note1;
}
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean isSelected) {
this.isSelected = isSelected;
}
}
Adapter.java:
...
public class ListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
CheckBox chkSelected;
TextView cardBlankText2;
TextView cardBlankText3;
public ListViewHolder(View itemView) {
super(itemView);
chkSelected = (CheckBox) itemView.findViewById(R.id.chkSelected);
cardBlankText2 = (TextView)itemView.findViewById(R.id.cardBlankText2);
cardBlankText3 = (TextView)itemView.findViewById(R.id.cardBlankText3);
#Override
public void onBindViewHolder(ListViewHolder holder, int position) {
holder.chkSelected.setChecked(dbList.get(position).isSelected());
holder.chkSelected.setTag(dbList.get(position));
holder.cardBlankText2.setText(dbList.get(position).getTodo());
holder.cardBlankText3.setText(dbList.get(position).getNote1());
}
...
}
Probably the problem of third textview's match parent width
<TextView
android:id="#+id/cardBlankText3"
android:layout_width="match_parent"
It's a common bug that "match_parent" couldn't just fill up the rest of the space but occupied the whole view. (And, yes, in preview, it's totally no problem)
If you want to have the third textview fill up just the rest space after the checkbox and 2nd textview. You could try
<TextView
android:id="#+id/cardBlankText3"
android:layout_width="0dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
To avoid textview 2 is too long and leave no space for textview 3, you should also set a max width too. Something like this.
<TextView
android:id="#+id/cardBlankText2"
android:maxWidth="160dp"
Just take a magic number you like.
If you want to have textview 2 and textview 3 divide the space evenly. You can make use of LinearLayout, weight_sum and weight.

Categories

Resources