Is lint detect overdraw issue correctly in Android studio? - android

lint shows an overdraw issue because I set a background to a linear layout
<LinearLayout
android:background="#color/red">
but even I use a custom theme in the project the lint shows the issue with Holo theme. I don't know how (no usage for Holo theme)
Possible overdraw: Root element paints background #color/red with a theme that also paints a background (inferred theme is #android:style/Theme.Holo)
I made others overdraw issues on purpose but lint could not detect it.

Related

Error using "Theme.AppCompat" with Android Studio 2.1.0

In my android project, I am getting the error "Cannot find symbol for Theme.AppCompat" while using it in styles.xml file:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
I have included the app compat dependency in my gradle file:
'compile 'com.android.support:appcompat-v7:23.3.0'
I tried finding the solution all over internet but nothing helps. Can someone help me with this..
Just wanted to let you know that you're not alone. Everything was working fine in the app I was developing, testing heavily on a marshmallow nexus 5 and a lollipop samsung tablet. I must have let something automatically update, because now I'm having problems with appcompat.
Here's an example of what's going wrong:
in a layout XML I have an edit text with style specified:
style="#style/Widget.AppCompat.EditText"
This will cause 'Error inflating class EditText'
if I create a style for the edit text in v21 styles with a parent of 'Widget.AppCompat.EditText' and specify the background (my color, or drawable, or ?android:attr/editTextStyle) it will work - though all of these options lose the edittext underline style.
If I specify background of ?attr/editTextStyle it will fail to inflate, which I assume is the default, it will fail to inflate. Likewise, if I leave any style tag off of the edit text, it will also fail to inflate.
Glad I'm not alone, but I wish this wasn't a thing...

ActionBar Material - removing space to left of custom view

I'm making a custom ActionBar for the first time. I was using the Theme.AppCompat.Light, but noticed that there is still space to the left of my custom view.
I made a test app to analyze the effect of altering different ActionBar elements, here are the comparisons.
How can I remove that annoying space?
This is what I want!
theme: Theme.Holo.Light
This is what I'm getting with material theme, LOOK AT THAT ANNOYING SPACE!
theme: Theme.Material.Light
Here are with the other toggle buttons checked
This happens because you are selecting DeviceDefaultTheme. You could use android:style/Theme.Holo.Light.DarkActionBar for action bar.
You need to add:
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp"
which app is
xmlns:app="http://schemas.android.com/apk/res-auto"

Android 5.0 Lollipop: setColorFilter "leaks" onto other buttons

I am using setColorFilter to set the color filter of ONE of my button. This has been working perfectly until the Android 5.0 Lollipop update. Now, the color filter seems to leak onto my other buttons, even when I close the activity and reopen (it resets if I close the app and reopen).
My styles.xml (v21): (same as older except here its parent is Material, before it was Holo)
<style name="Theme.FullScreen" parent="#android:style/Theme.Material.Light.NoActionBar.Fullscreen">
<item name="android:buttonStyle">#style/StandardButton</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
My styles.xml (for all versions):
<style name="StandardButton" parent="android:style/Widget.Button">
<item name="android:background">#android:drawable/btn_default</item>
</style>
My Button:
<Button
android:id="#+id/mainMenuButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="mainMenu"
android:text="#string/button_mainMenu"
android:visibility="gone" />
My code:
Button mainMenuButton = (Button) findViewById(R.id.mainMenuButton);
mainMenuButton.getBackground().setColorFilter(new PorterDuffColorFilter(getResources().getColor(R.color.light_green), PorterDuff.Mode.MULTIPLY));
mainMenuButton.setVisibility(View.VISIBLE);
The color:
<color name="light_green">#5CD65C</color>
The result:
I open the app, then the game activity and all the buttons are displaying correctly. I press the button to set the color filter, go back to the main Menu and reopen the game activity and now all buttons are green.
Any ideas?
The problem is that the background Drawable is reused across many Views. To ensure the Drawable is not shared between multiple Views you should use the mutate method.
See: mutate()
Example code:
Drawable background = mainMenuButton.getBackground();
background.mutate();
background.setColorFilter(new PorterDuffColorFilter(getResources().getColor(R.color.light_green), PorterDuff.Mode.MULTIPLY));
mainMenuButton.setBackground(background);
The instance of the drawable is shared across all your buttons, so setting a colorfilter changes all of them (you don't see the changes immediatly because the buttons are not invalidating immediatly).
Try to load the drawable manually (BitmapFactory.decodeResource(getResources(), android.R.drawable.btn_default, null)) and then set it as the button background.
OP here. Thank you for your suggestions. Using the following code fixed the problem.
Setting the filter
Drawable background = getResources().getDrawable(android.R.drawable.btn_default);
background.setColorFilter(new PorterDuffColorFilter(getResources().getColor(R.color.light_green), PorterDuff.Mode.MULTIPLY));
mainMenuButton.setBackground(background); // Use setBackgroundDrawable for API<16
mainMenuButton.setVisibility(View.VISIBLE);
The other buttons did not become green.
Clearing the filter
What happened next was, that it seemed that Android (partly) ignored my call to clear the color filter (which happens later on in the code). "Partly", because it was green, but when I pressed it, it became yellow (which is btn_default behavior as opposed to dark green with the filter). To fix this, I had to change my function call order to the following:
mainMenuButton.setVisibility(View.VISIBLE)
mainMenuButton.getBackground().clearColorFilter();
mainMenuButton.invalidate();
NOTE: This however inversed the behavior of the button. When I press (and hold) on the button, it seems to still have the green color filter behavior on it and becomes dark green. When I release it returns to being grey as per btn_default. Any suggestions on this?
I hope this at least somehow helps someone else with this strange problem, which for me only occured on Android 5.0 Lollipop API level 21. (I have tested API 8, 9, 12, 13, 16, 17, 18 and 19 where this problem did not occur. Also it doesn't seem to be ART with its AOT compilation as I expected, since enabling ART on an Android 4.4.4 device did not cause this problem.) Strangely, mutate() did not work either, as I would have expected.

what style/attr/drawable for inverse background?

I try to make a ExpandableListView where the group headers are drawn inverse. There is no problem with changing the text color, size etc. via XML. I even found out how to use the system defaults like
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
style="#android:style/TextAppearance.Medium.Inverse"
/>
But for the background color?! I don't understand why these styles don't include background color information?!
Of course I could use a direct color, but I look for good default background attributes or styles. Like "style/Background.For.TextAppearance.Medium.Inverse" ;-)
What would be a good solution? So that for the dark themed devices I get white/gray, and for the white themed I get black?
Or should I simply use R.color.background_light?
Greetings, Joerg
PS: First question here ;-) Thanx to all the people answering here the last months and years: You great people made it much more easier for me to find a re-entrance in programming after 12 years break ;-)
As you observe, the styles with "TextAppearance" in their name only affect the foreground text attributes of the view. They are appropriate for the android:textAppearance attribute. The styles with "Widget" in their names define all the UI properties and will work in a style attribute, but Android doesn't define a "Widget.TextView.Inverse" style.
When I wanted to display an console-like log as an inverse text view, I used the following XML:
<TextView
android:textAppearance="#style/TextAppearance.AppCompat.Small.Inverse"
android:background="?android:colorForeground"
... />
It uses the theme's foreground color as the background for the view. With a dark theme it displays dark text on white, and in a light theme it displays light text on black.

Style hierarchy in Android - what is the order of importance?

I have an android application that I'm working on with a custom theme applied to it in the android manifest on the main activity itself. This activity creates a listview, which has the style applied to it.
If I create a custom layout for that listview, including a separate xml file for the rows themselves, and I apply styling directly to those layouts, does this style overwrite the overall style for the application? I'm trying to get a grasp on the hierarchy of events as far as how styling and themeing works.
The way I'm GUESSING it works in my example is: apply style for row, referenced by row layout xml > apply style for overall listview, referenced by listview layout xml > apply style from custom theme, referenced by style xml referenced by android manifest
Am I right? Or am I approaching this incorrectly. (just for confusions sake, by ">" I mean has a greater importance than)
If you've specified the same attributes in multiple places, the list below determines which attributes are ultimately applied. The list is ordered from highest precedence to lowest:
Applying character- or paragraph-level styling via text spans to TextView-derived classes
Applying attributes programmatically
Applying individual attributes directly to a View
Applying a style to a View
Default styling
Applying a theme to a collection of Views, an activity, or your entire app
Applying certain View-specific styling, such as setting a TextAppearance on a TextView
Hope I am understand your question right here...
The styles you define in styles.xml will always overwrite the styles coming from the theme currently used by android.
But this only works for the attributes you overwrite.
If you leave an attribute untouched, android will provide the style for it, and sometimes this comes bite you in the butt :)
This system is best described like this:
A textview requires an attribute example
<item name="android:textColor">#00FF00</item>
Android will first look in the original layout.
If not found, it will look into your custom styles.
If not found, it will look into android styles.
Hope this helps.
The standard themes have lines like which define the ListView style:
<item name="listViewStyle">#android:style/Widget.ListView</item>
In your own theme you can do a
<item name="listViewStyle">#style/MyOwnListView</item>
Something that is not defined in the ListView style (own or default) will be what is defined in the theme if you have defined it there.

Categories

Resources