How to implement touch effect in Android App - java

Good day,
I'm currently building a chat app like WhatsApp and I have this problem that I have no clue on how to solve.
Below is the screenshot of my chat screen toolbar.
Problem: No touch event/sign whenever the user clicks on the back icon and contact name.
I wanted something like this,

Here's a simple android ripple effect but it works only with API 21 and above
Create button_effect.xml in drawable-v21
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#FFAEAE">
<item android:id="#android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#FFAEAE" />
</shape>
</item>
</ripple>
I was using it with red color if you want a whatsapp like effect use with translucent black colors
use it simply like this
android:background="#drawable/button_effect" on your layout's
EXAMPLE
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/button_effect" <!-- putting is as background-->
android:clickable="true"
android:focusable="true">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Stackoverflow" />
</RelativeLayout>

Related

How do I create a specified shape using TextView in Android Studio?

Please I am facing a challenge creating and getting the right angles of a shape in XML using android studio. I want the shape to cut out through the screen, but I am not getting the right angles.
I have already created an oval shape in my drawable folder, and I need the shape to position in the left corner of the screen. Can someone pls help? I have included the code snippet and the picture of the required screen needed. Thanks
<TextView
android:layout_width="300dp"
android:layout_height="200dp"
android:rotationY="14"
android:background="#drawable/ellipse_shape"/>
You can try this:-
custom_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/white" />
<!-- Replace with your dimensions-->
<size
android:width="10dp"
android:height="10dp" />
<corners android:bottomRightRadius="10dp" />
</shape>
it output be like this
And the textView code:-
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/custom_shape"/>
Hope You Helpful!

What's making the button background white, shouldn't it be transparent?

As you can see here, the background to the button is transparent. I can't seem to find where it's set to white in either xml files. The button is identified as btnCapture the code is below.
activity_main.xml
<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:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextureView
android:id="#+id/textureView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/btnCapture" />
<Button
android:text="#string/capture"
android:id="#+id/btnCapture"
android:layout_width="wrap_content"
android:layout_height="92dp"
android:background="#drawable/rounded_button"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
tools:layout_editor_absoluteX="157dp"
tools:layout_editor_absoluteY="202dp" />
</RelativeLayout>
rounded_button.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke android:color="#80000000"
android:width="2dp"></stroke>
<stroke android:color="#D3D3D3"></stroke>
<size android:width="60dp"
android:height="80dp"></size>
</shape>
You do not change anything related to the UI of the button in your Java code, so no, it's not because of the Java code. It's XML related.
Your background of the button is a drawable named 'rounded_button', but you haven't posted the contents of that drawable file. So all we can do is guess that the rounded_button drawable is white?
There are two quick steps you can take to make this work the exact way you desire:
First, change your view from a Button to a TextView and apply your rounded-button.xml background. This should work instantly. It's a bit tricky trying to set a background to a button as seen here.
Secondly, I noticed that your rounded-button.xml file has no solid element. The solid element allows you to set the background of a layout. For this reason, I changed one of your stroke elements to a solid element (there shouldn't be two stroke elements anyway). Your updated rounded_button.xml code is as shown below:
PS: Don't forget to change the view in your activity_main.xml from Button to TextView!
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke android:color="#80000000"
android:width="2dp">
</stroke>
<solid
android:color="#D3D3D3">
</solid>
<size
android:width="60dp"
android:height="80dp">
</size>
</shape>
I hope this helps. Merry coding!

2 attributes on 1 view android layout

I am trying to set a selectable background on a linear layout with a background color. I know the usual way is android:background="?android:attr/selectableItemBackground" but i already have another code in background. Here is the snippet of the code.
<LinearLayout
android:id="#+id/number"
android:layout_width="150dp"
android:layout_height="130dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="5dp"
android:layout_marginTop="0dp"
android:background="#color/category_colors"
android:gravity="center"
android:orientation="vertical"
android:weightSum="1">
The question is how do i set selectable background, there?
Try this code snipped on for size:
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
the android foreground attribute is relatively newly added XML attribute, but it does not work for API 22 and below! If this is the case then looks like we are going to have to stack attributes in a custom XML file,but don't worry, it is easier than it sounds!
1)In your project view go to res/drawable folder
2)right click the Drawable folder itself and select new>>>drawable resource file
3)Enter a file name my_custom_button.xml(the root doesn't really matter because you will replace it with the below code)
4)Click on the XML text tab (as opposed to the design view) if you aren't already there
5)Select all text and basically replace with the following:
(creating a custom color border is basically the same steps).
Feel free to change and play with the colors or to replace the gradient (mixture of colors) to a custom color of your own!
<?xml version="1.0" encoding="utf-8"?>
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/category_colors">
<item android:id="#android:id/ripple">
<shape android:shape="rectangle">
<solid android:color="#color/colorPrimaryDark" />
<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/colorPrimaryLight"
android:startColor="#color/colorPrimary"
android:type="linear" />
<corners android:radius="#dimen/button_radius_large" />
</shape>
</item>
</ripple>
HOPE THIS HELPS!!!
You should background selector XML file like below code and apply your initial colour along with press and pressed colour.
Then you would have to create the res/drawable/bg_selector.xml file like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#color/color_056DAE" /> <!-- pressed -->
<item android:state_focused="true"
android:color="#color/color_056DAE" /> <!-- focused -->
<item android:color="#color/color_333333" /> <!-- default -->
</selector>
then you have to apply this file to the background of your layout in XML.
android:background="#drawable/bg_selector"

Can you have a custom image for a button with a transparent background in android?

I'm making custom buttons for an android application, but I want the buttons to look like an overlay over a video being played.
At the moment the buttons get displayed over the video, but instead of having a transparent background the background is white. It looks like this:
Buttons over video
I'm setting the background for the button with an xml file in the drawable folder, so I can set different images for the different states. Therefore I can't just set android:background="color/transparent" because my line is android:background="#drawable/custom_button". Where I am setting the images like this: item android:drawable="#drawable/left_default_small".
Is there a line that can fix this or do I have to change a styles file or something?
btw I'm using API 21 so that's why the shadow is default, but I don't know how to fix it other than changing the API
Try to use ImageButton
<ImageButton android:id="#+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/media_skip_backward"
android:background="#null"></ImageButton>
OR
android:background="#android:color/transparent"
If you want to create a StateListDrawable like this one in res/drawable directory.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/my_bluish_color" android:state_pressed="true"/>
<item android:drawable="#android:color/transparent"/>
</selector>
This way the default background is transparent. When pressed the background has the color you specified (instead of color you can use any drawable here).
use this for global backgroud store this in your res/drawable and call this drawable for transperent image
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:radius="100dp"
/>
<gradient
android:angle="45"
android:centerX="35%"
android:centerColor="#00000000"
android:startColor="#00000000"
android:endColor="#00000000"
android:type="linear"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<size
android:width="100dp"
android:height="100dp"
/>
<stroke
android:width="1dp"
android:color="#99000000"
/>
</shape>
or simply use
android:background="#00000000"

How to set white background for checkbox tick in Android?

I'm developing Android app for 4.0 version, and all checkboxes have got a black background for tick, but I need white. If I use "background" xml attribute as "white" then all checkbox (with text) is white; I need only white place for tick.
The most simple way - emulate CheckBox using ImageView with 3 background states (stateSelected=true, stateSelected=false, statePressed = true). Just create appropriate xml file with 3 backgrounds and set it to ImageView background. Then in code, when click on ImageView just switch ImageView.setSelected = !ImageView.isSelected. Hope its help
I dont know if i am extremely late or what, but here is the solution
Code snippet:
say this selector is name "checkbox_selector"
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="#drawable/cbchk_blue"
android:state_focused="false">
</item>
<item android:state_checked="true"
android:drawable="#drawable/cbchk_blue"
android:state_focused="true">
</item>
<item android:state_checked="false"
android:drawable="#drawable/cbunchk_blue"
android:state_focused="false">
</item>
<item android:state_checked="false"
android:drawable="#drawable/cbunchk_blue"
android:state_focused="true">
</item>
in order to set it just for the checkbox and not the entire text also, you could have it as:
<CheckBox
android:layout_width="45dip"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:button="#drawable/checkbox_selector"
android:text="#string/text_here" />
Maybe my solution is not very elegant, but it works for me:
I simplily create another layout with the dimensions of the checkbox rectangle (15x15) and with white background. Then I add this layout to a RelativeLayout, where I also put the checkbox. With a margin of 9dp, I put the white layout-rectangle below the checkbox, so it seems that the checkbox bacground color is white.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/login_large_margin"
android:layout_marginTop="#dimen/login_medium_margin" >
<RelativeLayout
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_margin="9dp"
android:background="#color/white" >
</RelativeLayout>
<CheckBox
android:id="#+id/login_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:text="#string/login_check"
android:textColor="#color/white" >
</CheckBox>
</RelativeLayout>

Categories

Resources