Background property on custom compound control - java

I have a compound control that essentially combines a Button with a ProgressBar. It includes a background property that I have declared in attrs.xml as:
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<declare-styleable name="MyControl">
<attr name="background" format="reference" />
</declare-styleable>
</resources>
In the constructor of my control, I then pull the background out of the TypedArray and apply it.
All this worked fine until recently, when I had to add in a dependency on the v4 support library. Now I get this build error in my attrs.xml file:
Error APT0000: Attribute "background" has already been defined (APT0000)
Why is this? And what can I do as an alternative so that consumers of my compound control can set the background?

It's a conflict with an internal library that also defines the 'background' attribute.
For more details visit : https://groups.google.com/forum/#!topic/actionbarsherlock/_N0hn47zx6w

The attribute "background" has already been defined in the support library. So you dont have to define that again. you just add the attribute without defining it, like this,
If you want to use app:background in the custom view, then
<attr name="background"/> is enough in the attrs file.
If you want to use android:background in the custom view, then use this line in attrs.
<attr name="android:background"/>

Related

Custom callback function attribute android

As android defines attributes inside the attrs files and I have seen that Button has attribute onClick listed. Is there a way to create custom attributes referencing functions as so and how does on go about implementing it.
<Button
...
android:onClick="onClickFunction"/>
What I want to have
<CustomView
...
android:onItemClick="onItemClicked"/>
attrs.xml
<attr name="onItemClick" format="string"/>
How does one create attach this function as a listener to the view?

Using custom font for all text within app not working with Lollipop

The problem I am having is specific to Android 5.0 (including 5.0.1/5.0.2) on both phones and my tablet (Nexus 9). Earlier versions of Android work fine.
In my app I want to set a global font that overrides all text. The way I've been accomplishing this prior to 5.0 was using this method. This method of font overriding doesn't seem work on any version of Lollipop that I've tried but works perfectly in 2.x and 4.x. I'm also running this code in a BaseApplication class I have so the font is only initialized in the onCreate() of my BaseApplication.
It seems like this was a bug in the Developer Preview and reported here. I tried the fix suggested in post #16 to use the TTX Tool to convert your font file to .ttx and back to .otf but that didn't seem to fix the issue like it did for others. I also verified that my .otf font file is valid and isn't corrupted according to the OTS sanitizer tool.
I also have a custom TextView that I can set the font via the layout. An example would be:
<com.myapp.widgets.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Testing text view font"
myapp:fontName="my-font.otf" />
This CustomTextView uses setTypeface(typeFace) in the view's initialization to set the font. This works on 5.0 but isn't really a possible solution I can use since I would need to go through every layout and change TextView, EditText, etc to use the CustomTextView and doesn't work with Dialog text wither.
So setting a single TextView's font with my CustomTextView class works fine in all version of Android, just not setting it globally.
I've also looked through the styles source code to see if I can find any differences between the Material and Holo theme but nothing seemed like it would change the android:typeface. My initial thoughts with this were that the Material theme somehow is overriding the android:typeface attribute over my app theme but I wasn't able to find anything.
Any thoughts or input would be greatly appreciated, thanks!
What I ended up doing to resolve this was use Calligraphy to set global fonts. https://github.com/chrisjenx/Calligraphy
Simply just add this code to the onCreate() of my custom Application class.
// Custom font file located in the "assets/fonts/"
String customFont = "Helvetica-Neue.otf";
CalligraphyConfig.initDefault(
new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/" + customFont)
.build()
);
Update as of SDK v26
If you are only supporting v26+ or using the support library, there now us a built in way to handle custom fonts throughout your app. Here is a link to the Android Developers page. The basics are as follows:
1) Add your custom font (.otf or .ttf) to your res/font directory
2) Create a new font family with the fonts files you added: res/font/your_font_family.xml
<?xml version="1.0" encoding="utf-8"?>
<font-family
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- Normal -->
<font
android:font="#font/your_font_normal"
android:fontStyle="normal"
android:fontWeight="400"
app:font="#font/your_font_normal"
app:fontStyle="normal"
app:fontWeight="400"/>
<!-- Italic -->
<font
android:font="#font/your_font_italic"
android:fontStyle="italic"
android:fontWeight="400"
app:font="#font/your_font_italic"
app:fontStyle="italic"
app:fontWeight="400"/>
<!-- Bold -->
<font
android:font="#font/your_font_bold"
android:fontStyle="normal"
android:fontWeight="700"
app:font="#font/your_font_bold"
app:fontStyle="normal"
app:fontWeight="700"/>
</font-family>
3) If you want to apply this font app-wide, create a style in your base theme style, usually located in: res/styles/style.xml
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Other app styling -->
<item name="android:fontFamily">#font/your_font_family</item>
</style>
4) If you only want to apply the font to a single or handful of views you can use the android:fontFamily attribute in XML or use:
val typeface = ResourcesCompat.getFont(context, R.font.your_font_family)
textView.typeface = typeface
This all seems to work on all versions of Android that I've tested.
i was also following this method which worked until Android 4.4 like a charm, but didn't work in Lolipop. In my case, the problem was solved (after some googling) by converting the ".ttf" font file to ".otf" I used this web for the conversion. Now it works in all versions. Hope it helps

Two xml with colors

I have two xml in folder values with colors:
colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="yellow">#23238E</color>
</resources>
second_colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="yellow">#DBDB7O</color>
</resources>
and I would like get this in TextView:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/yellow"
android:text="test"
/>
this working ok, but if i do:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#my_color/yellow"
android:text="test2"
/>
then I have error: No resource found that matches the given name (at 'textColor' with value "#android:my_colors/test")
i know - i can make:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="yellow">#DBDB7O</color>
<color name="yellow_second">#23238E</color>
</resources>
but I would like to have it in separate files.
Is this possible?
No you dont need to create a second xml file, that android only specify/use one xml for using color on different values folder. Creating a new xml asides from color.xml in the values will not register it as a color
You can go and read it from here
And states:
They contain things such as application source code and resource files.
Some are generated for you by default, while others should be created if required
The list of xml specified in that websites are the only xml that you can create if it is required by you.
You can't define two resources of the same type with the same name with the same qualifiers.
Setting a color in XML using #color/yellow is really just saying "find a color resource called 'yellow'". #color doesn't define a file name- it is declaring the resource type. Similarly, trying to use #second_color/yellow won't tell Android to look for a color named yellow in a file called second_color.xml.
When the build tools generate R, they will combine all of the <color> entries for a given configuration together. If you have a color named "yellow" defined in two separate files that still both reside in your values/ directory, you will have a problem because there can only be one color resource named "yellow."
You can define two yellow colors if they are in values/ folders with different qualifiers. For example, you could put one yellow in values-fr/ and a different one in values-en, and the first will be used for French speaking countries while the latter will be used for English speaking countries. If you do this, you still just reference the color with #color/yellow, but the system takes care of selecting the correct one for you.
If you just want two different shades of yellow for the same application in the same configuration, you should give them different names.
What do you want ? I don't understand :/
You can set color in Java :
TextView t = findViewById(R.id.yourText);
t.setTextColor(getResources().getColor(R.color.colors));
EDIT :
You have to name your colors differently. Although your colors are in differents files, they are in your unique R file so if name is the same there will be an error.
Try this :
colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="yellow1">#23238E</color>
</resources>
second_colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="yellow2">#DBDB7O</color>
</resources>
and in xml layout :
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/yellow1"
android:text="test2"/>
or
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/yellow2"
android:text="test2"/>

error: Multiple annotations found at this line:

I am implementing searchview in my app, for which I copied this options_menu.xml from a separate working project of searchview.
I get this error in res/menu/options_menu.xml
Multiple annotations found at this line:
- error: No resource identifier found for attribute 'actionViewClass' in package
'com.example.indianconstitution'
- error: No resource identifier found for attribute 'showAsAction' in package
'com.example.indianconstitution'
Code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_search" // error in this line
android:icon="#drawable/ic_launcher"
android:title="#string/action_search"
yourapp:actionViewClass="android.support.v7.widget.SearchView"
yourapp:showAsAction="always" />
</menu>
Can anyone figure out this problem?
Thanks in advance...
Looks like you copied this from a project that uses appcompat-v7 into one that doesn't.
Either include appcompat-v7, following the instructions, or just replace yourapp:x attributes with android:x attributes. If you go this way, though, you should also replace the action view class with the corresponding one in the framework (i.e. android.widget.SearchView).

Custom color not available

I am trying to add a custom Color to my project, but for some reason it is not appearing in R.color. I have added a colors.xml file as follows
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="bronze">#ffc125</color>
</resources>
and now when I try to use the new colour as follows
paint.setColor(getResources().getColor(R.color.bronze));
I get a compile error as bronze does not appear in the list. I've tried moving the definition to styles.xml but it still doesn't appear. I've also tried cleaning the project to force R to rebuild but it didn't make any difference either. Can anyone see what I'm doing wrong here?
Thanks
Remove the import of android.R and then build again.
getColor() returns int so set to the view you need to use the color
resource xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="bronze">#8C7853</color>
</resources>
setColor to the view,here i used in textview
textView.setTextColor(getResources().getColor(R.color.errorColor));

Categories