Kairi San
Kairi San

Reputation: 249

How can I resize my ViewPager depends on the size of my screen?

I'm trying to resize my ViewPager depends on the screen size of a device. My ViewPager is at the center of screen and has only a width of 300dp. I want my ViewPager height to resize depends on the screen size. Underneath of my ViewPager it has a ImageView, this ImageView covers a part of my ViewPager.

In my activity_main.xml

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="300dp"
    android:layout_height="match_parent"
    android:layout_below="@+id/img_memtext"
    android:layout_centerHorizontal="true"/>


<com.viewpagerindicator.CirclePageIndicator
    android:id="@+id/indicator"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/pager"
    android:layout_centerHorizontal="true"/>

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:background="@drawable/notification_bg"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentLeft="true"
    android:layout_alignTop="@+id/attention"
    android:layout_below="@id/indicator"/>

In my viewpager_item.xml

<?xml version="1.0" encoding="utf-8"?>

    <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"
        android:background="#ffffffff"
        android:isScrollContainer="true"
        android:fillViewport="true"
        android:layout_gravity="center"
        >

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            >

            <ImageView
                android:layout_width="280dp"
                android:layout_height="100dp"
                android:id="@+id/news_image"
                android:layout_centerHorizontal="true"/>

            <TextView
                android:id="@+id/news_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textStyle="bold"
                android:textSize="18dp"
                android:layout_below="@+id/news_image"/>

            <TextView
                android:id="@+id/space"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:gravity="center"
                android:layout_below="@+id/news_title"
                />

            <TextView
                android:id="@+id/news_body"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/news_title"
                android:textSize="14dp"/>

            <TextView
                android:id="@+id/read_more"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/news_body"
                android:textColor="#ff28c0ff"
                android:clickable="true"
                android:textSize="14dp"/>

        </RelativeLayout>

    </ScrollView>

Could anyone can help me please? Thank you very much.

Upvotes: 0

Views: 4784

Answers (2)

이남웅
이남웅

Reputation: 231

i make some class to resize view's width and height. I dont like Dp unit. it's hard to me, every device fit.

here part of my class :

public class SetViewScale {
    private static SetViewScale instance;
    public int height, width; //device height, width
    public final static int Full_w = 720; // base on design
    public final static int Full_h = 1230; //base on design

private SetViewScale(Activity a){
    DisplayMetrics displayMetrics = new DisplayMetrics();
    a.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    int deviceWidth = displayMetrics.widthPixels;
    int deviceHeight = displayMetrics.heightPixels;
    this.width  = deviceWidth;
    this.height = deviceHeight;
}
private SetViewScale(WindowManager wm){
    DisplayMetrics displayMetrics = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(displayMetrics);
    int deviceWidth = displayMetrics.widthPixels;
    int deviceHeight = displayMetrics.heightPixels;
    this.width  = deviceWidth;
    this.height = deviceHeight;
}

public synchronized static SetViewScale getInstance(Activity a){
    if(instance == null)
        instance = new SetViewScale(a);
    return instance;
}
public synchronized static SetViewScale getInstance(WindowManager wm){
    if(instance == null)
        instance = new SetViewScale(wm);
    return instance;
}

public synchronized static SetViewScale getInstance(){
    if(instance == null) {
        WindowManager wm = (WindowManager) AppManager.context.getSystemService(Context.WINDOW_SERVICE);
        instance = new SetViewScale(wm);
    }
    return instance;
}
/**
 * view resizing device width height
 * @param view
 * @param w 
 * @param h
 * @return
 */
public View setViewScale(View view, int w, int h) {
    int setW, setH;

    int x = width * w;
    setW = (int)(x/Full_w);
    int y = h*setW;
    setH = (int)(y/w);
    ViewParent parent = view.getParent();
    if(parent instanceof LinearLayout){
        LinearLayout.LayoutParams lp = (LayoutParams) view.getLayoutParams();
        lp.width = setW;
        lp.height = setH;
        view.setLayoutParams(lp);
    }else if(parent instanceof RelativeLayout){
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)view.getLayoutParams();
        lp.width = setW;
        lp.height = setH;
        view.setLayoutParams(lp);
    }else if(parent instanceof FrameLayout){
        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams)view.getLayoutParams();
        lp.width = setW;
        lp.height = setH;
        view.setLayoutParams(lp);
    }else if(parent instanceof AbsListView){
        AbsListView.LayoutParams lp = (AbsListView.LayoutParams)view.getLayoutParams();
        lp.width = width;
        lp.height = height;
        view.setLayoutParams(lp);
    }else{
        return null;
    }
    return view;
}
public View setFillWidthScale(View view, int w, int h) {
    int setW, setH;

    int x = width * w;
    setW = (int)(x/Full_w);
    int y = h*setW;
    setH = (int)(y/w);
    ViewParent parent = view.getParent();
    if(parent instanceof LinearLayout){
        LinearLayout.LayoutParams lp = (LayoutParams) view.getLayoutParams();
        lp.width = LayoutParams.MATCH_PARENT;
        lp.height = setH;
        view.setLayoutParams(lp);
    }else if(parent instanceof RelativeLayout){
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)view.getLayoutParams();
        lp.width = RelativeLayout.LayoutParams.MATCH_PARENT;
        lp.height = setH;
        view.setLayoutParams(lp);
    }else if(parent instanceof FrameLayout){
        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams)view.getLayoutParams();
        lp.width = FrameLayout.LayoutParams.MATCH_PARENT;
        lp.height = setH;
        view.setLayoutParams(lp);
    }else if(parent instanceof AbsListView){
        AbsListView.LayoutParams lp = (AbsListView.LayoutParams)view.getLayoutParams();
        lp.width = width;
        lp.height = height;
        view.setLayoutParams(lp);
    }else{
        return null;
    }
    return view;
}

use px unit.

i hope it is some hint to you.

Upvotes: 0

Louis CAD
Louis CAD

Reputation: 11529

Use LinearLayout with match_parent height and width to wrap your ViewPager and other Views you want to be in the same row or column as your ViewPager. Remove explicit dimensions, then, add layout_weight property to Views wrapped by the LinearLayout. More information on layout_weight in LinearLayout.

If you want to support a large range of screens sizes and resolutions, I suggest you to read this article, and look deeper in the android developers website for best practices and instructions to make your app behave weel on (almost) all screen sizes.

Upvotes: 1

Related Questions