Android widget width - match parent on all devices

Viewed 1015

I have widget with following info:

<appwidget-provider
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="328dp"
    android:minHeight="56dp"
    android:updatePeriodMillis="1000000"
    android:initialLayout="@layout/home_widget"
    android:previewImage="@drawable/widget_preview"
    android:resizeMode="horizontal"
    android:widgetCategory="home_screen">
</appwidget-provider>

The layout (not necessary to put it here) looks good on my device, but I found out that the widget is not applicable for smaller screen. Can somebody help me solve this issue? Instead of not displaying this widget on smaller devices, I would like to alter the layout (maybe create another xml layout for it). I tried putting minWidth to dimens, but as I have all the widget elements positioned and sized precisely, this approach would cut the widget. Thanks for any help.

4 Answers

Screen density is not same for all devices, hence a best solution is to introduce ssp and sdp instead of dp which is fixed for all devices ssp is used for setting size to text and sdp is used for layouts.

https://github.com/intuit/sdp

becuase of we use diffrent screen size and diffrent density of the screen to solve this dont use dp or sp rather than use sdp or ssp which is provide in this library https://github.com/intuit/sdp

And this make your space preamter screen responsive

As mentioned above, different phones got different screen sizes so when using a fixed size value (328dp for example) your layout may not be responsive to all devices.


Naman's answer will work for you, you can use something like this:

<view
    android:layout_height="@dimen/_100sdp"
    android:width="@dimen/_100sdp" />

But you can also use ConstraintLayout to tell your views how to spread on your screen with percents, like this:

 <view
    android:layout_height="0dp"
    android:layout_width="0dp"
    app:layout_constraintHeight_percent="0.2"
    app:layout_constraintWidth_percent="0.3"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

With this solution, your view will be equal to 30% of the screen width, 20% of its height and will remain responsive to all screen sizes.

If you want widget width match as parent then you can use the android:layout_width="match_parent"

match_parent defines the width/height as match of parent, there is not any need to define the static dimen. It is adjust the width according to parent's width.

I hope its work for you.

Related