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..
Related
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
In my android app, I've designed some layouts to set the custom dialog. But I face some problems while making border with glittering effect.
I've set the following style to my dialog:
<style name="MyDialog" parent="android:Theme.Holo.Light.Dialog">
<item name="android:windowTitleStyle">#style/DialogWindowTitle</item>
<item name="android:windowFrame">#color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFrame">#drawable/alertbgc</item>
In the java snippet,I added the lines as follows:
final Dialog mydialog= new Dialog(this,R.style.MyDialog);
mydialog.setContentView(R.layout.mylayout);
But with this snippets I'm getting very dark background (I used nine patch images too, but no use). Please help me providing your ideas.
Thanks in advance
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
I'm using the following menu XML in my project:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/user_cp"
android:icon="#drawable/gear"
android:showAsAction="always"
android:title="#string/usercp"
/>
<item
android:id="#+id/pm"
android:icon="#drawable/sym_action_email"
android:showAsAction="always"
android:title="#string/private_message"
/>
<item
android:id="#+id/refresh"
android:icon="#drawable/ic_menu_refresh"
android:title="#string/refresh"
/>
<item
android:id="#+id/settings"
android:icon="#drawable/ic_menu_preferences"
android:title="#string/settings"
/>
<item
android:id="#+id/logout"
android:icon="#drawable/ic_menu_logout"
android:title="#string/logout"
/>
</menu>
When I my application loads up on Google TV (emulator or an actual device) LeftNavBarLibrary (http://code.google.com/p/googletv-android-samples/source/browse/LeftNavBarLibrary) ignores the android:showAsAction="always" property set for the first two menu items. These items are always placed inside the soft menu. Looking in LeftNavBarLibrary, in OptionsDisplay.java I see this reference to the "Show always" menu options and nothing else:
private void refreshExpandedState() {
// Menu icon.
setOptionExpanded(mView.getChildAt(1), mExpanded);
// "Show always" options.
ViewGroup optionsContainer = getOptionsContainer();
for (int i = 0; i < optionsContainer.getChildCount(); ++i) {
setOptionExpanded(optionsContainer.getChildAt(i), mExpanded);
}
}
Does this feature work with LeftNavBarLibrary or am I stuck implementing it myself? It's critical for this app that I use action items on the ActionBar in certain cases since the buttons are actual actions and not something that can be replaced with a tab.
Edit: Looks like LeftNavBarLibrary doesn't truly honor the options menu at all, it will show the options button regardless of whether or not it needs to.
You should not be able to place action items on LeftNavBar , it would always show as ordinary Option menu item.
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