Android GUI Help: How to Create custom buttons - android

I was wondering how you can create buttons like the ones at the bottom of the screen and the home button at the top?

Quite hard to understand if you have doubts about button design and states or button position.
If it's a design issue, you should create your button states on your favorite software, as example, photoshop.
Then, in your android layouts you point your button background src to a drawable XML containing all button states.
Example XML for button:
<Button android:id="#+id/back"
android:layout_width="184px" android:layout_height="44px"
android:background="#+drawable/back" />
After this, you need your custom picture for the button, and the button XML on your drawables folder,
here's the XML you need for your button:
<?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/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
Follow up on: http://developer.android.com/reference/android/widget/ImageButton.html

The title bar you mentioned about is called "Action Bar"
You can learn how to implement it here:
http://android.cyrilmottier.com/?p=274
https://github.com/johannilsson/android-actionbar/
Regards.

Related

How do I set a "pressed" imagebutton in Android Studio?

I have an ImageButton:
<ImageButton
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/btn_70s"
android:src="#drawable/btn_70s"
android:background="#null" /* Sets transparent BG for button image(PNG) */
/>
The image is "btn_70s", how do I set a "pressed" version of the image for when the ImageButton is tapped?
The documentation states to create an XML file and place it in the drawable directory:
<?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/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
This to me says it would affect all the ImageButtons rather than an individual one. Would I save it as an individual XML for each button for example (btn_70s_custom.xml), and then use it like:
<ImageButton
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/btn_70s"
android:src="#drawable/btn_70s"
android:android:background="#drawable/btn_70s_custom"
/>
Only issue with this, I can't use #null to allow transparency. Any assistance is appreciate.
Have a look over there for a (possible) solution to your problem : Make button background transparent using selector
And for the record,
This to me says it would affect all the ImageButtons rather than an individual one. Would I save it as an individual XML for each button for example (btn_70s_custom.xml), and then use it like:
No, it won't set this theme for all the buttons, but only for the ones where you specify to use this selector.
Hope this helps!
You can try selectableItemBackground
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"/>

Customize the look of checkbox starStyle

I the following inside the xml layout for my ListView row:
<CheckBox
android:id="#+id/favbutton"
style="?android:attr/starStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
And it shows a blue star which can be checked to select a favourite. This is exactly the functionality that I need, but I just would like to customize the star icon that is shown. I would like to change the colour to yellow instead of blue and also make the star smaller if possible. Is it possible to make these changes by making changes to the theme applied to the application? What would I need to edit to change the look of the star icon?
I resolved this as follows. I created a file in res/drawable called btn_star_selector.xml and defined the selector as follows:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_pressed="true" android:drawable="#drawable/btn_star_on_pressed" />
<item android:state_checked="false" android:state_pressed="true"
android:drawable="#drawable/btn_star_off_pressed" />
<item android:state_checked="true" android:drawable="#drawable/btn_star_on" />
<item android:state_checked="false" android:drawable="#drawable/btn_star_off" />
</selector>
With this selector defined I then replaced this line in the checkbox definition
style="?android:attr/starStyle"
with this
android:button="#drawable/btn_star_selector"
I then created four images to represent the different states of the checkbox, namely on, off, on pressed and off pressed and also placed these in res/drawables.
Hope this helps anyone else that is trying to figure out how to customize checkboxes.

Android ImageButton background change when clicked

I have an image button that has a transparent background and a image as a foreground. What i want to do is to make the button flash when i press the button. Now i don't want to define two different images for the up and down state, is there any why i can define a selector color for the background and foreground and the background of the button ?
Kind Regards,
You can use this code for background selection of button for diff-2 state of button, save this file in drawable folder and set button background. Hope it will useful to you...:)
<?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/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>

Android custom image on/off button

I want to create custom ImageButton, but to work like on/ off button. On click on button image to be changed to pressed(until another button is pressed)!
On picture This month button is on , This year off .
How can I create button like this?
Do I need to use and how ?
Thanks
Use the selector XML tag to help you achieve this. Here, see this link about StateListDrawables. So their example shows
a button.xml file:
<?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/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
and then links an actual button to that xml:
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/button" />
Sergey Glotov and wheaties thanks for your help
For every ImageButton I create separate selectors for default and pressed state, I create ImageButton in xml:
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/month_button"
android:id="#+id/btnMonth"
android:onClick=ButonMonthClick"/>
And onClick event for all ImageButton I change background image programmatically:
btnWeek.setBackgroundResource(R.drawable.week_pressed);
btnMonth.setBackgroundResource(R.drawable.month_default);
Thant solve my problem!

How to change textColor on press Even in Android?

Is it possible to change the text color of a textview if the textview is pressed?
I want to achieve a flashing effect with the color change only lasting as long as the button is pressed.
I know how to change the background of the textview with a selector list and the correct state but how can I change the color of text if the user pushes a button or a simple textview?
You can define a selector for colors as well. A short example that only distinguishes between pressed and all other states is:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:color="#FFFFFF" />
<item
android:color="#4C566C" />
</selector>
For a full documentation on the selector see this unofficial documentation.
Put every color selector in a single file and put this files in a directory called color in the resources folder of your project.
Taken from official documentation:
XML file saved at res/color/button_text.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
This layout XML will apply the color list to a View:
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/button_text"
android:textColor="#color/button_text" />
search for color selector to use in the
android:setTexColor
attr
you can change it using the setTextColor(ColorStateList) method
myTextView.setTextColor(myColorStates);
myTextView.setTextColor( 0xFFFF0000 )

Categories

Resources