I would like to generate a pressend button design and load it on the fly.
The static version it is an xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- selected state -->
<item android:drawable="#drawable/bt_back_pressed" android:state_pressed="true" android:state_selected="false"/>
<item android:drawable="#drawable/bt_back_pressed" android:state_pressed="false" android:state_selected="true"/>
<item android:drawable="#drawable/bt_back_pressed" android:state_pressed="true" android:state_selected="true"/>
<!-- unselected state (default) -->
<item android:drawable="#drawable/bt_back_normal"/>
</selector>
which is located in the /res/drawable folder. When I want to use it just a line of the code:
android:background="#drawable/bt_back"
Now, the current project loads the design for the button from the server side, let it be the bt_back_normal.png loaded from www.somehost.com/some/resource/bt_back_normal.png.
It would be nice if I could get an API to generate the "pressed" version ( some darker) and link it at events chain to show, when needed.
Right now there is no visual effect, when the user press the button.
How can I generate that xml equivalent on the fly? -generate a pressed version and set to show when needed.
Thanks.
I think you're looking for the StateListDrawable class. You can create these in code, add states to it (e.g. your downloaded pressed png file) and then set it to your button with button.setBackgroundDrawable(stateList).
No, you can't do this on the fly. If you wanna use dynamically generated pressed drawables you should implement OnTouchListener and set needed background inside of it.
This is kind of a workaround for that, but you could just override the OnClickListener for the button, and change the background of the button inside there. ie.
final Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
button.setBackgroundDrawable(R.drawable.button_pressed);
}
});
EDIT:
I didn't realize you wanted to change the states; thought you just wanted to show it was pressed. In that case, use StateListDrawable: http://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html
Related
I created alert dialog with TextInputLayout programmatically in fragment. I need to hide password by default, TextInputLayout has toggle button and it is working as expected hide/show on click. I tried to keep password hidden by default by settingsingleline to true and other hack i got from other SO answers but i still not able to get desired result. Any idea why i am not getting desired result?
TextInputLayout passwordParent = new TextInputLayout(getActivity());
passwordParent = new TextInputLayout(getActivity());
TextInputEditText passWord= new TextInputEditText(getActivity());
passWord.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
passWord.setBackgroundResource(R.drawable.background);
passWord.setBackgroundColor(getResources().getColor(R.color.white));
passwordParent.setEndIconMode(TextInputLayout.END_ICON_PASSWORD_TOGGLE);
passwordParent.setEndIconDrawable(R.drawable.show_password_selector);
passwordParent.addView(passWord);
Background XML Selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_remove_red_eye_black_24dp" android:state_checked="true"/> // Visible Eye Icon
<item android:drawable="#drawable/ic_visibility_off_black_24dp"/> // Hide Eye Icon
</selector>
Edit: It's working by adding InputType.TYPE_CLASS_TEXT into input type like this passWord.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT); but the icon still stay the same, its still showing wrong eye icon.
Try moving addView(passWord) up by two lines, like this :
passwordParent.addView(passWord);
passwordParent.setEndIconMode(TextInputLayout.END_ICON_PASSWORD_TOGGLE);
passwordParent.setEndIconDrawable(R.drawable.show_password_selector);
First-time Android dev, though have used C# and Java in the past.
Trying to make a simple, Windows 8-like GUI. At the moment I have one tile (ImageButton) with a background color set in activity_main.xml.
<ImageButton
android:id="#+id/btn1"
android:layout_width="120dp"
android:layout_height="120dp"
android:background="#FF0000"
android:onClick="changeColor"/>
And I have a function to change the color in MainActivity.java.
public void changeColor(){
ImageButton btn1 = (ImageButton) findViewById(R.id.btn1);
btn1.setBackgroundColor(Color.GREEN);
}
Compiles fine, but every time I click the red square, the app crashes.
I'm assuming there's something fundemental about how Android is developed that I'm missing which is leading to a very obvious mistake. Is there a better way to be doing this rather than ImageButtons?
Thanks!
Compiles fine, but every time I click the red square, the app crashes.
Because when adding android:onClick in xml then method must be public and accept a View as its only parameter which we want to call on View click:
public void changeColor(View view){
ImageButton btn1 = (ImageButton) findViewById(R.id.btn1);
btn1.setBackgroundColor(Color.GREEN);
}
It's better to create a selector and set it as backgraund to the button.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#color/button_pressed" />
<item android:color="#color/button_normal" />
</selector>
Hope it'll help you.
P.S. useful link
Is there a better way to be doing this rather than ImageButtons?
Yes, ImageButtons are mainly for creating a 'clickable' image. If you simply want a colored button, a regular Button will do fine.
You can do this by setting your Buttons background right from XML using a selector with a Drawable state list.
A good example can be found here: https://stackoverflow.com/a/3882151/1683141 (a color will also qualify as a Drawable)
Why isn't my current code working?
You should add the view as parameter to your method like this:
public void changeColor(View view){
view.setBackgroundColor(Color.GREEN);
}
Is there easy way to manipulate colors in android ?
In dot.net we have color palette to change the color of
objects(button-background,text-color.. etc)
similarly is there any color palette/or/-any-plugins in object
browser of eclipse IDE at design time
Note:: I am aware of changing the color by adding color codes in styles.xml and referencing it in layout.xml
You can do this in some cases directly,
e.g in TextView you can change the Text Color by applying this statement in
Java code
textView1.setTextColor(Color.Red);
for other objects like buttons etc. you can not change their Color Directly like C#.
You have to create different .xml file and then you will store this .xml file inb drawable folder, in your activity_main(where button is created in your layout file) you have to make reference
e.g
<Button
android:background="#drawable/red_button" />
now the colored button .xml file located in
res/drawable/red_button.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/red_button_pressed" />
<item android:state_focused="true" android:drawable="#drawable/red_button_focus" />
<item android:drawable="#drawable/red_button_rest" />
</selector>
Check this Link for more Details
Okay, so here's the idea; I want to create a set of icons that persists on the bottom of the screen across all the activities I have on my app. The first thing that came to my mind was the TabWidget.
After I spent some time with it, I realized that I can't get the "focused state" off the tabs, I know tabs are supposed to work out that way but the idea is to making those tabs looks like icons - it shouldn't looked like they were focused all the time.
I tried using tabHost.setBackgroundColor(color) but unfortunately it did not work as I expected.
Here's some visualization to ease off the problem.. http://i.stack.imgur.com/uF7fu.png
p.s., sorry about the link. As a new user I weren't allowed to post images directly on the post.
One way to do this would be to use a custom Selector for the tab states that is identical regardless of whether or not the tab has focus. This might look something like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="#drawable/tab_unselected" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="#drawable/tab_unselected" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="#drawable/tab_unselected" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="#drawable/tab_unselected" />
<!-- Pressed -->
<item android:state_pressed="true" android:drawable="#drawable/tab_press" />
</selector>
You'd add that to your drawable directory, and then configure your tabwidget to use that in your activity (like so):
TabWidget tabWidget = getTabWidget();
for(int i = 0; i < tabWidget.getChildCount(); i++) {
RelativeLayout tabLayout = (RelativeLayout) tabWidget.getChildAt(i);
tabLayout.setBackgroundDrawable(res.getDrawable(R.drawable.tab_indicator));
}
A much more detailed guide is here. See if that works for you.
In general you should have a 'focused state' to indicate what is active for those devices without a touch screen (i.e. Google TV) or to assist the accessibility tools for the visually impaired. If you still want to go down the route of hiding the 'focused state' see Femi's answer regarding custom selector.
Modifying the selected style like Femi shows is the way to do this..
I'm developing an app and when I run it in the Emulator and I've selected a button (not clicked!) it shows an orange layer over it. When i click it i turns yellow.
On a HTC device the orange/yellow overlay is green. On a Samsung Galaxy S it's blue.
I want my app to have the same selection color's throughout the whole app (Blue to be precise).
Is this possible? If so, how?
I think link provided below could be helpful for you
link is: http://groups.google.com/group/android-developers/browse_thread/thread/a7ff938ce09e7c37
Yes it is possible. Use Themes to apply style to the whole app.
You r right
But u also set background color at runtime
& Another
possibility that may be HTC /Samsung has different Android Version ?
so this Happen
Create a selector in your drawable folder
for example drawable/item_selctor.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/item_button_selected"
android:state_pressed="true" />
<item android:drawable="#drawable/item_button_selected"
android:state_focused="true" />
<item android:drawable="#drawable/item_button_selected"
android:state_selected="true" />
<item android:drawable="#drawable/item_button" />
</selector>
In your code or xml file use
item.setBackgroundResource(R.drawable.item_selector);
in code
or
android:background="#drawable/item_selector"
in xml layout file