ImageButton no background but turn blue when touched - java

I've added an ImageButton(an image of a round gray X,transparent background) .It gets dynamically added to a layout.
final ImageButton btnx = new ImageButton(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
50, 50);
layoutParams.setMargins(14, 14, 14, 14);
l5.addView(btnx, layoutParams);
btnx.setImageResource(R.drawable.buttonx);
The problem here is that under it,there's a gray button.I know how i can hide it(make it transparent or set my image as the background) but then it won't have that nice effect of turning light blue when clicked.
How can i make it so that it still has this effect but the gray button doesn't show on the background?
Or is there any other way to make a button out of my gray X with transparent background and make it turn light blue(as the usual buttons do) when touched and get back to normal color when not touched/clicked ?
Also : does anyone know the standard size of an EditText.I need to align those X's to some EditTexts that are on the same row but on different layouts and i can't do that without knowing the exact size of the editTexts..if the size of the X is smaller or bigger it will start to be unaligned when i add like 100 rows dynamically.
Thanks !

You should use selector for button states:
final ImageButton btnx = new ImageButton(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
50, 50);
layoutParams.setMargins(14, 14, 14, 14);
l5.addView(btnx, layoutParams);
btnx.setImageResource(R.drawable.buttonx);
Also, add buttonx.xml into drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/buttonx_pressed"/>
<item android:drawable="#drawable/buttonx_normal"/>
</selector>
And put your images with normal and pressed states ("buttonx_pressed" and "buttonx_normal") into drawable folder too.

Image button is a combination of two views, ImageView and Button by default.
When you touch an ImageButton, ImageView remains static but the default Button view changes its state. So either set background of the ImageButton as null and set and image resource as the "src" of the imageButton or use background selector.
Also if you want that Image button should not turn blue but should give pressed effect, for that you need have some images of the image button size
- one complete transparent for the normal state
- one transparent from center but tranlucent from borders for clicked effect.
Then set the actual image as the background and make a selector with these frame images and set it as the src of the ImageButton.
For the edit text, take a screen capture of your mobile screen and measure it.

Related

How can I keep or change the navigationBar color?

I have developed an android application, there is a picture in the foreground. When I click on the picture, I make the background change (white if it is black, if it is black it will be white) at the same time I want to change the statusBarIcon color. and for that too
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
I use. In this way, the color of the statusbar icons is gray exactly as I want, and then I want to change the color of the icons to white.
getWindow().getDecorView().setSystemUiVisibility(0);
I am executing this command. everything is normal until here.. but when I do this, the color of the navigationBar icons also changes. How can I keep it or I want to change the color of the icons on the navigation bar
You can do it inside styles.xml using
<item name="android:navigationBarColor">#color/theme_color</item>
If you want to do it in code use
window.setNavigationBarColor(#ColorInt int color)

How can I make a dashed shaped Tab selection using buttons in Android?

I am using the button as the side tab menus for item like 'Home','Games',etc. I can't figure out how to get such rectangle shape selection when button is pressed.
Can I use drawable res for the same. Please help.
You should use a custom drawable with left margin rectangle drawable 1dp to 5dp width. I think you already have the logic for toggling the current tab's button to only have this drawable.

How can I expand a TextView programmatically in Android?

I have a TextView for description and I want it to display 4 lines by default, show the ellipsis and have a little expansion button. When the expansion button is pressed, the TextView should be expanded to display its entire contents, and the other elements on the layout should move down.
To show the ellipsis I am using desc.setEllipsize(TextUtils.TruncateAt.END); and I am setting the maxLines programmattically in the onClick event of the expansion/collapse button.
This works to expand the amount of text visible in the TextView, but it does not enlarge the TextView itself and push the rest of the layout down so you can see everything on the screen. How can I achieve this?
Simply using desc.setMaxLines(4); does not work to expand the TextView itself/increase its height. I have also tried using an ObjectAnimator to set the maxLines for the TextView but that has the same effect.
Thanks in advance for the help!
EDIT:
Code in onClick for animating the expansion:
ObjectAnimator animation = ObjectAnimator.ofInt(
desc,
"maxLines",
25);
animation.setDuration(400);
animation.start();
desc.setEllipsize(null);

How to give a JButton both an ImageIcon and background color?

Is there a way to do both
button.setBackground(Color.green.darker());
and
button.setIcon(new ImageIcon(buttonImg.getScaledInstance(15, 15, Image.SCALE_SMOOTH)));
so that both the image and the background color are visible? Is there some sort of transparency setting on the button's image that I'm missing? Any help would be greatly appreciated.
EDIT:
Here is the image I'm trying to use. As you can see, it has transparency.
http://www.clipartlord.com/wp-content/uploads/2013/09/bomb3.png
What I am trying to get is the image to show up on the Button with the background color of green filling in the transparency. Right now when I try to do it, if I add the image at all, the background color is not visible whatsoever. But if I don't add the image, the background color is visible. I hope this helps.
Probably the LookAndFeel does not support what you want to achieve. Here is an image from the meta theme, on the left, and osx aqua theme.
If you want all of your JButton backgrounds to change you can try using a LookAndFeel that supports changing the UI. Unfortunately the aqua them does not let you change the default background for a button even using the UIManager#set
If you only want to do one button, you can change the button UI.
button.setUI(new BasicButtonUI());
That one doesn't look too nice, you can set the UI to one from another LookAndFeel,
button.setUI(new MotifButtonUI());
(Also doesn't look very nice, but now it buttons.)
Finally you can also create your own ButtonUI, or do some custom painting.

libGDX, Scene2d Button status

Hello I have some questions for adding images for a ImageButton.
I have an icon for example a play button now if I hover over the button it just should get a blue surounding box the inside Image should not change.
Should I draw for every icon the two states or are there other options.
If an button is not clickable it should get Grey. Same how to do this? Add an additional Image to every Icon?
For what is the Checked drawable in the ImageButton?
ImageButtons have a drawable for their background (up, down, over, etc.) and a drawable for the image on top of the button (imageUp, imageDown, imageOver, etc.). The only ones you must provide are up for the background and imageUp for the icon. These are the defaults used when you leave the other states null. But for visual reasons, you need down for the background or it will be hard to tell if the button is being pressed. It's up to you whether you think you need to also change the icon appearance with an imageDown.
All you need is to add the background drawable for disabled. If you want the icon to also turn gray, you need to create a gray version of the icon and assign it to imageDisabled.
The Buttons in Scene2d all support toggle functionality. When you press it, it will toggle to the checked state, so you can use the checked drawable to change its appearance while it is stuck down. If you don't want that to happen, leave the checked drawable null.

Categories