I have a customm view which generates an arc and i’ve put in the xml like this
<com.example.application.ArcView
android:id="@+id/arcView1"
android:layout_width="@string/arc_size"
android:layout_height="@string/arc_size"
android:layout_centerInParent="true" />
I have the height and width that change based on the screen density
I’ve coded it like this but it doesn’t work
Resources res = getResources();
String arcSizeString = res.getString(R.string.arc_size);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
switch(metrics.densityDpi){
case DisplayMetrics.DENSITY_LOW:
arcSize = 170;
arcSizeString = (arcSize + "px");
break;
case DisplayMetrics.DENSITY_MEDIUM:
arcSize = 227;
arcSizeString = (arcSize + "px");
break;
case DisplayMetrics.DENSITY_HIGH:
arcSize = 342;
arcSizeString = (arcSize + "px");
break;
}
How can i set the width and height to a variable?
Probably, You want to ask ‘how to set width and height to View’.
You need to reference instance of ArcView in your Activity class, to be more precise in onCreate method of Activity like this :
Afther that, calculate width and height based on density and set it to your view via
LinearLayout.LayoutParams