Skip to content

Instantly share code, notes, and snippets.

@MinceMan
Last active April 22, 2016 13:51
Show Gist options
  • Save MinceMan/cf41d9f9d457217e1db1 to your computer and use it in GitHub Desktop.
Save MinceMan/cf41d9f9d457217e1db1 to your computer and use it in GitHub Desktop.
Easy way to make scrollable views(or any views) center on in a tablet and have them scrollable by thumbs.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CenteringView">
<attr name="maxWidth" format="dimension|enum">
<!-- Enums also defined in dimens -->
<enum name="match_parent" value="-1"/>
<enum name="wrap_content" value="-2"/>
</attr>
</declare-styleable>
</resources>
package com.example;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import com.exmaple.CenteringWidgetDelegate;
public class CenteringRecyclerView extends RecyclerView {
private CenteringWidgetDelegate centeringDelegate = new CenteringWidgetDelegate();
public CenteringRecyclerView(Context context) {
super(context);
centeringDelegate.setupViewCentering(this, null, 0);
}
public CenteringRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
centeringDelegate.setupViewCentering(this, attrs, 0);
}
public CenteringRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
centeringDelegate.setupViewCentering(this, attrs, defStyle);
}
/**
* @param maxWidth in pixels. Set to -1 for match_parent or -2 for wrap_content.
*/
public void setMaxWidth(int itemWidth) {
centeringDelegate.setMaxWidth(this, itemWidth);
}
}
package com.example;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Point;
import android.support.v7.widget.RecyclerView.LayoutParams;
import android.util.AttributeSet;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import com.example.R;
public class CenteringWidgetDelegate {
public void setupViewCentering(View viewToCenter, AttributeSet attrs, int defStyle) {
TypedArray tArray = viewToCenter.getContext().obtainStyledAttributes(attrs, R.styleable.CenteringView, defStyle, 0);
int maxWith = tArray.getDimensionPixelSize(R.styleable.CenteringView_maxWidth, LayoutParams.MATCH_PARENT);
if (!viewToCenter.isInEditMode()) {
setMaxWidth(viewToCenter, maxWith);
}
tArray.recycle();
}
/**
* @param maxWidth in pixels. Set to -1 for match_parent or -2 for wrap_content.
*/
public void setMaxWidth(View view, int maxWidth) {
if (maxWidth == -1) {
maxWidth = LayoutParams.MATCH_PARENT;
}
if (maxWidth != LayoutParams.MATCH_PARENT && maxWidth != LayoutParams.WRAP_CONTENT) {
Point screen = getScreenDimensions(view.getContext());
int viewPadding = (screen.x - maxWidth) / 2;
view.setPadding(
viewPadding + view.getPaddingLeft(),
view.getPaddingTop(),
viewPadding + view.getPaddingRight(),
view.getPaddingBottom());
}
}
private static Point getScreenDimensions(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point point = new Point();
display.getSize(point);
return point;
}
}
<resources>
<!-- System enums -->
<dimen name="match_parent">-1px</dimen>
<dimen name="wrap_content">-2px</dimen>
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment