4

I am using Android Studio for an android app development. I have a style resource inside ..../src/main/res/values -

colors.xml
dimens.xml
strings.xml
styles.xml

I have a couple of styles defined in the Styles.xml file and the studio throws error saying that the style cannot be resolved where it resolves and applies to one LinearLayout in the same activity code and from the same styles.xml file. I am trying to apply another style to the Button inside that LinearLayout that works.

In the styles -

<style name="LoginButton">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_marginTop">10dp</item>
        <item name="android:layout_marginLeft">25dp</item>
        <item name="android:layout_marginRight">25dp</item>
        <item name="android:background">@color/navy_blue</item>
        <item name="android:textColor">@color/white</item>
        <item name="android:text">@string/action_sign_in</item>
        <item name="android:paddingLeft">50dp</item>
        <item name="android:paddingRight">50dp</item>
        <item name="android:layout_gravity">center</item>
</style>

And on the activity_login.xml -

<Button style="@style/LoginButton"
                    android:id="@+id/sign_in_button"
            />

The error android studio shows -

Rendering Problems NOTE: One or more layouts are missing the layout_width or layout_height attributes. These are required in most layouts. <Button> does not set the required layout_width attribute:     Set to wrap_content, Set to match_parent <Button> does not set the required layout_height attribute:     Set to wrap_content, Set to match_parent  Or: Automatically add all missing attributes   Couldn't resolve resource @style/LoginButton (6 similar errors not shown) 
Harshad Kale
  • 17,446
  • 7
  • 30
  • 44
  • mind posting some more code? – Vaibhav Aggarwal Aug 11 '13 at 22:19
  • Added the code snippet in the question – Harshad Kale Aug 11 '13 at 22:27
  • Just move `android:layout_width` and `android:layout_height` to your button. Android is not always read this fields from styles. – nfirex Aug 11 '13 at 22:40
  • @nfirex thanks, that worked but I wonder why. I have another style in the styles.xml that i have applied to the LinearLayout (the one i said in the question) and have its layout_width and layout_height properties set in styles.xml and it works for that LinearLayout then why not for the Button? – Harshad Kale Aug 11 '13 at 22:52
  • It depends on what parameters need Android for positioning element. I think this guy can fully explain it: http://stackoverflow.com/a/8698713/1001401 – nfirex Aug 11 '13 at 22:58
  • @nfirex Can you please post your comment as an Answer so that this question can be marked as solved and might help others. Thanks. – Harshad Kale Aug 11 '13 at 22:58

2 Answers2

2

Just move android:layout_width and android:layout_height to your button. Android is not always read this fields from styles. It depends on what parameters need Android for positioning element.

I think this answer may be more helpful.

Community
  • 1
  • 1
nfirex
  • 1,523
  • 18
  • 24
  • Two confusing things: 1. People report it working in emulator/device, and 2. the first example in Google's own documentation uses layout_width and layout_height: http://developer.android.com/guide/topics/ui/themes.html – Nick Spacek May 28 '15 at 15:21
  • I don't understand what you want to say me in this commentary. If google's documentation work for you - use it. If no - try to find workaround. – nfirex May 28 '15 at 16:12
-2

use android:style. You are using style which isnt defined.

EDIT: Please disregard my answer.

Vaibhav Aggarwal
  • 1,381
  • 2
  • 19
  • 30