Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative method to TextView.getMaxLines() for Android 4.0.3 (API 15) and below

I am getting below crash logs while using getMaxLines() method of Texview on Ice Cream Sandwich Android 4.0.3 version,

So I got the real issue is, that my application is running lower sdk which does not have this getMaxLines() method.

How can remove this crash. I didn't find any alternatives method

java.lang.NoSuchMethodError: android.widget.TextView.getMaxLines
            at com.text.mobile.new.adapters.ServiceListAdapter.onBindViewHolder(ServiceListAdapter.java:232)
            at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5277)
            at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5310)
            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4568)
            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4461)
            at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1962)
            at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:438)
            at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1334)
            at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:563)
            at android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:171)
            at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2847)
            at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3145)
            at android.view.View.layout(View.java:11278)
            at android.view.ViewGroup.layout(ViewGroup.java:4224)
            at android.widget.RelativeLayout.onLayout(RelativeLayout.java:925)
            at android.view.View.layout(View.java:11278)
            at android.view.ViewGroup.layout(ViewGroup.java:4224)
            at android.widget.RelativeLayout.onLayout(RelativeLayout.java:925)
            at android.view.View.layout(View.java:11278)
            at android.view.ViewGroup.layout(ViewGroup.java:4224)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
            at android.view.View.layout(View.java:11278)
            at android.view.ViewGroup.layout(ViewGroup.java:4224)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
            at android.view.View.layout(View.java:11278)
            at android.view.ViewGroup.layout(ViewGroup.java:4224)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
            at android.view.View.layout(View.java:11278)
            at android.view.ViewGroup.layout(ViewGroup.java:4224)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
            at android.view.View.layout(View.java:11278)
            at android.view.ViewGroup.layout(ViewGroup.java:4224)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489)
            at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4424)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            at dalvik.system.NativeStart.main(Native Method)
like image 509
Lavekush Agrawal Avatar asked Dec 10 '15 08:12

Lavekush Agrawal


People also ask

How do I assign text to TextView?

Set The Text of The TextView You can set the text to be displayed in the TextView either when declaring it in your layout file, or by using its setText() method. The text is set via the android:text attribute. You can either set the text as attribute value directly, or reference a text defined in the strings.

Can we change the text in TextView?

TextView tv1 = (TextView)findViewById(R. id. textView1); tv1. setText("Hello"); setContentView(tv1);

How do I add a newline to a TextView in android?

for the new line in TextView just add \n in middle of your text it works..

How do you print from TextView?

This example demonstrate about How to print number of words in textview in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.


1 Answers

The getMaxLines() of TextViewCompat looks like the appropriate solution for this.

You can also take a look in the SourceCode of TextViewCompat, and copy the functions you need.

Code:

int maxLines = TextViewCompat.getMaxLines(yourtextView);

instead of:

int maxLines = yourtextView.getMaxLines();

I highly recommend using the support lib and TextViewCompat. But just for completeness, here is also the (current) source of TextViewCompatDonut 's getMaxLines :

static int getMaxLines(TextView textView) {
        if (!sMaxModeFieldFetched) {
            sMaxModeField = retrieveField("mMaxMode");
            sMaxModeFieldFetched = true;
        }
        if (sMaxModeField != null && retrieveIntFromField(sMaxModeField, textView) == LINES) {
            // If the max mode is using lines, we can grab the maximum value
            if (!sMaximumFieldFetched) {
                sMaximumField = retrieveField("mMaximum");
                sMaximumFieldFetched = true;
            }
            if (sMaximumField != null) {
                return retrieveIntFromField(sMaximumField, textView);
            }
        }
        return -1;
    }
like image 107
foxy Avatar answered Sep 27 '22 17:09

foxy