I want to manage multi line edit text control's height with respect to device screen height. Height should be 40% of screen.
I have used max lines, lines and min lines properties. If I set these properties then height does change but if screen height changes or device shifted to horizontal then edit text goes out of the screen. in short, multi line edit text height must be dynamic with screen size. It should shrink and expand as other control does with screen size.
Screenshot:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusableInTouchMode="true"
android:background="#e8eeff">
<EditText
android:id="#+id/etTextMultiLine"
android:inputType="textMultiLine"
android:gravity="top|left"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:scrollbars="vertical"
android:hint="Enter a Text"
android:background="#layout/EditTextStyle"
android:textColorHint="#404144"
android:textColor="#404144"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:padding="5dp" />
Since you wish to achieve sizes with percentages, consider using PercentFrameLayout as your EditText's parent and just match both weight and height. The documentation says PercentFrameLayout is deprecated, so consider the replacement suggested, ConstraintLayout.
Related
I know this sounds simple but I wanted to change a button's font size to fill the Button .Even though the text doesn't take all the space inside the button when I decrease text height for example the Button's height decreases as well.Is there any way I can change The text-size so it fills that space inside the Button or do I have to just use an Image Button .
Here is the case :-
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#android:color/black"
android:id="#+id/led"
>
<Button
android:id="#+id/grow"
android:layout_width="match_parent"
android:layout_height="39dp"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:textSize="15dp"
android:background="#color/Lightbrown"
android:text="A▲"
android:textAllCaps="false"
/>
<Button
android:layout_width="match_parent"
android:layout_height="39dp"
android:text="A▼"
android:textAllCaps="false"
android:id="#+id/shrink"
android:background="#color/Lightbrown"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:padding="0dp"
android:textSize="10dp"
/>
See I used my Linearlayout as a background for my Buttons the second button's size changes with its font size I just want its size to remain the same as the first Button but with a smaller textsize.
Update
Your second button is not actually smaller, it is just aligned in a way you wouldn't necessarily expect.
Horizontal LinearLayouts with TextView (or subclass, which Button is) children will "baseline align" the children. That means they will make sure that the bottom edge of all the text in the row is at the same height. Since your second button uses smaller text, the text bottom would be higher up inside the button, so the LinearLayout forces the whole button down to accomodate.
Add this attribute to your LinearLayout:
android:baselineAligned="false"
Original
First, I assume you're using android:layout_height="wrap_content". If you don't want your button's height to scale with font size, you'll have to change this to some fixed value (or match_parent if you want it to be the same size as its parent).
As for why the text "doesn't take up all the space", that's because Buttons have padding built into them automatically. You can remove this padding by defining android:padding="0dp"
However, you'll soon notice that the button looks really bad if you give it no padding and too-large text. How to solve that is really up to the requirements of your design.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="32dp"
android:layout_gravity="center_horizontal"
android:padding="0dp"
android:textSize="36sp"
android:text="Hello world"/>
</FrameLayout>
Use a fixed size instead of wrap_content for your button.
f that doesn't work for your design, consider overlaying a TextView on top of your Button (with ConstraintLayout or FrameLayout or RelativeLayout). After that, you can set the TextView's focusable, focusableInTouchMode, and clickable properties to false so that it doesn't intercept your Button's clicks.
How can I adjust EditTexts and Buttons to fit any screen size for android ? I'm trying to put some transparent EditTexts and Buttons in certain positions to fit my background image, but when I change screen size every thing changes. Here are my background image and my XML 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/login_page"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="10sp" >
<EditText
android:id="#+id/etLogin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10sp"
android:layout_marginTop="135sp"
android:background="#android:color/transparent"
android:hint="Login"
android:inputType="textCapWords"
android:padding="8sp" />
<EditText
android:id="#+id/etPassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="0sp"
android:layout_marginTop="-3sp"
android:background="#android:color/transparent"
android:hint="Password"
android:inputType="textPassword"
android:padding="8sp" />
<Button
android:id="#+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12sp"
android:background="#android:color/transparent"
android:minHeight="40sp"
android:minWidth="500sp"
android:text="Login"
android:textColor="#08b0ef" />
</LinearLayout>
I tried other units like dp and dpi and dip but no one is giving the expected result.
Have you tried using "android:layout_weight" ?
I think what your talking about to set your Layout objects in a certain position relative to the background and once set, stay fixed proportionally so when the background stretches our contracts so do the buttons so they stay in the same position over the background?
If so (I could be way off),
setting the layout_weight attribute inside of each child object of the LinearLayout can let you position everything relative to the screen size so it automatically changes with each screen size. It will take a little trial and error to get the right percentages but should work.
Also consider creating multiple xml layout definitions for the major screen sizes so the OS automatically calls the one it needs for a particular screen with a resource qualifier, that way you know it will display in the right position. For example a xml called activity_main in R.layout is inflated by default but if you also create a activity_main in R.layout-land, this XML will only be inflated if the screen is in landscape mode. So you can set the sizes of your editText and Buttons for multiple screen size.
If you want to create only one file for all layout and trying to make your screen universal, you should try Linearlayout with weight property.
Instead of using sp or px, you should use pd for margin, padding or any other properties in your layout. dp will render differently as per screen resolution.
I have 2 TextViews which I need to both horizontally side by side, like category + nummber of products for category. The code is like below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:paddingBottom ="10dp"
android:background="#color/navigation_background_sub">
<TextView
android:id="#+id/category_name_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp"
android:textColor="#color/ebuy_color_navigation_name" />
<TextView
android:layout_toRightOf="#+id/category_name_second"
android:paddingLeft="7dp"
android:id="#+id/category_number_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp"
android:textColor="#color/ebuy_color_navigation_number" />
</RelativeLayout>
Now the problem is that once the text for the first TextView is to long , the space left for the second is to little and the text is display in two ore more lines.
I have no reputation to post pictures so I am just trying to explain like below:
How it is:
Laptops (34)
Computer Software (1
4
)
How it should be:
Laptops (34)
Computer (14)
Software
use
layout_toRightOf= "#id/category_name_second"
It may help you
If you replace your RelativeLayout with a horizontal LineraLayout the available space will be distributed evenly among your text views. You can add a weight attribute to control the portion of each TextView in one line of length.
Well, the problem is that you have not enough space at the horizontally line, this is why the text view display more lines.
You must define how much space you want for each Textview. If your layout is so simple like that, you can choose a linear layout to align better the textview's.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingLeft="15dp"
android:weightSum="1" >
<TextView
android:id="#+id/category_name_second"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:text="Computers"
android:textSize="22sp"
android:singleLine="true"
android:textColor="#color/ebuy_color_navigation_name" />
<TextView
android:id="#+id/category_number_second"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:paddingLeft="7dp"
android:text="(24)"
android:textSize="22sp"
android:singleLine="true"
android:textColor="#color/ebuy_color_navigation_number" />
</LinearLayout>
You must add also the property singleLine to avoid the textview's display more lines. As you see, using the weight you can set the proportional width for each component in the horizontal line.
In TextView When you use wrap_content android device will adjust its width and height(if you use wrap_content for height) according to data you put in (length of text). If text is not fix in textview then don't use wrap_content(only if you don't want that text automatically write in second line).So if first Textview has taken more than half space, second textview must adjust its height to show full text.There is no permanent solution of it, because this text line depends on device's size. if device's screen is larger to show both textview in single line it will show you unless it will increase height of second textview in your case.
you can also make different layout for all type of screen size and set text's size according to it. you can check for supporting multiple screen size.
I have a TextView in a ScrollView with a specific background image. My problem is that if the text is longer than one line, the TextView will stretch as much as the screen allows it. I would like the text view's sizes to remain the same (the sizes of the background)
<ScrollView android:id="#+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/padding_large"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="#dimen/padding"
android:paddingBottom="#dimen/padding"
android:paddingLeft="#dimen/padding_large"
android:paddingRight="#dimen/padding_large"
android:background="#drawable/textfield_background_small"
android:inputType="textMultiLine"
android:text="#string/step_description"
/>
</ScrollView>
Using wrap_content makes a layout dynamic, i.e. its size will grow and shrink depending on its content. If you instead change all your wrap_content to fill_parent the layouts will fill their parent container entirely.
You can read more about how to properly declare layouts here: http://developer.android.com/guide/topics/ui/declaring-layout.html
I have a horizontal LinearLayout and in it I have EditText and Spinner elements.
Which attributes I need to adjust so I would get proportional widths: I want EditText to take 3/5 and Spinner - 2/5 of all available width?
My code looks like this:
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="#+id/LinearLayout01">
<EditText
android:layout_height="wrap_content"
android:id="#+id/EditText01"
android:singleLine="true">
</EditText>
<Spinner
android:layout_height="wrap_content"
android:id="#+id/Spinner01"
android:layout_width="wrap_content">
</Spinner>
</LinearLayout>
I tried setting android:layout_weight, but somehow it does not look "stable" enough for me - when EditText has no text - everything looks fine, but as soon as I start entering text into it - it starts expanding and Spinner shrinking accordingly...
To literally achieve your request, try setting both widgets' android:layout_width="0px", then set your android:layout_weight values as appropriate (3 and 2).
Try setting the layout_width to 0dip as well as using layout_weight. That should do it.