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.
 



Thursday, 12 December 2013

Dealing with back button by Implementing onBackPressed() (Updated with androidx package)


For exiting an application you may have noticed that not every app has a close button inside and therefore the hardware's back button is pressed to get away.

So now we will be using hardware's back functionality in order to get user's confirmation for exiting.
If user presses on "No" then, only the dialog disappears and the Activity does not exits otherwise the app will terminate which is usual.

Please Note: Here you must not call the parent activity's onBackPressed() method.


-----------------No extra xml editing, only the Java code will do-----------------------
package com.app.yourappname;

import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class BackButtonDealerActivity extends AppCompatActivity {

@Override
public void onCreate(Bundle b) {
super.onCreate(b);
}

@Override
public void onBackPressed() {
AlertDialog.Builder b = new AlertDialog.Builder(this)
.setTitle("Alert!")
.setCancelable(false)
.setMessage("Do you really want to close the app?")
.setIcon(R.mipmap.ic_launcher)
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(BackButtonDealerActivity.this, "'No' button was pressed...", Toast.LENGTH_LONG).show();
dialog.dismiss();
}
}).setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
BackButtonDealerActivity.this.finish();
Toast.makeText(BackButtonDealerActivity.this, "'Yes' button was pressed...", Toast.LENGTH_LONG).show();
dialog.dismiss();
}
});
b.show();
}
}
----------------------------------------------------------------------------------------

-------------------------- Declare the Activity name in the manifest file -------------
<?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=".BackButtonDealerActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

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


Wednesday, 11 December 2013

How to use Handler to schedule tasks? A simple Slide Show Example (Updated with androix package)

In this tutorial we will be using Handler, which is a very useful class in Android that allows to display a cool animation and also perform various operations where tasks scheduling is required. It also has many other capabilities but for now we will just concentrate on the task scheduling in a very creative way.

In this example we will be creating a slide show app where the user can see the slide show of photos in forward as well as backward. For forward and backward slide show we have just set a boolean in onUserInteraction() method.

For understanding onUserInteraction() method, just hit this link.


Please note: The images are taken from Pixabay.

-----------------------The xml file's content --------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#1c6d8d">

<ImageView
android:id="@+id/bbd_imgIV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/i1"/>

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

----------------The Java file's code as follows------------------
package com.app.yourappname;

import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class HandlerSlideShowActivity extends AppCompatActivity {

private boolean forwardSlideShow = true;
private Handler imgHandler = new Handler();
private ImageView imgIV;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.handler_slideshow_activity);
imgIV = findViewById(R.id.bbd_imgIV);
}

private Runnable mUpdateTimeTask = new Runnable() {
int imgs[] = {R.drawable.i0, R.drawable.i1, R.drawable.i2, R.drawable.i3, R.drawable.i4};

int no = 0;
public void run() {
if(forwardSlideShow)
no++;
else
no--;

if(no>imgs.length-1)
no = 0;

if(no<0)
no = imgs.length-1;

if(imgIV!=null)
imgIV.setImageResource(imgs[no]);
imgHandler.postDelayed(mUpdateTimeTask, ( 2 * 1000)) ;
}
};

@Override
public void onUserInteraction() {
super.onUserInteraction();
if(forwardSlideShow) forwardSlideShow = false;
else forwardSlideShow = true;
}

@Override
protected void onResume() {
super.onResume();
imgHandler.removeCallbacks(mUpdateTimeTask);
imgHandler.postDelayed(mUpdateTimeTask, 500);
}
}
------------------------------------------------------------------

---------------------------- Just declare the Activity name in your Manifest file --------------------
<?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=".HandlerSlideShowActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

ElevenLabs For Audio Models