android.widget.TextView.setTextAlignment(): java.lang.NoSuchMethodError
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.
Related videos on Youtube
Paulo Roberto Rosa
const myself = new Person(); myself.setRole("Full Stack Developer"); myself.setAttribute("Otaku"); myself.setAttribute("Geek"); myself.setAttribute("Nerd"); myself.setTimezone("America/Sao_Paulo"); myself.setCity("Florianópolis"); myself.setCountry("Brazil"); myself.setState("Santa Catarina");
Updated on September 30, 2020Comments
-
Paulo Roberto Rosa over 3 years
Situation:
I have a
TextView
that have the propertyandroid:textAlignment="center"
I am generating another
TextView
dinamically, based on myTextView
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 andMyTextView
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 theTextAlignment
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 myTextView
: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 theActivity
I'm using API Level 15
Any help?
-
Raghunandan over 10 yearswhat is the api level you are testing on?
-
Paulo Roberto Rosa over 10 yearsMy device have API Level 15(Android 4.0.3)
-
Raghunandan over 10 yearsIt is available in api levle 17. developer.android.com/reference/android/view/… and that's why the error.
-
-
Paulo Roberto Rosa over 10 yearsOk, this was helpful, but what i can do to align my
TextView
assuming that i use a API Level 15 Device? -
Raghunandan over 10 years@PauloRoberto looking for this stackoverflow.com/questions/432037/…
-
Paulo Roberto Rosa over 10 years@Raghunandan this dont solve my problem because i need to set it programatically, and i have the same error if i try to set/get the
Gravity
of myTextView
-
Raghunandan over 10 years@PauloRoberto how did you try setting the gravity programatically. show the code.
-
Paulo Roberto Rosa over 10 yearsi do not remember exactly, can you show me how to do that, so i will try again
-
Paulo Roberto Rosa over 10 years@Raghunandan i edited my whole question and voted to be re-opened, you can see it later if you want, and your comment is correct, all the problem was API Level.