Home > Enterprise >  Add TextView programatically to a layout in onCreateViewHolder
Add TextView programatically to a layout in onCreateViewHolder

Time:09-19

I am new to android developemnt. I have a adapter class where currectly, in onCreateViewHolder method, a fragment(XML) is displayed. I want to add a textView to this fragment only if a 'if condition' is met. This is what i have but I am getting null pointer

java.lang.NullPointerException: Attempt to invoke virtual method void android.widget.RelativeLayout.addView(android.view.View, android.view.ViewGroup$LayoutParams) on a null object reference
 @NonNull
 @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if(AppUtil.isIT(taskList)){
            RelativeLayout ig_msg_layout = (RelativeLayout) viewGroup.findViewById(R.id.ig_message_layout);
            RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.FILL_PARENT);

                TextView if_tv = new TextView(context);
                inop_fuel_tv.setText("Test");
                inop_fuel_tv.setTextColor(context.getResources().getColor(R.color.error_red));
                inop_fuel_tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
                inop_fuel_tv.setTypeface(null, Typeface.BOLD);
                inop_gauge_msg_layout.addView(if_tv, params1);
            }

            View view = LayoutInflater.from(context).inflate(R.layout.fragment_task_list, viewGroup, false);
            return new ViewHolder(view);
    }
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@ id/fuel_list_rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/spinner_border">

    <RelativeLayout
        android:id="@ id/ig_message_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="5dp"
        android:layout_marginEnd="5dp"
        android:orientation="vertical">

    </RelativeLayout>
</RelativeLayout>

CodePudding user response:

There is nothing named ig_message_layout in viewGroup, so findViewById() is returning null.

If the layout XML in your question is fragment_task_list, then please bear in mind that you are inflating that layout at the bottom of onCreateViewHolder() and are trying to find ig_message_layout at the top of onCreateViewHolder(). You need to inflate the layout before you try working with its contents.

Beyond that:

  • It will be simpler to have this TextView in the layout and toggle its visibility than it will be to create and add the TextView at runtime
  • RelativeLayout was replaced by ConstraintLayout several years ago

CodePudding user response:

I'm not sure but I think you are trying to find a view in the parent view group. You should find it in the view that you are inflating.

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(context).inflate(R.layout.fragment_task_list, viewGroup, false);
    RelativeLayout ig_msg_layout = (RelativeLayout) view.findViewById(R.id.ig_message_layout);
    RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.FILL_PARENT);

    if(AppUtil.isIT(taskList)){
        TextView if_tv = new TextView(context);
        inop_fuel_tv.setText("Test");
        inop_fuel_tv.setTextColor(context.getResources().getColor(R.color.error_red));
        inop_fuel_tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
        inop_fuel_tv.setTypeface(null, Typeface.BOLD);
        inop_gauge_msg_layout.addView(if_tv, params1);
    }
    return new ViewHolder(view);
}

  • Related