Change Text size according to screen programatically - android

Hello I would like to ask
Now I have lots of textviews adjusted and styled correctly the problem is when screen size changes the textviews either become too big or too small
I have tried making different layout files but It does not work though
I used SP and still text overlaps the screen when switching from a device to another
I have made over than 10 layout files for different screens and still I cannot control over it
The question is :
Is there a way to change text size logically instead of having to create alternative layouts or dimens files ??
Here is an example of my code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="org.ultradroid.metaldetectorfree.welcome1"
android:orientation="vertical"
android:weightSum="1"
tools:ignore="UseCompoundDrawables">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:ignore="UselessParent">
<ImageView
android:id="#+id/imageView8"
android:layout_width="match_parent"
android:layout_height="260dp"
app:srcCompat="#drawable/metal"
tools:ignore="ContentDescription" />
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/welcome"
android:textAlignment="center"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:textColor="#color/aqua"
android:textSize="60sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/WelcomeTextDescription"
android:textAlignment="center"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textColor="#color/aqua"
android:textSize="40sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="horizontal">
<android.support.v7.widget.AppCompatButton
android:id="#+id/next"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/next"
android:textAllCaps="false"
android:textAppearance="#style/TextAppearance.AppCompat.Button"
android:textColor="#color/white"
android:textSize="20sp"
android:textStyle="bold"
app:backgroundTint="#color/aqua" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>

Hello guys I found the answer
This awesome library will help everyone do it https://github.com/intuit/sdp
No need for many dimens or many layouts you just need this

You can use autosizing for TextView
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
app:autoSizeTextType="uniform" />
This will automatically adjust the text size according to your screen width
for more details read android official documentation

What you are doing wrong is you are defining a static text size for all the text views use dimens.xml for different screen sizes and assign the values of the dimen to the textViews like in the below image
I would suggest you to have a look at Dimenfy plugin and also at this link.

Related

What type of operation I'm missing..?

I read this Android Studio documentation: https://developer.android.com/guide/practices/screens_support.html#DeclaringTabletLayouts
Well the documentation is pretty obvious, to support different screens.
Use match_parent, wrap_content, create layout-sw folders.. Hmm, eazy task.. Yep, I made 5 layout-sw300dp-700dp folders, every layout owns match-parent and wrap content characteristic.. But somehow my image isn't getting scaled and all I want to do is to scale the image in one of my LinearLayouts..
I know theres a lot of answer out there on the forum, but as you can see its not confusing for me, but I'm sure I'm missing something important to end the screen support..
Should I create different size images and place them in a drawable folder?
Because whenever I'm reviewing another layout file for example sw600dp the image isnt scaled there.. Even the layouts aren't getting scaled..
Should I configure every layout-sw folders? I mean to place bigger image, to resize layouts.
I need to fill up the LinearLayout with the image. Now there's white spaces on the left and right side of the LinearLayout
The code belongs to the RelativeLayout, which has match-parents aswell.
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:padding="15dp"
android:text="This is random text"
android:textColor="#000000"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.39"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
</TextView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="50dp"
android:id="#+id/linearLayout"
android:layout_above="#+id/linearLayout2"
android:layout_alignParentEnd="true">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="50dp"
android:id="#+id/linearLayout2"
android:layout_above="#+id/linearLayout4"
android:layout_alignParentStart="true"
android:layout_marginTop="15dp">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="50dp"
android:layout_marginTop="15dp"
android:id="#+id/linearLayout4"
android:layout_above="#+id/linearLayout5"
android:layout_alignParentStart="true">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="15dp"
android:id="#+id/linearLayout5"
android:gravity="fill">
<ImageView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/info1"
android:layout_gravity="fill_horizontal"
android:contentDescription="" />
</LinearLayout>

Overlay Text on an Image in Android

I would like to know if there is a way to overlay text on an image in Android and cater for various different screen sizes? Currently I simply use the image as the background image to the layout and then I just use textview's over that.
The problem I have is that when the image is scaled on another device with a smaller or bigger screen, the position of this text changes and it looks different since the text does not scale the same way the image does on a different device, even though I use DP to position the textview's.
Is there some way to anchor these textview's to a certain point on the screen so that it looks more or less the same on different screen sizes?
Check this. this Xml will work well in any Device.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/yourimage"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true" />
<TextView
android:id="#+id/advrttext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Center Text in any Device"
android:textStyle="bold"
android:textSize="40sp"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="59dp" />
</RelativeLayout>
You can also use a relative layout and do something like this
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="250dp"
android:layout_height="250dp"
android:background="#drawable/uninorsample"
android:id="#+id/imageView12"/>
<TextView
android:id="#+id/example_Txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Example Text"
android:textSize="16sp"
android:gravity="bottom|center_horizontal"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="#+id/imageView12"
android:layout_alignRight="#+id/imageView12"
android:layout_alignEnd="#+id/imageView12"/>
</RelativeLayout>
You can set image as background in any widget or layout than set textview on that with appropriate dimension.i.e you can set shadow image in relative layout and set textview on it use property like centerinparent so it will always center of the parent in any DP.
Try this:-
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/clock_number"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Hello World!" />
</RelativeLayout>
Use FrameLayout
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorAccent"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Overlayed Text"
android:layout_gravity="center"/>
</FrameLayout>

text sizes different Android phones

Since I have a Nexus 5, I'm seeing strange stuff regarding text sizes.
An example: I am creating listview items. I put a bigger text at 22sp, a smaller one at 17sp.
On my HTC One X it looks like this:
So I think "ok, that's nice!"
Then I run it on my Nexus 5:
I do not understand why this is happening? Shouldn't the 'sp' part make sure it will look ok
with the default values? I am not talking about the user increasing the font sizes in the settings, I've left everything to defaults.
Eventhough, if I would (don't hit me) use DP here to disable the font size changes for text, it will give the same result (not tested, but I am fairly certain this is the case from other tests).
What is the problem here? Is it the newer Android version, or is it the 1080p screen vs the 720p ?
Should I use different values for different DP using 'values' folder?
here is de layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/background"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<com.android.volley.toolbox.NetworkImageView
android:id="#+id/image"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="2">
<ImageView
android:id="#+id/arrow"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:adjustViewBounds="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:layout_toLeftOf="#id/arrow"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:fontFamily="sans-serif-condensed"
android:textSize="22sp"
android:textStyle="bold" />
<TextView
android:id="#+id/address"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="17sp" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
For instance, the default size for same SP value may vary depending on device, just to match its readability designed for the device.
The problem you got is because you have some elements sized to fixed size, post your XML code you use for the row and we can make a deep analyse.
Edit -- After xml
Well, instead of point you the wrongs, I did prefer to rebuild your layout, take a look:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal" >
<com.android.volley.toolbox.NetworkImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<TextView
android:id="#+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/image"
android:layout_toLeftOf="#+id/arrow"
android:fontFamily="sans-serif-condensed"
android:textSize="22sp"
android:textStyle="bold" />
<TextView
android:id="#+id/address"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/image"
android:layout_toLeftOf="#id/arrow"
android:layout_below="#id/name"
android:textSize="17sp" />
<ImageView
android:id="#id/arrow"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true" />
</RelativeLayout>
Your layout was too complex to a simple row... now the layout will be as tall as the TextViews needs it to be, and it is not a problem, as the user is already used with his text size.
you have to Create two different layout folder and see the result

How to implement correct UI for several screens in android

Here i am trying to handle different screens ,These are several screens i am trying to handle ,Here if i adjust for one screen remaining screens getting error ,not getting what is problem
here is
Here is my code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/transactionpagebg" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="160dp"
android:gravity="center"
android:paddingLeft="50dp"
android:text="Current Meter Reading"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/white" />
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="center"
android:orientation="horizontal" >
<EditText
android:id="#+id/editText1"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:inputType="number" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="150dp"
android:layout_height="50dp" />
</LinearLayout>
<AbsoluteLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/saveButton"
android:layout_width="113px"
android:layout_height="wrap_content"
android:layout_x="61dp"
android:layout_y="330dp"
android:background="#drawable/savebutton" />
<Button
android:id="#+id/settingsButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="208dp"
android:layout_y="330dp"
android:background="#drawable/settingbutton" />
</AbsoluteLayout>
</RelativeLayout>
to do as it need to be, you need to have support for different screen sizes you need to have different layouts, and also some changes in your manifest file...
check this question How to support different screen size in android
and this link http://developer.android.com/guide/practices/screens_support.html
There are many ways to make each view look right on various screens. However, I have found two methods are working best for me (at the moment):
To get the views to dynamically set their screen positions, wrap the widgets in a LinearLayout (LL), and put those LLs in a single LL. Set the weight of the inner LL to 1, and they will space themselves equally within the parent LL. This works for horizontal and vertical layouts. The distance between the widgets gets adjusted automatically by the OS.
create different dimens.xml folders/files for the different screen sizes (small, medium, large) and further differentiate them by density of needed (mdpi, hdpi, xhdpi, etc.). This way you can set margins, paddings, font sizes, for each size/density to get what you want.
EDIT: I see you put "TRANSACTION" in the background drawable? You can't do that! You need to create an image with just the text, and put it in an ImageView at the top of the layout. That way, you can set its position and size as needed for different screens, and make the other widgets adjust their positions relative to that image.
Even better would be to find a free font (ttf file) you can use that matches what you want, and put it in a TextView.
Here is a quick example of your layout with the ideas I mentioned. You would put the margins and font sizes in a dimens.xml file, so that you can adjust them.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0000ee"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/hh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="36dp"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="36dp"
android:text="TRANSACTION"
android:textColor="#ffffff"
android:textSize="28sp" />
<TextView
android:id="#+id/test_textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Current Meter Reading"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/white" />
</LinearLayout>
<LinearLayout
android:id="#+id/test_linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal" >
<EditText
android:id="#+id/test_editText1"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_marginRight="24dp"
android:background="#ffffff"
android:inputType="number" />
<Spinner
android:id="#+id/test_spinner1"
android:layout_width="150dp"
android:layout_height="50dp"
android:background="#999999" />
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1" >
<Button
android:id="#+id/test_saveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="24dp"
android:text="Save" />
<Button
android:id="#+id/test_settingsButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>

Android .xml designing

There has been a flaw on my design for most of .xml files where the sizes usually goes larger on some screens and some on lower screens. I use px(Pixels).
I know that this might be a bad choice on my end because new phones will end up with more pixel ratio. eg Samsung Galaxy Nexus making all text into tiny text.
What would be a far more suitable type to use for future phones to come?
Extra:
I have been using linear layout for a long time but now that I need a scrolling for long menu I designed a RelativeLayout that nested a ScrollView which ScrollView nests a linear layout. Would it be a better Option to nest a tableLayout to have more control over the view?
Didn't want to flood the question screen cause it would look ugly.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/tab_tv_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/auto_Action"
android:background="#666666"
android:text="#string/options_dialog_menu_Action"
android:textSize="27px"
/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="#+id/FirstOption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/manual_Action"
android:layout_below="#+id/tab_tv_one"
android:text="#string/options_dialog_menu_Auto_Action"
android:textSize="27px" />
<RadioButton
android:id="#+id/SecondOption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/tab_tv_two"
android:layout_below="#+id/auto_Action"
android:text="#string/options_dialog_menu_Manual_Action"
android:textSize="27px"
android:onClick="onClick"
android:clickable="true"
/>
<RadioButton
android:id="#+id/ThirdOption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/tab_tv_two"
android:layout_below="#+id/manual_Action"
android:textSize="27px"
android:onClick="onClick"
android:clickable="true"
/>
</RadioGroup>
<TextView
android:id="#+id/tab_tv_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/text_size"
android:layout_below="#+id/manual_Action"
android:background="#666666"
android:text="#string/options_dialog_menu_Layout_Options"
android:textSize="27px"
/>
<TextView
android:id="#+id/text_size"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/background_clr"
android:layout_below="#+id/tab_tv_two"
android:text="#string/options_dialog_menu_Text_Size"
android:textSize="27px"
android:onClick="onClick"
android:clickable="true"
/>
<TextView
android:id="#+id/Options_clr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/brightness_ctrl"
android:layout_below="#+id/text_size"
android:text="#string/options_dialog_menu_Color_Options"
android:textSize="27px"
android:onClick="onClick"
android:clickable="true"
/>
<TextView
android:id="#+id/brightness_ctrl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/background_clr"
android:text="#string/options_dialog_menu_Brightness_color"
android:textSize="27px"
android:onClick="onClick"
android:clickable="true"
/>
<TextView
android:id="#+id/backToBasics"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/brightness_ctrl"
android:text="#string/options_dialog_menu_Back_To_Basics"
android:textSize="27px"
android:onClick="onClick"
android:clickable="true"
/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Don't use pixels, use dp (aka dip aka density independent pixels). Otherwise, as you noted, size will be dependent on density, which isn't good.
Its not just the galaxy nexus, ldpi phones will look different than mdpi, which will look different than hdpi etc. There is rarely any good reason to use px sizes, particularly with regards to view dimensions.
With regards to your extra question it depends entirely on what you want your layout to look like. Without seeing code and/or at least knowing what you want to occur (vs what you see now), no one is going to really be able to answer that.

Categories

Resources