Reputation: 239
My button layout is as follows
<Button
android:id="@+id/Button01"
android:layout_width="420px"
android:layout_height="120px"
android:background="@drawable/butt1"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:onClick="clk_fault"
android:text="Enter Fault"
android:textColor="#FFFF00"
android:layout_marginTop="100px"
android:textSize="50px" />
When the activity starts i run code that gets the display width and then adjusts the button size according to the resolution of the device
this all works fine but the text size always stays the same
is there a way of increasing the textsize depending on the size of the button containg the text?
50px is great when button width is 420px but if button width is only 200px i need to be able to reduce the text size so it looks the same whatever the size
any help appreciated
Mark
EDIT
Activity code as requested
public void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.RGBA_8888);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DITHER);
setContentView(R.layout.activity_activity1);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int h = metrics.heightPixels;
int w = metrics.widthPixels;
int w1 = (w/2);
Button txt1 = (Button) findViewById(R.id.Button01);
txt1.setLayoutParams(new LinearLayout.LayoutParams(w1, 100));
setMargins(txt1, 0, 100, 0, 0);
}
public static void setMargins (View v, int left, int top, int right, int bottom) {
if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
p.setMargins(left, top, right, bottom);
v.requestLayout();
}
}
Upvotes: 1
Views: 2402
Reputation: 38098
Please, don't use px. Use dp for widget sizes and sp for font sizes.
This way you will be granted a perfect scalability.
sp ~ dp (almost equals).
At mdpi resolution, 1px = 1dp.
At ldpi resolution, the scale multiplier is 0.75
At mdpi resolution, the scale multiplier is 1.0
At hdpi resolution, the scale multiplier is 1.5
At xhdpi resolution, the scale multiplier is 2.0
At xxhdpi resolution, the scale multiplier is 3.0
At xxxhdpi resolution, the scale multiplier is 4.0
mdpi is to be considered the reference resolution
While working in Java code, you are using pixels.
This means you have to:
1 - find the scaling factor to "transform them to dp".
2 - multiply your values by this factor.
To find the multiplier, I use this code:
final DisplayMetrics metrics =
Resources.getSystem().getDisplayMetrics();
final float scale = metrics.density;
Now, you can multiply your values by scale
and have your pixels scaled accordingly
Upvotes: 1
Reputation: 1902
Firstly text sizes should be in sp and widget & component sizes in dp. Secondly dynamics size change based on viewport size change like on different devices needs you to create different layout files for them not do dynamics expansion or contraction based on view sizes.
Upvotes: 0