How to create a button with round edges programatically in Java? - java

Is it possible to create a button with round edges without using a shape XML in my drawable folder?
My app has a lot of buttons that will change colors based on what the user assigns and touches. This will mean I will have to create a lot of shapes in my xml for it's specific color (have about 20 colors).
Or is there a way I can easily change the background color of my shape's button? I have about 45 buttons on one page of in my app.

You can use imageView.setColorFilter(Color.RED) function to change color of xml drawable
here are some example
ImageView redCircle = (ImageView) findViewById(R.id.circle_red_imageview);
ImageView greenCircle = (ImageView) findViewById(R.id.circle_green_imageview);
ImageView blueCircle = (ImageView) findViewById(R.id.circle_blue_imageview);
// we can create the color values in different ways:
redCircle.getDrawable().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY );
greenCircle.getDrawable().setColorFilter(0xff00ff00, PorterDuff.Mode.MULTIPLY );
blueCircle.getDrawable().setColorFilter(getResources().getColor(R.color.blue), PorterDuff.Mode.MULTIPLY );

Related

How to define a clickable area in an image used in ImageView

Is there a way to define a clickable area in an image? Something equivalent or similar to map-area tags in HTML? Or is there a standard way of doing this sort of thing?
A possible method
1) get the width, and height of the image view using (in pixels)
2) get the coordinates of the image view (which is the top left corner of the image view)
3) create a touch listener, and record the coordinates of the users touch
4) if the user's touch coordinates fall on the image do whatever you want to
If you only want to do certain things based on where the user touches you will have to calculate the coordinates of those regions using the coordinates of the image view, and the image views width and height.
I think there might be no standard way for this, but you can achieve it by placing a transparent VIEW over your imageview. Set click listener to that view and make imageview clickable false.
When i have to do something similar, i use an include view.
As for the layout the include view will be accessing, i would use FrameLayout as root.
Its children would be the ImageView that i want to depict, along with a LinearLayout infront of it containing Buttons at the positions i want the user to choose from.
Then in code, i set listeners to these buttons.
Lastly, i would set the buttons so that there background is transparent and text is not available.
For example in a project i am working on, i have an ImageView with an Image of Ocean as background.
In front of the view i have a LinearLayout with smaller ImageViews with images of islands.
I place listener on the islands to see when they are clicked to open a new activity.

Having trouble with moving a drawable in Android

I'm having trouble with some code I've set up that allows me to move an imageView's drawable to another imageView simply by clicking on the two imageViews (one with source image and then destination imageView) in succession.
The goal is to simulate a chessboard.
My starting display is the following:
And after I click the leftmost pawn then click the square directly in front of it, the result is the following. You'll see that the target imageView (first column 3rd row) is losing its horizontal boundary definition and expanding across the entire tableRow:
Now, I'm also resetting tags along with the drawables in each square but I'd imagine that the issue stems from the way in which I'm getting and setting the drawables.
My code is as follows:
int selectedId = getResources().getIdentifier(selectedSquare, "id", getPackageName());
ImageView sourceSquare = (ImageView) findViewById(selectedId);
Drawable sourceDrawable = sourceSquare.getDrawable();
ImageView targetSquare = (ImageView) findViewById(R.id.s20);
targetSquare.setImageDrawable(sourceDrawable);
My guess is that the sourceDrawable variable I'm creating somehow grabs too much or too little information and when I move that drawable into the new imageView targetSquare this destroys my formatting.
You can give the new ImageView the source LayoutParams of the origin:
targetSquare.setLayoutParams(sourceSquare.getLayoutParams);

Progress bar sizing Android

I am looking for a way to make a progress bar fill effect on Android.
I have a FrameLayout called progressBar and inside this FrameLayout is a LinearLayout called filledBar. I don't know the literal widths of either progressBar or filledBar, but I would like filledBar to take up a certain amount of progressBar. How would I do this? (I am creating both bars programatically)
quite simple, do a XML layout like this:
FrameLayout
View // background set to be a custom drawable
TextView // gravity right
in code you inflate it and set the drawable:
private CustomDrawable drawable;
drawable = new CustomDrawable(context);
View v = layout.findViewById(...
v.setBackground(drawable);
then to update the percentage
drawable.setLevel(10); // for 10% for example
when you create your custom drawble inside the draw method it's a simple call to drawRect
{
canvas.drawRect(0, 0, getLevel()/100*canvas.getWidth(), canvas.getHeight, paint);
}
the paint you create with the color you want it to be.
That's a very rough idea, but I hope it's enough to guide you to the correct direction.

Android Expandable ListView Icon (Group Indicator)

I have been able to successfully swap in my own image for the expandable listview arrows. I have two issues at the moment.
One is that the icons i substitue in with the code below are each strechted to the height of the row and too wide as well. The second is, that this code is only changing the initial state of the Group Indicator. How do I change it from one image to another when a particular row is open?
Drawable plus = (Drawable) getResources().getDrawable(R.drawable.plus);
getExpandableListView().setGroupIndicator(plus);
Android's default behavior is to stretch the groupIndicator icon. It's a ninepatch, so if you don't want it to stretch the image, you have to make the stretchable area of your ninepatch the transparent regions on both sides of the icon.
To make it change appropriately when the ExpandableListView opens, you have to use a StateListDrawable. Add the appropriate Drawables for the following states:
int[][] states = new int[][] {
new int[] { android.R.attr.state_expanded },
new int[] { },
};
You do that like so:
StateListDrawable sld = new StateListDrawable();
sld.addState(states[0], YOUR_EXPANDED_DRAWABLE);
sld.addState(states[1], YOUR_UNEXPANDED_DRAWABLE);
And set that to the groupIndicator.
Hope that helps.
What you could do, is onCollapse and onExpand change the group indicator drawable. Source

Libgdx ImageButton padding?

so I have this problem with ImageButton class of libgdx, the problem is that in android the image doesnt expand to the full size of the button, and in desktop it does or at least it does more, so I have to ask if theres a way to force the image to get the size of the background(button)? so i can try to make an equal visualitation on both plataforms.
heres a screen shot, the back space button is the ImageButton...
edit: heres the code....
private void createButtons() {
ImageButtonStyle ibs = new ImageButtonStyle(buttonD,buttonD_p,buttonD,bsIcon,bsIcon,bsIcon);
buttonBS = new ImageButton(ibs); // This is the backspace button
....
}
private void addButtonsToTable() {
float pad = 1;
float BUTTON_SIZE = this.BUTTON_SIZE - pad *3;
table.top();
table.center();
table.add(buttonBS).width(BUTTON_SIZE).height(BUTTON_SIZE).pad(pad);
table.row();
...
}
If you want the image to be the background instead of just an icon, you should consider using plain Button or TextButton instead of ImageButton. ImageButton should be used only for buttons that draw an icon additionally to its background. An example of ImageButton usage could be the window closing button with the "X" (cross) image, or music toggle button with a loudspeaker icon.
When you need the image to fill the whole button area, set it as ButtonStyle#up - it will become button's background. ImageButton#imageUp is just an icon that will not be scaled in any way (by default), so that might be the reason why your application behaves differently on each platform.
(Although it still shouldn't, unless you use different assets.)
If you need an icon and still want to use ImageButton, consider that internally it is just a Button with an Image instance added to one of its cells (Button is a Table). You can use ImageButton#getImageCell() to access Cell in which the Image is stored and modify it - for example, force a specific width and height. You might also want to use ImageButton#getImage() to change scaling with Image#setScaling(Scaling).
Anyway, creating styles at runtime can be error-prone - the style constructor is huge and I'm honestly unable to guess which drawable draws what without checking out image button style sources. You should consider using Skin and define your styles with JSON files (you can find some free themes here).

Categories

Resources