rollover android - android

I want to make a rollover with android.
I saw that it is necesarry to write a XML like :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/verde" />
<item android:drawable="#drawable/rojo" />
</selector>
I saved it with the name button.xml.
I don't know in which folder save it.
Then I tried to used the reference in my layout.xml
I change the background attribute for my button in my layout.xml from
android:background="#drawable/red"
and use the
android:background="#drawable/button"

Save button.xml in the folder res/drawable if it's applicable for all screen sizes/devices, otherwise put it in the folder with the appropriate qualifier suffix (e.g. drawable-hdpi, drawable-ldpi etc).
See here (http://developer.android.com) for a full example, and explanation of other states you could include.

Related

Error by including drawable xml-file

Hello community,
I want to iclude different drawable xml-files into my activity_main.xml
However,when I want to includ following code :
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<size
android:layout_height="110dp" />
</shape>
</item>
</selector>
the name is button_height.xml into my main-file:
<Button
android:id="#+id/cmda1"
android:layout_width="110dp"
android:layout_height="#drawable/button_height" />
It does not work and Eclipse says only:
You must supply a layout_height attribute.
and
error in an XML file: aborting build.
I searched an answer in the internet but dont found one.
But I think its an easy mistake lots of people are doing.
so, I hope for usefull answers
and sorry for my awful english.
Your whole idea of drawable, selector and dimension is messed up. Can't put height in selector and also selectors (which are drawable) as height.
Do this.
If you want to use xml to get height
Add a file in res folder with name res/values/dimens.xml and add this line in that
<resources>
<integer name="btn_cmda_height" >110dp</integer>
</resources>
and change the button cmda1 height property as
android:layout_height="#dimen/btn_cmda_height"
Make it dynamic for various screen sizes by creating these files which will hold different values. Although this is the old way.
Read more about new way to do this here
res/values/dimens.xml
res/values-small/dimens.xml
res/values-normal/dimens.xml
res/values-xlarge/dimens.xml
If you don't want to use xml to get height
just do this android:layout_height="110dp" in the button cmda1 height property
NOTE:
Your selector code is wrong and not required at all. Selectors are used to define the background state of a view

how can I implement non-rectangular shapes for buttons in android

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

Change icon of expandable list view

I would like to change the expanded and default icon of my Expandable List View.
I did some research how to do this and found this question.
I think I did it just as it is described there.
I have 2 icons, the first is called defaulticon.png and the second one expandedicon.png. Bother are located in the drawable folder (not in drawable-hdpi).
My list view declaration looks like this:
<ExpandableListView
android:id="#+id/expandableListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:groupIndicator="#drawable/groupindicator">
</ExpandableListView>
The groupindicator is an xml file also located in the drawable folder and looks like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/defaulticon" android:state_empty="true"/>
<item android:drawable="#drawable/expandedicon" android:state_expanded="true"/>
</selector>
However the icons do not change. The default icon from the list view disappears and instead there is no icon.
Are there any restrictions with the resources (how big the resources can me (both are 160x160), where they have to be located,...).
I am testing the app on my S3 running Android 4.1.2.
Cheers
Change your icon size to 32 X 32, put your icon in drawable-mdpi and then try this code
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/expandedicon" android:state_expanded="true"/>
<item android:drawable="#drawable/defaulticon"/>
</selector>
First of all, resize your image to 24*24 and then copy it to the drawable-mdpi folder in Android. After that, copy and paste this code:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/abc"android:state_empty="true"></item>
<item android:drawable="#drawable/downarrow" ></item>
</selector>

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.

android layout load different image resources at orientation change

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".

Categories

Resources