Android - Image button size - android

My imagebutton code wont respond to "fill_parent" and "wrap_content".
Instead, it just shows the middle of the image (because it is a pretty big image).
I tried setting specific values for the image but it still didn't work! Can somebody help me?
My button.xml in the res/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/largishbutton" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/largerbutton2" /> <!-- focused -->
<item android:drawable="#drawable/largerbutton" /> <!-- default -->
</selector>
My main.xml in the res/layout folder:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white">
<ImageButton
android:src="#drawable/button"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
/>
</RelativeLayout>
I tried using a LinearLayout, but the app wouldnt run, and I tried replacing the imagebutton with an imageview, but then the button didn't work.

If your image is bigger than your button, you will need to use something like android:scaleType or setMaxHeight() to have it resize to fit.
Also, I recommend that you have all images in your <selector> be the same size, at least until you get things working the way you wish.

Related

Custom buttons doesn't display android studio

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ImageButton Selected"/>
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button"/>
This is in my activity_main.xml
and this
<item android:drawable="#drawable/round" android:state_pressed="true"/>
<item android:drawable="#drawable/round2" android:state_focused="true"/>
in my button.xml
the custom buttons are made with image button function
when I start the application It doesn't give error and shows only "imagebutton selected"
The problem can be with my image?being too large?
here are the images and they are png btw with 1000x1000..I know is big but being png I thought will be everything fine..now I have to find how to make the white background dissapear.
here are the images round and round2
I don't know what is a selector,I know that I've put them in the folder drawable and then I followed a tutorial to make custom buttons.
the images are vectors
here is how the emulator looks like emulator
Your button.xml should look like this.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- pressed -->
<item android:drawable="#drawable/round2" android:state_pressed="true"/>
<!-- focused -->
<item android:drawable="#drawable/round3" android:state_focused="true"/>
<!-- default -->
<item android:drawable="#drawable/round"/>
</selector>
Also, you should reduce the size of your PNGs, that would take up a lot of space, and a lot of memory.

Button with background image is not shown proportionately

Here is my button image file from my drawable folder called ooo.png:
And that's how it looks like on the device:
As you can see the width height proportion changed. It's a bit wider than the original.
I need it to be looking the same though.
I don't understand what causes this stretching. Here is my xml-file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
style="#style/styleBtns"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
My styleBtns-part of the styles.xml:
<style name="styleBtns" >
<item name="android:background">#drawable/btns_abc</item>
</style>
And in my btns_abc I set the background to my image resource. Currently state pressed and the normal state are the same, I want to change that as soon as it works properly:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/ooo" android:state_pressed="true"></item>
<item android:drawable="#drawable/ooo"></item>
</selector>
Thanks !
I don't know the exact reason but for a solution if u don't have any text for button ,use ImageView will show the same drawable u have added.

The Same Selector Does Not Work Android

I am using a selector working fine in all activities as you can see its XML below.
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="#drawable/active_date_horizontal_5_pressed" /> <!-- pressed -->
<item
android:state_focused="true"
android:drawable="#drawable/active_date_horizontal_5_pressed" /> <!-- focused -->
<item
android:drawable="#drawable/active_date_horizontal_5" /> <!-- default -->
Except one activity, it does not work ridiculously. You can see three screenshots below which the selector works fine in the first two screenshots, and does not work in the last screen shot.
1)
2)
3)
These are customized GridViews which filled with customized BaseAdapter. Cells in the GridViews have an Xml Layout which its background is the selector. Any ideas?
It seems I solved my own problem.
You can see the Xml Layout which I am applied the selector below
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:background="#drawable/calendar_cell_selector" >
<TextView
android:id="#+id/txtWeeklyEventName"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/imgCornerWeekly"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/txtWeeklyEventName"
android:layout_alignRight="#+id/txtWeeklyEventName"
android:src="#drawable/corner_orange"
android:visibility="visible" />
</RelativeLayout>
You can see the background of the RelativeLayout is the selector, but the TextView fills upon the whole layout so Android cannot handle the selector.
Thus, I removed the background from the RelativeLayout and set it for the TextView, it totally works.
Thanks to me!

How to make flash effect when clicking on complex LinearLayout just like clicking on ListView item?

I have some kind ListView. All items in it are in rectangle shape, but the top one has round corners as shown in this photo. To create it I've cut the top stripe with corners and saved it as item_bg_white_top image and a stripe with 1px height saved as item_bg_white_line image. And this is how I've constructed it.
I want to make flash effect when clicking on ltest layout just like clicking on ListView item.
How I can do this?
I've tried the code below on ltest but it it didn't helped. When I tried this code on ltest_inner it just changed its background to black.
final LinearLayout ll = (LinearLayout)findViewById(R.id.ltest);
ll.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ll.setBackgroundResource(android.R.drawable.list_selector_background);
}
});
item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/ltest"
android:layout_width="300dp"
android:layout_height="33dp"
android:orientation="vertical">
<LinearLayout
android:id="#+id/ltest_inner"
android:layout_width="300dp"
android:layout_height="8dp"
android:background="#drawable/item_bg_white_top" />
<LinearLayout
android:layout_width="300dp"
android:layout_height="25dp"
android:background="#drawable/item_bg_white_line_repeat" >
</LinearLayout>
</LinearLayout>
item_bg_white_line_repeat.xml
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/item_bg_white_line"
android:tileMode="repeat" />
What you want to do is set up a selector and use it as the drawable for the background.
Create an xml file in your drawable folder and add a selector to it:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:state_pressed="true" android:drawable="#drawable/disabled_pressed_image" />
<item android:state_enabled="false" android:state_focused="true" android:drawable="#drawable/enabled_focused_image" />
<item android:state_enabled="false" android:drawable="#drawable/enabled_image" />
<item android:state_focused="true" android:drawable="#drawable/focused_image" />
<item android:state_pressed="true" android:drawable="#drawable/pressed_image" />
<item android:drawable="#drawable/default_image" />
</selector>
The drawables above reference images also in your drawable folder. You don't have to implement all the states. Those are just some possible combinations.
Then attach this as the background to your Linear Layout:
<LinearLayout
android:id="#+id/ltest"
android:layout_width="300dp"
android:layout_height="33dp"
android:background="#drawable/selector_file_name"
android:orientation="vertical">
where selector file name is simply the name you gave to the selector xml file I mentioned above.
Also consider removing the inner LinearLayouts. You can do what you are trying to do with a 9-patch, then you'll only have the one LinearLayout (which you could just change to an ImageView if it is not going to host other Views).

Image pressed button changing size

I realize this has been asked multiple times, but my image is changing size when it is pressed (which is should be the same size, but a different image). I've previously tried setting the size of the button in java, but it didn't work either. Here is my code.
XML Selector
<?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/testbuttonpressed" /> <!-- pressed -->
<item android:drawable="#drawable/testbutton" /> <!-- default -->
</selector>
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/backtest1">
<Button
android:id="#+id/start_button"
android:layout_width="225px"
android:layout_height="55px"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:text="#string/start"
android:background="#drawable/buttonsxml"
/>
</LinearLayout>
It turns out my pictures were a pixel off...

Categories

Resources