Custom buttons doesn't display android studio - android

<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.

Related

Android Studio 2.2 custom button

How do I add custom button in Android Studio 2.2?
I found info that I have to:
make an .xml file in app/src/main/res/drawable (for example custom_button.xml):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_on"
android:state_pressed="true" />
<item android:drawable="#drawable/button_focused"
android:state_focused="true" />
<item android:drawable="#drawable/button_off" />
</selector>
add my .png images (button_on, button_off, button_focused) to app/src/main/res/drawable-xxhdpi (I do not have this folder - I placed images in drawable folder - making xxhdpi folder by myself and placing images there doesn't work either)
edit activity_main.xml:
<Button
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button_microphone"
android:layout_alignBaseline="#+id/button_add_resource"
android:layout_alignBottom="#+id/button_add_resource"
android:layout_centerHorizontal="true"
android:background="#drawable/custom_button"/>
And now what? It doesn't work, although the button shows up in activity_main.xml visualiser.
Errors:
Solved.
I just forgot to use Android\sdk\tools\sdraw9patch.bat to make 1 pixel width borders on my images.

Changing Button UI in Android

I am trying to customize a Button UI in Android.
I tried the following things:
btn.setBackgroundColor
btn.setBackgroundResource
btn.setBackgroundColor
But all of these are increasing the size of the Button, and because of that the Buttons near by can not be segregated (??).
Please suggest something.
If you want to apply a Hover effect then you have to do this in your XML where button layout is like
Button
android:id="#+id/xyz"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="15dp"
android:background="#drawable/general_btn_hover_effect"
android:onClick="somefunction"
android:text="#string/search_number"
android:textColor="#ffffff"
android:textSize="20sp"
/>
Notice android:background="#drawable/general_btn_hover_effect" there and then in #drawable folder make a general_btn_hover_effect.xml and write this into it
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/blank_normal_bg" android:state_focused="true" android:state_pressed="false"/>
<item android:drawable="#drawable/blank_hover_bg" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="#drawable/blank_hover_bg" android:state_focused="false" android:state_pressed="true"/>
<item android:drawable="#drawable/blank_normal_bg"/>
</selector>
Changing background color can not change size of button. Maybe you changed size in xml layout. Check again

Make radiobutton icon smaller

I'm developing an Android application and I use this to customize RadioButtons:
<!-- Radio button style -->
<style name="RadioButton" parent="#android:style/Widget.CompoundButton.RadioButton">
<item name="android:button">#drawable/checkbox_unchecked</item>
</style>
But the image #drawable/checkbox_unchecked is very big.
Do you know how can I make it smaller?
I know you can achieve what you want by reducing the size of your image. But I would recommend using the following code. This will help you set images for both the unselected and selected modes.
This is how you can achieve it, in a few steps:
-Create Image Drawables for their two states (checked/unchecked).
-Create selector for this two drawables. The sample content should be something like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#drawable/unchecked" />
<item android:state_checked="false" android:drawable="#drawable/checked" />
</selector>
-Add this selector as a android:button attribute to RadioButton
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/background">
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/checkbox_selector"
android:text="Custom RadioButton" />
</LinearLayout>

ImageButton state when pressed not showing up

I am trying to create a button that has a default image, but when it is pressed it switches to a new image. I have used this tutorial along with this question but it will not work.
This is my info_button_selector.xml:
<?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/ic_menu_info_details2" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/ic_menu_info_details2" /> <!-- focused -->
<item android:drawable="#drawable/ic_menu_info_details" /> <!-- default -->
</selector>
This is the code with my ImageButton:
<ImageButton
android:id="#+id/apModeInfoButton"
android:layout_width="0dip"
android:background="#null"
android:layout_height="match_parent"
android:layout_weight="1.15"
android:clickable="true"
android:src="#drawable/info_button_selector" />
The button stays at the initial state looking like ic_menu_info_details the entire time.
I changed my info_button_selector.xml file to this:
<?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/ic_menu_info_details2" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/ic_menu_info_details2" /> <!-- focused -->
<item android:drawable="#drawable/ic_menu_info_details2" /> <!-- default -->
</selector>
The ic_menu_info_details2 image was the displayed the entire time, which is expected. This shows that the button IS using the XML file as its drawable resource, but why is it that the button does not change its image when pressed?
EDIT
Using Joss's answer below, the image still does not change, and is now stretched in a strange way. Note: Both images are exactly the same except the second image is scaled to be 60% the size. This might mean that it is working but I cannot tell as the view is stretched.
<ImageButton
android:id="#+id/apModeInfoButton"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1.15"
android:clickable="true"
android:background="#drawable/info_button_selector" />
I used both of these versions of the XML file as well as different combinations of the images, all of the combinations resulted in the same sized image never changing.
<?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/ic_menu_info_details" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/ic_menu_info_details" /> <!-- focused -->
<item android:drawable="#drawable/ic_menu_info_details" /> <!-- default -->
</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/ic_menu_info_details2" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/ic_menu_info_details2" /> <!-- focused -->
<item android:drawable="#drawable/ic_menu_info_details2" /> <!-- default -->
</selector>
Set that drawable to the ImageButton background rather than the src.
<ImageButton
android:id="#+id/apModeInfoButton"
android:layout_width="0dip"
android:background="#drawable/info_button_selector"
android:layout_height="match_parent"
android:layout_weight="1.15"
android:clickable="true" />
Edit to follow up the distortion problem:
Well what you can do is to have the layers separate.
Because the background is stretchable, set this to the BACKGROUND and separate the image which shouldn't stretch and set this to the SRC.
<ImageButton
android:id="#+id/apModeInfoButton"
android:layout_width="0dip"
android:src="#drawable/non_scaleable_image"
android:background="#drawable/gradient_bg"
android:layout_height="match_parent"
android:layout_weight="1.15"
android:clickable="true" />
This way, you could have a background as a gradient and have it stretch without loosing quality, and the SRC image would not resize and distort.
That is really what an ImageButton should be used for.
Alternatively, do what I suggested in the first answer, but use a Button instead. Let me know if this helps.
You should try using a completed different image for details2 and see if the image is in fact changing, but not being re-sized. I wouldn't expect a stateful button to re-size, maybe use two different images and set visibility.

Android - Image button size

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.

Categories

Resources