In my app, I have several buttons with little images with them. I've added them to the buttons with
android:drawableLeft="picture"
Now if my button is fill_parent, the picture is shown at the left, of course and my text is centered. Now my question is, is there any way to center these images or put them next to the text?
you can use this as it help me
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
// Call here getWidth() and getHeight()
applyTextOffset(yourButton, yourButton.getWidth(),R.drawable.recents); // where R.drawable.recents is your image which you want to set as background
}
This the method when you can set your image near to your text
public void applyTextOffset(Button button, int buttonWidth, int image) {
int textWidth = (int) button.getPaint().measureText(
button.getText().toString());
int padding = (buttonWidth / 2)
- ((textWidth / 2)
+ ((BitmapDrawable) this.getResources().getDrawable(image)).getBitmap().getWidth());
button.setPadding(padding, 0, 0, 0);
button.setCompoundDrawablePadding(-padding);
}
Maybe it's not the best way to solve your problem, but did you try to work with a layout instead of a button. For example, a LinearLayout with everything you want in the right order, and then just add a click listener to make it work like a button. Hope this helps.
Related
I'm making a custom View thats derived from an ImageViewand I control where the image has to be within this ImageView using padding.
I have set OnClickListener on my custom ImageView that resizes it:
image.setOnClickListener(new ImageView.OnClickListener(){
#Override
public void onClick(View v) {
image.resize_image();
}
});
and this is how this function is looking like
public void resize_image(){
ViewGroup.LayoutParams params = getLayoutParams();
params.height = new_heigth;
params.width = new_width;
setLayoutParams(params);
}
After this resizing is done I don't want my displayed image to change size (only the custom ImageView is changing size so I can draw an extra stuff around this image) within my custom ImageView so inside onDraw(Canvas) member function I set the new padding
class custom_ImageView extends ImageView{
//...
#Override
protected void onDraw(Canvas canvas) {
//...
setPadding(new_left_padding, new_top_padding, new_right_padding, new_bottom_padding);
//...
}
//...
}
Result is that width and heigth are changed like I wanted but my displayed image is neither in the right position or size.
Interesting this is that if I add an extra line of invalidate(); in the end of my resize_image() and I click on my custom_ImageView twice - on the 2nd click image draws itself in right size and position like I wanted.
Can anyone tell me why this is happening?
I kind of worked around this by not using ImageView at all but just drawing image where it needs to be by using Canvas.drawBitmap(Bitmap, RectF, RectF, Paint)
This way I don't have to specify images location using setPadding within onDraw, but I can do it simply by specifying position using RectF.
My app shows a widget with a total of 8 buttons. I would like to give the user the possibility to style that widget and customize the size of the ImageViews which are below the buttons.
The aim is to leave the Buttons as they are, but to change the ImageView sizes dynamically.
For this the User gets to set an icon Size, which is stored in a SharedPreference as an Integer iconSize.
How can I change the size of an ImageView in a Widget?
For now, the ImageViews are created with the size set in the xml file. How can I achieve a redraw with another size?
I assume there is not much code to post here but I will be glad to do so if necessary, just tell me if you have an idea what code could help here.
What I don't want to do:
resize the entire widget
create tons of layout files to switch according to the iconSize
Some Code:
This is how I set the ImageView size in my activity as a preview of the widget. icons is an array of ImageViews, progress refers to a ProgressBar that is used to choose iconSize.
for (ImageView icon : icons) {
icon.requestLayout();
// convert into actual Pixels
progress = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_PX,
progress,
getResources().getDisplayMetrics()
);
// set width and height
icon.getLayoutParams().height = progress;
icon.getLayoutParams().width = progress;
sizeText.setText("" + progress);
}
Here is a little workaround I found:
simply use
views.setViewPadding(R.id.vieId, left, top, right, bottom);
(views = RemoteViews)
You just have to make some calculation, so that an iconSize of 100% (of the biggest possible size) equals 0 padding and 1% iconSize equals max padding.
It worked without, too but I think it can'T harm to add the
android:cropToPadding="true"
attribute to ImageViews if you use this method.
Edit:
I forgot to mention that at some point after setting the padding you should update the widget (I do it in onPause() when the user quits the application to look at the widget).
Using the setPadding() in an activity will also lead to nowhere without calling invalidate() on the View, to force a redraw/update of it.
Here more code:
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// Map the values, 100 is the custom max of my seekBar
// I am adding 1 to because the seekBar goes from 0-99,
// but the size from 1% to 100%
int iconSize = 100 - (progress+1);
for (ImageView icon : icons) {
// set the padding
icon.setPadding(iconSize, iconSize, iconSize, iconSize);
// force the ImageView to redraw with the new padding, this
// serves as a live preview of the icons' sizes.
icon.invalidate();
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// you might want to do something here
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// map the value
int iconSize = 100 - (seekBar.getProgress()+1);
// save it in your SharedPreferences or somewhere else
Utils.setDefaultsInt(Con._ICONSIZE, iconSize, MainPage.this);
}
});
I have framelayout which contains two relative layouts, one is on top of the other. When user clicks a button, the one on the top move 80% off the screen to the right. Then one on the bottom becomes clickable. This is what it looks like.
FrameLayout
RelativeLayout (bottom) RelativeLayout (top)
FilterWidgets Open/close button, ListView
It's really easy to achieve on 3.0+ with the new animation api which is Property base Animation. For the pre 3.0, because animation is view based. So I end up manually modify the layout property on onAnimationEnd. The call requestLayout to make it permanent, but only to find out the layout reverts back to original position. Anybody know how to move layout permanently?
see my other post if you want to see the whole picture:
https://stackoverflow.com/questions/14541265/changecursor-cause-layout-container-of-the-listview-to-reposition
theTranslationX.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator nullPointer) {
v.clearAnimation();
int theL = isMenuOn() ? 0 : v.getLeft() + getFilterWidth();
int theR = isMenuOn() ? v.getWidth() : v.getLeft() + getFilterWidth() + v.getWidth();
int theHeight = v.getHeight();
int theT = 0;
v.layout(theL, theT, theR, theHeight);
v.requestLayout();
}
});
This is 9 months late but try using:
yourView.layout(left,top,right,bottom); //all parameters are type int
However I don't think this is permanent, the position of the view will still be reset when you call requestLayout(), but give it a try.
Greets ,
How can I get the Coordinates of the Bitmap not of the screen. Screen Coordinates got by event.getX(),event.getY() methods but not getting coordinates of the bitmap. Please help anyone.
You can't get the coordinates of the bitmap directly. You need to calculate to yourself.
Use the position of the ImageView, and with that you can handle it.
Of course, when you are after Activity onCreate you can acces to any inflated (active) views parameter.
Exemple.
ImageView a;
a.getPaddingBottom();
Like all coordinats (left right etc...)
After this you need the Height and Width of the ImageView.
Nah, when you know the views position, hegiht and width you can calculate.
Example:
final ImageView iv_YourImage = (ImageView) findViewById(R.id.iv_imageview);
public boolean onTouch(View v, MotionEvent event){
int topParam = iv_YourImage.getPaddingTop();
int rightParam = iv_YourImage.getPaddingRight();
int maxTopParam = topParam+iv_YourImage.getMaxHeight();
int maxRightParam = rightParam + iv_YourImage.getMaxWidth();
if(event.getX>topParam&&event.getX<maxTopParam){
//the x coordinate is in your image... do the same to Y
}
return true;
}
look at the following code if the answer is too short:
Android: Detect touch on actual image in ImageView
the code replaces the padding and max height coding of this answer and replaces it with the actual width and height of the displayed image using getWidth() and getHeight(). And uses the screen width and height to calculate its position. If you want more detailed answers for your problem you should give more information such as where did you draw the bitmap... did you define it at the activity XML or did you draw it at a random location such as at the X and Y coordinates of a touch event, the answer depends on that information.
package com.example.img;
imports....
public Image extends Activity {
ImageView imgYourImage;
// int imgWidth;
// etc...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
imgYourImage = (ImageView) findViewById(R.id.yourImage);
imgDimension();
}
private void imgDimension() {
imgWidth = imgYourImage.getWidth();
// at newer api's you can use getLeft() and getTop() of imageView
// if getLeft() works you have the leftX coordinate of image already.
// do same with height
// also get the screen Width and Height by display.getWidth() (depreciated)
// Or the newer code display.getSize()
leftX = (screenwidth - imgWidth) / 2;
rightx = leftx + imgWidth;
}
Notice that this code only works when the image is displayed at the horizontal center of the screen. Or use top and bottom if the image is displayed at the vertical center of the screen.
I have some irregular shaped buttons, created as ImageButtons. The "android:src" attribute of the ImageButtons are .PNG files with transparent backgrounds. And the parent layout of these ImageButtons, has a custom background image, which is defined with "android:background" attribute. So the background of the activity is not transparent or just black.
My question is; how can I detect if a click on a button is on the transparent area of the source image, or on the visible part of the source image?
I tried using onTouchListener to get the coordinates of the event and make a decision according to the color of the pixel; but since the background is colorful, I couldn't get to a point.
Any help is very much appreciated. Thanks in advance!
Hi bro i think this link may help you.
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/view/View.java#View.dispatchTouchEvent%28android.view.MotionEvent%29
You need to override this method in your custom button to return false if point is not in the desired area. I suggest you go about it like this:
public static class MyButton extends ImageButton {
#Override
public boolean dispatchTouchEvent(MotionEvent event) {
int iX = (int) event.getX();
int iY = (int) event.getY();
// TODO Or use a more sophisticated, pixel value-based condition
if (!(iX >= 0 & iY >= 0 & iX < TheBitmap.getWidth() & iY < TheBitmap.getHeight())) {
return false;
}
return super.dispatchTouchEvent(event)
}
}