I'm new to Android programming and want to create my own piano app.
I use absoluteLayout with dp - or is there a better way?
I have buttons declared like this
<Button
android:id="#+id/d3"
android:layout_width="55dp"
android:layout_height="200dp"
android:layout_x="55dp"
android:background="#drawable/selector_button"
android:scrollbars="horizontal"
android:gravity="bottom|center_horizontal"
android:paddingBottom="20dp"
android:textSize="60dp"
android:textColor="#ff4444" />
selector_button
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/white_pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/key" />
</selector>
The problem is that the white key when pressed down draws over the black one
here's a screenshot
I've tried the bringToFront-method on the black key, an the elevation option on it, but haven't been able to solve the problem.
Do you have a hint?
Related
I have a really weird issue, on every device that I test my application the edittext behaves as you'd expect, the text is highlighted and it is black on a white background.
Specifically on Lenovo Yoga tablets when the edittext is focused the text will become black and blend in with the background.
The layout for this is:
<RelativeLayout
android:id="#+id/ip_layout"
android:layout_below="#id/display_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip">
<LinearLayout
android:layout_marginBottom="10dip"
android:background="#8A8A8A"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="1dp"
android:layout_width="match_parent" />
<EditText
android:layout_marginTop="10dip"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="false"
android:hint="#string/Hostname"
android:singleLine="true"
android:imeOptions="actionNext"
android:background="#android:color/transparent"
android:id="#+id/host_address"
android:maxLength="50" />
</RelativeLayout>
If it is a problem on a particular device it may be due to the devices custom theme. Recent Android devices need to support the Holo theme and you can explicitly ask for that theme in your manifest using:
<style name="AppTheme" parent="android:Theme.Holo" />
You'll have to create your own nine patch image and use that as the background of your EditText otherwise befaviour of Edittext will chnage with device and version of android.
If you want to create more effects for edittext backgrounds like pressed, focused and default state then you need to create a selector for background image of edittext:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="#android:drawable/Epressed"/>
<item
android:state_focused="true"
android:drawable="#drawable/Efocused"/>
<item
android:drawable="#android:drawable/Edefault"/>
</selector>
I have designed my own graphics composed of two images for an Image Button - one focused and one unfocused. I have the following button in the layout and XML file for it in drawable folder:
<ImageButton
android:id="#+id/btnPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
style="?android:attr/borderlessButtonStyle"
android:src="#drawable/btn_play" />
.
<?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/ic_playfocused" />
<item android:state_focused="true" android:drawable="#drawable/ic_playfocused" />
<item android:state_selected = "true" android:drawable = "#drawable/ic_playfocused" />
<item android:drawable = "#drawable/ic_playdefault" />
</selector>
When I click the button, it switches the two images properly, but the problem is that I also see a partially transparent blue rectangle that normally highlights button clicks. How can I get rid of this blue highlight so that when my button is clicked, the only thing that happens is the switch between the two images?
Thank you in advance :)
Ok so just set the button background to transparent, worked for me just now.
<ImageButton
android:id="#+id/btnPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#android:color/transparent"
style="?android:attr/borderlessButtonStyle"
android:src="#drawable/btn_play" />
Is it possible to create a custom rating bar like this?
If yes, can you give me an example on how to do it?
Thanks in advance!
Great tutorial here.
I will copy important parts in this answer in case the link will be invalid.
First you should create a style which extends the original RatingBar
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="foodRatingBar" parent="#android:style/Widget.RatingBar">
<item name="android:progressDrawable">#drawable/food_ratingbar_full</item>
<item name="android:minHeight">48dip</item>
<item name="android:maxHeight">48dip</item>
</style>
</resources>
Then you need to provide 3 drawables, why? Because you should fill the 3 cases: empty, 50% and full.
This file will be food_ratingbar_full.xml
> Here’s an example of a filled rating (cookie):
<!-- This is the rating bar drawable that is used to
show a filled cookie. -->
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:state_window_focused="true"
android:drawable="#drawable/cookie" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="#drawable/cookie" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="#drawable/cookie" />
<item android:drawable="#drawable/cookie" />
</selector>
I just use one image for all states (and it actually looks decent),
but as you can see from the selector, there are four different states
possible (#drawable/cookie is finally an actuall cookie png image).
And the cool thing here is that RatingBar component will automatically
fill in part of the cookie when needed based only on “full” and
“empty” images (if you support half ratings, as in my example image).
Then to use your style you should just add style attribute in RatingBar XML.
style="#style/foodRatingBar
The point is: you should create a custom style if you want.
Then you could use setRotation to rotate it.
Sets the degrees that the view is rotated around the pivot point. Increasing values result in clockwise rotation.
EDIT: This is not an appropriate answer, as OP wanted to customize RatingBar. But it is still a solution.
It is possible. You just have to put each one of the five image resources in your project and then, make a layout like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="60dp"
android:layout_height="match_parent"
android:background="#android:color/black"
android:gravity="left"
android:orientation="vertical"
android:weightSum="5" >
<ImageView
android:id="#+id/oneStar"
android:layout_width="50dp"
android:layout_height="0dp"
android:layout_weight="1"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/twoStars"
android:layout_width="50dp"
android:layout_height="0dp"
android:layout_weight="1"
android:src="#drawable/ic_launcher"
android:visibility="visible" />
<ImageView
android:id="#+id/threeStars"
android:layout_width="50dp"
android:layout_height="0dp"
android:layout_weight="1"
android:src="#drawable/ic_launcher"
android:visibility="visible" />
<ImageView
android:id="#+id/fourStars"
android:layout_width="50dp"
android:layout_height="0dp"
android:layout_weight="1"
android:src="#drawable/ic_launcher"
android:visibility="visible" />
<ImageView
android:id="#+id/fiveStars"
android:layout_width="50dp"
android:layout_height="0dp"
android:layout_weight="1"
android:src="#drawable/ic_launcher"
android:visibility="visible" />
Put your resources as source of your ImageViews. That should be a start for you :D
I would like to style the buttons in my Android application like this:
Whats the easiest way to create the buttons like above?
Currently I simply have a rectangle, here is the xml:
<Button
android:id="#+id/Main_DownloadCenter"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="25dp"
android:layout_marginLeft="90dp"
android:layout_marginRight="90dp"
android:background="#drawable/main_button_about_state"
android:text="txt" >
</Button>
What I need to do now is add the extra small rectangles that are on both sides of the main big rectangle. Meaning add a small_rect_grey and after that small_rect_aqua .. how can I achieve that?
Check out the Styling Your Button section:
http://developer.android.com/guide/topics/ui/controls/button.html#Style
Mainly borderless and custom background
button_custom.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/button_focused"
android:state_focused="true" />
<item android:drawable="#drawable/button_default" />
</selector>
activity_main.xml
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button"
android:onClick="action"
android:background="#drawable/button_custom" />
How can I remove the border with appears when focusing an EditText View?
I need it cause this view has little space in the screen, but without the border it's enough. When running on Emulator an orange border appears, on Device a blue one.
Have you tried setting the background of the EditText to a transparent colour?
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/hello"
android:background="#00000000"
/>
<EditText
android:id="#+id/edittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:drawable/editbox_background_normal"
/>
It is possible. However I would not recommend it because users are used to certain metaphors and you should not change the general UX.
You can apply different styles to your views. In your case it sounds like you want an EditText View element which looks like a TextView element. In this case you would have to specify other backgrounds for the EditText depending on the state of the View element.
In your desired layout.xml you assign a background to your EditText:
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/hello" android:background="#drawable/custom"
/>
Then you create the custom.xml in your drawable folder and add the following:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/textfield_default" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="#drawable/textfield_disabled" />
<item android:state_pressed="true" android:drawable="#drawable/textfield_default" />
<item android:state_enabled="true" android:state_focused="true" android:drawable="#drawable/textfield_default" />
<item android:state_enabled="true" android:drawable="#drawable/textfield_default" />
<item android:state_focused="true" android:drawable="#drawable/textfield_disabled" />
<item android:drawable="#drawable/textfield_disabled" />
</selector>
Those are the possible states of your EditText View element. Normally you can access Android platform drawables directly by using #android:drawable/textfield_default, but in this case the textfield drawables are private so you have to copy them into your own drawable folder. The original resources can be found in your SDK installation folder at ANDROID_HOME\platforms\android-(API LEVEL)\data\res\drawable-(*dpi)\.
Once you are done you end up with an EditText which looks like a TextView but completely without those borders. Those orange borders you saw in the emulator are the default Android drawables. The blue ones are vendor specific (possibly Samsung).
Hope that helped and didn't confuse that much.
This is the fastest solution:
<EditText
android:id="#+id/edittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00000000"
/>
You can keep the background color as transparent to remove the EditText border on focus.
Method 1
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00000000"
/>