My application has a big button with an oval shape as background, it is runtime created. Let's name this button A.
When I enable some function the visibility of A is set to GONE, and four smaller buttons are shown, with the same background of A.
When I hide the buttons and show A again, it has proper size, but the background drawable is still of the size of the smaller buttons previously drawn. How can i resize it so that it fills the size of the A button?
Update: My code
//BigButton.java
//...
GradientDrawable idle = new GradientDrawable();
idle.setColor(Color.rgb(red, green, blue));
idle.setShape(GradientDrawable.OVAL);
idle.setStroke(2, Color.rgb(red2, green2, blue2));
btn.setWidth(50*3f);
btn.setHeight(65*3f);
btn.setBackgroundDrawable(idle);
//...
//ChildButton.java
//...
GradientDrawable idle = BigButton.this.getIdleDrawable();
btn2.setWidth(50);
btn2.setHeight(65);
btn2.setBackgroundDrawable(idle);
//...
Update2: The following creates a new drawable so that there's no resource sharing between parent and child buttons.
GradientDrawable idle = (GradientDrawable) ButtonSetupPanel.this.caller.getIdle().getConstantState().newDrawable();
You're using the same reference for both buttons. Use two different references, but with the same settings. This way changes to the drawable for the small button won't be visible on the big and vice versa.
Related
I'm new to Android and need advice. I have a GridLayout with multiple ImageViews. Each ImageView has a drawable and a background color. On button click, I want to animate two things, depending on user's input: 1) move the entire view to a new position (this part is clear and doesn't cause problems), and 2) move only the image's drawable to a new cell, leaving the view with the background color at the original position. I'm completely stuck on this second task. How do I move drawables using animation? Thanks for any help.
move only the image's drawable to a new cell, leaving the view with the background color at the original position
I think you can't "move a drawable". But you can do the following:
introduce a View - let's call it movingDrawableView - showing just the drawable but with a transparent background, this View is hidden at first
Set the position of this movingDrawableView to overlap the View with the drawable and the colored background and make it visible
Set the drawable of the View with the colored background to null
start the animation for movingDrawableView
as soon as the animation is finished, fill the underlying View with the drawable and show a background as required
hide movingDrawableView
I'm trying to create an EditText with custom background, and a compound drawable to the left, but outside the background (currently it's appearing overlapped to the background); something like this.-
Is this possible?
If your background is the white box - then no, it's not possible to draw outside of the TextView's bounds.
Although you can use a compound background, set the orange colour with the white box inside of it as your TextView's background, then you'll be able to set the mailbox as the left drawable of your TextView.
I'm trying to make a circle of one color on a background of another.
background = new ShapeDrawable(new OvalShape());
background.getPaint().setColor(main.getResources().getColor(R.color.XXX));
view.SetBackground(background);
will work for the colored circle, and
view.setBackgroundColor(getResources().getColor(R.color.XXX));
will work for the background, but they're mutually exclusive. It just ends up with what I did last. Is there a way to make the circle on another overlapping view or something like that?
setBackgroundColor() is basically a short cut for changing the view's background to a colour drawable.
To do what you want you could try one of the 2 things described below:
Put a view in a FrameLayout, set the background colour in the FrameLayout, and put the shape in the view.
You could also try to use ImageView, which can have a background and another drawable with setImageDrawable() method.
I'm changing Background color of a button programmatically as below
Button button =(Button)findViewById(R.id.one);
button.setBackgroundColor(0xFFFF0000);
But after this the size of button getting increased.. Im using relative layout in UI.
Default button background drawable has some kind of margins. These margins are just transparent pixels on the sides of the image. When you set background color for a button these margins disappear because the whole rectangle of the button is filled with the color.
Earlier, I was able to dynamically create an android.widget.Button whose background color was visible through the transparent parts of the Button's background image. I have not been able to re-create this. I have tried:
Button button = (Button) findViewById(id.button1);
try {
button.setBackgroundColor(Color.RED);
Bitmap bm = BitmapFactory.decodeStream(getAssets().open("transparent.png"));
button.setBackgroundDrawable(new BitmapDrawable(bm));
button.invalidate();
} catch (IOException e) {
throw new RuntimeException(e);
}
Only the image is visible if I run the above. If I move the call to setBackgroundColor below the call to setBackgroundDrawable, I only see the red, not the image. How can I make it so I can see both the image and, through its transparent parts, the background?
First it is easier to use an ImageButton, which has two layers, one is the background and the other the image on top.
Set your background color with button.setBackgroundColor
then set the transparent image on top: button.setImageDrawable(getResources().getDrawable(R.drawable.transparent)); or set SRC preperty in XML
Use the button padding settings to adjust how much backround color should go around the image.