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

29,912

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.

Share:
29,912

Related videos on Youtube

Paulo Roberto Rosa
Author by

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, 2020

Comments

  • Paulo Roberto Rosa
    Paulo Roberto Rosa over 3 years

    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.MyTextViewis 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?

    • Raghunandan
      Raghunandan over 10 years
      what is the api level you are testing on?
    • Paulo Roberto Rosa
      Paulo Roberto Rosa over 10 years
      My device have API Level 15(Android 4.0.3)
    • Raghunandan
      Raghunandan over 10 years
      It is available in api levle 17. developer.android.com/reference/android/view/… and that's why the error.
  • Paulo Roberto Rosa
    Paulo Roberto Rosa over 10 years
    Ok, this was helpful, but what i can do to align my TextView assuming that i use a API Level 15 Device?
  • Raghunandan
    Raghunandan over 10 years
    @PauloRoberto looking for this stackoverflow.com/questions/432037/…
  • Paulo Roberto Rosa
    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 my TextView
  • Raghunandan
    Raghunandan over 10 years
    @PauloRoberto how did you try setting the gravity programatically. show the code.
  • Paulo Roberto Rosa
    Paulo Roberto Rosa over 10 years
    i do not remember exactly, can you show me how to do that, so i will try again
  • Paulo Roberto Rosa
    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.