Android Studio drawable selector unknown issue - android

I seem to have a problem that I just do not know how to solve with Android Studio. The code worked find on Eclipse and the weird thing is that it also works on the Design preview window of Android Studio, but does not work when I execute the application on any real device.
The problem I'm having is with the drawable folder where I have a selector xml and a shape xml file. In my layout, I set the background of the button to the selector xml file. I noticed a very annoying red circle with an exclamation mark in the selector xml file where I specify the drawable to be the shape xml file. It doesn't state what the problem is, if any. It looks just like the following picture:
The Design preview window shows the button as specified in the drawable xml files:
Here is the code for my files:
bshape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="#FFAA33"/>
<corners android:radius="5dp" />
</shape>
button.xml (selector)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Normal state of button -->
<item
android:state_enabled="true"
android:drawable="#drawable/bshape" />
</selector>
myLayout (RelativeLayout)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Button Test"
android:textColor="#000000"
android:background="#drawable/button"/>
</RelativeLayout>
I've made sure the xml files have the .xml extension. I've noticed several posts regarding missing the extension in the xml file.
This is really driving me nuts. I've spent about 3 hours attempting to solve this problem and I just don't know what it could be. Any help is extremely appreciated. Thank you in advanced!
Edit:
Below is a picture of all the files listed in my project.

First, I want to thank Eric for his attempt at providing a solution to my problem. Thank you very much Eric!
It seems to be working, however the red circle with the exclamation point still shows in the button.xml selector file.
This is what I did, I used the Design view to select the button and under the Properties pane, I clicked on background ... button and the Resources window came up. I selected the Project tab, then I searched for Drawables->button and clicked OK.
I performed a 'Clean Project' by going to Build->Clean Project. Then I build the project by executing Build->Rebuild Project in the Menu bar.
Now it seems to be working as expected. I believe this could very well be a bug in Android Studio. There goes 4hrs of my life I will never get back :)

Related

How to make a button with rounded edges?

what is the essence of the question:
Why is the color that I write in solid in the shape not taken into account
Why do I have a white background on the back of the button now (attached a photo)? I don't understand where I can fix it
I found a video on YouTube and there was the following solution: Create an xml file in drawable and there I specified the following:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#color/teal_700"></solid>
</shape>
</item>
</selector>
The problem is that when the videos came out, there were no themes and everything worked out perfectly right away, and I tried to figure out why I didn't apply color to the button if I set it in the shape, but I never figured it out and it doesn't take into account my color that I set in solid :) I decided to change the primaryColor in the theme to the color I needed and then it applied.
And can you also throw, please, useful modern resources to the latest versions of android studio? I'm just learning, so I haven't found much yet and it would be interesting to see some examples for new updates, because all the videos are from earlier versions and some of them don't converge, as, for example, now in the question
If there are not enough details to solve the problem, then write to me
To make A Button Round Use MaterialButton
<com.google.android.material.button.MaterialButton
android:layout_width="100dp"
android:layout_height="50dp"
android:text="1"
app:cornerRadius="18dp" />

gradient color is not applying on button in android studio

I am trying to set gradient color in Button (in android studio whose version is Arctic Fox).
gradientbuttonmainactivity.xml
I made this file in Drawable.
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#93501C"
android:endColor="#A67E7E"
android:type="linear"
android:angle="360"/>
<corners
android:radius="25dp"/>
</shape>
activity_main.xml
<Button
android:id="#+id/btnR"
android:layout_width="275dp"
android:layout_height="55dp"
android:layout_marginStart="55dp"
android:layout_marginTop="25dp"
android:background="#drawable/gradientbuttonmainactivity"
android:text="#string/register" />
but Button color is as it was, corners values applied but gradient values did'nt.
I searched it on google but didn't found any working solution.
In one stackoverflow answer, a writer said to change the colorPrimary (in theme.xml) with #null. I done this , and gradient color applied, but on Run, the app failed to open in the smartphone, however, it launched successfully.
So, the question is clear, How do I make the button with gradient color ? Thank You.
As an alternative, try using
androidx.appcompat.widget.AppCompatButton
instead of
Button
it might be a recent change with Button and Android's material design, I've noticed on older projects what you have implemented works with Button but on a more recent project, that background is not working

Button in android ignore background drawable resources

So, I am developing an android application, and across all the layouts that I have created so far, I have buttons, not AppCompatButton or Material Button just plain simple button which has the following attributes set:
<Button
android:id="#+id/logout"
android:layout_margin="8dp"
android:text="#string/log_out_button"
android:textColor="#000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/rounded_buttons"
app:layout_constraintBottom_toBottomOf="parent"/>
rounded_buttons.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFF"/>
<corners
android:radius="8dp"/>
</shape>
This throws a fidelity warning:
Paint.letterspacing() not supported.
The weird part is this xml when referenced in layouts created earlier works as intended.
I appreciate any help you could provide.
If it helps this started after I installed a third party plugin to import material icons:
Material Icon Generator
I have since disabled this plugin, invalidated caches and restarted the ide, somewhere on SO, it suggested to change the theme, tried that to no avail, so changed it back to the one I was using.
This is pretty embarrassing, All I had to do was change the theme in the preview from
Material Components to AppTheme.
Pretty stupid to not have figured this out earlier.
Also the Material Icon generator is pretty great and did not cause this issue.

Graphical layout won't show my button background

I have a button background described as follows in its own "custom_easy_but.xml" in a directory res/drawable as follows:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/but_easy_p"
android:state_pressed="true"/>
<item android:drawable="#drawable/but_easy" />
</selector>
Then I have a layout called modeselect.xml which includes the following code:
<Button
android:id="#+id/easy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:background="#drawable/custom_easy_but"
android:text="#string/Easy" />
then I have a set different sized images in files called but_easy.png and but_easy_p.png in separate directories res/drawable-large and res/drawable-normal. The code compiles, runs and displays exactly the right button background images on a variety of phones... but if I look at the file modeselect.xml using eclipse and switch to the "graphical layout" view, I do not see the background images at all, and underneath the graphical view I see
failed to parse file c:\blah\blah\res\drawable\custom_easy_but.xml
and
couldn't resolve resource #drawable/but_easy_p
How can it be that the real phones can sort out all the xml but eclipse can't?
Be sure you have set the correct Screen size in the graphical Editor. If You had chosen a screen size, where You got no resources, Your layout will not be shown.

Nine-patch StateListDrawable rendering issue

After updating to SDK tools revision 15 from revision 12, my EditTexts and Buttons all appear distorted like so...
Now is this a bug with the revision? Because when I preview what the layout looks like with the "Graphical Layout" tab it appears normal. But as soon as it is compiled and put on my phone or the emulator, it is distorted.
Here is the StateListDrawable xml for the buttons. Each drawable referenced in this is a 9patch. The EditTexts are done in the same fashion.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_grey_pressed" android:state_pressed="true"></item>
<item android:drawable="#drawable/button_grey_pressed" android:state_focused="true"></item>
<item android:drawable="#drawable/button_grey_pressed" android:state_selected="true"></item>
<item android:drawable="#drawable/button_grey_default"></item>
</selector>
This is an issue I've had too when I updated to SDK 14.
I found a solution in Window>Preferences>Android>Build by setting "Build output" on Normal and then Project>Clean>Clean All.
I've copied and pasted from here and it worked:
Disable auto-refresh, build automatically.
Full clean all projects.
Build all projects.
Clean the main project.
Build the main project.
Try wrapping those nine-patch drawables (I suppose #drawable/button_grey_pressed refers to /drawable/button_grey_pressed.9.png) into xml nine-patch drawables:
<?xml version="1.0" encoding="utf-8"?>
<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/button_grey_pressed"
android:dither="true" />
You will have to give this xml file the name which will be different from that of button_grey_pressed.9.png, let's say button_grey_pressed9.xml

Categories

Resources