Jarin Finmomenta
Jarin Finmomenta

Reputation: 25

How to inflate a view in android in API level 25 and below?

I am getting error in inflating a view in recycler view. It works on Oreo and Pie but not working in Nougat and devices below it.

This is the code I have used to inflating the view.

View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.listing_row_item, parent, false);

My xml file is

 <?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:background="@drawable/outlined_blue_rectangle"
    android:clickable="true"
    android:focusable="true"
    android:orientation="vertical"
    android:layout_marginBottom="@dimen/row_padding"
    android:paddingBottom="@dimen/contentPadding"
    android:paddingStart="@dimen/contentPadding"
    android:paddingEnd="@dimen/contentPadding"
    android:paddingTop="@dimen/contentPadding">

    <LinearLayout
        android:id="@+id/ll_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="Name"
            android:layout_height="wrap_content"
            android:textColor="@color/blackColor"
            android:textSize="@dimen/textSizeMedium" />

        <TextView
            android:id="@+id/name"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textColor="@color/blackColor"
            android:textSize="@dimen/textSizeMedium" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_status"
        android:layout_below="@+id/ll_name"
        android:layout_marginTop="@dimen/verticalSpacing"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="Loan Application Status"
            android:layout_height="wrap_content"
            android:textColor="@color/blackColor"
            android:textSize="@dimen/textSizeMedium" />

        <TextView
            android:id="@+id/status"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textColor="@color/blackColor"
            android:textSize="@dimen/textSizeMedium" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_eligibility"
        android:layout_below="@+id/ll_status"
        android:layout_marginTop="@dimen/verticalSpacing"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="Eligibility Status"
            android:textColor="@color/blackColor"
            android:textSize="@dimen/textSizeMedium" />

        <TextView
            android:id="@+id/preliminary_eligibility"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:textColor="@color/blackColor"
            android:textSize="@dimen/textSizeMedium" />

    </LinearLayout>

    **<Button**
        android:id="@+id/check_eligibility"
        android:layout_below="@+id/ll_eligibility"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/verticalSpacing"
        android:paddingStart="@dimen/row_padding"
        android:paddingEnd="@dimen/row_padding"
        android:text="Check\nEligibility"
        android:layout_alignParentEnd="true"
        android:textAllCaps="false"
        android:background="@drawable/button_selection_selected"
        android:textColor="@color/whiteColor"
        android:textSize="@dimen/textSizeSmall" />

    <Button
        android:id="@+id/all_info"
        android:layout_below="@+id/ll_eligibility"
        android:layout_marginTop="@dimen/verticalSpacing"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingStart="@dimen/row_padding"
        android:paddingEnd="@dimen/row_padding"
        android:text="All\nInfo"
        android:textAllCaps="false"
        android:background="@drawable/button_selection_selected"
        android:textColor="@color/whiteColor"
        android:textSize="@dimen/textSizeSmall"/>

</RelativeLayout>

The error message I am getting

android.view.InflateException: Binary XML file line #88: Binary XML file line #88: Error inflating class Button
    Caused by: android.view.InflateException: Binary XML file line #88: Error inflating class Button
    Caused by: java.lang.ArrayIndexOutOfBoundsException: length=31; index=464
        at android.content.res.StringBlock.get(StringBlock.java:65)
        at android.content.res.XmlBlock$Parser.getPooledString(XmlBlock.java:458)
        at android.content.res.TypedArray.loadStringValueAt(TypedArray.java:1212)
        at android.content.res.TypedArray.getString(TypedArray.java:202)
        at android.widget.TextView.<init>(TextView.java:1100)
        at android.widget.Button.<init>(Button.java:109)
        at android.widget.Button.<init>(Button.java:105)
        at androidx.appcompat.widget.AppCompatButton.<init>(AppCompatButton.java:71)
        at androidx.appcompat.widget.AppCompatButton.<init>(AppCompatButton.java:67)

Upvotes: 0

Views: 293

Answers (1)

Tenten Ponce
Tenten Ponce

Reputation: 2506

If you are using layout that only supports API level 25+, you should put it inside a res/layout-v25 folder (create the folder if it doesn't exist), and you must create a substitute layout for API level 24 below inside res/layout folder.

Android will automatically use layout-v25 on API level 25+.

Upvotes: 1

Related Questions