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

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

Related

Image View doesn't match parent's rounded cornered background

<FrameLayout
android:id="#+id/flTodayTraining"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/fl_today_training_bg_margin_bottom"
android:layout_marginTop="#dimen/fl_today_training_bg_margin_top"
android:layout_marginHorizontal="#dimen/fl_today_training_bg_margin_horizontal"
android:background="#drawable/today_training_bg"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/tvDaysTrained"
>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="#dimen/iv_today_training_detail_height"
android:src="#drawable/male_upper_body"
android:scaleType="fitXY"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/upper_body"
android:textSize="#dimen/tv_today_training_detail_text_size"
android:textColor="#color/black"
android:paddingVertical="#dimen/tv_today_training_detail_padding_vertical"
android:gravity="center"
/>
</androidx.appcompat.widget.LinearLayoutCompat>
</FrameLayout>
That's the cornered background of the frame layout
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/white" />
<corners
android:radius="15dp"
/>
</shape>
As you can see in the image, it's rounded in the bottom, but not in the top, how do I make it rounded in all corners?
Please give me an answer that works on all version, not only api 31 +
Very simple. Just use cardView
see below XML code
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardCornerRadius="15dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="160dp"
android:scaleType="centerCrop"
app:srcCompat="#drawable/fruit1" />
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg"
android:gravity="center"
android:paddingBottom="5dp"
android:text="Apple"
android:textColor="#color/white"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
</androidx.cardview.widget.CardView>
and the result is below
pros-
you can easily give elevation to card view to show shadow
easily give it radius.
looks more attractive.
much more.
I hope it'll help you.
Instead of setting the background to the FrameLayout set it to the immediate ViewGroup. Also remove fitXy in the imageview
<FrameLayout
android:id="#+id/flTodayTraining"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/fl_today_training_bg_margin_bottom"
android:layout_marginTop="#dimen/fl_today_training_bg_margin_top"
android:layout_marginHorizontal="#dimen/fl_today_training_bg_margin_horizontal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/tvDaysTrained"
>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/today_training_bg"<--set it to immediate viewgroup instead
android:orientation="vertical"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="#dimen/iv_today_training_detail_height"
android:foreground="#drawable/some_foreground" <-- insert the new background drawable you created
android:scaleType="centerCrop"
android:src="#drawable/male_upper_body" <-- removed fitXy
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/upper_body"
android:textSize="#dimen/tv_today_training_detail_text_size"
android:textColor="#color/black"
android:paddingVertical="#dimen/tv_today_training_detail_padding_vertical"
android:gravity="center"
/>
</androidx.appcompat.widget.LinearLayoutCompat>
</FrameLayout>
Create this background drawable
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<stroke
android:width="8dp"
android:color="#color/white" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<corners android:radius="#dimen/padding_margin_8" />
<stroke
android:width="8dp"
android:color="#color/white" />
</shape>
</item>
</layer-list>

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>

Add dividers and ripple effect for each GridLayout square

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?

Android drawable oval with image drawn differently on design view and device

I have an XML drawable that draws a red circle and centres a microphone image in the middle of it. I then have it on top of a relatively layout background. However, the design view layout looks exactly like I want it to, the device layout does not. Where is the discrepancy?
Drawable XML
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:width="125dp"
android:height="125dp"
android:gravity="center"
>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
>
<solid android:color="#FD91B5" />
<!--
<stroke
android:width="10dp"
android:color="#" />
-->
</shape>
</item>
<item
android:gravity="center"
android:width="26dp"
android:height="44dp"
android:drawable="#drawable/kiosk_microphone"
/>
</layer-list>
View XML (Look for the ImageButton)
<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/kiosk_bg_default_view">
<Button
android:layout_width="wrap_content"
android:layout_height="60dp"
android:text="Close"
android:id="#+id/kiosk_close_button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="false"
android:layout_alignParentTop="true" />
<ImageButton
style="#style/Base.Widget.AppCompat.ActionButton"
app:layout_widthPercent="34%"
app:layout_heightPercent="34%"
android:scaleType="fitCenter"
android:src="#drawable/kiosk_record_circle"
android:id="#+id/kiosk_record_button"
app:layout_marginTopPercent="33%"
app:layout_marginLeftPercent="23%"
/>
<LinearLayout
app:layout_marginTopPercent="10%"
app:layout_marginLeftPercent="5%"
android:orientation="vertical"
android:background="#android:color/darker_gray"
app:layout_widthPercent="25%"
app:layout_heightPercent="20%"
>
<me.grantland.widget.AutofitTextView
android:text="3:45 PM"
android:textSize="45sp"
android:textColor="#B9CFDF"
android:id="#+id/time_textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:maxLines="1"
android:layout_weight="1"
/>
<me.grantland.widget.AutofitTextView
android:text="Monday, December 15th, 2016"
android:textSize="10sp"
android:textColor="#B9CFDF"
android:id="#+id/date_textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:maxLines="1"
android:layout_weight="3"
/>
</LinearLayout>
<LinearLayout
app:layout_widthPercent="25%"
app:layout_heightPercent="20%"
app:layout_marginTopPercent="10%"
app:layout_marginLeftPercent="60%"
android:orientation="vertical"
android:background="#android:color/darker_gray"
>
<me.grantland.widget.AutofitTextView
android:text="25 deg"
android:textSize="45sp"
android:textColor="#android:color/white"
android:id="#+id/weather_textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:maxLines="1"
android:gravity="center"
/>
<me.grantland.widget.AutofitTextView
android:text="Island Park, NY"
android:textSize="10sp"
android:textColor="#android:color/white"
android:id="#+id/location_textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:gravity="center"
android:maxLines="1"
/>
</LinearLayout>
</android.support.percent.PercentRelativeLayout>
I want this
But I get this
circularbutton.xml in your drawable
<?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="#FD91B5"/>
</shape>
</item>
<item android:state_pressed="true">
<shape android:shape="oval">
<solid android:color="#FD91B9"/>
</shape>
</item>
Edit the color for Pressed State as per your wish.
Add circularbutton.xml to your ImageView
<ImageButton
style="#style/Base.Widget.AppCompat.ActionButton"
app:layout_widthPercent="34%"
app:layout_heightPercent="34%"
android:scaleType="fitXY"
android:src="your_image"
android:background="#drawable/circularbutton"
android:id="#+id/kiosk_record_button"
app:layout_marginTopPercent="33%"
app:layout_marginLeftPercent="23%"
/>
If scaleType doesn't work, you can apply padding to make the icon smaller.
Doing something like :
android:width="125dp"
android:height="125dp"
And:
android:width="26dp"
android:height="44dp"
And on the button:
android:layout_height="60dp"
Is destined to backfire on you, because it will not look the same on different devices. Especially if you try to combine a few different items together each of whom has some random values like this. This is due to a significant discrepancy in screen sizes and screen densities among Android devices. You can make it work on one screen properly, and yet it can look completely different on other screens.
For this kind of thing, since you basically need an icon, you can use a drawable or an SVG file.
EDIT
As an alternative solution, I thought of using this SVG as the src of your imageButton and the button should have the custom circular shape like in this answer.

Categories

Resources