android text not showing in button - android

This happens very frequently to me when starting to work on android. The text within the button element is not showing fully. What can be wrong? I tried text direction, alignment, gravity but nothing works.
The code behind it:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="#string/latestButtonString"
android:background="#drawable/roundbutton" />
Please help and much appreciated.
Update: This is the roundbutton.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<!-- you can use any color you want I used here gray color-->
<solid android:color="#ABABAB"/>
<corners
android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"/>
</shape>

I had this problem, I solved it by chaning the xml for the button from using tools: to android: for the text attribute. Example below (last line is the line thats changed):
<Button
android:id="#+id/btn_settings"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#F6F6F6"
tools:text="App Settings" />
becomes...
<Button
android:id="#+id/btn_settings"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#F6F6F6"
android:text="App Settings" />

try to replace
android:text="#string/latestButtonString"
and put direct hardcode string here
as,
android:text="hello"
or
try to replace this line in roundbutton.xml:
<android:shape="rectangle" android:padding="10dp">
with
<android:shape="rectangle" android:padding="3dp">

Can you please check by removing android:background="#drawable/roundbutton" from your button attribute. If text shown fully, then you need to modify the #drawable/roundbutton" there may be you have set fixed width thus your text in button is not fully visible.

It seems that you have a paddingLeft on your Button. Can you check if this is present in your styles.xml?
If it is in your styles.xml, it is probably important for the theme of your app.
If you would just want to cancel it in that certain Button, just put
android:paddingLeft="0dp"

Faced a similar problem...following attribute made it work:
android:layout_weight="1"

please remove these line android:background="#drawable/roundbutton" but view not create like you if you want to create same view please try these code :
code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<LinearLayout
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="40dp"
android:padding="20dp"
android:background="#drawable/roundbutton">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="latestButtonString"
android:textColor="#000000"
android:textSize="18sp"/>
</LinearLayout>

Related

Inline alignment title in circular custom button

I created a simple circular button to create - and + buttons for a custom dialog picker.
As seen in the screenshot the - and the + are shifted a little bit down and not centered in the middle of the custom background.
My custom background in the drawable/circular_button.xml looks like this.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke android:color="#color/colorPrimaryDark" android:width="1dp" />
<solid android:color="#color/colorPrimary" />
<size android:width="30sp" android:height="30sp"/>
</shape>
The button is configured like this inside the LinearLayout
<Button
android:id="#+id/decrease_one"
android:layout_width="40sp"
android:layout_height="40sp"
android:layout_marginRight="20sp"
android:background="#drawable/rounded_button"
android:text="-"
android:textColor="#color/white"
android:textSize="30sp"
android:textStyle="bold" />
In one of the posts I read about the tag minHeight but it didn't solve my problem.Any ideas ?
Batter to use FloatingActionButton mini
<android.support.design.widget.FloatingActionButton
android:id="#+id/activity_my_digital_executor_fabDigitalex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:fabSize="mini"
card_view:srcCompat="#drawable/ic_new_plus" />
Add 'com.android.support:design:26.0.+' in your App dependencies
This is happen because of your Button's android:textSize="30sp" if you set 50sp its goes down the line more,
Just convert your Buttons to Textviews and set android:gravity="center" also keep your textsize 30sp
<TextView
android:id="#+id/decrease_2"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginRight="20dp"
android:background="#drawable/rounded_button"
android:text="+"
android:gravity="center"
android:textColor="#color/white"
android:textSize="30sp"
android:textStyle="bold" />
If you are use a Imageview instead of Textview than your problem is solved.

Android Button with two images and Text

I am trying to build the android app and need to build a button with two images and some text .
here is the example image :
how can i build this button in android ? any help would be appreciated .
Here is some code to get you started. Lets begin with the background. If that background is not provided to you and you have to create it yourself, lets create a custom_button_bg.xml file and add to your drawable folder.
custom_button_bg.xml Note: that I am just doing the basic shape for you. Adjust shadow and corner radius as you wish
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:endColor="#color/teal"
android:startColor="#color/teal"/>
<corners android:radius="4dp"/>
<stroke
android:width="1dp"
android:color="#color/shadow"/>
</shape>
Now simply create your layout, something like below. Just replace the src and labels.
<RelativeLayout
android:layout_width="160dp"
android:layout_height="80dp"
android:gravity="center_vertical"
android:background="#drawable/custom_rounded_white_button">
<ImageView
android:id="#+id/iv_video_cam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/video_cam"/>
<TextView
android:id="#+id/tv_desc"
android:layout_toRightOf="#+id/iv_video_cam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="See a Medical \nDoctor Now"/>
<ImageView
android:layout_toRightOf="#+id/tv_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/arrow"/>
</RelativeLayout>
Cheers!
You can use something like this:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:drawableLeft="#drawable/video_camera"
android:drawableRight="#drawable/right_arrow"
android:text="See a Medical\nDoctor Now"/>
Two Options:
You can utilize setBackgroundDrawable() on your Button to set the background of the button to an image with the icons you want, then your text will appear above the background.
Or you could try and utilize android:drawableLeft="#drawable/yourimage" and/or android:drawableRight="#drawable/yourimage" in your XML file nested in your Button.
HERE you can find more information on drawableRight/Left
I face to same problem in my case below code worked ;)
android:layout_width="370dp"
android:layout_height="62dp"
android:layout_marginTop="100dp"
android:drawableLeft="#drawable/flag_us_little"
android:drawableRight="#drawable/ic_arrow"
android:text="English language"
android:textAllCaps="true"
android:textColor="#color/black"/>
Cheers!
Three steps:
Took a screenshot of the image you provided.
Places the screenshot in the drawable project of my folder and named it "background"
in my layout xml file. set the image as the button background:
<Button
android:layout_width="165dp"
android:layout_height="35dp"
android:background="#drawable/background"
android:textColor="#FFFFFF"
android:id="#+id/button"
android:paddingTop="2sp"
android:drawablePadding="-1sp"
></Button>
Final result:

Making Custom RadioButton with image on center

I'm trying to my custom RadioButton that I trying to make to look like this:
So I did custom drawables that respond to the RadioButton status and used it as background. This is alright.
The problem is that I'm not been able to center the images that I set through the android:button atribute.
Here is how I'm trying to use it in my layout.
<RadioGroup
android:id="#+id/presenter_options"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="4dp"
android:gravity="center"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/cluster_centered"
android:gravity="center"
android:padding="8dp"
android:background="#drawable/presenter_radiobutton_left"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="8dp"
android:background="#drawable/presenter_radiobutton_right"
android:button="#drawable/fire"
/>
</RadioGroup>
With this I'm getting this as result:
I've already tried to define a drawable that sets the gravity to center, but nothing changed. Here is the custom drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap android:src="#drawable/cluster" android:gravity="center"/>
</item>
</selector>
So, how could I center the button images?
PS: I can't set each kind of button as background because it'll be dynamic in the future, so the only thing that could be in background is the blue shape.
My solution was to set android:button=#null, then set the image that I want into the android:drawableLeft attribute. So my RadioButton code is like this:
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="8dp"
android:background="#drawable/presenter_radiobutton_left"
android:button="#null"
android:drawableLeft="#drawable/fire"
/>
"on center" solution is
...
<RadioButton>
...
android:button="#null"
android:foreground="#drawable/your_selector_for_center_drawable"
android:background="#drawable/your_selector_for_background_drawable"
android:foregroundGravity="center"

Place ImageView over Button android

I am trying to place an ImageView over a Button using RelativeLayout. Here is my xml:
<RelativeLayout
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="0.50" >
<Button
android:id="#+id/btnFindDaysInBetween"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/blue_500"
android:text="#string/dt_text_days" />
<ImageView
android:id="#+id/imageview_find_days_in_between"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription="#string/empty"
android:src="#drawable/ic_check_circle_white" />
</RelativeLayout>
Here is the image screenshot:
As you can see, the ImageView's src image is not visible. However if i change the button at the back to an ImageView, the image of the top ImageView is visible. Please refer below..
Changed xml:
<RelativeLayout
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="0.50" >
<!--
<Button
android:id="#+id/btnFindDaysInBetween"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/blue_500"
android:text="#string/dt_text_days" />
-->
<ImageView
android:id="#+id/imageview_find_days"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:contentDescription="#string/empty"
android:src="#drawable/ic_send_black" />
<ImageView
android:id="#+id/imageview_find_days_in_between"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription="#string/empty"
android:src="#drawable/ic_check_circle_white" />
</RelativeLayout>
Changed xml's screenshot:
What is it that i am doing wrong in the first layout?
The reason is actually very simple. :) We are so caught up thinking in 2D that we overlook the elevation - in Z.
There is nothing wrong with your first layout. The Button simply has a higher elevation than the ImageView - exactly 1dp higher. Therefore, no matter how you arrange the two views, the Button rises above.
A bit of proof:
A Button, by default gets the Widget.Material.Button style:
<!-- Bordered ink button -->
<style name="Widget.Material.Button">
<item name="background">#drawable/btn_default_material</item>
<item name="textAppearance">?attr/textAppearanceButton</item>
<item name="minHeight">48dip</item>
<item name="minWidth">88dip</item>
<item name="stateListAnimator">#anim/button_state_list_anim_material</item>
<item name="focusable">true</item>
<item name="clickable">true</item>
<item name="gravity">center_vertical|center_horizontal</item>
</style>
The attribute that introduces this elevation is android:stateListAnimator. StateListAnimator is similar to StateListDrawable, and provides state change animations. The complete xml is here: Link. But here's the base state of the button:
<!-- base state -->
<item android:state_enabled="true">
<set>
<objectAnimator android:propertyName="translationZ"
android:duration="#integer/button_pressed_animation_duration"
android:valueTo="0"
android:startDelay="#integer/button_pressed_animation_delay"
android:valueType="floatType"/>
<objectAnimator android:propertyName="elevation"
android:duration="0"
android:valueTo="#dimen/button_elevation_material"
android:valueType="floatType" />
</set>
</item>
As you can see, the elevation value for the button is set to #dimen/button_elevation_material:
<dimen name="button_elevation_material">1dp</dimen>
And that's how the ImageView ends up being behind/below the Button.
So, what can we do?
A straight-forward solution would be to set the ImageView's elevation to the same amount - 1dp.
Another solution, which will require a bit of work, is to remove the Button's elevation rather than change ImageView's. Based on the default StateListAnimator, we can create our own - and remove the elevation. Then, in your res/values-v21/styles.xml, define a style that inherits from Widget.Material.Button:
<style name="MyDepressedButtonStyle" parent="android:Widget.Material.Button">
<item name="android:stateListAnimator">#anim/customized_state_animator</item>
</style>
Now, set this style on your Button:
<Button
style="#style/MyDepressedButtonStyle"
....
.... />
Edit:
Actually, we can apply the customized StateListAnimator directly:
<Button
android:stateListAnimator="#anim/customized_state_animator"
....
.... />
No need to take the scenic route!
I found a solution:
simply android:elevation="2dp"
<Button
android:id="#+id/btnAccess"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_gravity="right|center"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView4"
android:background="#drawable/or"
android:layout_alignBottom="#+id/btnRegister"
android:layout_centerHorizontal="true"
android:layout_margin="5dp"
android:elevation="2dp" />
Actually it's much easier to just set the StateListAnimator to #null
<Button
...
android:stateListAnimator="#null" />
Source Android 5.0 android:elevation Works for View, but not Button?
Use ImageButton replace Button and set ImageButton background as transparent.
<ImageButton
android:id="#+id/btnFindDaysInBetween"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/blue_500_text"
android:background="#android:color/transparent"
/>
<ImageView
android:id="#+id/imageview_find_days_in_between"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription="#string/empty"
android:src="#drawable/ic_check_circle_white" />
You can make a image that has your blue_500 color and text(that is easy to create), then set this image to your ImageButton. After that, your ImageView will see on the top of ImageButton.
Hope this help!
Button is just a TextView with a certain style applied to it, so if you replace Button with TextView you can display an ImageView on top of it. This also works on API < 21.
android:background exists for all the view. As the name suggests this is what is going to be there in the background.
android:src exists for ImageViews and its subclasses. You can think of this as the foreground. Because ImageView is a subclass of View you even have android:background for that.
If the foreground is smaller than background, the background portion which is not covered by the foreground would be visible.
Also, you can use transparency in the foreground in which case the background would be visible(transparently).
You can use BACKGROUND FOR ALL THE VIEWS.. But You can use SRC only for ImageView & ImageButton.....
#Vamsi I tried your both combinations and first one is not working with Button. You have to go through ImageView. This is what I tried with with ImageView:
While I tried to do it with Button and see what was result:
I tried to change the order but all in vain! It seems you have to go with either ImageView or ImageButton.
At the end! You can see what I had tried:
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent">
<!-- <ImageView
android:id="#+id/btnTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_launcher_web"
android:contentDescription="#string/app_name"
android:text="#string/app_name" /> -->
<ImageView
android:layout_width="wrap_content"
android:id="#+id/imgView"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription="#string/app_name"
android:src="#drawable/ic_launcher" />
<Button
android:id="#+id/btnTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imgView"
android:layout_alignTop="#+id/imgView"
android:text="#string/app_name" />
</RelativeLayout>
I had done same kind of work either with ImageView or ImageButton (probably with ImageView) and tried same approach as you were trying with Button.
Thanks
If you want to get an ImageView on top of a Button, and you are developing for Android API < 21 (for instance, KitKat = 19), the easiest way is to not use a Button at all and use 2 ImageView instead. Why would you want to do that? May be because you defined a drawable shape to make the button look "cooler", so you are already using android:background with that shape.
Ex:
<Button
android:id="#+id/button01"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/buttonshape"
/>
<ImageView
android:id="#+id/image01"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginTop="10dp"
android:layout_marginStart="10dp"
android:src="#drawable/desiredImageOnTop"
/>
Where #drawable/buttonshape.xml is:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="30dp"
/>
<gradient
android:angle="45"
android:centerColor="#47A891"
android:centerX="35%"
android:endColor="#000000"
android:startColor="#E8E8E8"
android:type="linear"
/>
<padding
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp"
/>
<size
android:width="100dp"
android:height="100dp"
/>
<stroke
android:width="3dp"
android:color="#878787"
/>
</shape>
In that case, you should replace the Button with an ImageView, change android:background to android:src, and then, in the java code, you just add an OnClickListener as if it was a Button (both controls derive from View, and OnClickListener is a View event). Works like a charm!
Actually, This is not an error, it is a simple design issues. I have been working on it for last 2 hours. And at last get an easy way.
Now, I want to share the code.
<FrameLayout
android:layout_alignParentStart="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<androidx.appcompat.widget.AppCompatButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="4dp"
android:background="#drawable/custom_button"
android:paddingLeft="33dp"
android:layout_marginStart="33dp"
android:text="CHECK APPOINTMENT"
android:textAlignment="viewEnd"
android:textSize="18sp"
/>
</LinearLayout>
<androidx.cardview.widget.CardView
android:layout_width="60dp"
android:layout_height="wrap_content"
app:cardCornerRadius="40dp">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:elevation="2dp"
android:src="#drawable/care_logo"
android:scaleType="fitCenter"
/>
</androidx.cardview.widget.CardView>
</FrameLayout>
It is working.
I placed ImageView on a Button with a diversion of RelativeLayout, hope this helps.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<View
android:id="#+id/tempview"
android:layout_width="0dp"
android:layout_height="0dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tempview"
android:background="#drawable/ic_launcher" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tempview"
android:src="#drawable/img_cancel" />
</RelativeLayout>

using xml dotted line in android TextView

I have created an xml dotted line as explained in How do I make a dotted/dashed line in Android?. If I use it as the background of my TextView it shows up.
<TextView
android:id="#+id/segment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/dotted_lines"
android:gravity="left"
android:text="First segment"
android:textSize="12sp" />
But if I use it as an accompanying drawable, it does not show up.
<TextView
android:id="#+id/segment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawableBottom="#drawable/dotted_lines"
android:gravity="left"
android:text="First segment"
android:textSize="12sp" />
Essentially, I couldn't care less either way, except: I need the dotted lines to appear below the text in the TextView. Please help.
Use following code to make textview with dotted line.
Create file in drawable folder named dotted.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:color="#F59C51"
android:width="2dp"
android:dashGap="1dp"
android:dashWidth="2dp"/>
</shape>
Then set in as background of textview like below.
<TextView
android:id="#+id/segment"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/dotted"
android:gravity="left"
android:text="First segment"
android:textSize="12sp" />
Try this. i think textview height might cause problem for you.
<TextView
android:id="#+id/segment"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:drawableBottom="#drawable/dotted_lines"
android:gravity="left"
android:text="First segment"
android:textSize="12sp" />

Categories

Resources