1

I'm trying to create a row of action items that sits centered just above the player controls bar in a video player using Leanback.

The action items are a ListRow of image views that are actionable (to represent emoticon reactions).

My problem: I can't get them to position in the center, just like the primary and secondary controls bar are centered on the player. The current list row appears like this: emojicon items

Here's the XML layout that defines an individual action:

<?xml version="1.0" encoding="utf-8"?>
<com.makeramen.roundedimageview.RoundedImageView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/rounded_image_view"
        android:src="@drawable/ic_action_insert_emoticon"
        android:scaleType="fitStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        app:riv_corner_radius="30dip"
        app:riv_border_width="2dip"
        app:riv_border_color="#333333"
        app:riv_tile_mode="repeat"
        app:riv_oval="true">
</com.makeramen.roundedimageview.RoundedImageView>

And the presenter that instantiates each row item:

public class RoundedViewPresenter extends Presenter {
    private static final String TAG = RoundedViewPresenter.class.getSimpleName();

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View rootView = inflater.inflate(R.layout.rounded_image_view, parent, false);
        RoundedImageView roundedImageView = (RoundedImageView) rootView.findViewById(R.id.rounded_image_view);

        //roundedImageView.mutateBackground(true);
        //roundedImageView.setBackground(backgroundDrawable);
        roundedImageView.setFocusable(true);
        roundedImageView.setFocusableInTouchMode(true);
        ((HorizontalGridView)parent).setGravity(Gravity.CENTER);
        return new ViewHolder(roundedImageView);
    } ...

And then add the rows in the player's fragment like this:

// add emoji rows
...
ArrayObjectAdapter emojiRowAdapter = new ArrayObjectAdapter(new RoundedViewPresenter());
emojiRowAdapter.add(mObjectModel0);
emojiRowAdapter.add(mObjectModel1);
emojiRowAdapter.add(mObjectModel2);
emojiRowAdapter.add(mObjectModel3);
ListRow emojiRow = new ListRow(emojiRowAdapter);
mRowsAdapter.add(emojiRow);
...

I have tried both the android:layout_gravity="center" and programmatic HorizontalGridView.setGravity() approaches without success.

Any idea why, and/or alternative methods to center the row?

kip2
  • 6,473
  • 4
  • 55
  • 72
  • Instead of inflating another layout of Rounded image view do it directly in `Play Back Overlay Activity Layout` which will only fulfill your requirement. As of i know their is not any other methods using which you can do it programatically. – Jay Rathod May 10 '16 at 08:34
  • @jaydroider, I cannot inflate it in the Activity because we need this custom row to overlay on the video play, just like the player controls row is overlaid. This will make sure the custom row disappears along with the other secondary views when playback is ongoing, and reappear on dpad action – kip2 May 10 '16 at 10:01

4 Answers4

0

You can add a second row of actions under the main controls using setSecondaryActionsAdapter call in PlaybackControlsRow.

Look at: https://android.googlesource.com/platform/development/+/29dadb6d144817840372af52629b4b34c074b78d/samples/SupportLeanbackDemos/src/com/example/android/leanback/PlaybackOverlayFragment.java for an example on how to do it.

Isaac K.
  • 11
  • 1
  • 2
  • Hi Isaac, I considered this option but due to design constraints we had to keep it above the main controls row – kip2 Jun 09 '16 at 22:03
0

Turns out it's oddly difficult to center any HorizontalGridView-based row, which seems to be pretty much every row created with ListRowPresenter. All seem to be left-justified. None of the suggestions offered here seemed to work, so instead I decided to replace the default video title/description dock with my own custom view. Details in this other closely-related question:

https://stackoverflow.com/a/37736890/1145905

Community
  • 1
  • 1
kip2
  • 6,473
  • 4
  • 55
  • 72
-1

Try this property android:layout_gravity="center_horizontal|center_vertical".

Like below xml code.

<?xml version="1.0" encoding="utf-8"?>
<com.makeramen.roundedimageview.RoundedImageView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/rounded_image_view"
        android:src="@drawable/ic_action_insert_emoticon"
        android:scaleType="fitStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|center_vertical"
        app:riv_corner_radius="30dip"
        app:riv_border_width="2dip"
        app:riv_border_color="#333333"
        app:riv_tile_mode="repeat"
        app:riv_oval="true">
</com.makeramen.roundedimageview.RoundedImageView>

It will exact center as you want in your output.

Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
  • That didn't seem to change anything. Why should 'center' produce a different result from 'center_horizontal|center_vertical' anyway? After all, I see in the source code that they're the same: `public static final int CENTER = CENTER_VERTICAL|CENTER_HORIZONTAL;` – kip2 Apr 21 '16 at 14:22
  • @kip2 I have issue in video view i want progress bar and also on play button and center so i have design it with `Center Horizontal` and `Center Vertical` so that's why i have suggested you to do that. I am also using leanback for my tv app and face same issue. – Jay Rathod Apr 21 '16 at 15:50
  • well I can confirm that your suggestion did not work for me. Hardly surprising because of what I mentioned above with the 2 "variants" of gravity essentially the same. I'm probably missing something else and that's what I need help with – kip2 Apr 22 '16 at 09:01
  • @kip2 Well bro can you tell me what smiley output you get with my code ? is shows any changes to your layout ? – Jay Rathod Apr 22 '16 at 09:05
  • The output is exactly the same as the image attached in the OP. No change so I thought no need to repost it – kip2 Apr 22 '16 at 09:12
  • May be you have used rounded image view so it not taking effect. i have used with progress bar widget in play back overlay activity of lean back library. Is this full xml layout you posted in OP. – Jay Rathod Apr 22 '16 at 09:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/109885/discussion-between-kip2-and-jaydroider). – kip2 Apr 22 '16 at 09:25
-1

I believe the alignment is an inherent property of the HorizontalGridView. I believe the problem is that the grid view, which means the entirety of the view, takes up the width of the screen.

In order to reduce the view's size, you'll have to either programatically or through the XML change the layout params to be WRAP_CONTENT which means the smallest width that makes sense or supply a set width.

For either of these, the grid will be smaller and thus your centering attribute will have a noticeable effect.

Nick Felker
  • 11,536
  • 1
  • 21
  • 35
  • I have tried to wrap_content on the parent view like this with no success: `LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); layoutParams.gravity = Gravity.CENTER; parent.setLayoutParams(layoutParams); parent.requestLayout();` – kip2 Apr 30 '16 at 01:40
  • Does the `HorizontalGridView` have a height and width of `match_parent` or `wrap_content`? – Nick Felker Apr 30 '16 at 23:05
  • As you can see in the above snippet, both are WRAP_CONTENT. I have tried with MATCH_PARENT too but no significant change – kip2 May 02 '16 at 08:59