Auto resize Textview - android

My goal is to create Auto rotate textView.Here is my xml code
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<TextView
android:id="#+id/u_major_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="#dimen/u_common_margin_left"
android:maxLines="1"
android:paddingRight="#dimen/u_common_margin_left"
android:singleLine="true"
android:text="0.00"
android:autoSizeTextType="uniform"
android:autoSizeMinTextSize="12dp"
android:autoSizeMaxTextSize="70dp"
android:autoSizeStepGranularity="2dp"
android:textColor="#000000"
android:textSize="70dp"
/>
<TextView
android:id="#+id/u_minor_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/u_major_text"
android:layout_marginLeft="-12dp"
android:layout_marginTop="7dp"
android:layout_toEndOf="#+id/u_major_text"
android:layout_toRightOf="#+id/u_major_text"
android:gravity="center"
android:singleLine="true"
android:text="₾"
android:textColor="#000000"
android:textSize="30dp" />
</RelativeLayout>
my goal is to my minor_textview can to be auto resize function.first time text size is 70dp but I want to decrease size depends on a text. What am I doing wrong ?

Apparently, since Android 8.0 and below (thanks to the Support Library) you can use an attribute to auto resize your Textview:
TextView minorText = findViewById(R.id.u_minor_text);
TextViewCompat.setAutoSizeTextTypeWithDefaults(text, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
Check this link:
https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview

Related

Aligning multiple checkboxes vertically

I try to align four checkboxes but one is not in the center of the other. If I shorten the text it fits but if its longer it looks like in the screenshot. The problem is it will be in different languages so the text size can be different. Also if I remove the scale it fits perfect but I need this bigger checkboxes
This is my code:
<CheckBox
android:id="#+id/cb1"
android:scaleX="1.3"
android:scaleY="1.3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="23dp"
android:text="#string/unmounted"
android:textColor="#color/textBlack"
android:layout_below="#+id/abc"
android:layout_toEndOf="#+id/imageView6"
android:focusable="false"/>
<CheckBox
android:id="#+id/cb2"
android:scaleX="1.3"
android:scaleY="1.3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/problem_not_working"
android:textColor="#color/textBlack"
android:layout_below="#+id/cb1"
android:layout_alignStart="#+id/cb1"
android:focusable="false"/>
<CheckBox
android:id="#+id/cb3"
android:scaleX="1.3"
android:scaleY="1.3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/problem_cable_defective"
android:textColor="#color/textBlack"
android:layout_below="#+id/cb2"
android:layout_alignStart="#+id/cb2"
android:focusable="false"/>
<CheckBox
android:id="#+id/cb4"
android:scaleX="1.3"
android:scaleY="1.3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/problem_cable_tie"
android:textColor="#color/textBlack"
android:layout_below="#+id/cb3"
android:layout_alignStart="#+id/cb3"
android:focusable="false"/>
Here is the exact solution for your problem, the thing which you are actually trying to achieve is the bigger checkbox view (corresponding to its text which is as big as the checkbox view, obviously) with proper alignment of all the views in the series, something like this:
Your desired UI.
But the thing you are doing wrong is that you are using the scaleX and scaleY attributes for achieving your desired UI that distorts your view as these two attributes scale the complete view i.e. the squared checkbox view with its text/label in the X and Y directions respectively (together with the text/label of the checkbox), so there is a dependency on the text as well (which is distorting your view).
So, the below code will solve your problem:
(In this I have taken the squared checkbox view as separate and beside it I have placed a textview in which I have written the text which corresponds to the text/label of that checkbox to complete the UI). Thereby using two views (i.e. the CheckBox and the TextView beside it) instead of using a single compound view (i.e. the CheckBox).
Note: In the solution given below, the CheckBox is a compound view but I haven't used it in the
same manner.
<?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">
<View
android:id="#+id/some_view"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="20dp"
android:layout_marginTop="23dp" />
<!--android:text="Unmounted"-->
<CheckBox
android:id="#+id/cb1"
android:scaleX="1.3"
android:scaleY="1.3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="23dp"
android:layout_below="#+id/some_view"
android:layout_alignStart="#+id/some_view"
android:focusable="false" />
<TextView
android:id="#+id/tv_for_cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#id/cb1"
android:layout_alignTop="#+id/cb1"
android:layout_alignBottom="#+id/cb1"
android:layout_marginStart="40dp"
android:text="Unmounted"
android:textColor="#android:color/black"
android:textSize="24sp" />
<!--android:text="Problem Not Working"-->
<CheckBox
android:id="#+id/cb2"
android:scaleX="1.3"
android:scaleY="1.3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="23dp"
android:layout_below="#+id/cb1"
android:layout_alignStart="#+id/cb1"
android:focusable="false"/>
<TextView
android:id="#+id/tv_for_cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#id/cb2"
android:layout_alignTop="#+id/cb2"
android:layout_alignBottom="#+id/cb2"
android:layout_marginStart="40dp"
android:text="Problem Not Working"
android:textColor="#android:color/black"
android:textSize="24sp" />
<!--android:text="Problem Cable Defective"-->
<CheckBox
android:id="#+id/cb3"
android:scaleX="1.3"
android:scaleY="1.3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="23dp"
android:layout_below="#+id/cb2"
android:layout_alignStart="#+id/cb2"
android:focusable="false"/>
<TextView
android:id="#+id/tv_for_cb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#id/cb3"
android:layout_alignTop="#+id/cb3"
android:layout_alignBottom="#+id/cb3"
android:layout_marginStart="40dp"
android:text="Problem Cable Defective"
android:textColor="#android:color/black"
android:textSize="24sp" />
<!--android:text="Problem Cable Tie"-->
<CheckBox
android:id="#+id/cb4"
android:scaleX="1.3"
android:scaleY="1.3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="23dp"
android:layout_below="#+id/cb3"
android:layout_alignStart="#+id/cb3"
android:focusable="false"/>
<TextView
android:id="#+id/tv_for_cb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#id/cb4"
android:layout_alignTop="#+id/cb4"
android:layout_alignBottom="#+id/cb4"
android:layout_marginStart="40dp"
android:text="Problem Cable Tie"
android:textColor="#android:color/black"
android:textSize="24sp" />
</RelativeLayout>
And the other way out to your mentioned problem would be to create custom drawables for the checkbox and then handle them using a state list drawable as for the checkbox to be shown differently in different states as when it is unchecked, checked etc.
This link would help you if you are interested in doing it in this manner or way and would be great if you want to customize your checkbox's view.
Creating custom checkbox using selectors and shapes.
For further help in this context refer:
Android: How to change CheckBox size?

How to make TextView in Android wrap and adjust text beautifully?

When text is small then I get this:
But when text is big I get this:
Is it possible that text adjust itself by maybe better using spaces or decreasing little size automatically?
This is my TextView xml:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/seaGreen"
android:gravity="center_vertical"
android:padding="10dp"
android:textColor="#color/white"
android:textSize="35sp" />
For that you need to use external library for that, else you can set gravity as center
Library :
https://github.com/grantland/android-autofittextview
For Text Justify
https://github.com/bluejamesbond/TextJustify-Android
https://github.com/programingjd/justified
OR
you can check this answer
this work for me:
<TextView
android:id="#+id/your_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:textAlignment="center"
android:gravity="center"
android:padding="5dp"
android:text=""
android:textSize="14sp"/>

how do I make an image fit in my button

each time I add and image to my button (want to add it below the text) it only shows the top left of the image and adds itself at the top of the button even when I use android:drawableBottom .
here is my XML code :
<Button
android:layout_width="162dp"
android:layout_height="200dp"
android:text="#string/weather_station"
android:textStyle="bold"
android:textSize="18dp"
android:background="#drawable/weather"
android:paddingBottom="120dp"
android:paddingLeft="5dp"
android:typeface="monospace"
android:id="#+id/weather_station" />
android:drawableBottom="#drawable/weatherman"
/>
use ImageButton instead of using Button and try to scale the image`
<ImageButton
android:id="#+id/my_image_button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:src="#drawable/my_img" />
<Button
android:layout_width="162dp"
android:layout_height="200dp"
android:text="bUTTON"
android:textStyle="bold"
android:textSize="18dp"
android:background="#drawable/shape1"
android:paddingBottom="120dp"
android:paddingLeft="5dp"
android:drawableBottom="#drawable/cast_ic_notification_2"
android:typeface="monospace"
android:id="#+id/weather_station" />
how cant it possible even your drawable line out of scop ? try above WORKING CODE
Try setting up the below scale type in the xml for fill image in button.
android:scaleType="scaleXY"
So put this attribute in your xml code.
<Button
android:layout_width="162dp"
android:layout_height="200dp"
android:text="#string/weather_station"
android:textStyle="bold"
android:textSize="18dp"
android:background="#drawable/weather"
android:paddingBottom="120dp"
android:scaleType="scaleXY"
android:paddingLeft="5dp"
android:typeface="monospace"
android:id="#+id/weather_station"
android:drawableBottom="#drawable/weatherman"/>

Android TextView ellipsize

I'm using Android TextView to display the name of the CATALOG.
I added the specific width for TextView outer layout (150dp).
I wants to display "..." when text has more length.
<RelativeLayout
android:id="#+id/textview_layout"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/catalog_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_vertical"
android:textColor="#color/blue"
android:textSize="20dp"
android:singleLine="true"
android:ellipsize="end"
/>
</RelativeLayout>
Catalog Name: Furniture and Books
My result showing:
Furniture an...
But my expectation result is
Furniture and... (or) Furniture...
I wants to show the "..." when there is no space for word in catalog name.
Please check whether this works
<RelativeLayout
android:id="#+id/textview_layout"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/catalog_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:singleLine="true"
android:text="Furniture and Books"
android:textColor="#android:color/holo_blue_dark"
android:textScaleX="1.1"
android:textSize="20dp" />
</RelativeLayout>
You can try this in java code, if it's really necessary.
String str = "Furniture and Books";
str = str.substring(0,8)+"...";
text_view.setText(str);
try to set the EMS for your text view, like :
<TextView
android:id="#+id/catalog_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_vertical"
android:textColor="#color/blue"
android:textSize="20sp"
android:singleLine="true"
android:ellipsize="end"
android:maxEms="4"
/>
by the way, using "sp" in fonts is a good practice ... any ways
where EMS is the size of characters your textview should hold, in practice, 4 EMS makes the text view holds 8 charachters (you can test it with your devices), and if you want to give it a fixed width based on the numer of charachters, you can use
android:ems="4"
instead

Problem with sizes of EditText and Button in Android

I want to make the edittext width the same size as button. My EditText is currently very small. I use relative layout.
<TextView
android:id="#+id/aha4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17dip"
android:text="Vzdevek:"
android:layout_below="#id/aha3" />
<EditText android:id="#+id/nick"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="#id/nivo"
android:layout_toRightOf="#id/aha4"/>
<Button android:id="#+id/poslji"
android:text="Pošlji"
android:layout_height="wrap_content"
android:layout_width="20dip"
android:typeface="serif" android:textStyle="bold"
android:layout_alignParentRight="true"
android:layout_below="#id/nivo"
android:layout_toRightOf="#id/nick"/>
What i currently get is this:
alt text http://www.shrani.si/f/1Z/x8/2lWB3f8p/device.png
What is the appropriate layout_width for edittext and button?
As you are using something like a row to show the TextView, EditText and button, you can try to use TableLayout: http://developer.android.com/guide/tutorials/views/hello-tablelayout.html
Also, make sure you set android:layout_width="wrap_content" so that the EditText use as much space as possible.
Try to assign equal weight to both button and edit text
Yeah, you're right about weight.
As a hack you can do this. Not exactly right though.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/aha4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17dip"
android:text="Vzdevek:" />
<Button
android:id="#+id/poslji"
android:text="Pošlji"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingLeft="40dip"
android:paddingRight="40dip"
android:typeface="serif"
android:textStyle="bold"
android:layout_alignParentRight="true" />
<EditText
android:id="#+id/nick"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toRightOf="#id/aha4"
android:layout_toLeftOf="#id/poslji" />
</RelativeLayout>
I managed to solve it with specifying minWidth and maxWidth for EditText.

Categories

Resources