android.widget.TextView.setTextAlignment(): java.lang.NoSuchMethodError

adt, android, android-xml, java, textview

Solution

The setTextAlignment method was added in API level 17. Maybe your compiler in the IDE is above 17 and the device/emulator which you are testing is less than that. Here the link to setTextAlignment.

Added from the comments:

For API level 15 and below, you want setGravity, per this question.

Problem

Situation: I have a `TextView` that have the property ``` android:textAlignment="center" ``` I am generating another `TextView` dinamically, based on my `TextView` from XML Layout, using a clone, cloning all the basic properties to work the way above. Problem: To do this i need to use this method: ``` this.myTextView.setMyTextViewProperty(MyTextView.getMyTextViewProperty()); ``` for example: ``` this.MyTextView.setText(MyTextView.getText()); ``` Note that `this.MyTextView`is a local variable and `MyTextView` is a private var declared on the top of the file, under class name. I do this on all the properties of the `TextView` but when i hit the following line of code from the `TextAlignment` property...: ``` this.myTextView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); ``` I tried to set it to a Custom Aligment instead of getting from my XML `TextView` It gives me an error: ``` 11-20 15:27:04.460: E/AndroidRuntime(9185): FATAL EXCEPTION: main 11-20 15:27:04.460: E/AndroidRuntime(9185): java.lang.NoSuchMethodError: android.widget.TextView.setTextAlignment ``` But i see on Ctrl + Space that the method exists, so i cant understand what is happening. A second try was, to set my `TextView` property to the property that comes from my `TextView`: ``` this.myTextView.setTextAlignment(MyTextView.getTextAlignment()); ``` With no success, too. Obs: i do not want a Android XML Layout solution, i want a solution on code, because i generate the `TextView` dinamically on the `Activity` I'm using API Level 15 Any help?

Original source

Related problems