0

I am trying to display borders like whatsapp so for that i used dash width and dash gap and those are working but its varying mobile to mobile. i have been trying from morning but i didn't got relevant answer. here is my code: myxml :

    <ImageView
    android:id="@+id/image_story"
    android:layout_width="55dp"
    android:layout_height="55dp"
    android:background="@drawable/border"
    android:padding="3dp"
    android:src="@drawable/default" />

and drawable border.xml:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
android:useLevel="true">
<stroke
    android:width="5dp"
    android:color="#f62b16"
    android:dashGap="10dp"
    android:dashWidth="200dp" />
<padding
    android:bottom="2dp"
    android:left="2dp"
    android:right="2dp"
    android:top="2dp" />
<corners android:radius="30dp" /></shape>

and in Activity

            var dashWidth: Float = 200f
        var dashGap: Float = 0f
        if(posts.size > 1) {
            dashWidth = dashWidth / posts.size
            dashGap = 10f
        }

        var drawable = image_story.getBackground() as GradientDrawable
        drawable.setStroke(5, Color.parseColor("#000000"), dashWidth, dashGap)

here i am taking total dashwidth as 200f but its working for some mobiles i don't understand why?

so if anybody have idea please let me know how to fix this Thanks in adavnce

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
tyson
  • 155
  • 7

1 Answers1

0

Yes, because setStroke applies dash gap in pixels not in dp. So, it will be depend on the screen resolution.

You can get the right number of pixel programmatically depends on screen resolution to get the same result for all screen sizes.

Ahmed M. Abed
  • 599
  • 3
  • 9
  • 22
  • Thanks Ahmed for reply.. how can we do that conversion? – tyson May 22 '18 at 11:41
  • here my dashgap is 10 so now i need to convert this value to dp right? – tyson May 22 '18 at 11:41
  • @tyson if you have the value 10 and need it to be the same in all screen sizes, then you have to get the right number of pixels depends on the screen resolution. Please check this way: https://stackoverflow.com/questions/4605527/converting-pixels-to-dp – Ahmed M. Abed May 22 '18 at 11:46
  • i used convertPixelsToDp method and tried but still i'm getting differently here is the image please check it.. https://78.media.tumblr.com/8dbed0db1be1347a9433731d0f234837/tumblr_p94pbv0tTL1u2z6tmo1_1280.png .. – tyson May 22 '18 at 12:02
  • and here i need to get the screen resolution or image view resolution i didn't understand.. if i get the screen resolution then how can i apply to imageview.. – tyson May 22 '18 at 12:09
  • your problem in the gap between dashes or in the width of border? – Ahmed M. Abed May 22 '18 at 12:13
  • width of border.. i just want maintain based on posts size.. if the posts size is 3 then i need to display 3 borders like whatsapp.. – tyson May 22 '18 at 12:14
  • here i have taken dashwidth as 200f is it right or should i need to calculate based on any formula.. – tyson May 22 '18 at 12:20
  • Yes, because this number in pixel and it will change depend on the screen resolution. – Ahmed M. Abed May 22 '18 at 12:21
  • so how can i get that value based on screen resolution? – tyson May 22 '18 at 12:22
  • try to use the way here https://stackoverflow.com/questions/4605527/converting-pixels-to-dp – Ahmed M. Abed May 22 '18 at 12:24
  • for the border width – Ahmed M. Abed May 22 '18 at 12:24
  • i tried but i'm not getting the result correctly if possible can you add solution here.. – tyson May 22 '18 at 12:29
  • for example dashWidth = convertDpToPixel(200,getContext()), then use dashWidth. – Ahmed M. Abed May 22 '18 at 12:34