Mask of the components of android when you click - android

I'd like to get this effect in android, I tried setting the background of the layout but without success, it would be like putting a mask on the elements of layout. An example of these is when you click a button on google play, which look like this:
when you click it puts a mask on blue button.
Could someone help me?
Thank you.
I did the following, I created the layout with image and text.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/android" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="APP" />
</LinearLayout>
and create framelayout
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg_default"
android:clickable="true">
<include
android:layout_gravity="center_vertical"
layout="#layout/test1" />
</FrameLayout>
but only the background is changed, the images and text are not placed the mask. Am I doing something wrong?

The first thing that comes to my mind to implement this kind of affect is:
1. Create a layout(Linear/Relative) with the Icon and the text (android icon and APPS text in your example.)
2. Set it to wrap-content for both dimensions.
3. Place this layout inside FrameLayout.
4. Inside this FrameLayout add a button and set it to match-parent, for this button create a selector like this and apply it as it background:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- pressed -->
<item android:state_pressed="true"
android:drawable="#drawable/botton_shape_pressed" />
<!-- focused -->
<item android:state_focused="true"
android:drawable="#drawable/botton_shape_selected" />
<!-- default -->
<item android:drawable="#drawable/botton_shape_released" />
5. For the pressed texture create a semi-transparent blue texture. for the other two states apply a full transparent texture. this will result in the mask affect you looking for.

Related

How to change background color of popup-keyboard for android soft keyboard?

It took me a long time to figure out how to get rid of the ugly black default and color my custom keyboard.
I worked from this very helpful answer and I can now color my keyboard nicely:
How to change background color of key for android soft keyboard?
Just the popup-keyboards are still in the default colors.
I found another helpful answer, which took me almost to a solution. But the answer is focusing on the creation and preview of the popups:
Creating a SoftKeyboard with Multiple/Alternate characters per key
#Graeme has mentioned
If you want to change the layout/style of the popup (which defaults to #android:layout/ keyboard_popup_keyboard.xml) you can specify a android:popupLayout attribute which points to a layout file
So I have made my own version of keyboard_popup_keyboard.xml and put it next to my main layout file input.xml into /res/layout and made a reference to it, like in the example given.
<org.gasana.android.aniikeyboard.LatinKeyboardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/keyboard"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:keyBackground="#drawable/samplekeybackground"
android:keyTextColor="#000000"
android:popupLayout="#layout/popup"
/>
Sadly there was no example for the popupLayout file. So I copied the original file all the way up from
C:\Users\martin\AppData\Local\Android\sdk\platforms\android-28\data\res\layout\keyboard_popup_keyboard.xml
and tried to tweak it as popup.xml to use the same background as my main keyboard:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/samplekeybackground"
>
<android.inputmethodservice.KeyboardView
android:id="#android:id/keyboardView"
android:background="#drawable/samplekeybackground"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:popupLayout="#layout/popup"
android:keyTextSize="22sp"
tools:ignore="ResourceCycle" />
<ImageButton android:id="#android:id/closeButton"
android:background="#drawable/samplekeybackground"
android:src="#drawable/btn_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="8dp"
android:clickable="true"
/>
My keyboard still builds and creates a working APK. Just the color of the popups is still the ugly default.
Context: I am a linguist, not a developper. I made this custom keyboard for a minority language with a special alphabet and tone-markers and have it free on the Play Store. It works. But people are hesitating, because of the aweful color-design. As soon as I get the popups colored, I will publish a fresh version. Thank you.
Since no answer was coming here for two months, I took time for more poking and guessing. Now I got lucky today and want to be nice to the next linguist, who also needs a custom keyboard and needs to work from examples:
mykeyboard.java is pointing to the layout file for the main keyboard so (third line "input"). I just give a three line quote:
#Override public View onCreateInputView() {
mInputView = (LatinKeyboardView) getLayoutInflater().inflate(
R.layout.input, null);
So inside my \res\layout\input.xml I added the reference to my popup-layout:
<org.my.project.here.LatinKeyboardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/keyboard"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:keyBackground="#drawable/samplekeybackground"
android:keyTextColor="#000000"
android:popupLayout="#layout/popup" <!-- here it is -->
/>
And my \res\layout\popup.xml looks like this; I believe I copied it from the provided sample project. Today I just changed the two marked lines for light blue background colour and for black text colour and that finally did the trick. Seems I had looped references earlier but no error messages, just the ugly black default layout.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/samplekeybackground">
<android.inputmethodservice.KeyboardView
android:id="#android:id/keyboardView"
android:background="#color/colorPrimary"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:keyTextSize="22sp"
android:keyBackground="#drawable/samplekeybackground" <!-- here it is -->
android:keyTextColor="#000000" <!-- and here -->
tools:ignore="ResourceCycle"/>
<ImageButton android:id="#android:id/closeButton"
android:background="#android:color/transparent"
android:src="#drawable/btn_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="8dp"
android:clickable="true"/>
The mentioned samplekeybackground.xml is just a very simple definition, pointing to two actual xml-colour-defintions:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item
android:state_focused="false"
android:state_selected="false"
android:state_pressed="false"
android:drawable="#drawable/normal" />
<!-- Pressed state -->
<item
android:state_pressed="true"
android:drawable="#drawable/pressed" /></selector>
And just to be complete, because I appreciate stuff I can just copy and play with for testing, here is the normal.xml; the pressed.xml is the same, just a darker blue:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="1dp" android:color="#A1B7F7" />
<solid android:color="#C7D4FA"/>
</shape>
All this is from guessing and building many versions until I got lucky. Can probably not answer any follow-up questions, but it does work:

Android change hover color of card view which is clickable

I am trying to change color of my card view when someone click on it. So far I have tried using this code below:
//drawable/selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"android:drawable="#color/button_pressed"/> <!-- pressed -->
<item android:state_focused="true" android:drawable="#color/button_focused"/> <!-- focused -->
<item android:drawable="#color/button_default"/> <!-- default -->
</selector>
This is my selector code this works perfect when I use this in button code is below:
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/selector"
android:text="Click Me"
/>
I am trying to do this inside card view I can't see anything inside my card view I am using selector like this:
<android.support.v7.widget.CardView
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:layout_width="170dp"
android:layout_height="150dp"
android:layout_margin="4dp"
android:onClick="economy"
android:background="#drawable/selector"
>
</android.support.v7.widget.CardView>
My card is clickable my question is can I do this inside card view
There is a limitation with Cardview on changing the background using selector.If You still want to go ahead with that. You can apply selector on foreground.
<android.support.v7.widget.CardView
android:foreground="#drawable/selector" // change
android:clickable="true"
android:layout_width="170dp"
android:layout_height="150dp"
android:layout_margin="4dp"
android:onClick="economy"
>
</android.support.v7.widget.CardView>
And add alpha to the color(like : #55FFFFFF) which you are applying on the selector so that the content of the card will be visible. This is not ideal but depends on your output expectation.

How to convert layout to drawable for use in splash screen

Background information
When a user enters my Android application. They are first taken to LoginActivity. LoginActivity takes some time to load as it is also responsible for performing background sqlite migrations as well as other housekeeping tasks (this takes 500-1000ms).
Unfortunately, the user sees a blank screen during this entire time. As setContentView has not executed yet.
I am trying to remediate this problem by following the guides
https://android.jlelse.eu/launch-screen-in-android-the-right-way-aca7e8c31f52
https://www.bignerdranch.com/blog/splash-screens-the-right-way/
They tell me I need to create a background_splash.xml in drawables and point to it using a custom style with <item name="android:windowBackground">#drawable/background_splash</item>
Unfortunately I noticed that I am not able to control padding margin width height, gravity of my logo in drawables.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#color/gray"/>
<!-- cant control android:margin=... (my min API is 21) -->
<!-- cant control android:width=... (my min API is 21) -->
<!-- cant control android:height=... (my min API is 21) -->
<item>
<bitmap
android:gravity="center"
android:src="#mipmap/ic_launcher"/>
</item>
</layer-list>
My problem
How to properly achieve same behavior (or at least similar) as my current activity_login.xml file and convert that into a drawable?
My code
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.LinearLayoutCompat
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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/material_red_500">
<!--***********************************************************
* Layout section: The login logo
************************************************************-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<android.support.v7.widget.AppCompatImageView
android:layout_centerInParent="true"
android:src="#drawable/image_symbol_lock"
android:scaleType="fitCenter"
android:layout_width="150dp"
android:layout_height="150dp"/>
</RelativeLayout>
<!--***********************************************************
* Layout section: The login button (facebook)
************************************************************-->
<android.support.v7.widget.AppCompatTextView
android:id="#+id/button_facebook"
android:paddingTop="28dp"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:paddingBottom="28dp"
android:background="#color/material_blue_500"
android:text="LOG IN"
android:textSize="32sp"
android:textColor="#color/white"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.v7.widget.LinearLayoutCompat>
You can create a layout_splash with your own design and in the Splash style only set a background color. So first you will see the background color and not the blank and next see the layout. You could do this with the login activity.

The Same Selector Does Not Work Android

I am using a selector working fine in all activities as you can see its XML below.
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="#drawable/active_date_horizontal_5_pressed" /> <!-- pressed -->
<item
android:state_focused="true"
android:drawable="#drawable/active_date_horizontal_5_pressed" /> <!-- focused -->
<item
android:drawable="#drawable/active_date_horizontal_5" /> <!-- default -->
Except one activity, it does not work ridiculously. You can see three screenshots below which the selector works fine in the first two screenshots, and does not work in the last screen shot.
1)
2)
3)
These are customized GridViews which filled with customized BaseAdapter. Cells in the GridViews have an Xml Layout which its background is the selector. Any ideas?
It seems I solved my own problem.
You can see the Xml Layout which I am applied the selector below
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:background="#drawable/calendar_cell_selector" >
<TextView
android:id="#+id/txtWeeklyEventName"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/imgCornerWeekly"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/txtWeeklyEventName"
android:layout_alignRight="#+id/txtWeeklyEventName"
android:src="#drawable/corner_orange"
android:visibility="visible" />
</RelativeLayout>
You can see the background of the RelativeLayout is the selector, but the TextView fills upon the whole layout so Android cannot handle the selector.
Thus, I removed the background from the RelativeLayout and set it for the TextView, it totally works.
Thanks to me!

Edittext with custom offset rectangle background

I'd like to create and edittext box with a custom offset background like this:
How should I go about doing this?
I'd go with a LinearLayout containing your border as the background drawable, same padding for top, left and right and higher padding at bottom.
Then, setting a custom, stateful 9-patch drawable for your EditText based on your background.
In fact, if you make your background 9-patched, you can use it both in your LinearLayout and your EditText.
Also, you don't have to make your EditText drawable stateful; but I strongly advise you to do so, so the user can actually get visual feedback from your EditText.
Sample code:
layout/your_layout.xml
...
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg_border"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="10dip"
android:paddingBottom="30dip">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg_border_statelist" />
</LinearLayout>
...
drawable/bg_border_statelist.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="#drawable/bg_border_selected" />
<item android:state_focused="true"
android:drawable="#drawable/bg_border_selected" />
<item android:drawable="#drawable/bg_border" />
</selector>
Assuming your background image is 9-patch and called bg_border.9.png, and the respective 9-patch version is called bg_border_selected.9.png.
Cheers!

Categories

Resources