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.
Related
Here's the XAML that I am using for a startup screen:
<?xml version="1.0" encoding="UTF-8" ?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<item android:drawable="#android:color/black"/>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Test"
android:gravity="center"
android:layout_gravity="center"
android:textSize="40sp"
android:textStyle="bold"
android:fontFamily="sans-serif-medium"
android:textColor="#fff"
/>
</layer-list>
I was hoping to see white text centered on a black screen. The screen is black, but instead I cannot see any text appear at all.
I understand what you are trying to do.
You want to display a plain Text on your "Splash Screen" that displays when you open an app. Since you are using Xamarin, you have access to all the native API's and you can do everything that native developers can.
Unfortunately, being able to show plain text on the splash screen is not possible in native development. Android restricted it because "the view comes from the theme. When you set up the UI for your splash activity in the theme, it is available immediately".
Why can't I have plain text on the splash screen, when I can have images? As you see over here in detail, the splash screen only accepts drawable resources. And there is no way to convert plain text into XML drawable resources.
You do have another option though. You can use a "Loading screen". You can make the app show the splash screen, then a Loading screen, and then load the right page. Let me know if that makes sense
Set background attribute to black. Either
android:background="#000"
or
android:background="#android:color/black"
I'm not used to Xamarin in specific, but if you normally want to create some sort of splash screen for your activity, as far as I know, there is no way of directly adding a TextView to it, because LayerList only supports bitmaps (image drawables) as child items. So you would have to create an .img file containing your text. Then, you can add the image with the text to the layer-list like this:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- This line below is for the background colour -->
<item android:drawable="#000" />
<item>
<!-- The bitmap below contains the image with the text -->
<bitmap
android:src="#drawable/my_text_on_screen"
android:gravity="center" />
</item>
</layer-list>
To see how to create an image from a text, have a look at this StackOverflow question.
Let me know of this is what you wanted to achieve! I hope this helps.
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 :)
hi i have to realize this layout . it has this layout.
I could try to use the icons as imagebuttons but the active state of a button is somewhat like this one !
How should i proceed with this ?
You should use selector as follows:
Prepare 2 images for button states, and put it into res/drawable folder.
button_normal_green.png – Default image button.
button_pressed_yellow.png – Display when button is pressed.
Now, create a new XML file in “res/drawable/” folder, in whatever name you want, in this case, we just give a name as “new_button.xml“. This file defined which button state is belong to which image.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_pressed_yellow" android:state_pressed="true" />
<item android:drawable="#drawable/button_normal_green" />
</selector>
3.set background to button
<ImageButton
android:id="#+id/imageButtonSelector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/new_button" />
Have a look at Complete Example
I am getting crazy with switch buttons in Android 4.
First of all, I have set a selector as background with two different PNG files selected from drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/form_switch_no" android:state_checked="false"/>
<item android:drawable="#drawable/form_switch_ok" android:state_checked="true"/>
<item android:drawable="#drawable/form_switch_no"/>
</selector>
but I don't know why they are x-scaled. I want to manage this size, but I have tried to change layout_width/height/weight... without succes.
I have also a custom PNG file to be used as the thumb of the switch. I also need to manage the size of this thumb, because if I set textOn and textOff propieties as "", thumb gets a very small size. Moreover, I would like to change thumb padding in respect of the background, because It does not appears centered.
Here is my Switch XML definition:
<Switch
android:id="#+id/switchUsers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/user"
android:textOn=""
android:textOff=""
android:thumb="#drawable/form_switch_slider"
android:track="#drawable/switch_user_selector" />
I have tried to change image sizes and making them nine-patch ones, but I can't reach my target.
Any help will be appreciated. Thank you!
The solucion for the extremely small slider with no text was solved with a combination of:
android:thumbTextPadding="25dp"
android:textOn=""
android:textOff=""
Then, the problem of the small padding in the same slider was a 9-patch image problem. I had to extend bottom and right black lines one more pixel simetrically.
Hope this helps someone with the same problem.
I would like to have a solution in xml or a keyword for what to search to solve my problem:
I have multiple screens. Each screen has his own xml in layout folder. For eg a part of the code :
<RelativeLayout
android:id="#+id/headerLayout"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:background="#drawable/header" >
<Button
android:id="#+id/btBack"
android:layout_width="55dp"
android:layout_height="33dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dip"
android:layout_marginTop="10dip"
android:background="#drawable/bt_back"
/>
</RelativeLayout>
now, I would like to change the "#drawable/header" value runtime when is a rotation, prefferable to configure and Android change not me in Java code. The same thing is done with
"#drawable/bt_back", which is a bt_back.xml file at drawable folder and has a selector. His content is here:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- selected state -->
<item android:drawable="#drawable/bt_back_pressed" android:state_pressed="true" android:state_selected="false"/>
<item android:drawable="#drawable/bt_back_pressed" android:state_pressed="false" android:state_selected="true"/>
<item android:drawable="#drawable/bt_back_pressed" android:state_pressed="true" android:state_selected="true"/>
<!-- unselected state (default) -->
<item android:drawable="#drawable/bt_back_normal"/>
</selector>
Now in folder what I want drawable-hdpi, drawable-ldpi, I just need to put those 2 files and the android system is auto-handling the image changes for pressed and normal state depending on user device dpi.
The same thing I would like to have for header for rotation from portrait and landscape. Eg it should alternate the header_port.png to header-land.png.
How is possible? Any ideas?
I don't wand different .xml layout for landscape, nor digg in java code.
Maybe in different folder the resources for header, described here ? port and land?
You are able to create a drawable-land-hdpi directory. Put your landscape versions in the qualified folders and android should automatically load them for you.
To elaborate on #toadzky's answer, you can create a different drawable directory for any qualifier that you find on this Android Developer page, and put images with the same name in each, and android will automatically know which one to use. The same is true for layouts in the layout (or layout-land, etc.) folder, value xml files (e.g., string resources--useful for localization--or themes), in the values folders, or any other things you normally find in the res folder.
It's also important to note that the way its precedence works (with respect to which directory it picks for resources) is, if I recall correctly, "first match after excluding all incompatible directories".
So, if you had only drawable-hdpi and drawable, and you had an mdpi device, it would eliminate all directories qualified with hdpi, and select from the remaining directories (so you'd end up with the version from the drawable folder). Further explanation is found on the linked page, under "How Android Finds the Best-matching Resource".