In my app i need to reduce the buton size if i did that the text of the button cannot displayed on that button. so how to align the text inside the button
this is my button.xml code
<Button
android:id="#+id/intro_button"
android:layout_width="560dp"
android:layout_height="80dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:background="#drawable/button_shape"
android:gravity="center_horizontal"
android:padding="50dp"
android:text="#string/Introduction"
android:textSize="10dp"/>
this is String.xml code
<string name="Introduction"></string>
If you reduce the button size, you have reduce the padding too.
<Button
android:id="#+id/intro_button"
android:layout_width="560dp"
android:layout_height="80dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:padding="30dp"
android:text="Introduction"
android:textSize="10dp"/>
Use a TextView,
set a background with some padding:
<TextView
android:id="#+id/intro_button"
android:layout_width="560dp"
android:layout_height="80dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:background="#drawable/button_shape"
android:gravity="center_horizontal"
android:padding="50dp"
android:text="#string/Introduction"
android:textSize="10dp"/>
Then, in your java file, find your textview using the id and set an OnClickListener on it
Use this,
Depending on your Text and textSize Button will Create:
<Button
android:id="#+id/intro_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:background="#drawable/button_shape"
android:gravity="center_horizontal"
android:padding="50dp"
android:textColor="#color/colorPrimary"
android:text="stringIntroduction"
android:textSize="20dp"/>
In my case adding android:padding="50dp" also added space on the whole button , so i just wanted to add some extra space on the right/left so that text didn't overlap with the button layout:
android:paddingRight="5dp"
android:paddingLeft="5dp"
Related
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2"
android:background="#ffffff">
<TextView
android:id="#+id/sorttxt"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.995"
android:text="Sort"
android:textSize="17dp"
android:textColor="#000000"
android:background="#25a65b"
android:drawableLeft="#drawable/leftImagesort"
android:gravity="center"
/>
<View
android:layout_width="0dp"
android:layout_height="40dp"
android:background="#color/grey_5"
android:layout_weight="0.01" />
<TextView
android:id="#+id/filtertxt"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.995"
android:text="Sort"
android:textSize="17dp"
android:textColor="#000000"
android:background="#25a65b"
android:drawableLeft="#drawable/leftImagefilter"
android:gravity="center"
/>
I am using above code but wants to remove gap between image and text i have used android:drawableLeft to insert image in textview
right now i am getting output shown in image
Set attribute android:gravity="left" instead of android:gravity="center" in both TextView
If you are using drawable left for showing image and you want to remove gap between image and text then you need to use android:drawablePadding="-20dp". you need to give drawablePadding in minus.
You can use like :
android:drawableLeft="#mipmap/ic_launcher"
android:drawablePadding="-20dp"
set drawablePadding="0dp" and android:gravity="left|center"
Check This Image:
<TextView
android:id="#+id/cusumerName"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:paddingLeft="10dp"
android:background="#drawable/button"
android:hint="Consumer Name"
android:textColor="#660000"
android:textSize="15sp" >
</TextView>
Here I found Text hint on Top, I want it vertically, How can I do?
use this in xml of TextView:
android:hint="H\ne\nl\nl\no"
This is my code:
<TextView
android:id="#+id/error_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:gravity="center"
android:drawableRight="#drawable/error_image"
android:visibility="invisible"
android:textColor="#aa0000"
android:textStyle="bold"
android:textSize="16dp"/>
There is space between image and text and I want reduce this space, how can I do it?
As you had set android:layout_width="match_parent", TextView took all the width so the drawablePadding which you set will not work. So you need to set android:layout_width="wrap_content".
Try following code :
<TextView
android:id="#+id/error_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="0dp"
android:layout_marginBottom="2dp"
android:drawableRight="#drawable/ic_launcher"
android:gravity="center"
android:textColor="#aa0000"
android:textSize="16dp"
android:text="adsfasdfa"
android:textStyle="bold"
android:visibility="invisible" />
You should set the property android:drawablePadding="0dp"
Or in your java code, call textView.setCompoundDrawablePadding(0);
I added 3 buttons to a Linear Layout, and assign to them an onClick listener in my activity, Two of them shrink and wrap only the text included when running the code,
and the third one shrinks after clicking on it :
The buttons i'm using:
The code i'm using in XML :
<LinearLayout
android:id="#+id/bottomButtons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:id="#+id/LivingRoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ac_button_active"
android:text="Living Room"
android:textSize="20dp"
android:textColor="#00CCCC"
android:layout_marginTop="3dp"
android:layout_marginLeft="3dp"
android:layout_marginBottom="3dp"
android:layout_marginRight="3dp"
android:tag="pressed" />
<Button
android:id="#+id/NoAC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ac_button_inactive"
android:text="No AC"
android:textSize="20dp"
android:textColor="#00CCCC"
android:layout_marginTop="3dp"
android:layout_marginLeft="3dp"
android:layout_marginBottom="3dp"
android:layout_marginRight="3dp"
android:tag="transparent"/>
<Button
android:id="#+id/office"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ac_button_inactive"
android:text="Office"
android:textSize="20dp"
android:textColor="#00CCCC"
android:layout_marginTop="3dp"
android:layout_marginLeft="3dp"
android:layout_marginBottom="3dp"
android:layout_marginRight="3dp"
android:tag="transparent"/>
when ever you use 9-patch images as background, they will be shrinked/adjusted to fit exactly to the Button size. Button size depends on the Text size and it's width.
so use paddings for Button to increase it's size along with margins if you need.
<Button
.......
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
.......... />
i want to set text upside of image in button property.
here is my code for it but i get it text is behind of image which property i have to set ?
<RelativeLayout
android:id="#+id/footer"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:layout_gravity="bottom"
android:gravity="bottom"
android:orientation="horizontal"
android:background="#drawable/nav_bar">
<Button
android:id="#+id/btnHome"
android:layout_width="60dp"
android:layout_height="70dp"
android:onClick="onHomeClick"
android:text="Home"
android:gravity="bottom|center_horizontal"
android:drawableTop="#drawable/btn_home"
android:background="#null"
android:textColor="#android:color/white"
android:textSize="16sp"
android:paddingBottom="10dp"
android:includeFontPadding="true"
/>
use android:drawableBottom instead of android:drawableTop there.
try this...
<Button
android:id="#+id/btnHome"
android:layout_width="wrapcontent"
android:layout_height="70dp"
android:onClick="onHomeClick"
android:text="Home"
android:gravity="bottom|center_horizontal"
android:drawableBottom="#drawable/btn_home"
android:background="#null"
android:textColor="#android:color/white"
android:textSize="16sp"
android:paddingBottom="10dp"
android:includeFontPadding="true" />
There is problem with your 'layout width and height' that's why your text appears behind the image.
<Button android:id="#+id/btnHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onHomeClick"
android:text="Home"
android:gravity="top|center_horizontal"
android:drawableBottom="#drawable/app_icon"
android:textColor="#android:color/white"
android:textSize="16sp"
android:paddingTop="10dp"
></Button>
try this one.instead of specifying width and height better to use wrap_content property.bcoz in future if u want to change the image that will be automatically fix it.