0

I am trying to display a RecyclerView inside a DialogFragment and I can't get the row-item XML to work right.

I am trying to display something like this:

TEXTVIEW1 EDITTEXT TEXTVIEW2

where TEXTVIEW1 takes up 1/2th of the width, EDITTEXT takes up 1/4th of the width by default, and TEXTVIEW2 takes up 1/4th of the width (the rest of it).

I stuck these three Views inside a horizontal LinearLayout. I gave each View a layout_height of wrap_content and a layout_width of 0dp and then I assigned layout_weight values to 2, 1, 1 respectively.

However it doesn't display as expected. After I populate all the rows with data and text, the EditText widths seem to wrap to their values instead of taking up the quarter portion I designated.

How do I fix this?

XML

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="end"
        android:text="starttext"
        android:textSize="16sp"
        android:layout_weight="2"
        />

    <EditText
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="numberDecimal"
        android:text="14.0"
        />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="end"
        android:text="endingtext"
        android:layout_weight="1"
        />

</LinearLayout>

My adapter:

public class SpecialobjectHistoryRecyclerViewAdapter extends RecyclerView.Adapter<SpecialobjectHistoryRecyclerViewAdapter.RecyclerViewHolder> {
    public static final String TAG = SpecialobjectRecyclerViewAdapter.class.getSimpleName();
    private List<SpecialobjectHistory> mSpecialobjectHistoryList;
    private Context mContext;

    public SpecialobjectHistoryRecyclerViewAdapter(Context context, List<SpecialobjectHistory> specialobjectHistoryList) {
        mContext = context;
        mSpecialobjectHistoryList = specialobjectHistoryList;
    }


    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new RecyclerViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_specialobject_history_row, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerViewHolder holder, int position) {
        holder.bindRow(mSpecialobjectHistoryList.get(position));
    }

    @Override
    public int getItemCount() {
        return mSpecialobjectHistoryList.size();
    }

    public class RecyclerViewHolder extends RecyclerView.ViewHolder {
        public SpecialobjectHistory thisSpecialobjectHistory;
        public TextView specialobjectTimestampTextView;
        public EditText specialobjectValueEditText;

        public RecyclerViewHolder(View itemView) {
            super(itemView);
            specialobjectTimestampTextView = (TextView) itemView.findViewById(R.id.item_specialobject_history_row_textview_timestamp);
            specialobjectValueEditText = (EditText) itemView.findViewById(R.id.item_specialobject_history_row_edittext_value);
        }

        public void bindRow(final SpecialobjectHistory specialobjectHistory) {
            thisSpecialobjectHistory = specialobjectHistory;
            specialobjectTimestampTextView.setText(specialobjectHistory.getTimestamp());
            specialobjectValueEditText.setText(specialobjectHistory.getValue() + "");
        }

    }
}
user6261756
  • 147
  • 1
  • 7

2 Answers2

0
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:ellipsize="end"
    android:text="starttext"
    android:textSize="16sp"
    android:layout_weight="2" />

<EditText
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:inputType="numberDecimal" />

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:ellipsize="end"
    android:text="endingtext"
    android:layout_weight="1" />

</LinearLayout>

not tested but should work

Matias Elorriaga
  • 8,880
  • 5
  • 40
  • 58
0

you should set the width of first Textview to 0dp

android:layout_width="0dp"
Mohamed Ibrahim
  • 3,714
  • 2
  • 22
  • 44