Recyclerview transparent background - android

How do I make my Recyclerview transparent. Not the items in it, but the actual component itself so I can see the view behind it. I tried this:
android:background="#android:color/transparent"
But it doesn't work. If set it to a color (like red or blue) using that attribute it works though...

Try using this:
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null" />
with this, your RecyclerView's background will be null and you can see the Parent View's background.

Related

selectableItemBackground and other Drawables disappear when there is an ImageView behind

My View hierarchy looks like this:
<RelativeLayout>
<ImageView
android:id="#+id/my_background_image"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
androd:id="#+id/my_image"
android:clickable="true"
android:foreground="?attr/selectableItemBackground" />
</LinearLayout>
</RelativeLayout>
If I set an image Drawable to my_background_image, the ripple drawable provided by ?attr/selectableItemBackground over my_image disappears. It doesn't matter what image I use, the ripple is completely gone. I've also found that the background (which includes the down arrow) of any "foreground" Spinner also disappears.
This is very strange behaviour. Does anyone know why this is happening?
Edit: I'd like to add that the ripple appears as long as src="..." is not included in the background ImageView or setImageDrawable() is not called on it.
Setting a transparent background to the LinearLayout solves the problem.
"Borderless buttons draw their content on the closest background. Your button might not be having background between itself and the ImageView, so it draws underneath the ImageView" from This Answer

Android - Change background color of margin

I have a fragment named HostFragment which nests one to four other fragments.
This is the layout of HostFragment:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/hostFragmentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="12dp">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<RelativeLayout
android:id="#+id/fragmentContainer1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"/>
<RelativeLayout
android:id="#+id/fragmentContainer2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<RelativeLayout
android:id="#+id/fragmentContainer3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"/>
<RelativeLayout
android:id="#+id/fragmentContainer4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"/>
</TableRow>
</TableLayout>
The important part of this is android:layout_marginTop="12dp".
Background: The nested fragments cover the entirety of HostFragment except for this margin. When the nested fragments change their background color (by calling Canvas#drawColor), HostFragment needs to also change the color of this margin to match. I store the needed color in SharedPreferences.
Behavior: If the user goes from HostFragment to SettingsActivity, changes the color, and comes back to HostFragment, the nested fragments will change their color immediately (through their onResume() methods), but HostFragment's margin will still be the old color. If the user then leaves HostFragment and goes to another fragment, then returns to HostFragment, the margin will update its color. I don't know how or why - I have no code in HostFragment to update the color. The code in HostFragment only deals in swapping in and out nested fragments.
Problem: I need the margin color to update right away, so in onResume(), I've tried something like mTableLayout.setBackgroundColor(...) or even mView.setBackgroundColor(...) (mView is the layout I inflate in onCreateView()). This still doesn't work, and the color will only update if the user leaves and comes back.
Question: How can I change the color of the margin to match an int value in SharedPreferences once the user returns to HostFragment from another Activity (i.e. Right after the user returns from the Settings)?
Thank you in advance!
Try giving paddingTop instead of marginTop and then change the color of the view in onResume by mView.setBackgroundColor(...).
Margin is the space outside the View so background color of a view won't reflect in margin space.
Padding is the space inside of a view and background color given to a View will be applied to padding space as well.
Setting the color of a margin is not possible. To achieve something that looks like that though, there's two things to do.
1) Use padding instead of margin.
Margin is outside of an element, while padding is inside of the element. This means that the element will grow in size, and that the color you've given your element as background-color will also apply to the area around the content.
2) Use a border, or drawable.
This way of doing it requires more work, but is very highly configurable. Creating a border is done by simply setting a drawable as background, and giving it a stroke width and color. For more information (and an example implementation) see https://stackoverflow.com/a/8203840/4330555.
For more information on margin, padding, border etc. see http://www.w3schools.com/css/css_boxmodel.asp. This website explains it for CSS, but the concept is the same pretty much anywhere.
try this,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/c1_cnxlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/black" >
<RelativeLayout
android:id="#+id/c2_cnxlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:background="#android:color/darker_gray" />
</RelativeLayout>
In order to change the color .setBackgroundColor(...) should work in onResume(), but you should be aware that as it has been pointed out above, a margin area is the space that is left outside from your view within the reference of its parent view. That is why changing a view´s background color would not have effect on the margin. What you could do is add a FrameLayoutthat wraps your TableLayout so that your TableLayout has a reference to set the margin from. In that case, you should be able to change FrameLayout's background and it should affect the desired margin area.
In the image below, the red rectangle represents your TableLayout as you can see on the left, it is the root view of your HostFragmentand the margin area is outside of your reach.On the right, the root view of your HostFragment is a FrameLayout and the red rectangle is still your TableLayout. In the later case, you can change the color of the FrameLayout.
image : http://oi59.tinypic.com/jz8q46.jpg
best way is to use background resource specifying multiple shapes with different color and use margin or padding..

Material ripple effect hidden by other view in layout

I added a ripple effect on a ImageButton, however it is hidden by an ImageView used as a background for the parent view RelativeLayout.
Here's the layout file:
<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="172dp"
android:orientation="vertical"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="#+id/drawerBackgroundImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/drawer_background"/>
[...]
<ImageButton
android:id="#+id/drawerLogoutButton"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_alignBottom="#id/drawerEmailTextView"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
style="#style/FlatButtonStyle"
android:scaleType="centerInside"
android:src="#drawable/ic_logout_white_24dp"/>
</RelativeLayout>
(there's a bunch of other views but they're irrelevant here)
I'm using an ImageView as the background for the RelativeLayout as I need to set a specific scaleType for the image, so I can't use the basic android:background property.
The ripple effect is hidden as it doesn't have a mask layer (I want it to extend out of the button's bounds) and thus uses the ImageButton's parent view to be displayed. The effect is perfectly visible if I remove the ImageView.
Is there a way to get the ripple effect to be shown above the problematic ImageView?
I had exactly the same issue and solved it using this thread: https://code.google.com/p/android/issues/detail?id=155880
Issue preview:
Before solved:
After solved:
Explanation:
"Borderless buttons draw their content on the closest background. Your button might not be having background between itself and the ImageView, so it draws underneath the ImageView."
Solution:
"Use a transparent background (android:background="#android:color/transparent") on some layout containing the button (beneath the ImageView). This will dictate what the maximum bounds of the ripple effect is."
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
...>
<!-- Your background ImageView -->
<ImageView
android:id="#+id/drawerBackgroundImageView"
android:src="#drawable/drawer_background"
... />
<!-- ... -->
<!-- HERE, you need a container for the button with the transparent
background. Let's say you'll use a FrameLayout -->
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent">
<!-- Maybe more items -->
<!-- Button with borderless ripple effect -->
<ImageButton
android:id="#+id/drawerLogoutButton"
android:background="?selectableItemBackgroundBorderless"
... />
</FrameLayout>
</FrameLayout>
Hope it helps.
I am experiencing same issue. Only solution I have found so far is not 100% okay since ripple is masked by view (its not borderless).
The solution (workaround):
surround your ImageButton with other view and set ripple to the foreground instead of the background in your layout like this:
<ImageView ... />
<FrameLayout
...
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless" >
<ImageButton />
</FrameLayout>
I would be really glad if someone explain why the ripple is drawn behind the image. Also if you look at Google Photos app, in image detail they have transparent icons over image view with ripple. I would like to replicate this, but I am not able to make the ripple to be in foreground. Does anybody know how to put transparent imagebuttons over everything but still have the ripple?
EDIT final solution
here you can find exactly same question link
with great explanation what is happening. the solution is the same but on top of that it solves rectangular mask by adding
android:clipChildren="false"
android:clipToPadding="false"
to your layout. now your ripple should be borderless (it worked for me).
The layout xml could be something like this:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
android:clipChildren="false"
android:clipToPadding="false">
<ImageView ... />
<FrameLayout
...
android:clickable="true"
android:foreground="?attr/selectableItemBackgroundBorderless">
<ImageView ... />
</FrameLayout>
</FrameLayout>
I'm aware this is an old post but I did struggle with this quite a bit today hence I'm posting what I was finally able to figure out and maybe someone else might benefit from it. One key emphasis beforehand, please do always RTFM!
1) The story
I aimed to use the unbounded ripple effect on Tab Items and consequently have it spread all over the AppBarLayout area. I had applied #android:color/transparent to TabLayout as the first wrapping parent and gave AppBarLayout a background color, nevertheless the ripple was still being cut off right at the borders of TabLayout's height.
2) The moral of the story (RTFM)
So I run to the nest of Android knowledge: The Documentation, and spotted this:
?android:attr/selectableItemBackgroundBorderless for a ripple that extends beyond the view. It will be drawn upon, and bounded by, the nearest parent of the view with a non-null background.
3) The course of action
Using Layout Inspector, I realized that #android:color/transparent although transparent (duh!) it actually assigns 0 as the value of the bg attribute of a View, but zero is not null hence the ripple gets bounded at the nearest parent.
4) The conclusion
With that in hand, I went and set the android:background property of my TabLayout to #null rather than transparent, and now I have a fancy little ripple spread onto the area of the AppBarLayout.
5) Outro: **ANDROID & SO FTW!
Props to everyone in this post who shed light on the matter in word. Cheers!
After wrapping ImageButton inside FrameLayout, I was getting rectangular shape on touch. Applied background with oval shape on FrameLayout and got the circular shape on touch.
Had the same issue. Used the solutions described above and worked. Managed to avoid the wrapping FrameLayout by setting foreground as ?attr/actionBarItemBackground and background as #null.
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|end"
android:background="#null"
android:contentDescription="#string/app_name"
android:foreground="?attr/actionBarItemBackground"
android:padding="#dimen/small_margin"
android:src="#drawable/ic_clear_text_icon" />

Android - How to get ripple effect on GridView items?

I have implemented image gallery using GridView. The layout for the grid item:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants" >
<ImageView
android:id="#+id/media_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/thumbnail_for_image"
android:scaleType="centerCrop" />
<View
android:id="#+id/overlay_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/selectableItemBackground" />
</FrameLayout>
My goal is to get this nice material ripple effect upon touching the grid item.
Unfortunately, I couldn't manage to find pure solution for my problem.
So I came up with using this, as author says, work around - adding overlay View on top of main ImageView, and setting background to
?attr/selectableItemBackground
Putting it to ImageView's background or parent FrameLayout background didn't work.
I'm sure, there must be a better way to handle it.
android:drawSelectorOnTop="true" in GridView did the trick
If you want this ripple to be supported in pre-lollipop versions I suggest you to use ripple libraries like The ones here. I guess using ?attr/selectableItemBackground won't give you ripple on pre-L androids.

Adding bottom margin to ListView last element

I need to add to add ListView with complicated items background: different for even/odd and rounded corners at the top and bottom. It looks like this:
I have implemented all this stuff via level-list, but there is one more thing I want to do.
Now the bottom item is near the bottom of the screen. It is better to add some space.
I don't want to add bottom margin to ListView, I need margin only for last item.
The ways I see to do this:
Footer
A kind of hack – add footer with empty TextView to ListView. But footers are quite unstable things, they usually disappear after notifyDataSetChanged and there is no way to get them back
Image with transparent pixels
I asked designer to add transparent pixels to bottom background resource. Unfortunately, in this case vertical centering is completely broken.
For example, there is 9patch like this:
And layout like this:
<?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 with background with transparent pixels on bottom -->
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
android:id="#+id/item"
android:background="#drawable/some_bgr"
android:padding="10dp"
>
<TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"
android:text="Title"
android:layout_gravity="center"
android:textSize="18sp"
/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Detail"
android:layout_gravity="center"
android:textSize="18sp"
/>
</LinearLayout>
<!-- Just for marking place took by view -->
<FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"
android:layout_below="#id/item"
android:background="#88ff55"
/>
</RelativeLayout>
The result:
As you see, centering is not working. Unfortunately.
(BTW, if specify this 9patch as background for TextView, centering works good. If you know any article, explaining this, please let me know.)
Add bottom margin to last item in Adapter implementation
That should work, but for unknown reason I still can't get it work.
I don't like this way, because I don't like to modify dimensions in code.
So
There is already imaginary way – construct some XML drawable with particular bitmap and margin. According to drawables concept it should be possible, but I can't find implementation. May be somebody knows?
Any other ideas?
In your ListView, set a paddingBottom and clipToPadding="false".
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="8dp"
android:clipToPadding="false"
android:scrollbarStyle="outsideOverlay"/>
This also works for RecyclerView.
Only use android:scrollbarStyle="outsideOverlay" if you want the scroll bar to not overflow into the padded area.
add an empty footer in your list like this:
TextView empty = new TextView(this);
empty.setHeight(150);
listview.addFooterView(empty);
you can also do it from code if you want, for example here I react to
to EditText different situations:
if(s.toString().length()>0)
{
contacts_lv.setClipToPadding(false);
contacts_lv.setPadding(0,0,0,270*screenDensity);
}
else
{
contacts_lv.setClipToPadding(true);
contacts_lv.setPadding(0,0,0,0);
}
Clocksmith's answer is the best and pretty clever. You can also create an empty footer view.
Add these two lines in your listView XML code:
android:transcriptMode="alwaysScroll"
android:stackFromBottom="true"
Another solution might be that you make a mock view with certain height.
In your adapter in getViewCount return 2.
In getCount return yourData.size+1.
In getViewType check if the element is last element return 2;
Use this type in getView to populate the mockview.
I guess you want to add margin only to last item:
So you can do in this manner, in your getview method the index of the list item and check if its the last item, then progrmatically add margin to the view.

Categories

Resources