I can't place textview under my Image in Android Studio? - android

<TextView
android:id="#+id/textView27"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
I use this code for TextView

Simplest way is to add DrawableTop:
<TextView
android:id="#+id/textView27"
android:drawableTop="#drawable/yourimage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
I also saw you have 0dp width and wrap_content, I am assuming you are using layout weights, and not constraint layout. So please do experiment with that.
You can also add android:drawablePadding="dp" to add padding in image to text.

Related

Customize AppCompatButton drawableLeft?

How can I place drawableLeft and button text of AppCompatButton like in the image below?
I have tried to apply paddingLeft, but the text is moving away from the drawable to the right.
<android.support.v7.widget.AppCompatButton
android:id="#+id/filterBy"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableLeft="#drawable/ic_btn_filter"
android:paddingLeft="20dp"
android:text="#string/filter_by"
android:textAllCaps="false" />
And so far the result is looking like
Layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/profileHeader"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp">
<android.support.v7.widget.AppCompatButton
android:id="#+id/findFriends"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableLeft="#drawable/ic_btn_search"
android:text="#string/find_friends"
android:textAllCaps="false" />
<android.support.v7.widget.AppCompatButton
android:id="#+id/filterBy"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableLeft="#drawable/ic_btn_filter"
android:text="#string/filter_by"
android:textAllCaps="false" />
</LinearLayout>
And the interpreted requirement for making things clear.
Using layout_weight property am adjusting the width of both buttons and what i have to do is, the text should be in center of the button
and the drawable should be aligned near left of the text.
Any suggestion please?
use
android:drawablePadding=""
probably you can't, if you want like this then you have to make it using complex layout like this see

How to text alignment to right when use weight for TextView

i want to set text alignment to right when my TextView is set to layout_weight
my application must be support old versions of android such as Android 2.2. this below layout does not set correctly alignment to right TextView:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:id="#+id/loading"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".42"/>
<TextView
android:id="#+id/response_text"
android:layout_width="0dp"
android:layout_height="43dp"
android:layout_weight="6"
android:textSize="18sp"
android:layout_gravity="right"
android:text="#string/please_wait_to_response_from_server"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".42"/>
</LinearLayout>
and i'm checking this ways: set layout_width to fill_parent, match_parent, wrap_contect
Using "weight" and "layout_gravity" won't work 'cause you are telling the layout 2 different things. If you want your text to be right aligned, you can use "gravity" instead of "layout_gravity".
android:gravity="right"
Hope it helps.

How to align two TextView in a LinearLayout in the same line

I have two TextView in a LinearLayout, I want to align them one to the left (or center) and one to right in the same line. How to do this? I try to use gravity but they ignore it.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="TextView" />
</LinearLayout>
The easiest way is to change your LinearLayout to a RelativeLayout.
You can use android:layout_alignParentRight="true" and android:layout_alignParentLeft="true". Or to center it use android:layout_centerInParent="true"
See here why gravity won't work
You are using gravity instead of layout_gravity which is what you would want. This post should help clarify the difference
The docs show you available properties.
android:gravity is used to set the gravity of content inside the view. However, in your case the width is wrap_content, hence the content has nowhere to go in the text views.
Use a RelativeLayout with layout_width as match_parent. Then use the android:layout_alignParentLeft="true" and android:layout_alignParentRight="true"with the textViews.
Use it with or without the android:gravity in the second textview and try .
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1" />
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right"
android:layout_weight="1" />
</LinearLayout>
If LinearLayout is Vertical, you can put only an object per line.
You can use RelativeLayout, or else put in a line a LinearLayout Horizontal, that contains textviews
ex.
<LinearLayout vertical>
<LinearLayout horizontal>
<textview 1></>
<textview 2></>
</LinearLayout>
</LinearLayout>
I fixed all my issues with GridLayout...is the best thing bcos u don't need to align anithing to nothing...just put what u want into the matrix (row,column)...and this will allow you to visualize all the field in exactly wrap content of your datas also in landscape is perfect!

Align textview in a block

I wat to create an app like this, I use textview to preview number
<TextView
android:id="#+id/generatenumber1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/generatenumber2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
How to align and color them like this, http://s24.postimg.org/sr53ezhtd/sdsd.png ?
I tried android:layout_gravity="center_vertical|right but not work.
Use android:gravity="center", and add padding to the left and right to create the whitespace between items.
Layout_gravity is used by layouts to define how they put the views inside it. Gravity is used for text/data in a view.
Try this,
<TextView
android:id="#+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FF0000"
android:padding="10dip"
android:text="7"
android:textColor="#fff" />

How to right-align text in an Android TextView or EditText?

I have an EditText in my application. I want to align the text in it to the right instead of the default left. I tried adding
android:layout_gravity="right"
but this doesn't seem to work. any other suggestions please?
You should use android:gravity="right". layout_gravity is for the view (EditText) alignment against the container.
You may use android:layout_alignParentRight="true" to align the EditText's right edge to its parent's right edge. Also, if you really want to use the layout_gravity or gravity attributes, check out this article that discusses the proper use of them.
You can jst set property for edit text in property window i.e. Gravity to right or by adding code of lin e in xml file of UI : android:gravity="right"
layout_gravity means that you are specifying the layouts attribute, not the element inside. In some conditions, such as layout_width and layout_height is wrap_content, layout specifications can give what you want about the element inisde your layout, since the boundries of the layout and the element (not elements) are the same..
use layout width and height as wrap content inside relative layout :
<RelativeLayout 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">
<ImageView
android:id="#+id/icon"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_alignParentStart="true"
android:paddingEnd="16dp"
android:paddingStart="8dp"
tools:src="#mipmap/ic_launcher"
android:layout_alignParentLeft="true"
android:paddingRight="16dp"
android:paddingLeft="8dp"
android:contentDescription="#string/todo" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/icon"
android:maxLines="1"
android:lines="1"
android:paddingTop="8dp"
tools:text="Google"
android:layout_toRightOf="#id/icon" />
<TextView
android:id="#+id/last_used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:paddingTop="8dp"
tools:text="Last used: yesterday at 18.54" />
</RelativeLayout>

Categories

Resources