Friday, 13 December 2013

Just One Touch! (Implementing onUserInteraction())

We used onclicklistener methods for detecting user interactions with images, buttons, layouts, TextView etc. But what if we want to detect any interaction with the device without implementing any of the listener methods?

Using onUserInteraction() method you do it very easily. This method is overridden and senses all the small and big interactions on all the layouts and buttons.






This is the simplest example of onUserInteraction() method implementation

Logic: Create a TextView so that when any interaction occurs, the desired text will be displayed to the user.

-----------------This xml code in layout file-------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView
android:id="@+id/jot_touchTV"
android:layout_marginTop="150dp"
android:layout_gravity="center"
android:textSize="25sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</LinearLayout>
-----------------------------------------------------------

Logic: Set the TextView in onUserInteraction() method.

---------------The Java code in JustOneTouchActivity.java------------------------------
package com.app.yourappname;

import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class JustOneTouchActivity extends AppCompatActivity {

private TextView tv;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.just_one_touch_activity);
tv = findViewById(R.id.jot_touchTV);
}

@Override
public void onUserInteraction()
{
super.onUserInteraction();
tv.setText("User Interaction occurred!");
}
}
------------------------------------------------------------

---------------------- In Manifest file, just declare the Activity Name----------------------

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.yourappname">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".JustOneTouchActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
---------------------------------------------------- XXX ----------------------------------------------------------------

What can you do with this awesome feature?

1) A forward and backwar Slide Show: Please hit the link to see an example.

2) Prank Call/Prank Message: Suppose you are in a party and you want to get out from there by showing people that you got a call or message. To do this just use the phone-call method in the onUserInteraction() method. With just one tap you will get a prank call.

3) Switch ON Phone Torch: When you are in a hurry and need to switch ON the phone torch, just open the app. To do this, call the torch switching method in onUserInteraction().

4) Do your work with no worries: If you are reading something very important and private text and if someone catches you suddenly, you don't have to panic. Just with one tap you can exit the app very easily. How? Call the finish() method of Activity in the onUserInteraction. That's it! Here is how to do it:
@Override
public void onUserInteraction()
{
super.onUserInteraction();
tv.setText("User Interaction occurred!");
finish();
}
5) Do fun: Generate sound, music, do light blinking effect, display animation. There are various cool stuff that can be done with this.
 



No comments:

Post a Comment