Friday, May 9, 2014

How to force the View Pager to wrap its content.

The problem is that the "wrap_content" doesn't seem to be working with the View pager.
Here is the solution:
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;

public class CustomViewPager extends ViewPager {

    public CustomViewPager(Context context) {
        super(context);
    }

public CustomViewPager(Context context, AttributeSet attrs){

        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int height = 0;
        for(int i = 0; i < getChildCount(); i++) {

            View child = getChildAt(i);

            child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

            int h = child.getMeasuredHeight();

            if(h > height) height = h;

        }

        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

reference:
http://stackoverflow.com/questions/8394681/android-i-am-unable-to-have-viewpager-wrap-content

That's it, don't hesitate to comment, to share your knowledge and to correct me.

Dimming the background behind the dialogs

I'm just reblogging what you can find here.
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes(); 
lp.dimAmount=0.78f; 
dialog.getWindow().setAttributes(lp); 
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
That's it, don't hesitate to comment, to share your knowledge and to correct me.