Spinner in ListView seems to appears outside of ListView - java

I am now developing a App which has a spinner in ListView.
It seems for me that spinner appuears outside of Listview.
What causes the problem ?
The window is as follows: (See 2 Red Circles)
Problem(Red Circle)
My MainActivity.xml is as follows :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
android:id="#+id/mainView"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/relativeLayoutButton">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_camera"
android:id="#+id/buttonCapture" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_receipt"
android:id="#+id/buttonSelect"
android:layout_toEndOf="#+id/buttonCapture"
android:layout_marginStart="20dp" />
</RelativeLayout>
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/switchAddSelection"
android:text="#string/addSelection"
android:textSize="20sp"
android:gravity="end"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textViewDate"
android:textSize="20sp"
android:layout_below="#+id/relativeLayoutButton"
android:layout_centerHorizontal="true"/>
<ListView
android:id="#+id/listViewBoughtItems"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/buttonSave"
android:layout_below="#+id/textViewDate" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonSave"
android:src="#drawable/ic_database"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
My List Item xml os as follows:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="30dp"
android:layout_height="wrap_content"
android:id="#+id/textViewNumber"
android:textSize="14sp"
android:gravity="end"
android:layout_centerVertical="true"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editTextItem"
android:inputType="textMultiLine"
android:textSize="14sp"
android:layout_toEndOf="#+id/textViewNumber"
android:layout_toStartOf="#+id/buttonDelete"
android:layout_marginStart="10dp"/>
<Spinner
android:id="#+id/spinnerCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editTextItem"
android:layout_marginStart="10dp"
android:layout_toEndOf="#+id/textViewNumber" />
<TextView
android:id="#+id/textViewCurrencySymbol"
android:layout_width="20dp"
android:layout_height="40dp"
android:layout_below="#+id/editTextItem"
android:layout_toStartOf="#+id/editTextPrice"
android:gravity="end|center_vertical"
android:textSize="14sp" />
<EditText
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/editTextPrice"
android:singleLine="true"
android:gravity="end"
android:inputType="numberDecimal"
android:textSize="14sp"
android:layout_toStartOf="#+id/buttonDelete"
android:layout_below="#+id/editTextItem"/>
<Button
android:layout_width="30dp"
android:layout_height="30dp"
android:id="#+id/buttonDelete"
android:background="#drawable/ic_cancel_red_24dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginStart="10dp"/>
</RelativeLayout>
My code concerning spinner is as follows :
public View getView(final int position, View convertView, #NonNull ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.bought_list_item, null);
}
final ItemBean item = this.getItem(position);
if (item != null) {
TextView mNumber = convertView.findViewById(R.id.textViewNumber);
mNumber.setText(String.valueOf(position + 1));
mBoughtItem = convertView.findViewById(R.id.editTextItem);
mBoughtItem.setText(item.getBoughtItem());
mBoughtItem.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
gReceipt.boughtItemList.get(position).description = mBoughtItem.getText().toString();
}
});
TextView mCurrencySymbol = convertView.findViewById(R.id.textViewCurrencySymbol);
mCurrencySymbol.setText(R.string.currencySymbol);
mPrice = convertView.findViewById(R.id.editTextPrice);
mPrice.setText(String.format(Locale.JAPANESE, "%.1f", item.getPrice()));
mPrice.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
gReceipt.boughtItemList.get(position).price = Float.valueOf(mPrice.getText().toString());
}
});
mSpinner = convertView.findViewById(R.id.spinnerCategory);
ArrayAdapter<String> adapterCategory = new ArrayAdapter<>(thisContext, android.R.layout.simple_spinner_item);
adapterCategory.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapterCategory.add("AAA");
adapterCategory.add("BBB");
adapterCategory.add("CCC");
mSpinner.setAdapter(adapterCategory);
}
return convertView;
}
}
P.S.
This phenomenon occurs on Scrolling ListView.
I am appreciated any advise.
Thank you in advance.

That's wired problem! try this and check it's ok or not
<ListView
android:id="#+id/listViewBoughtItems"
android:layout_width="match_parent"
android:layout_height="wrap_content" //change this line from match_parent to wrap_content
android:layout_above="#+id/buttonSave"
android:layout_below="#+id/textViewDate" />

Related

buttons in Onclick Recycler View doesn't work

when Click on Button it suppose to Open a browser but it didn't get to the code to start the Activity
public class RecyclerViewAdapterDetailsScreen extends RecyclerView.Adapter<RecyclerViewAdapterDetailsScreen.ViewHolder> {
private Context mContext;
private int numberOfButtons;
private String [] trailers;
public RecyclerViewAdapterDetailsScreen(Context mContext, int numberOfButtons,String [] trailers) {
this.numberOfButtons=numberOfButtons;
this.mContext = mContext;
this.trailers=trailers;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem2, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, final int position) {
/*** Toast.makeText(mContext, trailers[position]+" From OnBindViewHolder", Toast.LENGTH_SHORT).show();
* Toast is working from Here but no sign from on click
*/
holder.parentlayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
// TOAST IS NOT WORKING SO THE CODE DOESNT gett HERE...............................
Toast.makeText(mContext, trailers[position], Toast.LENGTH_SHORT).show();
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(trailers[position]));
mContext.startActivity(browserIntent);
}
});
}
#Override
public int getItemCount() {
return trailers.length;
}
public class ViewHolder extends RecyclerView.ViewHolder
{
Button mbutton;
RelativeLayout parentlayout;
public ViewHolder(#NonNull View itemView) {
super(itemView);
mbutton=itemView.findViewById(R.id.button);
parentlayout = itemView.findViewById(R.id.parent_layout2);
}
}
}
layout_listitem2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/parent_layout2"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
/>
</RelativeLayout>
activity_details_screen.xml that has the recyclerview
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DetailsScreen">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/image_thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
<TextView
android:id="#+id/original_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="25sp"
android:layout_below="#+id/image_thumbnail"
android:layout_centerHorizontal="true"
/>
<TextView
android:id="#+id/overview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/original_title"
android:textSize="18sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="vote average "
android:id="#+id/vote"
android:layout_below="#+id/overview"
android:textStyle="bold"
android:textSize="20sp"/>
<TextView
android:id="#+id/vote_average"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/vote"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="release date"
android:layout_below="#id/vote_average"
android:textStyle="bold"
android:textSize="20sp"
android:id="#+id/release"
/>
<TextView
android:id="#+id/release_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/release"
/>
<TextView
android:id="#+id/trailers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="25sp"
android:layout_below="#+id/release_date"
android:layout_centerHorizontal="true"
android:text="Trailers"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view_details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/trailers"
android:padding="8dp"
/>
</RelativeLayout>
</ScrollView>
it has to get in the onClick and start new activity to open youtube app or browser if any missing code you wanna review tell me
Your code is perfect.
Only one problem is here, your parent_layout2 is not getting click-event it is consumed by child(the button).As by Android definition first priority is given to child.
Just update parents width to match_parent. It will work. I have tested your code just now.
Here is your updated xml
layout_listitem2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/parent_layout2"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
/>
</RelativeLayout>
Happy Coding. Please Vote if it works.
Thanks

How show a Horizontal Recyclerview - have trouble showing horizontal items

I want to show horizontal items in recyclerview, but I've tried but no work it all, only show me horizontal items with enlarge widh.
In Android Studio show as I want
But in runtime in my movile, show me
The items is width wraped but the other items appear after scroll right separated of my previus item.
This is my code.
LIST_ITEM
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/linearFondo"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="#drawable/item_list_sel"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView2"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
app:srcCompat="#drawable/ruta"
tools:srcCompat="#drawable/ruta" />
<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/tnombre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:inputType="textMultiLine"
android:lines="1"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textSize="12sp"
android:textStyle="bold"></TextView>
<TextView
android:id="#+id/thorarios"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textAppearance="#style/TextAppearance.AppCompat.Small"
android:textColor="#color/colorPrimary"
android:textSize="10sp"
android:visibility="gone"></TextView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
And My Xml to show the recyclerview
<LinearLayout 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"
android:orientation="vertical"
tools:context="sitetech.NFCcheckPoint.ui.operador.CheckFragment">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#color/SuccessColor"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<TextView
android:id="#+id/tfecha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Fecha"
android:textSize="20sp" />
<Button
android:id="#+id/bpruebas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pasar Tarjeta" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="Ruta para el registro" />
<com.omega_r.libs.omegarecyclerview.OmegaRecyclerView
android:id="#+id/rlista"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#888"
android:dividerHeight="0dp"
android:fadeScrollbars="true"
android:orientation="horizontal"
android:overScrollMode="never"
android:scrollbarAlwaysDrawHorizontalTrack="true"
android:scrollbarAlwaysDrawVerticalTrack="false"
android:scrollbars="horizontal"
app:itemSpace="0dp"
tools:listitem="#layout/ruta_sel_template">
</com.omega_r.libs.omegarecyclerview.OmegaRecyclerView>
</LinearLayout>
</LinearLayout>
And my Fragment code to load
dataAdapter = new rutaSelAdapter(lista, new onItemClick() {
#Override
public void onClickItemList(View v, int position) {
ToastHelper.info(lista.get(position).getNombre().toString());
}
});
rlista.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
rlista.setAdapter(dataAdapter);
And my dataAdapter class
public class rutaSelAdapter extends OmegaRecyclerView.Adapter<rutaSelAdapter.ViewHolder> {
public List<Ruta> lista;
private onItemClick onItemClick;
private Ruta selectedItem;
public rutaSelAdapter(List<Ruta> l, onItemClick onclick) {
lista = l;
this.onItemClick = onclick;
if (l.size() > 0)
setSelectedItem(l.get(0));
}
#Override
public int getItemCount() {
return lista.size();
}
#Override
public rutaSelAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new rutaSelAdapter.ViewHolder(parent);
}
#Override
public void onBindViewHolder(rutaSelAdapter.ViewHolder holder, int position) {
Ruta dato = lista.get(position);
holder.display(dato);
}
public void setSelectedItem(Ruta rx){
selectedItem = rx;
}
public Ruta getSelectedItem(){
return selectedItem;
}
public void updateData(Ruta bx) {
boolean nuevo = true;
for (Ruta rx : lista) {
if (rx.getId() == bx.getId()) {
lista.set(lista.indexOf(rx), bx);
nuevo = false;
}
ToastHelper.info("Se a modificado el horario.");
}
if (nuevo) {
lista.add(bx);
ToastHelper.exito("Horario asignado.");
}
notifyDataSetChanged();
}
public void deleteData(Ruta rx) {
//ToastHelper.normal("Se a eliminado el horario " + rx.getHora());
lista.remove(rx);
notifyDataSetChanged();
}
public class ViewHolder extends SwipeViewHolder implements View.OnClickListener {
private final TextView tnombre;
private LinearLayout linearFondo;
HorarioDao horarioManager = AppController.daoSession.getHorarioDao();
private Ruta currentItem;
public ViewHolder(ViewGroup itemView) {
super(itemView, R.layout.ruta_sel_template, SwipeViewHolder.NO_ID, SwipeViewHolder.NO_ID);
tnombre = findViewById(R.id.tnombre);
linearFondo = findViewById(R.id.linearFondo);
contentView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setSelectedItem(currentItem);
onItemClick.onClickItemList(v, getAdapterPosition());
}
});
}
private Ruta backItem;
private void setSelection(Ruta rx){
//if (backItem == null)
}
#Override
public void onClick(final View v) {
}
public void display(Ruta rx) {
currentItem = rx;
if (rx.getNombre() == null) tnombre.setText("");
else tnombre.setText(rx.getNombre().toString());
}
}
}
In Android Studio show me right as I want.
But in runtime, in my mobile the items have full width but wrap content.enter image description here
i think you just add notifyDataSetChanged in your class adapter
public rutaSelAdapter(List<Ruta> l, onItemClick onclick) {
lista = l;
this.onItemClick = onclick;
if (l.size() > 0)
setSelectedItem(l.get(0));
notifyDataSetChanged();
}
In your recyclerview item layout update width of these 2 textviews [tnombre and thorarios] to wrap_content
<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/tnombre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:inputType="textMultiLine"
android:lines="1"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textSize="12sp"
android:textStyle="bold"></TextView>
<TextView
android:id="#+id/thorarios"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textAppearance="#style/TextAppearance.AppCompat.Small"
android:textColor="#color/colorPrimary"
android:textSize="10sp"
android:visibility="gone"></TextView>
</LinearLayout>
If someone else have this issue, I fixed adding this line in the fragment class.
rlista.setHasFixedSize(true);

Listview items duplicated/wrong position when scrolling

This question is been asked multiple times, but everyone has his specific usage of listview adapters (ArrayAdapter or BaseAdapter).
I searched a lot before asking this question, and this same problem happened to me before and i solved it using Holder class, and cleaning code to make it the minimum possible.
But now, i am creating another interface which contains a listview and needs a BaseAdapter to fill it using a List of Object (List).
The weird thing in my case is, if my listview contains only 4 items ( 4 rows ) then everything is ok, but if it contains more than 4, first items will be duplicated, and if i scroll down/up fast , items got wrong positions.
Here is my code :
public class OrdersAdapter extends BaseAdapter {
List<Order> ordersList;
FragmentActivity activity;
private int height;
public OrdersAdapter(FragmentActivity activity, List<Order> ordersList){
this.activity = activity;
this.ordersList = ordersList;
height = Constants.getOneThirdScreenHight(activity);
}
#Override
public int getCount() {
return ordersList.size();
}
#Override
public Order getItem(int position) {
return ordersList.get(position);
}
#Override
public long getItemId(int position) {
return ordersList.hashCode();
}
private class MyHolder{
TextView orderNumber, orderDate, menuCount, orderPrice, orderStatus, orderTelephone, orderStoreName;
ImageView orderDelete, menuPicture;
Order order, mOrder;
String numberText = activity.getResources().getString(R.string.order_number);
HashMap<String, String> map;
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
}
#Override
public View getView (final int position, View convertView, ViewGroup parent) {
View vi;
final MyHolder holder;
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater)activity.getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
holder = new MyHolder();
vi = layoutInflater.inflate(R.layout.sent_order_item, null);
holder.order = getItem(position);
holder.mOrder = new Order();
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams.height = height;
vi.findViewById(R.id.order_history_layout).setLayoutParams(layoutParams);
holder.orderNumber = (TextView) vi.findViewById(R.id.order_number);
holder.orderDate = (TextView) vi.findViewById(R.id.order_date);
holder.orderDelete = (ImageView) vi.findViewById(R.id.order_delete);
holder.orderPrice = (TextView) vi.findViewById(R.id.order_history_price);
holder.orderStatus = (TextView) vi.findViewById(R.id.order_history_status);
holder.orderStoreName = (TextView) vi.findViewById(R.id.order_history_store);
holder.orderTelephone = (TextView) vi.findViewById(R.id.order_history_phone);
holder.menuCount = (TextView) vi.findViewById(R.id.order_menu_count1) ;
holder.menuPicture = (ImageView) vi.findViewById(R.id.order_picture);
vi.setTag(holder);
}else{
vi = convertView;
holder = (MyHolder) vi.getTag();
}
//Getting Restaurants infos ( name and telephone ) from firebase
holder.mDatabase.child(Constants.STORES_PROFILES + "/" + holder.order.getOwner_uid() + "/" + holder.order.getStore_id())
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(holder.map==null){
holder.map = (HashMap<String, String>)dataSnapshot.getValue();
}
if(dataSnapshot.getValue()!=null) {
holder.orderStoreName.setText(holder.map.get(Constants.NAME));
holder.orderTelephone.setText(holder.map.get(Constants.TELEPHONE));
}
//notifyDataSetChanged();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
//checking Order's status (received, validated, in progress, delivered)
holder.mDatabase.child(Constants.ORDERS + "/" + holder.order.getOwner_uid() + "/" + holder.order.getStore_id() + "/" + holder.order.getOrderId())
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getValue() != null) {
try {
holder.mOrder = dataSnapshot.getValue(Order.class);
if (!holder.order.getStatus().equals(holder.mOrder.getStatus())) {
holder.order.setStatus(holder.mOrder.getStatus());
//update status value for the user
holder.mDatabase.child(Constants.USERS+"/"+ Authentication.getCurrentUser().getUid()+"/"+Constants.SUBMITTED_ORDERS_KEY
+"/"+holder.order.getOrderId()+"/"+Constants.STATUS).setValue(holder.mOrder.getStatus());
//notifyDataSetChanged();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
displayOrder(holder);
return vi;
}
public void displayOrder(MyHolder holder){
holder.orderStatus.setText(holder.order.getStatus());
holder.orderNumber.setText(holder.numberText + holder.order.getOrderNumber());
holder.orderDate.setText(Utils.parseDateToDisplay((Long)holder.order.getCreated_at()));
holder.menuCount.setText(holder.order.getMyMealsList().get(0).getQuantity()+"");
holder.orderPrice.setText(holder.order.getTotal_price_formatted());
Glide.with(activity)
.load(holder.order.getMyMealsList().get(0).getMain_image())
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(holder.menuPicture);
if(OrdersContainerFragment.pDialog.isShowing()){
OrdersContainerFragment.pDialog.dismiss();
}
}
}
Here is my "xml" file :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/order_history_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100">
<LinearLayout
android:id="#+id/save_order_linear"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="25"
android:background="#fcf5ef"
android:orientation="horizontal"
android:weightSum="100">
<TextView
android:id="#+id/order_number"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="50"
android:gravity="center"
android:text="#string/order_number"
android:textColor="#color/colorPrimary"
android:textStyle="bold"
android:typeface="serif"/>
<TextView
android:id="#+id/order_date"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="50"
android:gravity="center"
android:text="00/00/0000"
android:textColor="#color/colorPrimary"
android:typeface="serif"/>
</LinearLayout>
<LinearLayout
android:id="#+id/save_order_pics"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="#+id/save_order_linear"
android:layout_marginLeft="5dp"
android:layout_marginTop="15dp"
android:layout_weight="74"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:weightSum="100">
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="33">
<ImageView
android:id="#+id/order_picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<ProgressBar
android:id="#+id/order_progressBar1"
style="#android:style/Widget.DeviceDefault.Light.ProgressBar.Large"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:indeterminate="true"
android:indeterminateDrawable="#drawable/my_progressbar"
android:padding="22dp"
android:visibility="gone"/>
<TextView
android:id="#+id/order_menu_count1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="false"
android:layout_alignParentEnd="false"
android:layout_alignParentRight="false"
android:gravity="bottom|right"
android:paddingBottom="15dp"
android:paddingRight="15dp"
android:text="ABCD"
android:textColor="#color/gray"
android:textSize="22dp"
android:typeface="serif"/>
</android.support.v7.widget.CardView>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="67"
android:orientation="vertical"
android:paddingLeft="15dp"
android:weightSum="100">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="25"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignParentTop="false"
android:layout_centerHorizontal="false"
android:layout_weight="1"
android:text="#string/order_price"
android:textColor="#color/colorPrimary"
android:typeface="monospace"/>
<TextView
android:id="#+id/order_history_price"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignParentTop="false"
android:layout_centerHorizontal="false"
android:layout_weight="1"
android:text="..."
android:textColor="#color/colorPrimary"
android:typeface="monospace"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="25"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:text="#string/order_status"
android:textColor="#color/colorPrimary"
android:typeface="monospace"/>
<TextView
android:id="#+id/order_history_status"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:text="..."
android:textColor="#color/colorAccent"
android:typeface="monospace"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="25"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:text="#string/Phone"
android:textColor="#color/colorPrimary"
android:typeface="monospace"/>
<TextView
android:id="#+id/order_history_phone"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:text="..."
android:textColor="#color/colorPrimary"
android:typeface="monospace"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="25"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:text="#string/order_store"
android:textColor="#color/colorPrimary"
android:typeface="monospace"/>
<TextView
android:id="#+id/order_history_store"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:text="..."
android:textColor="#color/colorPrimary"
android:typeface="monospace"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/save_order_pics"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:background="#fcf5ef"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>

how to automatically align long text in next line in TextView under CardView

I am using a TextView under android.support.v7.widget.CardView. However, whenever I try to set a long text, TextView is not giving the full result.
List<User> userList = new ArrayList<>();
userList.add(new User(R.mipmap.ic_launcher,
"Amit", "9988776655",
"amit#sonevalley.comamit#sonevalley.comamit#sonevalley.comamit#sonevalley.comamit#sonevalley.comamit#sonevalley.com"));
This is my java code(as an example) and this shows in the app like this:
for this first one 'Amit'.
How to solve this? If the text is long then it will automatically set it to next line.
Here is my full Cardview.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<android.support.v7.widget.CardView
android:id="#+id/cvSingleUser"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp">
<ImageView
android:id="#+id/ivProfilePic"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="16dp" />
<TextView
android:id="#+id/tvProfileName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_toRightOf="#id/ivProfilePic" />
<TextView
android:id="#+id/tvPhoneNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tvProfileName"
android:layout_toRightOf="#id/ivProfilePic" />
<TextView
android:id="#+id/tvEmailId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false"
android:scrollHorizontally="true"
android:layout_below="#id/tvPhoneNumber"
android:layout_toRightOf="#id/ivProfilePic" />
</RelativeLayout>
</android.support.v7.widget.CardView>
and this is the snapshot of the java code:Java Snap Shot
this is the java full code
public class AllUsersAdapter extends RecyclerView.Adapter<AllUsersAdapter.UserViewHolder>{
private List<MainActivity.User> userList;
private Context context;
public AllUsersAdapter(List<MainActivity.User> userList, Context context) {
this.userList = userList;
this.context = context;
}
#Override
public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.single_cardview_layout, null);
UserViewHolder userViewHolder = new UserViewHolder(view);
return userViewHolder;
}
#Override
public void onBindViewHolder(UserViewHolder holder, int position) {
MainActivity.User user = userList.get(position);
String a=user.getEmailId().toString();
holder.tvProfileName.setText(user.getProfileName());
holder.tvPhoneNumber.setText(user.getPhoneNumber());
holder.tvEmailId.setText(a);
}
#Override
public int getItemCount() {
return userList.size();
}
public static class UserViewHolder extends RecyclerView.ViewHolder {
TextView tvProfileName;
TextView tvPhoneNumber;
TextView tvEmailId;
public UserViewHolder(View itemView) {
super(itemView);
tvProfileName = (TextView) itemView.findViewById(R.id.tvProfileName);
tvPhoneNumber = (TextView) itemView.findViewById(R.id.tvPhoneNumber);
tvEmailId = (TextView) itemView.findViewById(R.id.tvEmailId);
}
}
}
Thank you.
use this :
yourTextView.append("\n anotherTextPart")
First of all - if your string will not have white spaces TextView will not able to properly arange text in multi line.
Try to set following attributes :
<TextView
...
android:maxLines="4"/>
Update here is my test that works even without flag above:
MainActivity.java:
private class ViewHolder extends RecyclerView.ViewHolder {
public TextView email;
public ViewHolder(View itemView) {
super(itemView);
email = (TextView) itemView.findViewById(R.id.tvEmailId);
}
}
private class Adapter extends RecyclerView.Adapter<ViewHolder> {
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View card = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_card, parent, false);
return new ViewHolder(card);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.email.setText("test#gmail.comtest#gmail.comtest#gmail.comtest#gmail.comtest#gmail.comtest#gmail.comtest#gmail.comtest#gmail.com");
}
#Override
public int getItemCount() {
return 5;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
.....
RecyclerView view = (RecyclerView) findViewById(R.id.recyclerView);
view.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
view.setAdapter(new Adapter());
}
user_card.xml (changed layout_height to wrap_content):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<android.support.v7.widget.CardView
android:id="#+id/cvSingleUser"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp">
<ImageView
android:id="#+id/ivProfilePic"
android:layout_width="60dp"
android:src="#drawable/user"
android:layout_height="60dp"
android:layout_marginRight="16dp" />
<TextView
android:id="#+id/tvProfileName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="test test test"
android:layout_toRightOf="#id/ivProfilePic" />
<TextView
android:id="#+id/tvPhoneNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test"
android:layout_below="#id/tvProfileName"
android:layout_toRightOf="#id/ivProfilePic" />
<TextView
android:id="#+id/tvEmailId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false"
android:text="test#gmail.comtest#gmail.comtest#gmail.comvtest#gmail.com"
android:scrollHorizontally="true"
android:layout_below="#id/tvPhoneNumber"
android:layout_toRightOf="#id/ivProfilePic" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
main_activity :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Result:
Solved by using
TableLayout
and using specific width.full code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<android.support.v7.widget.CardView
android:id="#+id/cvSingleUser"
android:layout_marginRight="2dp"
android:layout_marginLeft="2dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tableLayout1"
android:layout_width="270dip"
android:layout_height="wrap_content">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal">
<TextView
android:id="#+id/tvProfileName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="test test test"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal">
<TextView
android:id="#+id/tvPhoneNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:text="test"
android:layout_below="#id/tvProfileName" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:id="#+id/tvEmailId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="test#gmail.comtest#gmail.comtest#gmail.comvtest#gmail.comtest#gmail.comtest#gmail.comtest#gmail.comvtest#gmail.comtest#gmail.comtest#gmail.comtest#gmail.comvtest#gmail.comtest#gmail.comtest#gmail.comtest#gmail.comvtest#gmail.com"
android:layout_below="#id/tvPhoneNumber" />
</TableRow>
</TableLayout>
</android.support.v7.widget.CardView>

RecycleView do not displaying the data

I have very popular question - why the data not showing in the RecycleView.
That is my Adapter:
public class CommentsAdapter extends RecyclerView.Adapter<CommentsAdapter.ViewHolder> {
private static final String TAG = "CommentsAdapter";
public ImageView mThumbView;
private List<PostComment> postCommentList;
public CommentsAdapter(List<PostComment> postCommentList) {
this.postCommentList = postCommentList;
}
#Override
public CommentsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_comment_item, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(CommentsAdapter.ViewHolder holder, int position) {
PostComment record = postCommentList.get(position);
holder.mAuthorTextView.setText(record.getUser().getFull_name());
holder.mDateTextView.setText(record.getCreated_at());
holder.mTextTextView.setText(record.getText());
}
#Override
public int getItemCount() {
Log.i(TAG + " comments size", Integer.toString(postCommentList.size()));
return postCommentList.size();
}
public void addItems(List<PostComment> postCommentList) {
this.postCommentList.addAll(postCommentList);
}
public class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mAuthorTextView;
public TextView mDateTextView;
public TextView mTextTextView;
public ViewHolder(View v) {
super(v);
mAuthorTextView = (TextView) itemView.findViewById(R.id.comment_author);
mDateTextView = (TextView) itemView.findViewById(R.id.comment_date);
mTextTextView = (TextView) itemView.findViewById(R.id.comment_text);
}
}
}
That is my view:
<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/container"
....>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp">
<ImageView
android:id="#+id/comment_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="centerInside"
android:src="#drawable/cat"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/middleLayout"
android:layout_toRightOf="#+id/comment_image"
android:layout_marginTop="8dp">
<TextView
android:id="#+id/comment_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:singleLine="true"
android:text="Dark Plastic"
android:textColor="?attr/colorPrimary"
android:textSize="14dp" />
<TextView
android:id="#+id/comment_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="8dp"
android:layout_toRightOf="#+id/comment_author"
android:text="21 minutes"
android:textSize="14dp" />
</RelativeLayout>
<TextView
android:id="#+id/comment_text"
android:layout_width="wrap_content"
android:layout_toRightOf="#+id/comment_image"
android:layout_height="wrap_content"
android:maxLines="3"
android:layout_marginTop="8dp"
android:layout_below="#id/middleLayout"
android:text="sd"
android:textColor="#android:color/black"
android:textSize="16dp" />
</RelativeLayout>
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_gravity="bottom"
android:paddingLeft="16dp"
android:paddingRight="16dp"></View>
That is how I set up the adapter:
mLayoutManager = new LinearLayoutManager(this);
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
post_details_comments_list.setLayoutManager(mLayoutManager);
mCommentListAdapter = new CommentsAdapter(new ArrayList<PostComment>());
post_details_comments_list.setAdapter(mCommentListAdapter);
After I set up the adapter, I the getItemCount returns the quantity of the items - so that is not null.
RecycleView layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.gms.maps.MapView
android:id="#+id/post_details_mapview"
android:layout_width="wrap_content"
android:layout_height="200dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/post_details_comments_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/TextLabelStyle"/>
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/post_details_comments_list"
android:layout_width="match_parent"
android:scrollbars="vertical"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/edit_comment"
android:hint="#string/hint_type_comment"/>
</LinearLayout>
I make the request to the server and here is the callback, where I get the results and set up the adapter:
#Override
public void onAllPostCommentsCallback(AllComments allComments) {
Log.i(TAG + " I've the comments", allComments.getPostCommentList().toString());
mCommentListAdapter.addItems(allComments.getPostCommentList());
post_details_comments_list.setAdapter(mCommentListAdapter);
mCommentListAdapter.notifyDataSetChanged();
}
But the list is not displaying.
What can be the reason?
put null inplace of parent in View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_comment_item, parent, false);
I've already solved the issue. The problem was that I had RecycleView inside the NestedScrollView
Answer in this post helped me a lot:
How to use RecyclerView inside NestedScrollView?

Categories