1. 2015 is going to be a great year for Android! Why wait??
    Samsung Galaxy Note 5 | Samsung Galaxy S6 | HTC One M9
  2. New Forum Created: Samsung Gear VR!

Change Widget TextView Attributes from Code


  1. Matt M

    Matt M Member

    Hello,

    I am looking to allow the user to change text color, text size, text weight, and gravity for a widget, thus these attributes need to be edited from the code. I've managed to change text color easy enough using RemoteViews:

    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
    views.setTextColor(R.id.widgetName, color);

    But RemoteViews of course doesn't have methods for altering the other 3 attributes. I know TextView has methods to do exactly what I want, but how would I write the code exactly to manipulate a widget layout?

    Thank you!

    Matt.

    Advertisement
    :
  2. Boogs

    Boogs Well-Known Member

    Normally in your onCreate method you will save a reference to your TextView which you have already specified in your layout file and loaded into your activity. Once you have that reference to your TextView you should be able to change any if its attributes. Something like below:
    Code:
    setContentView(R.layout.your_layout);
    TextView textView = (TextView)findViewById(R.id.text_view_id_in_layout);
    textView.setColor(...);
  3. Matt M

    Matt M Member

    Thank you Boogs,

    Although, the layout I use for the widget is only referenced in the AppWidgetProvider class which does not have an onCreate() method and never calls setContentView().

    Does setContentView() have to be called in order to make the changes I desire?
  4. Boogs

    Boogs Well-Known Member

    Oops, sorry Matt ... that's what I get for not *really* reading your entire question. I thought you were talking about just adjusting View widgets within an activity - instead of what you're really talking about: adjusting views from a home screen widget. Although it should've been obvious to me by your references to RemoteViews ... I kind of glossed over that part of your post.

    Anyway, to answer your question: I don't know of any way possible to change those attributes that you're talking about for a home screen widget. The only workaround that I can think of is by defining several layouts which are identical except for having different predefined text sizes, gravities, etc. that you want the user to be able to select from. This can obviously get kind of get out of control the more options you have; that is, if you want the user to be able to select from 5 different text sizes and 3 different gravities, then you're going to have to make 15 different layout files to define all those permutations.

    --Boogs
  5. disco6stu9

    disco6stu9 Member

    This is a shot in the dark, but you could try to use the method setInt(int, string, int) to invoke the setGravity method. The second parameter is the name of the function you would call on TextView if you had a reference to the View by calling findViewById (i.e. "setGravity"). Under the covers this may be using reflection to get the proper method. For other things like setTextSize you would use setFloat(int, string, float) since it takes a float value. The weight may be trickier, but if you find a method on the TextView class there is a chance you could call it the same pattern.

    Note, I haven't tried this - I'm just guessing based on the documentation online:


    RemoteViews | Android Developers
  6. Matt M

    Matt M Member

    Thank you!! I was able to get this to work for setTextSize() and setMaxLines(). Unfortunately I did not have as much luck with setGravity() but nonetheless thank you for the insight!!

    Matt.
Loading...

Share This Page