I put two android.support.v7.widget.AppCompatButton in a linear layout. They show and work fine in Android versions 4.x and 5.x, but do not show up on Version 6 (The phone is S7 Edge).
<android.support.v7.widget.AppCompatButton
android:id="#+id/btnAsk"
android:textSize="16sp"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/btn_ask"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:textStyle="bold"
android:background="#drawable/round_shape_btn"
android:textColor="#color/white"/>
<android.support.v7.widget.AppCompatButton
android:id="#+id/btnBuy"
android:layout_width="0dip"
android:layout_weight="1"
android:textSize="16sp"
android:layout_height="wrap_content"
android:text="#string/btn_buy"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:textStyle="bold"
android:background="#drawable/round_shape_btn"
android:textColor="#color/white" />
</LinearLayout>
I googled it and checked SOF for a possible solution but could not find anything working.
Any help is appreciated!
You can simply use Button in your layout instead of specifying AppCompatButton as according to the docs for AppCompatButton:
[AppCompatButton] will automatically be used when you use Button in your layouts. You should only need to manually use this class when writing custom views.
I don`t know exactly how that happened but I changed the layout from
LinearLayout to RelativeLayout
and the buttons showed up. Weird!
I believe your problem is that the Orientation property from the LinearLayout was not set, the default is horizontal which causes the views not to show specially if there's a first view with MATCH_PARENT width.
Related
I am using the following code to put the google sign in button in my application. However the text in the button is off center. How can I make it centered?
<com.google.android.gms.common.SignInButton
android:id="#+id/sign_in_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:visibility="visible"
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp" />
By inspecting with the debugger and the decompiled class code, I've noticed:
the SignInButton class sets up the real button in setStyle. It uses inter-process binding APIs to create some class from some other library, and I couldn't identify that class nor locate its decompiled class code.
the inner button has side paddings set, which is larger on the side with G icon.
the G icon is displayed via a background drawable, and not the TextView drawableStart.
Thus, it seems like some manual padding is put on the real button, on the side with the G icon. Why they would do this, no idea. From the above, it's pretty safe to "fix" the padding after the button is inflated from your XML. I think this is also safe in the case of RTL, since the padding on the side with the icon is always larger, I think (or I sure hope so, somebody please let me know if this is broken in RTL):
loginBinding!!.googleSignInButton.getChildAt(0)?.let {
val smaller = Math.min(it.paddingLeft, it.paddingRight)
it.setPadding(smaller, it.paddingTop, smaller, it.paddingBottom)
}
That's Kotlin, of course.
Better create a button by yourself.
I always prefer this approach because i can develop any kind of google login button as i want.
<RelativeView
android:height="wrap_content"
android:width="match_parent"
android:marginLeft="20dp"
android:marginRight="20dp">
<ImageButton
android:parentRight="true"
android:id="google_icon"
android:background="#null"
android:padding="10dp"/>
<TextView
android:id="google_text"
android:height="wrap_content"
android:width="match_parent"
android:centerHorizontal="true"
android:text="Google Login"
android:padding="10dp"/>
</RelativeView>
Above is the structure of the code that you can use exactly to achieve what you want.
Just use your own custom view button instead of using their built-in button. BUT you need to follow the guideline. Even facebook button i custom to my need..
<RelativeLayout
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:layout_gravity="center"
android:background="#drawable/border_radius_white"
android:clickable="true"
android:id="#+id/google_signin" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/g_logo"
android:adjustViewBounds="true"
android:layout_gravity="center_vertical"
android:padding="12dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:typeface="normal"
android:textSize="#dimen/log_button_size"
android:text="#string/text_sign_in_with_google"
android:textStyle="bold"
android:textColor="#color/scGrey"/>
</RelativeLayout>
An easy way I got around this was to add spaces after my text.. so Instead of
android:text="Connect with Google"
I changed it to
android:text="Connect with Google_____________"
replace the underscores with spaces
however this was using the custom button "com.shobhitpuri.custombuttons.GoogleSignInButton"
Use gravity to fix the center of text:
<com.google.android.gms.common.SignInButton
......
android:gravity="center_vertical|center"
...... />
Good evening,
I am developing an Android app and I am currently doing the Login interface in XML.
I am trying to create buttons with icon and text, like in the picture below :
http://i.imgur.com/J5Cj1w4.png
And here is my actual result :
http://i.imgur.com/VPALdDD.png
With this code :
<Button
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/sign_up_facebook"
android:text="#string/signup_with_facebook"
android:textColor="#color/primaryTextLight"
android:drawableLeft="#drawable/ic_facebook_box_white_24dp"
android:layout_weight="0"
android:background="#drawable/button_shape_login"/>
<Button
style="?android:textAppearanceSmall"
android:layout_marginTop="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/sign_up_google"
android:text="#string/signup_with_google"
android:textColor="#color/primaryTextLight"
android:drawableLeft="#drawable/ic_google_plus_box_white_24dp"
android:layout_weight="0"
android:background="#drawable/button_shape_login"/>
I am stuck at this step.
How can I get the final needed result with XML code ?
Thank you!
You can give padding to drawable like this : android:drawablePadding="50dip"
Also this question answered in this post.
Use android:drawablePadding attribute
You only need to specify android:paddingLeft attribute.
Try specifying a value of 36dp for example
The better option is to actually make the button a Relative/Linear layout with the layout set inside, the drawablePadding will not work so well with different lengths of text and buttons.
Essentially, RelativeLayout which is your button with a nested ImageView and TextView and you'll have good control of the layout, with consistent paddings around images and text within the button.
I haven't tested the following, this is essentially what you need
<LinearLayout
android:id="#+id/facebookButton"
android:layout_width="250dp"
android:layout_height="80dp"
android:orientation="horizontal">
<ImageView
android:src="#drawable/your_drawable"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="50dp"/>
<TextView
android:text="Sign up with Facebook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"/>
</LinearLayout>
android:gravity="center_vertical"
//use in both textView
<Button
android:paddingLeft="50dp"
android:textAlignment="textStart"
android:drawableLeft="#drawable/"
/>
I know this is a question that is asked quite a lot, but none of the answers that have been provided so far have helped me resolve my problem. It's best described with pictures. I unfortunately don't have enough reputation to embed them here, but here's an imgur with three screen sizes demonstrating my issue: http://imgur.com/a/D5uAu
I'm looking for a way to have the "Retrumpets" ImageButton and TextView properly centered for all screen sizes. Right now, they're currently centered using layout_centerHorizontal on the retrumpetTextView, with the retrumpetButton attached to the left of the TextView with layout_toLeftOf, like so:
<ImageButton
android:layout_width="35dp"
android:layout_height="30dp"
android:id="#+id/retrumpetButton"
android:src="#drawable/retrumpet"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:background="#00FFFFFF"
android:layout_above="#+id/detailedTrumpetSeparationLine"
android:layout_toLeftOf="#+id/retrumpetCountTextView"/>
<TextView
tools:text="30 Retrumpets"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/retrumpetCountTextView"
android:textColor="#FFFFFF"
android:paddingBottom="6dp"
android:paddingLeft="2dp"
android:textSize="14sp"
android:layout_centerHorizontal="true"
android:layout_below="#+id/trumpetTextView"/>
This would work perfectly, if not for the fact that what I truly want to center is a combination of BOTH of these views, so that one view or the other isn't too far to the left or right.
My question is: what is the best way to go about doing this? Can I somehow provide an adjustment value to shift both of these views a bit to the right? layout_marginStart and layout_marginLeft don't seem to work when a view is centered.
Thank you very much!
EDIT: Thanks very much all! Combining the two into a horizontally centered LinearLayout did the job perfectly.
You can use a LinearLayout to contain them.
<LinearLayout
android:layout_centerHorizontal="true"
android:layout_below="#+id/trumpetTextView"
android:orientation = "horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="35dp"
android:layout_height="30dp"
android:id="#+id/retrumpetButton"
android:src="#drawable/retrumpet"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:background="#00FFFFFF"
android:layout_above="#+id/detailedTrumpetSeparationLine"
android:layout_toLeftOf="#+id/retrumpetCountTextView"/>
<TextView
tools:text="30 Retrumpets"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/retrumpetCountTextView"
android:textColor="#FFFFFF"
android:paddingBottom="6dp"
android:paddingLeft="2dp"
android:textSize="14sp"
/>
</LinearLayout>
Or just use drawableLeft
<TextView
tools:text="30 Retrumpets"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/retrumpetCountTextView"
android:textColor="#FFFFFF"
android:paddingBottom="6dp"
android:paddingLeft="2dp"
android:textSize="14sp"
android:drawableLeft="#drawable/retrumpet"
android:layout_centerHorizontal="true"
android:layout_below="#+id/trumpetTextView"/>
You should put all those components i.e the "Retrumpets" ImageButton and TextView all in 1 Layout. LinearLayout. Then you can put layour gravity to this LinearLayout to be centered.
You might want to consider putting the ImageButton and TextViews in a horizontal linear layout and use the weights to adjust them correctly. Try setting the layout_weight of each ImageButton to 0 and the TextViews each to 1.
I am an entry level software developer, and have found tons of great answers from this site, but I can't seem to find how to hide the 'box' of a checkbox in Android. I just want the check mark to show, when a user selects an option. Here are some of the most recent things I have tried.
chkSortOrder.setBackgroundResource(android.R.color.transparent);
chkSortOrder.setBackgroundResource(android.R.drawable.checkbox_off_background);
Both of these still show the box.
put the following attribute in your checkbox tag in XML.
android:button="#android:color/transparent"
It's quite late but in any case, if it helps anyone. If you wanna set custom background to RadioButton programmatically, then you can set it like this,
checkbox.setButtonDrawable(null);
checkbox.setBackgroundResource(R.drawable.your_custom_drawable);
It's 2020! and many things have changed. But if you are struggling with this like me and tried everything here (and other solutions) with no luck, then try this one too. What I've got with other solutions came to this (Emulator, API 16):
Weird behavior! Two drawables, right is my custom box and default one on the left. And then accidentally i added this:
app:buttonCompat="#null"
and the second one is gone finally!
Here is the complete definition:
<CheckBox
android:id="#+id/cb_fs_ExactSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#null"
android:gravity="center_vertical"
android:padding="5dp"
android:text="#string/fs_options_exact"
android:textColor="#color/textPrimary"
android:textSize="#dimen/textSmall"
app:buttonCompat="#null"
app:drawableRightCompat="#drawable/drw_checkbox" />
You need to set both button and buttonCompat to null.
I found this error when updating from androidx.appcompat:appcompat:1.0.0 to 1.1.0.
None of the other answers were useful to me as setting button attribute to #null or #android:color/transparent doesn't work on Android API Level 20 or lower.
What I did is to change my CheckBox to ToggleButton and set textOn and textOff attributes to an empty string
<ToggleButton
...
android:background="#drawable/custom_selector"
android:textOff=""
android:textOn="" />
This way I could update to androidx appCompat 1.1.0
You can also toggle between a CheckBox and a TextView. Just make one view visible and another one hidden. This also makes sense because the CheckBox contains paddings around the check mark.
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<CheckBox
android:id="#+id/checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkMark="#null"
android:checked="true"
android:gravity="center_vertical"
android:paddingStart="3dp"
android:paddingLeft="3dp"
android:text="My text"
android:textColor="#color/black"
android:textSize="12sp"
/>
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My text"
android:textColor="#color/black"
android:textSize="12sp"
/>
</FrameLayout>
I've noticed that layout_alignBaseline of TextVew control doesn't work with Spinner control.
I'm trying to place a text to the left of spinner, but it goes to the top of the parent control.
<Spinner
android:id="#+id/locations"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/userCode"
android:layout_alignLeft="#id/userCode"
android:layout_marginTop="15dip"
/>
<TextView
android:id="#+id/locationsLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Region"
android:layout_alignBaseline="#id/locations"
/>
Is it a bug or I do something wrong? The same technique works fine with EditText controls.
I am also facing the same problem but found the following workaround
<Spinner
android:id="#+id/locations"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/userCode"
android:layout_alignLeft="#id/userCode"
android:layout_marginTop="15dip"/>
<TextView
android:id="#+id/locationsLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Region"
android:layout_alignTop="#+id/locations"
android:layout_alignBottom="#+id/locations"
android:gravity="center_vertical"/>
Newer versions of Android support base line alignment for Spinners. The Spinner will create the first View of the list and use its baseline alignment.
If that doesn't work, make sure your spinner adapter creates a View that supports base line alignment. (getBaseline() != -1). For LinearLayouts, you can use the baselineAlignedChildIndex attribute to specify a child within the linear layout to use.
android:layout_toLeftOf="#id/locations" ?