Android: width and height of launcher icon in DP

In android, we could determine resources by screen density, such as LDPI, HDPI, and MDPI.

I hope to build a user interface with TextLayout that will have the same width and height as the ImageView next to it. ImageView contains an image with the same size as the launch icon (72px for HDPI, etc.).

Is there a way to find out that the icon has a specific width-width in 'dp' (without kwnoi, so I can hard code XML. I think this is possible because dp is device independent, but could not find a way to get the values ​​in dp .

<TextView android:id="@+id/holidaylistitem_day" android:layout_width="<X>dp" android:layout_height="<Y>dp" android:text="1st"/> <ImageView android:id="@+id/holidaylistitem_type" android:src="@drawable/ic_launcher" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 

I need X and Y values, so both will have the same size.

+4
source share
4 answers

The Android Design Guide contains a page that describes the recommended icon sizes . In short, the launch icons are 48x48 dp.

+7
source

There are 4 cases, since in android there are various types of devices:

 ldpi --> 36x36 mdpi --> 48x48 hdpi -->72x72 xhdpi --> 96x96 

Check Link .

+1
source

As Android Killer says, "There are 4 cases, since Android has different types of devices ...". And you can get dpi. To convert px to dp, use this:

dp = (px * 160) / dpi
Reference:
developer.android

+1
source

I have the best solution for your situation, but it is a bit complicated. Here we go:

  • If you cannot determine the size of the icon, but you are sure that it is square, and you want its width / height to be the same as the icon, placed horizontally. You can do it:

    Android: layout_alignTop = "@ + id / holidaylistitem _type" Android: layout_alignBottom = "+ ID / holidaylistitem_type"

  • Now the position and height of your TextView are fixed. Then you expand your TextView and do this:

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

      int width = MeasureSpec.getSize(widthMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec); if (width > height) { width = height; } super.onMeasure( MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY) ); } 
  • Image size in pixels.

0
source

Source: https://habr.com/ru/post/1399781/


All Articles