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.
Related
I have a activity layout which represents a form. The form contains a few form fields including an Spinner.
The layout is of type ConstraintLayout.
Because the Spinner control doesn't have a baseline i can't vertical align my "Kind" label correctly. I used 24 dp at the top as a temp. solution, but because the Spinner will increase in height i don't like this solution.
Is there a better solution to get the Kind label aligned correctly?
<TextView android:id="#+id/video"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lorem Ipsum"
app:layout_constraintBottom_toBottomOf="#+id/spinner"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="#+id/spinner" />
When you anchor the bottom and top of a view to another, it will be centered relatively. This is valid vertically and horizontally.
Hope this helps.
In my experience, the TextView and the Spinner aligned better when using baseline alignment (layout_constraintBaseline_toBaselineOf).
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Repeat"/>
<Spinner
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="#id/textview"
app:layout_constraintBaseline_toBaselineOf="#id/textview"
android:layout_marginLeft="50dp"/>
I am having a table with each row containing 3 buttons.
Now, I want to add some padding between each buttons in a single row.. How can I do that..?
When i add this statement in my code,
tableRow.setPadding(20,20,20,20);
I am able to observe padding but between each row.
I want to have padding between each button...
Note: I want to do it from java programmatically, not from xml..
You can do it in this way:
<Button android:id="#+id/myBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="Click Me"
/>
padding is the margin inside the button between the edges and the text.
This will increase button size not the distance between buttons
You need to use margin like this in your XML inside your button tag
android:layout_margin="10dp"
this will set all margins to 10dp
if you need to set margins only for certain side use :
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
I hope this helps you out.
Regards
you need to add padding to your buttons.
btn1.setPadding(left, top, right, bottom);
btn2.setPadding(left, top, right, bottom);
.
.
.
also make sure to take units in dp, hard-coded values will result in uneven padding on devices with various resolutions.
The TableRow class, as with all View subclasses, indeed has a setPadding method.
However, since you mention that you found setMargin, I believe you are looking at the TableRow.LayoutParams instead of the TableRow itself.
Margins are set in a View's LayoutParams, whereas padding is set on the View.
You need to use margin for your buttons instead of padding for the whole row. Because padding is actually a space between the button text and border, e.g. inner spacing, while margin is the space between button border and the it's container (row) e.g. outer space
Depending on what look you are trying to achieve, use something like this
<Button android:id="#+id/yourButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="Button1"
/>
<Button android:id="#+id/yourButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="Button2"
/>
<Button android:id="#+id/yourButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="Button3"
/>
i am suppose to place a TextView right of a multiline TextView.
please check the code below .
<RelativeLayout
android:id="#+id/rltest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/rlanswer"
android:layout_marginLeft="#dimen/grdiviewspacing"
android:layout_marginRight="#dimen/grdiviewspacing"
android:background="#android:color/transparent" >
<TextView
android:id="#+id/lblanswer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:ellipsize="end"
android:lines="1"
android:text="#string/strbookmarkmessage"
android:textColor="#color/black"
android:textSize="#dimen/listviewlocationsize"
android:textStyle="bold" />
<TextView
android:id="#+id/btnyesiwillcollectitby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_toRightOf="#id/lblanswer2"
android:autoLink="all"
android:linksClickable="true"
android:text="#string/stryesiwillcollectitby"
android:textColor="#color/linkcolor"
android:textSize="#dimen/listviewlocationsize" />
</RelativeLayout>
i have tried the problem with the above code is its not showing the second TextView. where us if i try layout_below - its showing the second text view. can anyone please assists me..
i want something like this - blue textview text to read textview.
Use LinearLayout instead of RelativeLayout and give layout_weight=1 to both the TextView you will get result whatever you want.
If you want to keep using a RelativeLayout, you will have to set the width in dip (instead of wrap_content) for the TextView "lblanswer2", otherwise it will always take the whole place if your text is long.
If using a RelativeLayout is not a necessity, you can follow pratik's answer and implement a LinearLayout, this way your 2 TextViews will share the width equally.
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.
i want to move a seekbar 20dip margin left from center.
android:progressDrawable="#drawable/progress_vertical"
android:thumb="#drawable/seek_thumb" android:layout_height="80dip"
android:layout_width="20dip" android:layout_marginBottom="50dip"
android:layout_alignParentBottom="true" android:visibility="gone"
android:layout_centerHorizontal="true" android:layout_marginLeft="20dip" />
but the above xml code shows it center only.
I made a workaround with a TextView in the middle that has no content (and is not visible).
<TextView
android:id="#+id/viewMiddleInvisible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btnAtTheTop"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp" />
<TextView
android:id="#+id/tvAtLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#id/viewMiddleInvisible"
android:layout_marginRight="10dp"
android:layout_toLeftOf="#id/viewMiddleInvisible"
android:text="Some text" />
You would obviously have to replace tvAtLeft with whatever you want. Same principle if you want to have something to the right of the center.
android:layout_centerHorizontal and android:layout_marginLeft don't work together.
A workaround is to use an invisible view, such as Space:
Space is a lightweight View subclass that may be used to create gaps
between components in general purpose layouts.
So, for your case, inside your RelativeLayout, this should do:
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="#+id/blankspace" />
<ProgressBar
android:progressDrawable="#drawable/progress_vertical"
android:thumb="#drawable/seek_thumb"
android:layout_height="80dip"
android:layout_width="20dip"
android:layout_marginBottom="50dip"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#id/blankspace"
android:visibility="gone" />
The cleanest answer, which doesn't require to pollute your XML with additional views is simply:
Add a 40dp padding on the right.
Rationale:
Padding belongs to your View, so it counts towards its final size. If you add a 40dp padding, your View will be 40dp wider without padding.
And because you are centering, For every 2dp of padding, your View will move just 1dp to the left, so your View will move only 20dp, as needed.
Remove centerHorizontal = true.