I am working on an app for ICS and I would like my Activity to have the same background than the settings, with the cool gradient.
I tried Theme.DeviceDefault and some other, but I cannot get it.
Is that possible or should I forget?
It is relatively simple to create this app background directly yourself.
drawable/app_background_ics.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="270"
android:startColor="#color/ics_background_start"
android:endColor="#color/ics_background_end"
android:type="linear" />
</shape>
values/colors.xml
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<color name="ics_background_start">#ff020202</color>
<color name="ics_background_end">#ff272D33</color>
</resources>
Then add the app_background_ics drawable as a background to your main view (in this example ListView).
Do not forget a transparent android:cacheColorHint in order to avoid a black background when scrolling in the ListView
<ListView android:id="#+id/video_list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:dividerHeight="2dip"
android:background="#drawable/app_background_ics"
android:cacheColorHint="#00000000"/>
Did you try Theme.Holo.Dark?
I needed 2 things:
<uses-sdk android:targetSdkVersion="11"/>
<application
android:hardwareAccelerated="true"
(...)/>
Works fine now..
Related
I'm working on an Android application where I want to create a windowBackground with a centered element and a layout also with a centered element. I want these elements to be in the exact same position, with the layout overlapping the background. The problem I'm having is that the layout and the background seem to be calculating center differently (see image). Why is this happening, and what can I do to line the elements up?
This is what I see right now. The red box is created by the background and the green box is created by the foreground. Screenshot was created with a Nexus 5X API 26 emulator.
Foreground layout:
<?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="match_parent">
<View
android:background="#color/foreground_box"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_centerInParent="true" />
</RelativeLayout>
Background Drawable (applied via android:windowBackground in my theme)
<?xml version="1.0" encoding="UTF-8" ?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/background" />
<item android:gravity="center">
<shape
android:gravity="center"
android:shape="rectangle">
<solid android:color="#color/background_box" />
<size android:width="10dp"
android:height="10dp" />
</shape>
</item>
</layer-list>
For clarity, my colors file is
<?xml version="1.0" encoding="utf-8"?>
<resources>
...
<color name="background">#ffffff</color>
<color name="background_box">#AAFF0000</color>
<color name="foreground_box">#AA00FF00</color>
</resources>
Full source for this sample project is available at https://github.com/HofmaDresu/AndroidCenteredTest
The reason windowBackground includes both the heights 1) statusBar and 2) actionBar
Modify below line in your background.xml
<item android:gravity="center" android:top="80dp"> // 56 actionBarSize + 24 statusBarHeight
You may need to manage this programatically as statusBarHeight and actionBarSize varies based on device API/resolution.
Here is the result. For testing, have resized background size bit bigger so that overlapping between views and background become visible.
It is probably because of the extra space taken up by the ActionBar in the foreground.
To fix this, you can add a margin to your View in the foreground layout as:
android:layout_marginBottom="?android:attr/actionBarSize"
After test it in AS, I can say you that the right code for you background_drawable is this:
<?xml version="1.0" encoding="UTF-8" ?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/background" />
<item android:gravity="center" android:bottom="48dp">
<shape
android:gravity="center"
android:shape="rectangle">
<solid android:color="#color/background_box" />
<size
android:width="10dp"
android:height="10dp" />
</shape>
</item>
using android:top, the red square go more down than center. Need to use android:bottom instead to center background. By my tests results that 48dp is the right value.
I'm currently implementing an app which has a RecyclerView in which there are several custom views. From each one of these views the user can open a context menu (which requires a long click) but it's quite hard to figure out as generally, they will just perform a simple click and then think there is nothing more to it. But if I manage to give some UI feedback it could be much clearer. The idea is a simple ripple animation that highlights the background an which wouldn't complete from a simple click but which would go all the way for a long click action.
As I have been stuck on this for two days I have done my research and actually found some SO questions asking the same thing, for example this one from Cheok Yan Cheng is very well written and he even posted a video showing the desired effect (my question is pretty much the exact same) but there are no good answers as the first one says that we should use ?attr/selectableItemBackground but the given effect is different from the one I'm aiming for and I tried the second one, it doesn't do anything for a simple click as you start the animation in onLongClick.
EDIT :
Note that the expected behavior cannot be achieved from
?attr/selectableItemBackground nor by creating a ripple xml file and then set it as the background as these will give a normal onClick animation and a different longClick animation from the desired one again, look at this video to see what the desired effect is.
Try to follow these steps, it might help:
Step1:
Create ripple.xml in drawable: (This is for Android >= v21)
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<item android:id="#android:id/mask">
<color android:color="#android:color/transparent" />
</item>
</ripple>
In this line <color android:color="#android:color/transparent" /> will make your button transparent.
Step2:
in your item.xml where I used ConstraintLayout:
<Button
android:id="#+id/btnItemClickOnRecyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerInParent="true"
android:layout_marginTop="16dp"
android:background="#drawable/ripple"
android:paddingBottom="4dp"
android:text="#string/view_details"
android:textColor="#color/White"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
And That's it.
Since your button will be fully covering your Item and it will hover on top of other view, it will act like an item.
To implement longClick listener on the button, make sure you register it in holder and then holder.button.setOnLongClick......
Try it, If any doubts please comment.
Make a xml file named ripple.xml then set the ripple.xml as the foreground of your view. It will work like a charm!!!
<?xml version="1.0" encoding="utf-8"?>
<!-- An white rectangle ripple. -->
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="#3fce29"
tools:targetApi="lollipop">
<item
android:id="#android:id/mask"
android:gravity="center">
<shape android:shape="rectangle">
<solid android:color="#3fce29"/>
</shape>
</item>
</ripple>
Try this first make ripple effect xml
ripple.xml
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="#color/light_gray"
tools:targetApi="lollipop">
<item android:id="#android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#color/light_gray"/>
</shape>
</item>
</ripple>
than your adapter decalr xml parent layout like LinearLayout or if use RelativLayout set background as below
<?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:background="#drawable/ripple"
>......
EDITED
Decided to delete all of the previous question, since I made a standalone app just for solving this issue.
Problem: xml drawable does not works properly on API21, when defined as a background of a Button. and works on all other API's. Including API22 which is still Lollipop. really strange..
After making this standalone app, I realize that it might really be an API21 bug related to xml gradients. So in this app I put just a button on it in which I set the background to a drawable xml selector that changes the background drawable file depending on the state of the Button.
From what I could understand was that on API21 it only uses the start color of the gradient.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
>
<Button
android:layout_width="200dp"
android:layout_height="200dp"
android:id="#+id/imageView"
android:background="#drawable/key_pressed_selector"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
background_key_default.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#color/grey"
android:endColor="#color/grey"
/>
<corners android:radius="15dp"/>
background_yellow.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:type="radial"
android:gradientRadius="100"
android:startColor="#color/dark_blue"
android:endColor="#color/yellow"
/>
<corners android:radius="15dp"/>
</shape>
background_pressed_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/background_yellow"
android:state_pressed="true"/>
<item android:drawable="#drawable/background_key_default"/>
</selector>
res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="yellow">#ffff39</color>
<color name="dark_blue">#332ff2</color>
<color name="grey">#b1b2b6</color>
</resources>
Solved! To me there is a bug with this xml attribute android:gradientRadius on version API21.
The way I solved this was to create a drawable-v21 folder and copy all of my xml drawable that have this xml attribute and use one of this appendix: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).
While leaving the default drawable folder without appendix.
otherwise I was not able to make it work, when it worked on API21 it didn't on all others API's and when it worked on all others API's it didn't on API21.
To me it worked like this.
ex:
drawable
android:gradientRadius="110"
drawable-v21
android:gradientRadius="55%p"
you do not have to override onTouch() method
simply try giving your selector xml file to the button's background.
EXAMPLE
<Button
android:id="#+id/id_Login_login_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#drawable/your_selector_xmlfile"
android:text="Login" />
I have the following gradient_bg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:type="radial"
android:gradientRadius="200"
android:startColor="#666666"
android:endColor="#AAAAAA"
/>
</shape>
I would like to use this gradient as the android:windowBackground setting for my application. The application would then have a few views with transparent backgrounds on top of it.
I've tried applying this gradient directly using:
android:windowBackground="#drawable/gradient_bg"
within my application manifest, and then setting the scrollview background as transparent, but I just get the default black background.
How would I go about achieving this result? I specifically don't want to apply the background to the scrollview.
Thanks for your help in advance.
Another way might be to create a theme (in values/styles.xml):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="windowbg" parent="android:Theme">
<item name="android:windowBackground">#drawable/rectangle</item>
</style>
</resources>
Now you can add this theme to your activity/application like this:
android:theme="#style/windowbg"
Try making a bitmap from shape. So create antoher xml in which you relate to your shape. Some example:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/background"
android:tileMode="repeat"/>
I want to add a panel in android activity.I'll try explain its behaviour.
It should show small arrow on right side of my activity, If i click on that a panel having few buttons should slide out consuming around 20%-30% of current activity.
Somewhat same like menu appears on screen.
Thanks in advance.
See the SlidingDrawer widget. However, this widget will only slide up and down on older SDK versions.
Else, you should consider using fragments (using the compatibility API if necessary). You can look for some explanations about how the GMail app is built, or the HoneyComb gallery example to get information about animating fragments.
Here is my example of Panel
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="#drawable/border">
<TextView
android:text="#string/panel_title"
android:id="#+id/groupTitle"
android:paddingLeft="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/blue" />
Here you add panel content...
</LinearLayout>
You also need to add in drawable resource broder.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="3dip"
android:color="#d3d3d3" />
</shape>
And color in values/Colors.xml
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<item name="blue" type="color">#FF33B5E5</item>
</resources>
And panel Title in values/Strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="panel_title">Panel 1</string>
</resources>
As for button and animation that can be added separatly.