Android textView size is bigger than dynamically created one - android

I got some issue with dynamically created TextViews. To be more specific:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some text"
android:textColor="#color/black"
android:textSize="30px" />
appears much larger than:
TextView prName = new TextView(this);
prName.setTextColor(getResources().getColor(R.color.black));
prName.setText("Some text");
prName.setTextSize(TypedValue.COMPLEX_UNIT_PX, 30);
How to made them equal? Thanks beforehand

For text you should use scale points (SP) instead of pixel.
For xml:
android:textSize="30sp"
For code:
prName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);

Set height and width as wrap_content for your textview.
LinearLayout.LayoutParams Params1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
prName.setLayoutParams(Params1);

Make sure when you setTextSize for any type of view, you should set it in scalable points (sp) and not in pixels (px) like this:
In xml:
android:textSize="18sp"
In code:
prName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
Using scalable points will let your TextView text size be equal on all devices, while using pixels will let your TextView text size be unequal on devices with different resolutions.

use setTextSize(int unit, float size)
TypedValue.COMPLEX_UNIT_PX //Pixels
TypedValue.COMPLEX_UNIT_SP //Scaled Pixels
TypedValue.COMPLEX_UNIT_DIP //Device Independent Pixels
In here just set
prName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);

Remove this line:
prName.setTextSize(TypedValue.COMPLEX_UNIT_PX, 30);
... or use this instead:
prName.setTextSize(30);

Related

TypedValue.applyDimension producing wrong dimensions

I use TypedValue.applyDimension and displayMetrix.scaledDensity to set text size in the TextView on my Xiaomi Redmi 5 plus and get surprizing results: text in the TextView is much larger than the one (same sp value) set in layout attribute. Here's my layout:
<TextView
android:id="#+id/my_text_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center|start"
android:layout_margin="4dp"
android:text="Some Text"
android:textSize="18sp" />
And here is how I change text size:
TextView titleView = itemView.findViewById(R.id.my_text_view);
titleView.setTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 18,
getResources().getDisplayMetrics()));
I also tried to use formula as follows:
(int) getResources().getDisplayMetrics().scaledDensity * 18
On the emulator, I get the right text size, so I am not sure how to reproduce this. If someone manages to do that, please, leave comments for me and others to understand what is actually wrong.
Thanks
So, I opend TextView source code and found out that TextView.setTextSize accepts sp, not px. So the answer is not to convert sp to px, but to use sp by default, or, if you need to support several size units, use setTextSize(int unit, float size)

Android, Change Image size in ImageView

For Android, How can I change the image size in an ImageView in a layout?
My image is jpeg format.
<ImageView
android:id="#+id/imageView1"
android:layout_margin="20dp"
android:src="#drawable/stop"
android:layout_width="50dp"
android:layout_height="50dp"/>
Just change width and height attribute.
You can also set Params using LayoutParams
ImageView yourImageView= (ImageView)findViewById(R.id.imageId);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30);
//width and height of your Image ,if it is inside Relative change the LinearLayout to RelativeLayout.
yourImageView.setLayoutParams(layoutParams);
There are a few ways to change the size of an image in an imageView in XML.
Scale the image by pulling around the edges of the imageView in the Graphical Layout (this is not really advised since the Graphical Layout sometimes adds undesired properties into the XML).
Apply a weight to the imageView (this requires the parent object to have specified a weightSum), causing the image to scale according to the screen size. This seems to be the most reliable way to gauge how your layout will look on many different sized screens.
Simply adjust the weight and height in the imageView (as others have mentioned).
<ImageView
android:id="#+id/imageViewId"
android:src="#drawable/icon"
android:layout_width="xxdp"
android:layout_height="yydp"/>
where xx is the width in pixels and yy is the height in pixels.
(give integer values as you desire the size to be)

Is there a way to specify android:layout_height dynamically during onCreate?

I would like to set
android:layout_height="wrap_content"
to some pixels(int) during my application start. This is a textView and the reason behind this is that i want the height of the textView dynamic based on some inputs from my end which will be computed when the onCreate method is called.
Is this possible? if yes any example would be great.
Forgot to add my textView xml looks like this
<TextView
android:id="#+id/sometext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fadingEdge="vertical"
android:background="#drawable/dropshadow"
android:scrollbars="vertical"
android:text="Getting data on your slow n/w..."
android:textColor="#ffffff"
/>
Just edit the layout params of the view.
TextView v = (TextView) findViewById(R.id.sometext);
LayoutParams lp = v.getLayoutParams();
lp.height = 50;
v.setLayoutParams(lp);
The view will be 50px high in this example.
Note that it's generally not recommended to use pixel dimensions for layouts due to the many device specs out there. Rather use dp (density independent pixels) instead. You can calculate pixel dimensions from dp values in the following way (50dp to px here):
float dp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50,
getResources().getDisplayMetrics());

Have TextView scale its font size to fill parent?

Is there a way to have a TextView pick a font size such that it fills whatever space it has available? For example:
<LinearLayout
layout_width="fill_parent"
layout_height="50dip" >
<TextView
layout_width="fill_parent"
layout_height="fill_parent"
textSize="scale?" />
</LinearLayout>
Thanks
No, there is not built in font scaling in a TextView. You could overload the onDraw() method in a child of TextView, but TextView does not support scaling the text automatically.
Take a look at this other question.
Just to update, from API 26 (Android 8.0) TextViews has an attribute to scale text on horizontal and vertical axes
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:autoSizeTextType="uniform" />
Check it at Autosizing TextViews
As in the answer Nick Campion quoted, I used a loop and a static layout to iterate to the correct size. My requirements were to fit the width and use no more than one line (no ellipses):
int i = 0;
while(i < 2) {
layout = new StaticLayout(text, textPaint, w, Alignment.ALIGN_NORMAL, 1, 0, true);
i = layout.getLineCount();
textSize += 2;
textPaint.setTextSize(textSize);
}
textPaint.setTextSize(textSize*0.95f);

How to dynamically set textview height in Android

In my application I need to set dynamic text to my textview so I want it to get resized dynamically. I have set:
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="normal"
android:text="Frontview"
android:layout_weight="1"
android:gravity="center_vertical"
android:textColor="#0099CC"
android:singleLine="false"
android:maxLines="4"
android:ellipsize="marquee"
/>
My textview height is not going beyond 2 lines and the text is getting cut.
As Konstantin said, this code will probably be ignored after you exceed 4 lines unless you remove android:maxLines="4"
This is how you would set the height in code:
TextView tv = (TextView) findViewById(R.id.TextView02);
int height_in_pixels = tv.getLineCount() * tv.getLineHeight(); //approx height text
tv.setHeight(height_in_pixels);
If you want to use dip units, which allows your app to scale across multiple screen sizes, you would multiply the pixel count by the value returned by getResources().getDisplayMetrics().density;
This depends on your desired behavior, but you might also consider having the TextView size fixed, and allowing the user to scroll the text:
TextView tv = (TextView)findViewById(R.id.TextView02);
tv.setMovementMethod(ScrollingMovementMethod.getInstance());
TextView tv;
----------------------
tv=new TextView(this); // you take TextView id (R.id.textview1)
LinearLayout.LayoutParams Params1 = new LinearLayout.LayoutParams(15,50);
tv.setLayoutParams(Params1); //you take linearlayout and relativelayout.
Remove android:maxLines="4", it restricts height of your view to 4 lines of text.

Categories

Resources