Android Webview text selection handles - android

I've overridden the default blue text selection handles in most of my app by adding these items to the app theme and adding the appropriate 9 patch drawables:
<item name="android:textSelectHandleLeft">#drawable/text_select_handle_left</item>
<item name="android:textSelectHandleRight">#drawable/text_select_handle_right</item>
<item name="android:textSelectHandle">#drawable/text_select_handle_middle</item>
However, this doesn't apply to text selection in a WebView (the blue handles are still there). How can I override the corresponding items in the WebView style?

WebView seems to be referencing the system resources (e.g. android.R.attr.textSelectHandleLeft etc) directly for drawing the handles on its own:
http://src.chromium.org/svn/trunk/src/content/public/android/java/src/org/chromium/content/browser/input/HandleView.java
So, I'd say there's no way to re-style them.

Create your own theme resources and reference them in your Android Manifest.
create your own theme and put it inside res/values
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme">
<item name="android:textSelectHandleLeft">
#drawable/my_own_handle
</item>
<item name="android:textSelectHandleRight">
#drawable/my_own_handle
</item>
</style>
</resources>
refer your own in AndroidManifest.xml.
<application android:theme="#style/MyTheme" >
PS: Referred from crbug: Overriding Selection Controls in Web view

Related

How to change the color of the android actionbar without using any image resource?

I want to change the color of my actionbar but I'm no good at making image resources so I want to know if there is anyway it is possible to do that without images. like setting a color to some property or something? I surfed a lot but there are too many confusing answers and I can't figure out which one to use.
I am developing for sdk above 8 so I'm using support libraries. Please help.
Generate what ever theme you want from Android Action Bar Style Generator
Copy all the files it generates to the respective folders in res folder and VOILA.. ;)
You could have easily found out the answer here. Or you can click here to create image resources very easily. But anyway, this is how you do it.
first of all, define a color you want for your actionbar in "colors.xml" in the "values" folder of your project like this:
<color name="customColor">#f1f1f1</color>
Then create a new .xml file in your drawable folder with the name say "actionbar_drawable.xml". It has to be a "shape" drawable.
drawable\actionbar_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid
android:color="#color/customColor"/>
</shape>
Now create "themes.xml" in "values" folder with the following code:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="CustomActionBar" parent="#style/Theme.AppCompat.Light">
<item name="android:actionBarStyle" tools:targetApi="11">#style/CustomBar</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">#style/CustomBar</item>
</style>
<style name="CustomBar"
parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">#drawable/actionbar_drawable</item>
<!-- Support library compatibility -->
<item name="background">#drawable/actionbar_drawable</item>
</style>
</resources>
Then in your manifest file, set the theme for your activity like this:
android:theme="#style/CustomActionBar"
That's all. Now you won't have to make any image resource. Hope it's helpful to you.
activity.getSupportActionBar().setBackgroundDrawable(new ColorDrawable(activity.getResources().getColor(R.color.my_color)));

Dependent preferences display wrong color font when disabled

In my app I am using several preferences, including some of them related with dependencies using the following attribute: android:dependency="pref_key".
Basically, when the checkbox is not selected, all the other preferences below are disabled:
The problem happens when I setup back the following 3 lines in my custom theme:
<style name="AppThemeOrange" parent="#style/AppTheme">
<item name="android:textColorPrimary">#color/OrangeMain</item>
<item name="android:textColorSecondary">#color/OrangeDark</item>
<item name="android:textColorTertiary">#color/OrangeLight</item>
(...)
The colors defined on these 3 attributes also override the default font color of the disabled preferences:
The preferences are still well disabled, but the fonts displayed make believe the contrary...
I searched in the default Holo Light styles and theme, but I have no idea where this is defined and why the styles above override these ones.
Did anyone already meet the problem?
you should define a color state list and put it inside the /res/color folder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="false" android:color="#FF00ff00"/>
<item android:color="#FFff0000"/>
</selector>

Themes in android

A create a simple Theme as
<style name='one'>
<item name='android:textColor'>#eea</item>
<item name='android:textSize'>20sp</item>
</style>
However on viewing in the emulator the screen goes black.when i do not apply theme the screen has a white background .
what really happens here.i am just starting with android.
In addition ,if a apply a theme to my activity then the attributes of the theme applies to all components of my activity say button,textfields and edittexts .
why would i then write
android:textSize=?android:textSize
to reference value from the theme for any button in my layout when the same value would already be applying.
is the syntax above the correct way to reference an attribute from my theme to assign to attribute for any view in my layout.
thanks
tejinder
Yeah, so you need to do a little more reading.
Let's start with the basics,
You need to understand the differente betweent an Attribute, a Style, and a Theme.
An Attribute is something that can be styled. For instance: android:textSize is an attribute that can have any value.
A Style is a set of specific attributes that will be applied to a Widget. They are defined
in your /values/styles.xml
For instance:
<style name="normalTextThin" parent="android:Widget.Holo.Light.TextView">
<item name="android:gravity">left|center_vertical</item>
<item name="android:padding">8dp</item>
<item name="android:textColor">#FFFFFF</item>
<item name="android:textSize">16sp</item>
</style>
The styles can be applied either as part of a theme or directly as theme-independent.
Theme-indepentent styling of a widget is like this:
<TextView
android:id="#+id/text"
style="#style/normalTextThin"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
You are then theming only that one TextView.
A Theme is a collection of Styles that can be applied to a part of your UI, such a a whole Activity, or your whole Application.
For instance:
<style name="AppTheme" parent="android:Theme.Light">
<item name="android:editTextStyle">#style/EditTextAppTheme</item>
<item name="android:buttonStyle">#style/ButtonAppTheme</item>
<item name="android:imageButtonStyle">#style/ImageButtonAppTheme</item>
</style>
Here, we are declaring that all EditText in your application will use the style named EditTextAppTheme, and so forth and on. When done like this, in order to actually have the theme be active, you declare it in the manifest:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
That means that you are not required to declare the style on each widget you create.
<EditText
android:id="#+id/input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/hint_search">
<requestFocus />
</EditText>
That widget right there would already be styled using EditTextAppTheme without the need of you explicitely declaring so.
I recommend you try to read on what attributes can be styled, how to style them, and so forth and on.
If you don't want to though, it's fine, you can still get a lot done with the following tools for styling:
ActionBarStyleGenerator to help you create styles for the ActionBar.
Android Holo Colors to help you style standard widgets.
Hope that helps.
Additional Info
Let me clarify on the whole ?attr/attributeName
The ? means that the system will choose the specific attributeName value for the current Configuration (not specific to different themes). This should be used only when you want the value to be different on different configurations. For example:
?android:attr/actionBarSize
This line is a dimension, and it will be different not based on the current theme, but on the current device screen size and orientation (values, values-land, values-sw600dp).
It's important to know that specifying ?android: means you are accessing preset Android values, not yours. If you have or want to create and use your own attribute values for specific configurations, you must do the following:
Create a file named attrs.xml on your /values/ folder.
Declare the desired custom attribute:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<attr name="my_custom_attr" format="reference" />
</resources>
Declare a value for the custom attribute, let's say on your own theme.
<style name="AppTheme" parent="android:Theme.Light">
<item name="my_custom_attr">#resource_type/resource_name</item>
<item name="android:editTextStyle">#style/EditTextAppTheme</item>
<item name="android:buttonStyle">#style/ButtonAppTheme</item>
<item name="android:imageButtonStyle">#style/ImageButtonAppTheme</item>
</style>
And then you can use it on the Widget you'd like:
Hope that clears things out.
EDIT 2
For a better answer to your question, please update your question. And like I said, read more on how to properly create styles.
The Theme named 'one', what do you want to apply it to? An activity, a Widget, the whole Application?
How are you applying the theme? Show the lines of code where you specify the usage of theme 'one'.
Your theme as you specified is simply not a properly constructed theme/style.
<style name='one'>
<item name='android:textColor'>#eea</item>
<item name='android:textSize'>20sp</item>
</style>
This says absolutely nothing, and it is definitely not suitable for an Activity-level theme. The reason you specify a parent is so your theme can inherit all of the attributes from the parent, and then you specifiy which ones to change.
For instance, if you want to use your theme and have a light background, do this:
<style name='one' parent="android:Theme.Holo.Light>
<item name='android:textColor'>#eea</item>
<item name='android:textSize'>20sp</item>
</style>
But even here, despite the fact that it will apply, you don't want to have the same text color and size for the whole application do you? That'd be nonsense, different text color and sizes account for a big part of the user experience, so rather than setting those values from what we can refer to as the main style, we can create substyles and apply them to certain widgets.
I can't really go any more detailed that what I already have, the above explains how to accomplish Widget-specific styling, and activity/application level theming.
For a complete start-up guide, read the Android Developer Site, try the test styles declared there, see how they work, and until then try to create your own, don't try to create something out of nowhere if no reading has been made.

Dynamic themes and custom styles

I've got an app with two themes (dark and light) that can be selected at runtime. This works. I also have a ListView with rows that can have one of three different layouts, each of which has a style (say, different colors). This also works. But I can't get these two features to work together. I really need six different styles, three for one theme (dark) and three for the other (light), but I can't figure out how to choose a style for a list item based on the current theme, or get that effect any other way by using XML files. My three layouts each point to a custom theme that sets the color, but that overrides whatever theme I've got set. Themes can only contain items that are "styleable", so I can't put my own custom items in there. There may be a way to do this programmatically, but I was hoping to do it declaratively. Any ideas?
Thanks to wingman for the hint. My situation involved colors, which are a bit more complicated, so I'll write up my solution here.
I have two themes (light and dark) which the user can choose from in the Settings screen. I have a ListView which can have two types of rows (plain and note), each with its own styling. Firstly each layout needs to point to a style:
<TextView style="#style/PlainItemText" ... />
(or NoteItemText) and we need to define the styles:
<style name="PlainItemText">
<item name="android:textSize">#dimen/list_item_font_size</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">?plainTextColor</item>
</style>
The text color can't be fixed because it depends on the selected theme. We must create a custom attribute and refer to it with a question mark, as above. We define the attribute in res/values/attrs.xml:
<!-- Attributes we use to set the text color of the various list items. -->
<attr name="plainTextColor" format="reference|color"/>
<attr name="noteTextColor" format="reference|color"/>
We can then define the various colors. Here we have two styles and two themes, so we need four color state lists, each in its own file under res/color. For example, here's res/color/plain_text_color_dark.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:color="#android:color/white"/>
<item android:state_selected="true" android:color="#android:color/black"/>
<item android:state_focused="true" android:color="#android:color/black"/>
<item android:state_pressed="true" android:color="#android:color/black"/>
<item android:color="#android:color/white"/>
</selector>
The selected/focused/pressed colors are the same in all these files because they're over the highlight color. Be careful with the state_window_focused version. It didn't behave as advertised, and I had to set it to the default color (the last line above) in all cases. Now we need to create our themes and bind the attributes to one of the colors. These lines go into res/values/themes.xml:
<style name="Theme.Dark" parent="android:Theme">
<item name="plainTextColor">#color/plain_text_color_dark</item>
<item name="noteTextColor">#color/note_text_color_dark</item>
</style>
<style name="Theme.Light" parent="android:Theme.Light">
<item name="plainTextColor">#color/plain_text_color_light</item>
<item name="noteTextColor">#color/note_text_color_light</item>
</style>
Finally we pick a theme at run-time, in an Activity's onCreate() method, before calling super.onCreate():
if (isDarkTheme) {
activity.setTheme(R.style.Theme_Dark);
} else {
activity.setTheme(R.style.Theme_Light);
}
Note that I don't take into account newer themes like Holo, so my app looks old on Honeycomb and later. I'll fix that at some point, but it wasn't a regression here.
A twist in my case is that some Activities have a larger title bar in order to fit some buttons. In principle I should have created four themes, a light and dark for a narrow title and a light and dark for a fat title. But instead I created a mix-in style:
<!-- Mix-in style for activities. -->
<style name="ButtonTitleBar">
<item name="android:windowTitleSize">44dp</item>
</style>
and procedurally add it to whatever theme I'm using. This code goes right after the above setTheme() calls:
if (buttonTitleBar) {
// Mix in this other style.
Resources.Theme theme = activity.getTheme();
theme.applyStyle(R.style.ButtonTitleBar, true);
}
I didn't see this documented anywhere, and I don't know if it's legit, but the code of Activity.getTheme() implies that it should work fine, and it has worked in all my testing. This can help avoid the combinatorial explosion of themes that you can find in the standard Android theme list.
It's a long time ago that Lawrence Kesteloot published his solution in 2012. Now it is six years later, a am new in Android and try to solve the similar problem:
How can I exchange the whole style of the application by just exchanging one theme?
This is a generalisation of Lawrences issue how to organise two exchangeable themes.
I figured out a solution based on Lawrence's and going a step further.
(Not claiming it is the perfect solution, yet an improvement.)
Lawrence figured out the power of user defined attributes to reach this goal. He uses them to address colours depending on the the currently selected theme.
While this is working it still requires to define attributes for each and every property. It doesn't scale well. So why not bundling the properties into styles and themes and using the same mechanism?
This results in a master theme, that is defining child themes and styles.
res/values/attrs.xml
<resources>
...
<attr name="mainViewTheme" format="string"/>
<attr name="asideViewTheme" format="string"/>
...
</resources>
When defining the attribute to set a theme, there is no special format for it. The format string does it.
res/values/styles.xml
<style name="MasterTheme">
...
<item name="mainViewTheme">#style/MainViewTheme</item>
<item name="asideViewTheme">#style/AsideViewTheme</item>
...
</style>
<style name="MainTextTheme">
...
</style>
<style name="MainViewTheme">
...
</style>
res/layouts/main.xml
<TextView
android:theme="?mainViewTheme"
...
By exchanging the master theme all styles are adjusted. It still requires the definition of a handful of theme attributes and then does a powerful job. Setting up attributes for every property is not required any more.

AutoCompleteTextView dropDownSelector

I have an AutoCompleteTextView component and I want to change its default drop down selector to another color. First I tried: android:dropDownSelector="#FF400000", but it caused no selector to appear at all! Next I put the color in a drawable resource:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#android:color/holo_blue_dark"/>
</shape>
And linked it: android:dropDownSelector="#drawable/drop_down_selector", but that did the same effect (no selector). Next I found a similar issue someone opened, just for spinner: http://code.google.com/p/android/issues/detail?id=24922, so I tried defining a style as was explained in the solution there:
<style name="AutoCompleteDropDown" parent="#android:style/Theme.Holo.Light">
<item name="android:dropDownListViewStyle">#style/AutoCompleteSelector</item>
</style>
<style name="AutoCompleteSelector" parent="#android:style/Widget.Holo.Light.ListView">
<item name="android:listSelector">#FF400000</item>
</style>
And linked it: style="#style/AutoCompleteDropDown", but it did nothing (however, the default selector got back since I removed the dropDownSelector attribute).
So what am I missing here? What am I doing wrong?
UPDATE: Ok, so as was suggested, I also tried a selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:state_pressed="true"
android:state_focused="true" android:state_activated="true"
android:drawable="#android:color/holo_green_light"/>
</selector>
But it didn't work, I still get no selector (at least not one I can see).
Theme's and styles are different by intent. And use different attributes. Theme define style for different widgets. Style define the widgets itself. Theme's are applied to application or activities.
1) If you want it to be for just one widget.
You need to go to /platforms//res/values/values.xml, find out how styles is defined for your widget (AutoCompleteTextView). Pick up required attribute. Define the same selector like in system but with your modifications. And you can even find out the selector in /res/drawable
2) If you want to be it all over the application:
a)You need to go to /platforms//res/values/themes.xml
b) There you can find out which style are in the theme you chose for AutoCompleteTextViewb
<item name="autoCompleteTextViewStyle">
#android:style/Widget.AutoCompleteTextView
</item>
c) Then got to res/values/styles.xml
d) There you can find out the style for widget.
e) Then you need to extend theme. And override autoCompleteTextViewStyle attribute by your new created style like in 1 option.
Try remove background from your views, which generated by adapter. That has helped me.

Categories

Resources