Adding a margin in android - android

I need to add a margin in front of a text view in android but it doesn't work when I use android:layout_marginLeft/Right. Is there another way to do so?
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="7dip"
android:paddingRight="27dip"
android:paddingTop="7dip"
android:text="Name"
android:textColor="#10a6a4"
android:textSize="20dp"
android:layout_marginRight="40dp"
android:textStyle="bold" />

Are you using a RelativeLayout ?
margin and padding do not work (as far as I am aware) in RelativeLayouts. Try a LinearLayout instead, or if you are using android:layout_above and android:layout_bellow or these things, put the txtView and the View you're padding it from in one new LinearLayout.
Hope I helped.

Related

android :gap between two textview in relative layouts

Android: I want to have gap of 20 dp between two textview i am using relative view and have already used textview 2 should be below textview 1 , but i dont understand how to give more gap in between these 2 textviews
You can use padding or margin. I recommend margin.
<TextView
android:text="text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/your_id"
android:layout_margin="20dp" />
EDIT:
Thanks to #Gabe Sechan to amplify the answer. If you want margin only in one side, you can use the specific margin instruction of the side you want. Example:
Margin only on bottom
<TextView
android:text="text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/your_id"
android:layout_marginBottom="20dp" />
And the other sides
android:layout_marginTop, android:layout_marginLeft and android:layout_marginRight.

Android: Properly horizontally centering two views for multiple screen sizes

I know this is a question that is asked quite a lot, but none of the answers that have been provided so far have helped me resolve my problem. It's best described with pictures. I unfortunately don't have enough reputation to embed them here, but here's an imgur with three screen sizes demonstrating my issue: http://imgur.com/a/D5uAu
I'm looking for a way to have the "Retrumpets" ImageButton and TextView properly centered for all screen sizes. Right now, they're currently centered using layout_centerHorizontal on the retrumpetTextView, with the retrumpetButton attached to the left of the TextView with layout_toLeftOf, like so:
<ImageButton
android:layout_width="35dp"
android:layout_height="30dp"
android:id="#+id/retrumpetButton"
android:src="#drawable/retrumpet"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:background="#00FFFFFF"
android:layout_above="#+id/detailedTrumpetSeparationLine"
android:layout_toLeftOf="#+id/retrumpetCountTextView"/>
<TextView
tools:text="30 Retrumpets"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/retrumpetCountTextView"
android:textColor="#FFFFFF"
android:paddingBottom="6dp"
android:paddingLeft="2dp"
android:textSize="14sp"
android:layout_centerHorizontal="true"
android:layout_below="#+id/trumpetTextView"/>
This would work perfectly, if not for the fact that what I truly want to center is a combination of BOTH of these views, so that one view or the other isn't too far to the left or right.
My question is: what is the best way to go about doing this? Can I somehow provide an adjustment value to shift both of these views a bit to the right? layout_marginStart and layout_marginLeft don't seem to work when a view is centered.
Thank you very much!
EDIT: Thanks very much all! Combining the two into a horizontally centered LinearLayout did the job perfectly.
You can use a LinearLayout to contain them.
<LinearLayout
android:layout_centerHorizontal="true"
android:layout_below="#+id/trumpetTextView"
android:orientation = "horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="35dp"
android:layout_height="30dp"
android:id="#+id/retrumpetButton"
android:src="#drawable/retrumpet"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:background="#00FFFFFF"
android:layout_above="#+id/detailedTrumpetSeparationLine"
android:layout_toLeftOf="#+id/retrumpetCountTextView"/>
<TextView
tools:text="30 Retrumpets"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/retrumpetCountTextView"
android:textColor="#FFFFFF"
android:paddingBottom="6dp"
android:paddingLeft="2dp"
android:textSize="14sp"
/>
</LinearLayout>
Or just use drawableLeft
<TextView
tools:text="30 Retrumpets"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/retrumpetCountTextView"
android:textColor="#FFFFFF"
android:paddingBottom="6dp"
android:paddingLeft="2dp"
android:textSize="14sp"
android:drawableLeft="#drawable/retrumpet"
android:layout_centerHorizontal="true"
android:layout_below="#+id/trumpetTextView"/>
You should put all those components i.e the "Retrumpets" ImageButton and TextView all in 1 Layout. LinearLayout. Then you can put layour gravity to this LinearLayout to be centered.
You might want to consider putting the ImageButton and TextViews in a horizontal linear layout and use the weights to adjust them correctly. Try setting the layout_weight of each ImageButton to 0 and the TextViews each to 1.

What would be a way working with layout weight

I have tried this. I planned to do like this:
But it showing in this way:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvHiragana"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hiragana"
android:layout_weight="2"
android:textSize="22sp" />
<TextView
android:id="#+id/tvKanji"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kanji"
android:textColor="#color/redColor"
android:layout_weight="1"
android:textSize="20sp" />
</LinearLayout>
What should I do make it work correct in the terms of layout_weight? How to avoid making new lines in TextView?
What should I do make it work correct in the terms of layout_weight?
First, when using weight with a horizontal layout, your width should be 0dp. Similarly, if your layout is vertical then your height should be 0dp.
How to avoid making new lines in TextView?
use android:singleLine="true" if you want to
Constrains the text to a single horizontally scrolling line instead of letting it wrap onto multiple lines
TextView Docs
If this doesn't answer your question then please be a little more clear because its a bit confusing on what you're asking.

Android: Align button to bottom-right of screen using FrameLayout?

I am trying to put the zoom controls of the map on the bottom right corner of screen. I could do it with RelativeLayout using both alignParentBottom="true" and alignParentRight="true", but with Framelayout I did not find any such attributes. How do I align it to the bottom-right of screen?
Actually it's possible, despite what's being said in other answers. If you have a FrameLayout, and want to position a child item to the bottom, you can use android:layout_gravity="bottom" and that is going to align that child to the bottom of the FrameLayout.
I know it works because I'm using it. I know is late, but it might come handy to others since this ranks in the top positions on google
I also ran into this situation and figured out how to do it using FrameLayout.
The following output is produced by the code given below.
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/contactbook_icon"
android:layout_gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="140"
android:textSize="12dp"
android:textColor="#FFFFFF"
android:layout_gravity="bottom|right"
android:layout_margin="15dp" />
</FrameLayout>
Change the margin value to adjust the text position over the image. Removing margin might make the text to go out of the view sometimes.
It can be achieved using RelativeLayout
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:src="#drawable/icon"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>
Setting android:layout_gravity="bottom|right" worked for me
Two ways to do this:
1) Using a Frame Layout
android:layout_gravity="bottom|right"
2) Using a Relative Layout
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
If you want to try with java code. Here you go -
final LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
yourView.setLayoutParams(params);
params.gravity = Gravity.BOTTOM; // set gravity
Use bottom|right as the layout_gravity value of the element which you wish to align in the bottom right position of the parent layout.
android:layout_gravity="bottom|right"
you can add an invisible TextView to the FrameLayout.
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:visibility="invisible"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Button
android:id="#+id/daily_delete_btn"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="DELETE"
android:layout_gravity="center"/>
<Button
android:id="#+id/daily_save_btn"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="SAVE"
android:layout_gravity="center"/>
</LinearLayout>
There are many methods to do this using constraint widget of the activity.xml page of android studio.
Two most common methods are:
Select your button view and adjust it's margins.
Go to "All attributes",then to "layout_constraints",then select
layout_constraintBottom_toBottomOf_parent
layout_constraintRight_toRightOf_parent
You can't do it with FrameLayout.
From spec:
http://developer.android.com/reference/android/widget/FrameLayout.html
"FrameLayout is designed to block out an area on the screen to display a single item. You can add multiple children to a FrameLayout, but all children are pegged to the top left of the screen."
Why not to use RelativeLayout?

Android TextView Text not getting wrapped

Can anyone tell me what's going wrong with the text? Text longer than one line doesn't wrap to the next line but goes beyond the screen.
Following is the code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="4dip">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="4dip">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="4dip">
<TextView
android:id="#+id/reviewItemEntityName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="event/venue"
android:textColor="#color/maroon"
android:singleLine="true"
android:ellipsize="end"
android:textSize="14sp"
android:textStyle="bold"
android:layout_weight="1" />
<ImageView
android:id="#+id/reviewItemStarRating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:src="#drawable/title_1_star" />
</LinearLayout>
<TextView
android:id="#+id/reviewItemDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Description comes here"
android:textSize="12sp"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
I fixed it myself, the key is android:width="0dip"
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="4dip"
android:layout_weight="1">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="4dip">
<TextView
android:id="#+id/reviewItemEntityName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/maroon"
android:singleLine="true"
android:ellipsize="end"
android:textSize="14sp"
android:textStyle="bold"
android:layout_weight="1" />
<ImageView
android:id="#+id/reviewItemStarRating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true" />
</LinearLayout>
<TextView
android:id="#+id/reviewItemDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:width="0dip" />
</LinearLayout>
<ImageView
android:id="#+id/widget01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/arrow_nxt"
android:layout_gravity="center_vertical"
android:paddingRight="5dip" />
</LinearLayout>
The only correct answer to this question is that you need to set the parents to a proper width (in this case FILL_PARENT, WRAP_CONTENT) and use android:layout_weight=1 for the textview that needs to be wrapped.
SingleLine is on by default so that won't make any changes.
A width set to 0px will work but is not a good solution.
Some example (in a tableview this time), using xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<TableLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:stretchColumns="*"
android:id="#+id/tableLayout1">
<TableRow android:id="#+id/tableRow1" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:text="test1" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="0" />
<TextView android:layout_weight="1"
android:text="test2 very long text that needs to be wrapped properly using layout_weight property and ignoring singleline since that is set by default..."
android:layout_width="fill_parent" android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
</LinearLayout>
If you want to set this in code you're looking for the layout_weight as a third parameter as in this example where it is set to 1:
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView label = new TextView(getApplicationContext());
label.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT, 1f));
You must use 2 parameters :
android:ellipsize="none" : the text is not cut on textview width
android:scrollHorizontally="false" the text wraps on as many lines as necessary
It is enough to use in your xml file.
android:singleLine="false".
Hope it will work.
All the best!
I could not get any of these solutions working when using a TableLayout>TableRow>TextView. I then found TableLayout.shrinkColumns="*". The only other solution that worked was forcing the TextView to layout_width:250px etc but i don't like forcing widths like that.
Try something like this if working with tables.
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:shrinkColumns="*">
Note you need shrinkColumns="*"
This is obviously within a <LinearLayout>. So something like <LinearLayout> <TableLayout> <TableRow> <TextView>
references:
TableLayout
http://code.google.com/p/android/issues/detail?id=4000
Hope that helps someone.
One of your layout parameters is wrong in your code. In the first TextView
android:layout_width="wrap_content"
change to
android:layout_width="fill_parent"
The text that out of screen width size will wrap to next line and set android:singleline="false".
Set the height of the text view android:minHeight="some pixes" or android:width="some pixels". It will solve the problem.
For my case removing input type did the trick, i was using android:inputType="textPostalAddress" due to that my textview was sticked to one line and was not wrapping, removing this fixed the issue.
I'm an Android (and GUI) beginner, but have lots of experience with software. I've gone through a few of the tutorials, and this is my understanding:
layout_width and layout_height are attributes of the TextView. They are instructions for how the TextView should shape itself, they aren't referring to how to handle the content within the TextView.
If you use "fill_parent", you are saying that the TextView should shape itself relative to it's parent view, it should fill it.
If you use "wrap_content", you are saying that you should ignore the parent view, and let the contents of the TextView define it's shape.
I think this is the confusing point. "wrap_content" isn't telling the TextView how to manage it's contents (to wrap the lines), it's telling it that it should shape itself relative to it's contents. In this case, with no new line characters, it shapes itself so that all the text is on a single line (which unfortunately is overflowing the parent).
I think you want it to fill the parent horizontally, and to wrap it's contents vertically.
Even-though this is an old thread, i'd like to share my experience as it helped me. My application was working fine for OS 2.0 & 4.0+ but for a HTC phone running OS 3.x the text was not wrapping. What worked for me was to include both of these tags.
android:maxLines="100"
android:scrollHorizontally="false"
If you eliminate either it was not working for only the os 3.0 device. "ellipsize" parameter had neutral effect. Here is the full textview tag below
<TextView
android:id="#+id/cell_description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:maxLines="100"
android:scrollHorizontally="false"
android:textStyle="normal"
android:textSize="11sp"
android:textColor="#color/listcell_detail"/>
Hope this would help someone.
Increase the height i.e.. android:height="Some size" , it is working fine for me.
I had a similar problem where were my two horizontally weighted TextViews didn't wrap the text. I later found out that the issue was because my viewparents parent had wrap_content instead of match_parent.
I think it depends on the particular combination of layouts in your display. Some flags may get overridden or ignored. I have a TabHost with tabs, each tab is a list of tables. So it is a tab of ListView, each row being a TableLayout of TextView. I tried the fixes listed above and none of them worked.
I know, that in question it is correct, but in my case, the problem was in setting textSize property in 'dp' - I've changed it to 'sp' and it works fine.
In my case, with a TableRow > ScrollView > TextView nesting, I solved the problem by setting android:layout_width to fill_parent on TableRow, and to wrap_content on ScrollView and TextView.
you have to use android:singleLine="false" in ur TextView tags.
I finally managed to add some pixels to the height of the TextView to solve this issue.
First you need to actually get the height of the TextView. It's not straightforward because it's 0 before it's already painted.
Add this code to onCreate:
mReceiveInfoTextView = (TextView) findViewById(R.id.receive_info_txt);
if (mReceiveInfoTextView != null) {
final ViewTreeObserver observer = mReceiveInfoTextView.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int height = mReceiveInfoTextView.getHeight();
int addHeight = getResources().getDimensionPixelSize(R.dimen.view_add_height);
mReceiveInfoTextView.setHeight(height + addHeight);
// Remove the listener if possible
ViewTreeObserver viewTreeObserver = mReceiveInfoTextView.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.removeOnGlobalLayoutListener(this);
}
}
});
}
You need to add this line to dimens.xml
<dimen name="view_add_height">10dp</dimen>
Hope it helps.
I just removed android:lines="1" and added android:maxLines="2", this got the text to wrap automatically. The problem was the android:lines attribute. That causes the text wrapping to not happen.
I didnt have to use maxEms or singleLine="false" (deprecated API) to fix this.
I've spent hours to figure out that in the text I was trying to display contained a single quote (in string.xml) so I just escaped it with a backslash and it worked nicely => the height of the TextView was correctly wrapping text:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-smallcaps"
android:text="#string/instructions"
android:textAlignment="center"
android:textSize="18sp"
android:textStyle="bold" />
<EditText
android:id="#+id/keyword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/defaultKeyword"
android:textSize="22sp"
/>
I'm using constraint layout mostly.
1) android:layout_width="match_parent" = tries to stretch to meet edges
2) android:layout_width="wrap_content" = based solely on the input text without regard for other views nearby. for example adding android:textAlignment="center" will change the shape of the text
3) android:padding="12dp"
android:layout_width="0dp"
android:layout_weight="1"
android:singleLine="false"
= The text will fold to accommodate nearby layouts without regard to the text itself
In my case, My text view has a background, so applying width as 0dp does not work for me, because even for small texts the background will take the whole available space. Managed to solve it by adding
app:layout_constrainedWidth="true"
no need to set the width as 0dp, wrap content is working fine, hope this helps someone with same issue
inside your TextView write this:
android:inputType="textMultiLine"
problem solved
I put this attribute:
android:inputType="textMultiLine"
into my TextView and it has wrapping line and user can "Enter" for a new line.
============================================================
When you come to this post, you may want to create a Big TextView which can display multiple lines so these attributes may also needed
android:layout_height="Xdp" //where X is a number depends on how big Textview you want
android:gravity="top" //in order to make your text start from the top of your TextView.
I used android:ems="23" to solve my problem. Just replace 23 with the best value in your case.
<TextView
android:id="#+id/msg"
android:ems="23"
android:text="ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab "
android:textColor="#color/white"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
I added a \n in the middle of my string and it looked okay.
To wrap the text and to put the text in next line we sholud use the "\n" i.e new line character in the layout xml file and check tht change on the emulator not on the layout screen.

Categories

Resources