Add dividers and ripple effect for each GridLayout square - android

I am using RecyclerView + GridLayout (3 columns). Now to make each square of the grid more "responsive", I want that each square will show some sort of divider, and that there will be a ripple effect within each square that the user clicks.
Edit : I added android:foreground="?attr/selectableItemBackground", but nothing happens.This is the single item xml code right now:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dip" >
<ImageView
android:foreground="?attr/selectableItemBackground"
android:id="#+id/icon"
android:layout_width="128dp"
android:layout_height="118dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:layout_marginTop="12dp"
android:contentDescription="TODO"
android:src="#drawable/ic_launcher_background" />
</RelativeLayout>
This is how it currently look:
And this is how I want it to look:
This is how it works currently after fix, after adding:
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"

you can create a ripple drawable like this if you want it to be customize:
ripple.xml
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/colorPrimaryDark">
<item android:id="#android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#color/colorPrimaryLight" />
<!--<corners android:radius="#dimen/button_radius_large" />-->
</shape>
</item>
<item android:id="#android:id/background">
<shape android:shape="rectangle">
<gradient
android:angle="90"
android:endColor="#color/background"
android:startColor="#color/background"
android:type="linear" />
<!--<corners android:radius="10dp" />-->
</shape>
</item>
and use it in your layout like this:
android:clickable="true"
android:background="#drawable/ripple"
OR
you can simply do it like this:
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"

You can add divider by adding a GridItemDecoration to your recyclerview. i suggest you to use this.
a simple way for adding ripple when you click on items is adding a foreground with selectableItemBackground value to your item's view:
android:foreground="?attr/selectableItemBackground"

try this way for Ripple Effect in xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dip"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackground">
<ImageView
android:id="#+id/icon"
android:layout_width="128dp"
android:layout_height="118dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:layout_marginTop="12dp"
android:contentDescription="TODO"
android:src="#drawable/ic_launcher_background" />
</RelativeLayout>
and follow this link for Dividers in between items
How to add dividers and spaces between items in RecyclerView?

Related

Android: Drawing a circle inside a button

I need to create a circle inside a button like the ones you'll see in the next picture:
But my problem is I'm already using background property of button to add a nice shadow effect in the following way:
android:background="#android:drawable/dialog_holo_light_frame"
so I guess I cannot use background for both purposes.
This is how my buttons currently look:
Any idea on how to get both, the same nice shadow effect and a circle inside? Maybe it is possible with the "Style" property? (don't know)
You can use a CardView like:
<com.google.android.material.card.MaterialCardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
app:cardCornerRadius="5sp"
app:cardElevation="10dp"
app:strokeColor="#color/black"
app:strokeWidth="1dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="Test"
android:textColor="#color/black"
android:textSize="20sp" />
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/round_button"
android:gravity="center_vertical|center_horizontal"
android:text="1"
android:textColor="#fff" />
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
round_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
<shape android:shape="oval">
<solid android:color="#fa09ad"/>
</shape>
</item>
<item android:state_pressed="true">
<shape android:shape="oval">
<solid android:color="#c20586"/>
</shape>
</item>
</selector>

Android layer-list affecting position of floating action button

I am creating an app where a layer-list is used as background and a floating action button is also used. When the layer-list is set as the background of the relative layout, it changes the position of the FAB, but I cannot understand why it happens.
I want the FAB to be positioned at the bottom left. When I don't have the background set as my layer-list, it appears where I have coded.
But when I add the background attribute to the Relative layout, FAB automatically moves in.
I can't understand what is affecting this behavior of FAB.
XML for layer_list:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#color/colorPrimary" />
<padding
android:bottom="64dp"
android:left="32dp"
android:right="32dp"
android:top="64dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#FFFFFF" />
<corners
android:bottomLeftRadius="64dp"
android:bottomRightRadius="64dp"
android:topLeftRadius="64dp"
android:topRightRadius="64dp" />
</shape>
</item>
XML for Activity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/home_layer_list">
<TextView
android:id="#+id/text_view"
android:layout_centerInParent="true"
android:textSize="36sp"
android:textStyle="bold"
android:textColor="#color/colorPrimary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello world" />
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:src="#drawable/ic_fab"
android:layout_margin="16dp"
app:backgroundTint="#FFFFFF"/>
How can I make the FAB move back to the position where it was initially?
Try to change your layout like below:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/home_layer_list">
<TextView
android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="hello world"
android:textColor="#color/colorPrimary"
android:textSize="36sp"
android:textStyle="bold" />
</RelativeLayout>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:src="#drawable/ic_fab"
android:layout_margin="16dp"
app:backgroundTint="#FFFFFF" />
</RelativeLayout>

Android - SearchView Add clickable icon on the right

I'm trying to add an icon at the end of my SearchView like this
Here's my actual code:
<RelativeLayout
android:id="#+id/home_relativelayout_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/color_primary">
<android.support.v7.widget.SearchView
android:id="#+id/search_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColorHint="#color/grey_medium"
android:layout_margin="#dimen/mpoint_divider_padding"
android:gravity="center"
android:layout_centerVertical="true"
android:inputType="textNoSuggestions"
android:background="#drawable/wo_search_background" />
</RelativeLayout>
Seems there are no property such as android:drawableRight, I even tried to add a child like an ImageView but nothing appears on screen
Is it possible to achieve what I want with a SearchView or should I switch to an EditText+ImageView ?
Thanks for your help
You can do it something like this:
Add the ShapeDrawable something like this to your drawable folder:
Border.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#android:color/white" /> //Background color here
<stroke android:width="3dip" android:color="*Border color*"/>
</shape>
Then Add the SearchView as follows:
<LinearLayout
android:layout_width="match_parent"
android:background="#drawable/Border"
android:layout_height="wrap_content"
android:Orientation="horizontal">
<android.support.v7.widget.SearchView
android:id="#+id/search_view"
android:layout_width="0dp"
android:layout_weight="4"
android:layout_height="wrap_content"
android:textColorHint="#color/grey_medium"
android:layout_margin="#dimen/mpoint_divider_padding"
android:gravity="center"
android:layout_centerVertical="true"
android:inputType="textNoSuggestions"
android:background="#drawable/wo_search_background" />
<ImageView android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
.../>
</LinearLayout>
Using this you can handle the click event by adding it to the image.
Revert in case of queries and if it doesn't work.

How to get square effect rather than ripple on android BottomNavigationView?

What I'm getting:
What I'm aiming to get:
I got the first one by adding this to my build.gralde:
compile 'com.android.support:design:25.3.1'
compile 'com.github.ittianyu:BottomNavigationViewEx:1.2.4'
And then this to my layout xml:
<com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx
android:id="#+id/bottomView"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_gravity="bottom"
app:menu="#menu/bottom_nav_items"
android:background="#f8f8f8"/>
Use a custom layout for BottomNavigationView. And your layout's clickable parent should contain attribute android:background="?selectableItemBackground" .
Below is an example.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?selectableItemBackground"
android:clickable="true"
android:duplicateParentState="true"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_launcher" />
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sel" />
This will give experience of rectangular Ripple above kitkat and normal rectangle below and on kitkat . If you do not want ripple at all(In all android versions). the use a selector as background like .
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#4C000000" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#color/transparent" />
</shape>
</item>
Use this instead .
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottomNavigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="?selectableItemBackground"
/>

Add image in imageButton

I trying to make a circular image button like WhatsApp.
I tried the below code, but I am only able to create a circular imageButton. The image did not show in the imageButton.
<ImageButton
android:id="#+id/imageButton"
android:layout_width="70dp"
android:paddingBottom="140dp"
android:src = "#drawable/camera"
android:scaleType="fitXY"
android:layout_height="70dp"
android:background="#drawable/round"
android:layout_gravity="center_horizontal" />
round.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="100dp" />
<stroke
android:width="5dp"
android:color="#090" />
</shape>
You added android:paddingBottom=140dp. Issue with that. Check this
<ImageButton
android:id="#+id/imageButton"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center_horizontal"
android:background="#drawable/round"
android:tint="#android:color/black"
android:scaleType="fitXY"
android:padding="5dp"
android:src="#drawable/camera" />
You just missed solid tag in your custom shape file.
<solid android:color="#090" />
Edit:
Never try to give margin or padding which has a larger value. Just like you are using android:paddingBottom="140dp". This is not a recommended way.
Try this
<ImageButton
android:id="#+id/imageButton"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center_horizontal"
android:background="#drawable/round"
android:scaleType="centerInside"
android:src="#mipmap/ic_launcher" />
Use a shape and fill with your desire color.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="100dp" />
<solid android:color="#090" /> // solid for filling empty space
</shape>
Use code in ImageButton
<ImageButton
android:id="#+id/imageButton"
android:layout_width="70dp"
android:paddingBottom="140dp"
android:src = "#drawable/camera"
android:scaleType="fitXY"
android:layout_height="70dp"
android:background="#drawable/round"
android:layout_gravity="center_horizontal" />
Add Solid Tag
<solid android:color="#090" />
and replace android:paddingBottom="140dp" with android:padding="whateversuitsyou"
For drawing a solid circle, try the code below:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#090"/>
<size
android:width="100dp"
android:height="100dp"/>
</shape>
Shape type oval is used for drawing circles.
WhatsApp used a floating action button, and you can implement it in two steps.
Add a dependency to your build.gradle file:
dependencies {
compile 'com.github.clans:fab:1.6.4'
}
Add com.github.clans.fab.FloatingActionButton to your layout XML file.
<com.github.clans.fab.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
android:src="#drawable/ic_message"
fab:fab_colorNormal="#color/app_primary"
fab:fab_colorPressed="#color/app_primary_pressed"
fab:fab_colorRipple="#color/app_ripple"/>
Download image of camera by clicking here.
And you are done.
Set width and height as below.
android:layout_width="match_parent"
android:layout_height="wrap_content"
This works for me. Try this...

Categories

Resources