Android: button with a part of it having a background - android

In android is there a mechanism by which a part of the button can have a background picture.
What I need is something like shown in the picture below (A white button which match the parent's width with a picture taking a part of the button)

In your Button XML
android:drawableLeft="#drawable/yourimage"

You can set background to whole button but if you want to show icon like above, then there is drawableLeft,drawableTo,drawableBottom,drawableRight properties in xml.
<Button
...
...
android:drawableLeft="#drawable/your_drawable" />

you need to use :
XML :
<Button android:id="#+id/btn_confirm"
android:text="#string/confirm_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dip"
android:drawableLeft="#drawable/left_icon"
android:drawablePadding="2dip"
android:gravity="center" />
Java Code :
btnRegisterMa.setCompoundDrawables(leftDrawable, topDrawable, rightDrawable, bottomDrawable); // can be null
// Or
btnRegisterMa.setCompoundDrawablesWithIntrinsicBounds(R.drawable.left_icon, R.drawable.topt_icon, R.drawable.right_icon, R.drawable.botton_icon); // use zero to null
// Drawable Padding
btnRegisterMa.setCompoundDrawablePadding(drawable_padding_intm);

Related

Change Cuboid circle button color on runtime

Cuboid circle button is a custom circle button in android (source: Github Cuboid Circle Button).
I'm using that button in my app. It works well, but I cannot find a way to change its color programmatically on runtime.
In the class, before onCreate I have:
com.cuboid.cuboidcirclebutton.CuboidButton btnReplayEnd;
Then in onCreate:
btnReplayEnd = (com.cuboid.cuboidcirclebutton.CuboidButton) findViewById(R.id.btnReplayEnd);
And in the 'setTheme' method (in the same class):
btnReplayEnd.setCircle_color(getResources().getColor(R.color‌.replayNormalDark));
there is no way to change the circler color of com.cuboid.cuboidcirclebutton.CuboidButton programatically you can only change it via xml using app:cub_color="#color/color_03A9F4"
like this
<com.cuboid.cuboidcirclebutton.CuboidButton
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_centerInParent="true"
android:text="No Border"
android:textColor="#000000"
android:textSize="18sp"
app:cub_border_radius="0dp"
app:cub_color="#color/color_03A9F4"
app:cub_fontstyle="fonts/Bellerose.ttf"
app:cub_hover_color="#00BFFF1"
/>

Android Button changing color and incrementing size

Hi guys im new in android stuff and i have a problem with a button when color change, because when it changes the button grows a bit and i dont know why. My code of the button click is below.
produzirbt.setBackgroundColor(Color.rgb(38, 198, 219));
Toast.makeText(getApplicationContext(), "Avaria iniciada.", Toast.LENGTH_SHORT).show();
Xml
<Button
android:layout_width="150dp"
android:layout_height="100dp"
android:text="Pausa"
android:id="#+id/Pausa"
android:textSize="50px"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_above="#+id/Avaria"
android:layout_toRightOf="#+id/terminaliniciado"
android:layout_toEndOf="#+id/terminaliniciado" />
In order to keep button size use setColorFilter() method.
In your case:
produzirbt.getBackground().setColorFilter(ContextCompat.getColor(this, R.color.your_color), PorterDuff.Mode.SRC_ATOP);
Your button by default has a backgrond definded by some xml file. It contains backround color, corner radiuses and padding. Then in java code you change this style to plain color and padding becomes zero. To prevent size changing you should define base background with a color. Like this:
<Button
android:layout_width="150dp"
android:layout_height="100dp"
android:text="Pausa"
android:id="#+id/Pausa"
android:textSize="32sp"
android:background="#color/my_button_color"
android:layout_above="#+id/Avaria"
android:layout_toRightOf="#+id/terminaliniciado"
android:layout_toEndOf="#+id/terminaliniciado" />
A the same time using color for button is a hacky method. It'll be better to define normal style as the documentation says.

Button with image: remove background color

I'd like to have an image in a button with text and I've already have it. But the problem is that my image is with some " grey background colour" (as you can see below) that I would like to remove.
In a normal situation I would use android:background="#null" and it would solve it, but I'm using as background a template to my button.
<Button
android:drawableLeft="#drawable/profile_32"
android:layout_marginRight="25dp"
android:layout_marginBottom="10dp"
android:textColor="#color/yellow3"
android:background="#drawable/buttons2"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="#string/button_profile"
android:textSize="18dp"
android:textStyle="bold"
android:id="#+id/buttonProfile"
/>
Does anyone know any other way to remove that grey background color from the image? Thanks.
Is the grey actually part of the image? If not you could try background:"#android:color/transparent"
If the grey background is part of the profile_32 image, there is not an easy way to remove it in Android. You will need to have the image re-exported or use an image editing program to remove the background.
Yes, use an ImageButton and just add
android:background="#00000000"
or
android:background="#null"
to your Button.
The first 2 "0" is alpha, "00" = trasparent

Split button text into two sections

I am trying to port my WP7 app to android.
Does anyone know how I can layout the text on a single button so that some text appears aligned left and other text appears aligned right? (See below). I need access to be able to dynamically change the percentage number on the right side using code but the text on the right is just static.
Anyone know the answer to this?
The image is here:
http://i.imgur.com/zW7YV.png
Yes you could make it two buttons.
Remove all padding and margin from between them.
Set the same background drawable.
And just ensure when the left is clicked it invokes the right's onPress method (so it looks as if they depress together).
Or wrap the buttons/imageviews/textviews in a layout and perform the onClick on that.
I would use a RelativeLayout for this.
<RelativeLayout
android:width="fill_parent"
android:height="wrap_content"
android:clickable="true"
android:background="#18a2e7"
android:padding="10dip">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Something" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_align_parentRight="true"
android:text="0%" />
</RelativeLayout>

What controls the default padding between views?

When I put a view on a layout, it automatically has some padding/margin on it.
See this picture:
The red rectangle show the space that the button takes. Note the empty space between the button and the red lines. That's the space in question.
What controls that space and how to I change it ?
It's messing up my lisview's button alignments!
The code for that layout:
<Button
android:text="#+id/Button01"
android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="#+id/Button02"
android:id="#+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
The button has a default background set by Android. If you look at Android source code, you can find that the button's background has a transparent area around it. This is done in order to make it into a nine-patch drawable. So you cannot remove that padding unless you specify your own background drawable for the button.

Categories

Resources