I am trying to move all these buttons a little up to top, like about 50dp.
here is my code for first button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="#+id/button2"
android:layout_centerVertical="true"
android:layout_marginBottom="10dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_marginTop="10dp"
android:background="#drawable/button1"
android:text="Load game"
android:textColor="#C3C3C3"
android:textStyle="bold|italic"/>
Try adding android:layout_marginBottom="50dp"
to your layout where are your all buttons
padding property is used to set the internal of the widget (for example paddingtop=10 then your text in button will be 10px below in the rect of button )
margine property is relatived to the Parent ViewGroup of the widget
Try to put your buttons in a linear layout and set margin or padding to it like this:
<LinearLayout
android:id="#+id/searchLinearLayout"
android:paddingTop="50dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
Related
I am trying to make it so that the Button in the LinearLayout starts from the right side and to set the Button 5dp from the right edge of the LinearLayout, with the TextView showing to the left of the button. Currently, it only allows to set the button from the "left" edge of the linear layout. I've tried all sorts of ways but nothing seems to work.
<LinearLayout
android:id="#+id/remote_button_layout"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginTop="30dp"
android:background="#drawable/btn_remote_button"
android:orientation="horizontal">
<TextView
android:text="Example Text"
android:textSize="10sp"
android:fontFamily="#font/mmedium"
android:textAllCaps="false"
android:layout_width="125dp"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#android:color/secondary_text_light"/>
<Button
android:id="#+id/btn_remote_button"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_marginEnd="5dp"
android:background="#drawable/btn_remote_button"
android:drawableStart="#drawable/logo"
app:layout_constraintHorizontal_bias="1.0" />
</LinearLayout>
You have to add android:gravity="right" in Layout. So all the inner widgets will be set from right to left manner. Like below.
<LinearLayout
android:id="#+id/remote_button_layout"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginTop="30dp"
android:gravity="right
android:background="#drawable/btn_remote_button"
android:orientation="horizontal">
Use layoutGravity tag in your xml as (right, left)
You can set android:layout_weight=1 parametr to TextView
Use this android:layout_gravity as (left,right) you can refer this link What is the difference between gravity and layout_gravity in Android?
In your case you can just change add the android:gravity="right" attribute in your LinearLayout.
<LinearLayout
android:gravity="right"
Before:
After:
You can also consider to do something different using a MaterialButton adding the text and the icon.
<com.google.android.material.button.MaterialButton
style="#style/Widget.MaterialComponents.Button.Icon"
app:icon="#drawable/..."
app:iconGravity="end"
android:text="#string/..."/>
I got a RelativeLayout:
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:id="#+id/auto_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|left"
android:layout_marginBottom="5sp"
android:layout_marginTop="5sp"
android:layout_toLeftOf="#+id/ButtonBackspace"
android:completionHint="#string/a_string"
android:completionThreshold="1"
android:gravity="center"
android:hint="#string/a_string"
android:textAppearance="#android:style/TextAppearance.Large" />
<ImageButton
android:id="#+id/ButtonBackspace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/myButton"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#android:color/white"
android:soundEffectsEnabled="true"
android:src="#drawable/dialer_btn_bks" />
<Button
android:id="#+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="New"
android:visibility="gone" />
</RelativeLayout>
The visibility of myButton starts as Gone. I was hoping that when I change the visibility of the button in run time to Visible, it would push the ImageButton and AutoCompleteTextView to the left but both the button and imageButton appear on top of each other since they both got android:layout_alignParentRight="true". I tried adding the android:layout_toLeftOf="#+id/myButton" to the ImageButton but that did not work.
How can I make the button push the other elemets when it become visible and have its own space?
if these elements are just next to each other, you can use a linear layout and set the gravity to right. the linear layout automatically pushes the elements.
in relative layout all elements are "relative" to another element or the parent view, but just to one element for each direction. and if this object is "gone" the relation in this direction also is gone.
I am sorry I know it's simple question but I can't make this happen now.
I want one text always will be in center and another in right.
But when texts is too loo long they come to each other.
Let's say I want second text always be in the right and first text is in center but when one of these texts is too long the centered texts have to move left.
How could I do this?
I write this but it doesn't work properly.
<RelativeLayout
android:id="#+id/workout_footer_text"
android:layout_width="match_parent"
android:layout_height="#dimen/workout_header_height"
android:background="#color/green"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingLeft="#dimen/activity_horizontal_margin">
<TextView
android:id="#+id/review_comments_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/text_color_white"
android:textSize="#dimen/review_top_text_size"
android:singleLine="true"
android:gravity="center"
android:layout_toLeftOf="#+id/review_write_label"
android:layout_centerInParent="true"
android:layout_gravity="center"/>
<TextView
android:id="#+id/review_write_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/text_color_white"
android:layout_alignParentRight="true"
android:gravity="center_vertical"
android:singleLine="true"
android:layout_marginLeft="20dp"
android:textSize="#dimen/review_top_text_size"
android:layout_centerVertical="true"/>
</RelativeLayout>
Instead of layout_toLeftOf="#+id/review_write_label" in your first TextView, remove that line and add layout_toRightOf="#id/review_comments_label" to your second TextView to make sure the first view stays centered and the second view stays to the right.
Update
Then make the second view's width match parent and set it's gravity to right. This will right justify the text in a container that stretches from the right edge of the centered TextView to the right edge of the parent thanks to the match_parent width.
You can try this:
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:orientation="horizontal">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="text1" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="text2" />
</LinearLayout>
I am trying to get an editText to sit on the right edge of my layout, with a button to its left, as follows:
<RelativeLayout
android:id="#+id/FindClientLayout"
android:layout_width="#dimen/third_width"
android:layout_height="#dimen/half_height"
android:layout_alignParentLeft="false"
android:layout_alignParentRight="true"
android:layout_alignParentTop="false"
android:layout_below="#id/HeaderLayout"
android:layout_margin="#dimen/fragment_separation"
android:layout_toRightOf="#id/scrollViewRecommend"
android:background="#drawable/login_border" >
<TextView
android:id="#+id/textViewFindClient"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="#string/find_client"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/Black" />
<EditText
android:id="#+id/editTextClient"
android:layout_width="#dimen/input_text_width"
android:layout_height="wrap_content"
android:gravity="center_vertical|right"
android:inputType="text"/>
<Spinner
android:id="#+id/spinnerClientList"
android:layout_width="#dimen/input_text_width"
android:layout_height="wrap_content"
android:layout_below="#id/editTextClient"/>
<Button
android:id="#+id/buttonDoFind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_toLeftOf="#id/editTextClient"
android:text="#string/find" />
</RelativeLayout>
The button is invisible, and the result looks like this:
I assume the button is invisible because the editText is left-aligned and overlays it or pushes it out of the layout frame. Why is are the editText and spinner left aligned rather than right-aligned? Also, why is the editText not centered vertically?
Edit you xml file like this and check for result
<TextView
android:id="#+id/textViewFindClient"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="#string/find_client"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/Black" />
<EditText
android:id="#+id/editTextClient"
android:layout_width="#dimen/input_text_width"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:inputType="text"/>
<Spinner
android:id="#+id/spinnerClientList"
android:layout_width="#dimen/input_text_width"
android:layout_height="wrap_content"
android:layout_below="#id/editTextClient"/>
<Button
android:id="#+id/buttonDoFind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_toLeftOf="#id/editTextClient"
android:text="#string/find" />
this should solve your problem which is , you are using gravity to place the edittext on right but gravity is actually use to position the content of view not the view itself.
If you still face problem leave a comment
android:gravity="center_vertical|right"
indicates the content in the edittext will be right aligned and in center_vertical postion
If you want to right align you can use alignParentRight="true" or toRightOf="id_to_which_it_relate_to"
You can't align RelativeLayout child views with just gravity.
Try to use android:layout_centerVertical="true" and android:layout_alignParentRight="true" instead
align Button to left of RelatativeLayout and TextView to right. Keep the EditText in between TextView and Button.
Hy!!
I want to have a button at the button and centered on the view in a linearlayout.
non working example:
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" android:id="#+id/login_btnew" android:text="Make a new Account"></Button>
Solution?
Thanks
You may want to try the following RelativeLayout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:id="#+id/login_btnew"
android:text="Make a new Account"></Button>
</RelativeLayout>
I did that for example in the following fashion:
I have the linear layout you mentioned on top I have scroll view and at the bottom I have th button. Using android:layout_weight="1000" for the scroll view and android:layout_weight="1" for the button gives the desired result. Center the button using android:layout_gravity="center_horizontal"
Hope that helps.
You have to work with layout_weight and put in another linearlayout for the layouting.
You could do sth like (pseudo-code)
<linearlayout layout_height=fill_parent layout_with=fill_parent>
#the "top" fields
<linearlayout layout_height=fill_parent layout_with=fill_parent layout_weight=1>
<elem1/><elem2/><elem3/>
</linearlayout>
#the bottom field (button)
<button layout_height=wrap_content layout_with=wrap_content layout_gravity=center_horizontal gravity=center ></button>
</linearlayout>
Use this
<Button android:id="#+id/login_btnew"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Make a new Account"
android:layout_gravity="bottom"/>