25

Im working on a View over Android Source Code. I was wondering how can we setup multiple gravities, to a custom View

This is the XML

<com.android.systemui.statusbar.policy.Clock
        android:id="@+id/clock"
        android:textAppearance="@style/TextAppearance.StatusBar.Clock"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:singleLine="true"
        android:paddingLeft="6dip"
        android:gravity="center_vertical|left"
        />

As you see, default gravity is center_vertical|left, so my Java code should be something like this

    View clock = mStatusBarView.findViewById(R.id.clock);

    if (clock != null) {
        SOMEHOW SET GRAVITY -> mCenterClock ? Gravity.CENTER : (Gravity.CENTER_VERTICAL | Gravity.LEFT);
    }

setGravity method don't work on View. So is there an alternative of setting gravity without losing the width and height by default?.

Thanks in advance.

kaiz.net
  • 1,984
  • 3
  • 23
  • 31
user809486
  • 888
  • 1
  • 7
  • 15

3 Answers3

41

Since com.android.systemui.statusbar.policy.Clock extends TextView just cast it to a TextView and set the gravity.

 TextView clock = (TextView) mStatusBarView.findViewById(R.id.clock);
 clock.setGravity(mCenterClock ? Gravity.CENTER : (Gravity.CENTER_VERTICAL | Gravity.LEFT));

Ref: Source code

HandlerExploit
  • 8,131
  • 4
  • 31
  • 50
  • Could you take a look here https://github.com/AOKP/frameworks_base/blob/ics/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java and here https://github.com/AOKP/frameworks_base/blob/ics/packages/SystemUI/res/layout/status_bar.xml Seems like is the best way. I think your way is not working because is a linearLayout. – user809486 Apr 27 '12 at 18:43
  • I ended up by porting code from AOKP source, however your answer was the more accurate. Thanks. – user809486 Apr 27 '12 at 19:04
8

Have you tried?

clock.setGravity(Gravity.CENTER | Gravity.CENTER_VERTICAL);

Edit:

Since you cannot use setGravity for a plain View, do something like this,

You can use a RelativeLayout and set a rule like this...

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL|RelativeLayout.CENTER_IN_PARENT, clock.getId());
Arif Nadeem
  • 8,524
  • 7
  • 47
  • 78
  • Yes. setGravity method is not available for this View. So it wont compile – user809486 Apr 27 '12 at 18:06
  • does Clock extend a certain type of viewgroup? Or just a generic view? It may make more sense to extend a viewgroup, such as LinearLayout. – ootinii Apr 27 '12 at 18:11
  • I'll give it a try, however HandlerExploit solution seems also possible – user809486 Apr 27 '12 at 18:32
  • Yes that is the best solution, my answer would have been the same if I knew that your Custom View extends TextView. +1 for his answer.. You can also do this if in future your View doesn't extend TextView. Thanks. – Arif Nadeem Apr 27 '12 at 18:34
2
 com.android.systemui.statusbar.policy.Clock clock = (com.android.systemui.statusbar.policy.Clock)mStatusBarView.findViewById(R.id.clock);

 clock.setGravity(Gravity.CENTER_VERTICAL);

try the above code and remove View clock = mStatusBarView.findViewById(R.id.clock);

Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64
  • Or just import `com.android.systemui.statusbar.policy.Clock` to not clutter your codebase, if you prefer – JRaymond Apr 27 '12 at 18:07