<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/off_background">
<ImageView
android:id="#+id/bottom_layer"
android:src="#drawable/handle"/>
<ImageView
android:id="#+id/upper_layer"
android:src="#drawable/switch_v"/>
</FrameLayout>
whenever this code is executed:
inflater.inflate(R.layout.settings_switch, this);
I get this error:
10-29 13:27:00.090: E/AndroidRuntime(22364): Caused by: java.lang.RuntimeException: Binary XML file line #7: You must supply a layout_width attribute.
how can it be?
I have
android:layout_width="match_parent"
android:layout_height="match_parent"
Add these attributes to imageview's
android:layout_width="wrap_content"
android:layout_height="wrap_content"
// can use dp like 20dp instead of wrap_content
All view groups include a width and height (layout_width and layout_height), and each view is required to define them.
wrap_content tells your view to size itself to the dimensions required by its content
fill_parent (renamed match_parent in API Level 8) tells your view to become as big as its parent view group will allow.
In general, specifying a layout width and height using absolute units such as pixels is not recommended. Instead, using relative measurements such as density-independent pixel units (dp), wrap_content, or fill_parent, is a better approach, because it helps ensure that your application will display properly across a variety of device screen sizes. The accepted measurement types are defined in the Available Resources document.
Check the link for more info
http://developer.android.com/guide/topics/ui/declaring-layout.html
Add android:layout_width and android:layout_height to both ImageView's
like this
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/off_background">
<ImageView
android:id="#+id/bottom_layer"
android:src="#drawable/handle"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="#+id/upper_layer"
android:src="#drawable/switch_v"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
If you meet this problem. you ImageView\TextView\Button. they lack of the attribute layout_with or layout_height.
i met this problems when i modified my values/styles.xml/. Because i removed the attribute of android:layout_width.
The original:
<style name="blue_btn_style">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">45dip</item>
<item name="android:layout_marginLeft">15dip</item>
<item name="android:layout_marginRight">15dip</item>
<item name="android:layout_marginTop">30dip</item>
<item name="android:layout_marginBottom">30dip</item>
<item name="android:background">#drawable/blue_btn_selector</item>
<item name="android:textColor">#android:color/white</item>
<item name="android:textSize">#dimen/btn_text_size</item>
<item name="android:gravity">center</item>
</style>
what i removed the attribute.
<style name="blue_btn_style">
<item name="android:background">#drawable/blue_btn_selector</item>
<item name="android:textColor">#android:color/white</item>
<item name="android:textSize">#dimen/btn_text_size</item>
<item name="android:gravity">center</item>
</style>
so,my code is warning those
[08-11 18:32:33.525: E/AndroidRuntime(2306): java.lang.RuntimeException: Binary XML file line #27: You must supply a layout_width attribute.]
The problem given here leads directly to the line in the XML causing the problem. This error can also happen if a theme is applied to an UI element and the theme is not defining the dimension, here the layout_width. E.g. this occurred to me constructing an AlertDialog and defining in a style file:
<style name="FooBar.DialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">#color/colorPrimary</item>
</style>
<style name="FooBar" parent="Theme.AppCompat.Light.NoActionBar">
<item name="alertDialogTheme">#style/FooBar.DialogTheme</item>
<!-- ... -->
</style>
Adding:
<style name="FooBar.DialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">#color/colorPrimary</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
solved the issue.
Related
Styles.xml:
<style name="windowTitleBackgroundStyle">
<item name="android:background">#A4D05F</item>
</style>
<style name="windowTitleStyle">
<item name="android:drawableLeft">#drawable/smartlogo</item>
<item name="android:textColor">#C6C09C</item>
<item name="android:padding">12dip</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">16sp</item>
</style>
I am customizing my titlebar in android using eclipse ide. I can also see my image that is being set by the drawableleft, however it is too big. Do you know how to set the width and size of the drawableleft?
you can use a xml-drawable, that you can customise as you wish.
You can find here some examples and a lot others on net.
Alternatively, you can also create a simple XML, containing a view, image etc, like this:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="#drawable/icon"
android:width="#dimen/icon_size"
android:height="#dimen/icon_size"
/>
</layer-list >
,and use this as your drawableLeft.
Hope this helps!
if API level 23 and higher you can change like this
<item
name="android:drawableLeft"
android:drawable="#drawable/smartlogo"
android:height="24dp"
android:width="24dp"/>
I'm using background image in my toolbar, and from some reason the toolbar is working only with mipmap and not with drawable.
In my app_bar_main.xml file:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#mipmap/bar"
app:popupTheme="#style/AppTheme.PopupOverlay" />
As you can see I've used mipmap instead of drawable, though when I'm using drawable the application crashes and the following error is given:
Binary XML file line #11: Binary XML file line #16: Error inflating
class android.support.v7.widget.Toolbar
Must mention this mipmap using is just making my app very slow, and it skipping frames, so I would like to change it to drawable.
How can I change the toolbar background resource of the image from mipmap to drawable?
EDIT:
Style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="LightDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:textColor">#android:color/primary_text_light</item>
<item name="background">#drawable/bar</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
Seems your xml file is perfect.
What I suggest ,Just resize that image or prepare background image with different different size, which you want to set in to your toolbar and put it in to different drawables / mipmap folders.
android:background="#mipmap/bar"
as we know icon size should be ,
drawable-ldpi (120 dpi, Low density screen) - 36px x 36px
drawable-mdpi (160 dpi, Medium density screen) - 48px x 48px
drawable-hdpi (240 dpi, High density screen) - 72px x 72px
drawable-xhdpi (320 dpi, Extra-high density screen) - 96px x 96px
run your app again and do let me know .
toolbar.xml
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/MyToolbarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
Themes/Style
<style name="MyToolbarStyle">
<item name="android:maxHeight">#dimen/abc_action_bar_default_height_material</item>
<item name="android:background">#color/primary</item>
<item name="popupTheme">#style/ThemeOverlay.AppCompat.Light</item>
<item name="titleTextAppearance">#style/Theme.Toolbar.Title</item>
<!-- No need for colorPrimary, colorPrimaryDark, colorAccent here
this should go to the AppTheme -->
Try this methos,it works..
Please add this code in your app level build.gradle file
android {
defaultConfig {
.....
vectorDrawables.useSupportLibrary = true
}
}
toolbar_default.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
style="#style/ToolBarStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="#dimen/abc_action_bar_default_height_material"/>
ToolBarStyle
<style name="ToolBarStyle" parent="">
<item name="popupTheme">#style/ThemeOverlay.AppCompat.Light</item>
<item name="theme">#style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
</style>
Other
Please remove below line from you toolbar and try to check it work or not.I am not sure but you just check it.
app:popupTheme="#style/AppTheme.PopupOverlay"
I hope this will help you
Im using ActionBar in my Android App, and to customize each of the tabs, i put a background image behing the ActionBar, which looks the following:
On my phone actually it looks fine, but on other phones it's look very squashed. How can i scale the ActionBar proportionally based on the background's ratio? Are there any solution for this? I had to determine the height in fix DPI because if i leave it out, the ActionBar is not even showing up. My style code is the following right now;
<style name="MyActionBar" parent="android:Theme.Holo.Light">
<item name="android:backgroundStacked">#drawable/tab_bg</item>
<item name="android:adjustViewBounds">true</item>
<item name="android:scaleType">fitEnd</item>
<item name="android:height">87dp</item>
</style>
Update:
I just do not know where to Mr.T's xml code. Im using the basic android ActionBar, and i have only one xml named menu.xml, but if i put the code into it, no effect.
I tried to style the actionbar with the following, but still, if i do not specify the android:height parameter, the actionbar remains 0 and do not showing up.
<style name="MyActionBar" parent="android:Theme.Light">
<item name="android:backgroundStacked">#drawable/tab_bg</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:src">#drawable/tab_bg</item>
<item name="android:scaleType">matrix</item>
</style>
The actionbar still looks like the following on some devices;
http://i.imgur.com/yQN67J3.png
You might do it with either scaleType(http://developer.android.com/reference/android/widget/ImageView.ScaleType.html)
You use ImageView instead of background placing it as the first layout's element and specify android:scaleType attribute for it:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/backgrnd"
android:scaleType="matrix" />
...
rest layout components here
...
</RelativeLayout>
or drawable xml
I have an xml file on my PC. Something like this:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/title_text"
style="?textTitle"/>
In attrs.xml is defined:
<attr name="textTitle" format="reference" />
In an style.xml file is written:
<style name="text_title_bl">
<item name="android:textColor">#FFF</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">24sp</item>
<item name="android:gravity">center_vertical|left</item>
<item name="android:paddingLeft">5sp</item>
<item name="android:shadowDx">1.0</item>
<item name="android:shadowDy">1.0</item>
<item name="android:shadowRadius">1</item>
<item name="android:shadowColor">#DDD</item>
<item name="android:background">#FF004488</item>
</style>
My question is how does it work, that eclipse knows that "?textTitle" belongs to this style above? Or why is "?textTitle" = "text_title_bl"? It is an example which works with multiple themes.
Thank for your help:)
The ? means "look this up in the current theme, and use that value". In this case, that value from the theme is then used as the style name.
Your theme has an entry with the key "textTitle" and the value "text_title_bl". If you changed this in your theme, the textview would use a different style.
How can I set the star color of the ratingbar? I want yellow stars.
The simpliest way:
android:progressTint="#color/color"
Smooth and shiny.
This worked for me:
Drawable drawable = ratingBar.getProgressDrawable();
drawable.setColorFilter(Color.parseColor("#FFFDEC00"), PorterDuff.Mode.SRC_ATOP);
I've found that just setting the progressTint is not a complete solution. It will change the color of the star but not on partial star fills if your stepSize is less than 1. In my experience you also need to set the secondaryProgressTint in order to stop the default star tint from rearing its ugly head. Here is my code:
android:progressTint="#color/accent"
android:secondaryProgressTint="#android:color/transparent"
Hope this helps someone in my situation.
Have you tried an xml theme, such as
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#android:id/background"
android:drawable="#drawable/star_empty_show"/>
<item
android:id="#android:id/secondaryProgress"
android:drawable="#drawable/star_empty_show"/>
<item
android:id="#android:id/progress"
android:drawable="#drawable/star_filled_show"/>
</layer-list>
calling it in your xml as
<RatingBar
android:id="#id/rate"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5.0dip"
android:isIndicator="true"
android:max="5"
android:numStars="5"
android:progressDrawable="#drawable/ratebar_theme"
android:stepSize="0.1" />
and using your own drawables?
You do need 3 star images (star_filled_show.png, star_half_show.png and star_empty_show.png) and one xml, that's all.
Put these 3 images into res/drawable.
Put there the following ratingbarstars.xml:
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#android:id/background"
android:drawable="#drawable/star_empty_show"/>
<item
android:id="#android:id/secondaryProgress"
android:drawable="#drawable/star_half_show"/>
<item
android:id="#android:id/progress"
android:drawable="#drawable/star_filled_show"/>
</layer-list>
Tell your ratingbar definition to use this drawable
<RatingBar android:progressDrawable="#drawable/ratingbarstars.xml"/>
The rating bar is used automatically at run time for change color on touch star.
First add style in app\src\main\res\values\styles.xml file:
<style name="RatingBar" parent="Theme.AppCompat">
<item name="colorControlNormal">#color/duskYellow</item>
<item name="colorControlActivated">#color/lightGrey</item></style>
Then your rating bar add theme like this:
<RatingBar
android:id="#+id/rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1"
android:theme="#style/RatingBar"/>
It's a little complicated at the mentioned blog, I've used a similar but simplier way. You do need 3 star images (red_star_full.png, red_star_half.png and red_star_empty.png) and one xml, that's all.
Put these 3 images at res/drawable.
Put there the following ratingbar_red.xml:
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background" android:drawable="#drawable/red_star_empty" />
<item android:id="#android:id/secondaryProgress" android:drawable="#drawable/red_star_half" />
<item android:id="#android:id/progress" android:drawable="#drawable/red_star_full" />
</layer-list>
and, finally, tell your ratingbar definition to use this, i.e.
<RatingBar android:progressDrawable="#drawable/ratingbar_red"/>
That's it.
try this one
RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
stars.getDrawable(2).setColorFilter(Color.YELLOW,PorterDuff.Mode.SRC_ATOP);
When I did it, and I had to put the TintMode too.
<RatingBar
android:progressTint="#color/colorAccent"
android:progressTintMode="src_atop" ---- This is important!
android:secondaryProgressTint="#color/colorPrimary"
android:secondaryProgressTintMode="src_atop" ---- This is important!
android:progressBackgroundTint="#color/black_alpha"/>
only for api version 21 and above
<RatingBar
android:id="#+id/rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="#style/RatingBar"/>
with theme in styles
<style name="RatingBar" parent="Theme.AppCompat">
<item name="colorControlNormal">#color/gray</item>
<item name="colorControlActivated">#color/yellow</item>