Change button background when clicked - android-layout

I want a button that it has a background image and when I click that button,the button's background or color change (and we go to another activity.)
Tell me how can I make this.

Create an xml file and save it in one of the drawable folders in your project,let bg.xml,set this one as the background of your Button
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/button_pressed" android:state_pressed="true" />
<item android:drawable="#drawable/button_notpressed" />
</selector>
where button_pressed and button_notpressed are the images which you want to show on transition of the clicks and normal view of button
In your activity's xml file,set the background of the button as bg.xml
<Button
android:id="#+id/btn"
android:layout_width="150dp"
android:layout_height="60dp"
android:textSize="20dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="450dp"
android:background="#drawable/bg" <--! set it like this-->
android:text="Submit"
android:textColor="#FFFFFF" />

Related

Password toggle icon is inverted in material design in Android Studio

When I run this code, the password visibility toggle icon is inverted. It shows 'open eye' icon if password is hidden and vice versa.
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/id_txtInpLayout_pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:passwordToggleEnabled="true"
android:layout_marginTop="10dp"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/id_txtInpEditTxt_pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/hint_pass"
android:inputType="textPassword"
android:ems="14"/>
</com.google.android.material.textfield.TextInputLayout>
I did not write any code in MainActivity.java.
app:passwordToggleEnabled has become deprecated. For a good use you should now use the endIconMode in your TextInputLayout like this:
app:endIconMode="password_toggle"
Then in your TextInputEditText use:
android:inputType="textPassword"
And that's it. Here is the full example XML code:
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:id="#+id/editTextLayoutPassword"
android:layout_width="300dp"
android:layout_height="match_parent"
android:hint="Password"
app:endIconMode="password_toggle">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:maxLength="20" />
</com.google.android.material.textfield.TextInputLayout>
EDIT: It seems it may still be inverted, so here is a final solution.
Create a new Drawable Resource File, call it custom_eye and insert this code:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_opened_eye"
android:state_checked="true"/>
<item android:drawable="#drawable/ic_closed_eye"/>
</selector>
Then just download two SVG icons (you can choose here your icons).
Lastly, just import them as "Vector Assets" in drawable folder and rename one ic_opened_eye, the other ic_closed_eye here you are, you can change your icons for password visibility.
Then just use app:endIconDrawable="#drawable/custom_eye" in your TextInputLayout.
If you still don't like it, just invert the two icons in the XML file whenever you want.

I have background color in button.How can I set selector for button also?

<Button
android:id="#+id/info_button"
android:layout_width="0dp"
android:layout_weight="1"
android:textColor="#color/white"
android:background="#color/colorPrimary"
android:onClick="getInformation"
android:text="#string/information"
android:layout_height="48dp" />
I have selector below I want to use in button and use background color also.Is there any way?
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/btn_preess" android:state_pressed="true"/>
<item android:drawable="#color/btn_preess1" android:state_focused="true"/>
<item android:drawable="#color/btn_preess1"/>
</selector>

Is there any way to center an image in a button using just the XML in 4.2.2?

I've got a great image of a right arrow and a button to put it in, but I definitely don't want it flush to a side. Is there any way to put it in the center using only the XML?
Here's the relevant code:
<style name="widget.numberPad.actionButton.done.small" parent="widget.numberPad.actionButton.done">
<item name="android:layout_width">90dp</item>
<item name="android:drawableStart">#drawable/ic_arrow_forward_white_48px</item>
<item name="android:gravity">center_horizontal|center_vertical</item>
<item name="android:layout_alignParentBottom">true</item>
<item name="android:layout_centerHorizontal">true</item>
</style>
The button should be exactly 90dp × 90dp, but I don't know how the 48px × 48px image translates into dp.
I've read How to center icon and text in a android button with width set to “fill parent”, but the solution is both in java and deals with a width issue that I do not.
I'm not sure I understand your question, but to center an icon on a button, this is my solution.
Other possibility to keep Button theme.
<Button
android:id="#+id/pf_bt_edit"
android:layout_height="#dimen/standard_height"
android:layout_width="match_parent"
/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignBottom="#id/pf_bt_edit"
android:layout_alignLeft="#id/pf_bt_edit"
android:layout_alignRight="#id/pf_bt_edit"
android:layout_alignTop="#id/pf_bt_edit"
android:layout_margin="#dimen/margin_10"
android:clickable="false"
android:elevation="20dp"
android:gravity="center"
android:orientation="horizontal"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:clickable="false"
android:src="#drawable/ic_edit_white_48dp"/>
<TextView
android:id="#+id/pf_tv_edit"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="#dimen/margin_5"
android:clickable="false"
android:gravity="center"
android:text="#string/pf_bt_edit"/>
</LinearLayout>
With this solution if your add <item name="colorButtonNormal">#color/your_color</item> and <item name="colorControlHighlight">#color/your_highlight_color</item> in your activity theme, you can have the Material theme on Lollipop with the shadow and ripple, and for previous versions, flat button with your color and highlight color when you press it.
Additionally, here is this autoresizing solution as a picture:
Lollipop device
On pre-lollipop device
Pre-lollipop device press button

How to design a spinner in Android

I want to design a spinner as displayed in the image below:
I am not getting the arrow symbol pointing downside in spinner. How can I do this?
If I make a button design like shown above then I have to write extra code to get similar functionality as for a spinner, as Spinner doesn't have android:drawableRight="#drawable/arraodown", but in the button we have this method.
Do not mess around with right/left/... drawables.
Just set one 9-patch drawable as background that limits the inner content.
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/you_spinner_drawable" />
Regarding the 9-patch drawable have a look at the android resources or at this example picture taken from this blog post (which shows in little more detail on how to make a custom spinner):
For information about 9-patch drawables see the android documentation:
http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch
http://developer.android.com/tools/help/draw9patch.html
Of course you can also specify a State list xml file as drawable, eg.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When disabled -->
<item android:state_enabled="false"
android:drawable="#drawable/your_disabled_spinner_drawable" />
<!-- When selected -->
<item android:state_pressed="true"
android:drawable="#drawable/your_selected_spinner_drawable" />
<!-- When not selected-->
<item
android:drawable="#drawable/your_default_spinner_drawable" />
</selector>
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/you_spinner_drawable" />
**You can use the below mentioned Drawable to set as background for this spinner**.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When disabled -->
<item android:state_enabled="false"
android:drawable="#drawable/your_disabled_spinner_drawable" />
<!-- When selected -->
<item android:state_pressed="true"
android:drawable="#drawable/your_selected_spinner_drawable" />
<!-- When not selected-->
<item
android:drawable="#drawable/your_default_spinner_drawable" />
</selector>
You will have to create a custom layout for the spinner. I think the following XML might give you an idea.
<?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="wrap_content"
android:padding="3dip">
<TextView
android:id="#+id/number"
android:padding="3dip"
android:layout_marginTop="2dip"
android:layout_marginLeft="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Just use GIMP as your image editor and change background color and border with a color you like.
For example, I did this image by editing an existing one I found on the Internet: http://sdrv.ms/1lRkTbG

spinner dropdown coming in the middle of the screen with android 2.3

I am trying following with android 2.3 as well as andorid 4.0.
<?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="wrap_content"
android:background="#color/myColor"
android:orientation="vertical" >
<Spinner
android:id="#+id/spinnerControl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textColor="#android:color/white"
android:drawSelectorOnTop="true"
android:divider="#000000"
android:dividerHeight="1dp"
style="#style/Sherlock.__Widget.Holo.Spinner"
/>
</LinearLayout>
The style xml file is -
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Sherlock.__Widget.Holo.Spinner" parent="#android:style/Widget">
<item name="android:dropDownVerticalOffset">0dip</item>
<item name="android:dropDownHorizontalOffset">0dip</item>
<item name="android:dropDownWidth">wrap_content</item>
<item name="android:gravity">left|center_vertical</item>
<item name="android:spinnerMode">dropdown</item>
<item name="android:clickable">true</item>
</style>
</resources>
With android 4.0, I am getting spinner dropdown, right under the spinner control. But with andorid 2.3, it is coming in the middle of the screen, I don't know why. Please help.
To be precise, it was a new theme added to 4.0 by android. To get this style you need to provide the theme called "holo". But unfortunately it is not available for 2.X devices. And so you will not be able to get such a drop down in 2.X.

Resources