woong's

Android Widget ReSize 사용하기 본문

Develop/Android

Android Widget ReSize 사용하기

dlsdnd345 2016. 2. 14. 16:20
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

Android Widget ReSize 사용하기


Android Widget Resize 관련해서 글이 별로없어 포스트를 쓰고 있습니다 .

요새 서버&웹 개발 , 스터디 을 하느라 정신이 없어 블로그를 신경을 못쓰고 있네요 ㅜ

고통이 있기에 성장이 있는것 같습니다 . 

 

사설이 길었네요 ^^ 


Android Widget 에 대해 부족하신분은 

Android Widget 사용하기 

를 선행 학습하면 학습에 도움이 될것 같습니다 .


Android 기존의 Widget은 1x1 2x2 등 고정의 크기를 지원해 사용자가 위젯의 크기를 선택해서 만들었습니다 .

하지만 이제 3.1 버젼부터 위젯을 설치하고도 위젯의 사이즈를 변경할수 있습니다 .



 

 

 

위젯 리사이즈에 대해 진행해 보겠습니다.


1. Widget layout xml 변경

2. Widget Configration xml 변경

3. (부가) AppWidgetProvider 상속 받은 class 


 

 

  • 1. Widget layout xml 변경

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffff00" >
 
        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
             />
    </RelativeLayout>
 
</LinearLayout>

 

 

제가 만든 위젯의 레이아웃 구성입니다 . 여기서 눈여겨 보셔야 할부분은 최상단의 LinearLayout 입니다 .

기존에는 아마 고정으로 72dp 나 크기에 맞춰 dp로 지정이 되어있을 것입니다 .


리사이즈를 지원해주기 위해서는 Match_parent 로 지정하면 사이즈 변경에 따라 레이아웃이 변경되는것을 볼수 있습니다 .


 

  • 2. Widget Configration xml 변경

 

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/widget_layout"
    android:minHeight="72dp"
    android:minWidth="72dp"
    android:updatePeriodMillis="0"
    android:resizeMode="horizontal|vertical"
     >
 
</appwidget-provider>

 

 

두번째로는 Widget Configration xml 파일을 변경해야 합니다 .

Android:resizeMode="horizontal|vertical"

코드를 추가 해야 합니다 .


이렇게만 해주면 사이즈 별로 유동적으로 위젯이 움직이는것을 볼수 있습니다 .





이정도만 지원해줘도 좋은것 같습니다 . 하지만 그런생각을 할수도 있는것 같습니다 .

위젯의 크기를 변경하면 위젯의 크기가 커져 조금더 이쁘고 자세한 위젯을 보여 주고 싶다 .


지금의 같은 경우에는 불가능 하죠 단지 화면이 작아졌다 커졌다하는일뿐이죠 .

 

 

  • 3. (부가) AppWidgetProvider 상속 받은 class 
AppWidgetProvider 상속 받은 class 의 코드를 조금만 수정하면 위젯의 크기에 따라 레이아웃 변경이 가능합니다 .

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class Widget extends AppWidgetProvider {
 
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
 
        RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        remoteView.setTextViewText(R.id.title, "웅스 블로그");
        appWidgetManager.updateAppWidget(appWidgetIds, remoteView);
 
    }
 
    @SuppressLint({"NewApi" })
    @Override
    public void onAppWidgetOptionsChanged(Context context,AppWidgetManager appWidgetManager, int appWidgetId,Bundle newOptions) {
        super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId,newOptions);
        
                int minWidth = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
                int maxWidth = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH);
                int minHeight = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT);
                int maxHeight = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT);
                RemoteViews rv = null;
                
                if(minWidth == 152 && maxWidth == 196 && minHeight == 58 && maxHeight == 84){
                    rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout3x3);
                } else {
                    rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
                }
                appWidgetManager.updateAppWidget(appWidgetId, rv);
    }
 
}

 

 

제가 만든 Widget Class 입니다 여기서 자세히 보셔야 할부분이 onAppWidgetOptionsChanged 메서드입니다 .


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@SuppressLint({"NewApi" })
    @Override
    public void onAppWidgetOptionsChanged(Context context,AppWidgetManager appWidgetManager, int appWidgetId,Bundle newOptions) {
        super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId,newOptions);
        
                int minWidth = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
                int maxWidth = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH);
                int minHeight = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT);
                int maxHeight = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT);
                RemoteViews rv = null;
                
                if(minWidth == 152 && maxWidth == 196 && minHeight == 58 && maxHeight == 84){
                    rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout3x3);
                } else {
                    rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
                }
                appWidgetManager.updateAppWidget(appWidgetId, rv);
    }


헤당 코드를 보면 높이와 넓이를 통해서 크기에 맞추어 레이아웃 변경도 가능합니다 ^^


틈틈히 해서 Widget 투명 지원 , 컬러변경들 포스트를 써볼예정입니다 ^^



'Develop > Android' 카테고리의 다른 글

Android Color Picker 사용 하기  (0) 2016.02.14
Android widget configure 사용하기  (1) 2016.02.14
Android widget 사용하기  (0) 2016.02.14
Android CheckBok 사용하기  (0) 2016.02.14
Android TextView ... 처리  (0) 2016.02.14
Comments