Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set gravity for RemoteView in android through java

How i can set the gravity through java code in android,i have a Remoteview like this RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget); i want to add gravity through java code.i tried using this way ,but it is not successfull.

 views.setGravity(Gravity.CENTER);
like image 214
jithu Avatar asked May 11 '26 08:05

jithu


1 Answers

The answer depends on the type of View you want to call the method on. If it's e.g. a LinearlLayout, this will do the job:

rv.setInt(R.id.MyView, "setGravity", Gravity.CENTER);

I tested it on a LinearLayout and a TextView and it does work with the layout but not with the TextView. So my assumption would be it's supported by ViewGroup classes only (you can check that assumption yourself by checking the setGravity(int) methods in the Android source for @android.view.RemotableViewMethod annotations).

So if you want to change TextView's gravity programmatically, you need to wrap the TextView in its own LinearLayout and change gravity of the LinearLayout instead, e.g. use this layout:

<LinearLayout
    android:id="@+id/day_header_title_wrapper"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/day_header_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

and set gravity like this:

rv.setInt(R.id.day_header_title_wrapper, "setGravity", Gravity.CENTER);
like image 184
Emanuel Moecklin Avatar answered May 13 '26 01:05

Emanuel Moecklin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!