Add image in imageButton - android

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...

Related

Button background gradient is not showing

I was trying to set a gradient and rounded shape in the button background. But, the gradient color is not showing in the button while shape is coming properly. Can you please tell me where I'm going wrong?
This is my Button xml code,
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="76dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent">
<ImageButton
android:id="#+id/verifyButton"
android:layout_width="300dp"
android:layout_height="70dp"
android:layout_marginBottom="20dp"
android:layout_marginEnd="50dp"
android:layout_marginStart="50dp"
android:layout_marginTop="20dp"
android:fontFamily="#font/aclonica"
android:background="#drawable/button"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/buttonText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/aclonica"
android:text="Verify"
android:textColor="#color/white"
android:textSize="25sp"
android:textStyle="bold"
android:layout_gravity="center"/>
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:visibility="gone" />
</FrameLayout>
And this is my drawable resource file xml code,
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient android:startColor="#color/light_blue" android:endColor="#color/dark_blue"/>
<corners android:radius="50dp"/>
</shape>
Please help me with this. Thank you
I got the solution. By adding this code in the button xml file, it worked just fine.
app:backgroundTint="#null"
Add this to your drawable folder and add it ass background of the button.
round_gradient_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#eeffffff" />
<corners android:bottomRightRadius="16dp"
android:bottomLeftRadius="16dp"
android:topRightRadius="16dp"
android:topLeftRadius="16dp"/>
<gradient
android:startColor="#ffff00"
android:centerColor="#00ffff"
android:endColor="#ff0000"
android:angle="45" />
</shape>
Then go to your res/values/themes.xml
change
<item name="colorPrimary">#color/purple_500</item>
to
<item name="colorPrimary">#null</item>

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?

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

Button with icon and text

I would like to make a Button like that in Android :
But I don't know how to do that.
Do I have to use a RelativeLayout with some elements (ImageView, TextView, separator,...) ?
I really don't know the best practice.
You can use Button with Property android:drawableLeft="#drawable/ic_done".
Refer this.
<Button
android:id="#+id/button111111"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="5dp"
android:layout_centerHorizontal="true"
android:drawableLeft="#drawable/ic_done"
android:text="Facebook" />
EDIT 1:
If you want to do it without using Button with Property android:drawableLeft="#drawable/ic_done" Then.
Refer this.
Create XML file named it Shape.xml inside res -> drawable folder.
If you don't see drawable folder in res directory then create folder named drawable.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="3dip"
android:color="#android:color/black" />
<corners android:radius="10dip" />
<padding
android:bottom="10dip"
android:left="10dip"
android:right="10dip"
android:top="10dip" />
</shape>
Then refer this Layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/shape"
android:gravity="center_vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/app_name"
android:src="#mipmap/ic_launcher" />
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:background="#android:color/darker_gray" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:background="#null"
android:text="Submit" />
</LinearLayout>
Here is screen.
Note : Change your Drawable and Color as of your choice.
I did for you. Save the image and set as background for the button
With text and an icon, using the Button class with the android:drawableLeft attribute:
Try this:
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text Here"
android:drawableLeft="#drawable/your_image"
/>
You can use just <Button> and set icon you want using i.e. android:drawableLeft attribute.
See docs: https://developer.android.com/guide/topics/ui/controls/button.html
Edit the image to remove the extra area around border and set the image as background

Background programatic drawable cropped

I want to make a button with a letter inside a circle, without using a picture.
My button is defined like this:
<Button
android:id="#+id/zoomInButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="10dp"
android:background="#drawable/circle"
android:text="+"
android:textColor="#color/white"
android:textSize="62sp" />
<Button
android:id="#+id/zoomOutButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="10dp"
android:background="#drawable/circle"
android:text="-"
android:textColor="#color/white"
android:textSize="62sp" />
And with circle being:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<stroke
android:width="3dp"
android:color="#color/white" />
<solid android:color="#android:color/transparent" />
</shape>
However, the circle is not centred and the text is cropped:
How to center the background on the text?
Try to reduce textSize of those Buttons' because there is not enough room for 62sp thats why the texts are cropped.

Categories

Resources