I am using a custom ProgressBar. Now while a task is going on, I am showing the progress bar, but user can still interact with the views and controls.
How do I disable the user interaction on whole view just like a ProgressDialog does , when it is visible.
Do I need to use a transparent view on top of main view and show the progress bar on that view and hide that view once a task is completed.
Or just get the id of my parentView and set it disabled ? But then I won't be able to dim the background, just like what happens when a dialog appears on the view/Activity/Fragment. Right?
I just want to know the way to disallow the user from any interaction while the progressbar is visible.
Thanks
Your question: How to disable the user interaction while ProgressBar is visible in android?
To disable the user interaction you just need to add the following code
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
To get user interaction back you just need to add the following code
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
Here is an example:
Note:I am giving you just an example to show how to disable or retain user interaction
Add a progress bar in your xml.Something like this
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar"
android:visibility="gone"/>
In MainActivity when a button pressed you show the progressbar and disable the user interaction.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = (ImageView) findViewById(R.id.imageView);
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
mImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mProgressBar.setVisibility(View.VISIBLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
}
});
}
And when user backPressed you remove the progressbar again retain the user interaction.Something like this
#Override
public void onBackPressed() {
super.onBackPressed();
mProgressBar.setVisibility(View.GONE);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
}
If you want to add a feature of disable and greyed out display, you need to add in your xml layout file a linear layout that fills the parent. Set its background to #B0000000 and its visibilty to GONE. Then programmatically set its visibility to VISIBLE.
Hope this help!
I have fixed this issue by adding root layout to the ProgressBar.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:clickable="true"
android:gravity="center"
android:visibility="gone"
android:id="#+id/progress">
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:indeterminate="true"
android:indeterminateTintMode="src_atop"
android:indeterminateTint="#color/primary"/>
</LinearLayout>
Made the root layout clickable
android:clickable="true"
NOTE: In my main view, I had RelativeLayout as root and have added above-mentioned code inside the root layout at the last position (last child).
Hope this helps!!
just set:
android:clickable="true"
in your xml
<ProgressBar...
Only this makes magic!
To extend (pun intended) on the accepted Answer :
When you use kotlin you can use extension functions. That way you have a quick and nice looking method for blocking and unblocking UI.
fun AppCompatActivity.blockInput() {
window.setFlags(
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE)
}
fun AppCompatActivity.unblockInput() {
window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE)
}
fun AppCompatActivity.blockInputForTask(task: () -> Unit) {
blockInput()
task.invoke()
unblockInput()
}
You can use the blocking and unblocking functions in your activity. Also, you can add more functionality like showing a Toast or something.
When using it in a custom view or any other view, you can simply cast the context to activity and use the functions.
Use blockInputForTask to surround simple linear tasks and blockInputand unblockInput when they are needed in different scopes.
You can use blockInputForTask like this:
blockInputForTask {
// Your lines of code
// Can be multiple lines
}
Use document default method progressbar.setCancelable(false)
Make a dialog with transparent background. The issue with getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE); is that when app will go in background and come back user will be able to interact with UI components, a lot more handling. So for blocking UI make a transparent dialog and if you want to set time for hide/show. Do this in a runnable thread. So the solution will be
public class TransparentDialogHelper {
private Dialog overlayDialog;
#Inject
public TransparentDialogHelper() {
}
public void showDialog(Context context) {
if (AcmaUtility.isContextFinishing(context)) {
return;
}
if (overlayDialog == null) {
overlayDialog = new Dialog(context, android.R.style.Theme_Panel);
overlayDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED);
}
overlayDialog.show();
}
public void hideDialog() {
if (overlayDialog == null || AcmaUtility.isContextFinishing(overlayDialog.getContext())) {
return;
}
overlayDialog.cancel();
}
}
-------- Timer
Handler handler = new Handler();
handler.postDelayed( () -> {
view.hideProgress();
}, 2000);
Make your parent layout as Relative Layout & add this :
<RelativeLayout ... >
<other layout elements over which prog bar will appear>
<RelativeLayout android:id="#+id/rl_progress_bar"
android:layout_width="match_parent" android:clickable="true"
android:layout_height="match_parent" >
<ProgressBar android:id="#+id/pb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:indeterminateOnly="true"
style="#android:style/Widget.DeviceDefault.ProgressBar"
android:theme="#style/AppTheme.MyProgressBar"
/>
</RelativeLayout>
If you have floating buttons in your UI, they still grab all the focus & remain clickable when the progress bar is visible. for this use : (when your prog bar is visible & re-enable them when you make your prog bar invisible/gone)
fb.setEnabled(false);
Related
This question already has answers here:
ProgressDialog is deprecated.What is the alternate one to use?
(22 answers)
Closed 4 years ago.
Since the ProgressDialog is deprecated from the Android version O, I'm still finding a better way out to do my task. The task is to move from my activity to the fragment. Everything is working fine but the progressdialog is not visible. I've tried implementing it but... the progressdialog doesn't work.
It seems the progressbar would work but still not working. I need a progressdialog because it is simply easy for me to set my title and the message. I need a spinner progressDialog but don't know how to do it. Here is one of my work but not implementing :
Java Class
ublic class SaveVideo extends AppCompatActivity {
private Button button;
private ProgressDialog mProgressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_save_video);
mProgressDialog = new ProgressDialog(this);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.back);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
button = (Button) findViewById(R.id.saveVideo);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//where it must be seen when the button is pressed
mProgressDialog.setTitle("Title");
mProgressDialog.setMessage("Message");
mProgressDialog.show();
Intent intent = new Intent(SaveVideo.this,MainActivity.class);
intent.putExtra("change",2);
startActivity(intent);
//as soon as the page moves from this to another fragment
mProgressDialog.dismiss();
}
});
}
I'm new to Android Version O. Any help would give me new thing to learn!
As it is mentioned in Android O documentation:
This class was deprecated in API level 26. ProgressDialog is a modal
dialog, which prevents the user from interacting with the app. Instead
of using this class, you should use a progress indicator like
ProgressBar, which can be embedded in your app's UI. Alternatively,
you can use a notification to inform the user of the task's progress.
You can create a custom view with TextView and ProgressBar and manage its visibilty.
You can use this library also because it is using AlertDialog instead of ProgressDialog.
ProgressBar is very simple and easy to use,
first step is that you can make xml layout of the dialog that you want to show, let say we name this layout
layout_loading_dialog.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="20dp">
<ProgressBar
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4"
android:gravity="center"
android:text="Please wait! This may take a moment." />
</LinearLayout>
next step is create AlertDialog which will show this layout with ProgressBar
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCancelable(false); // if you want user to wait for some process to finish,
builder.setView(R.layout.layout_loading_dialog);
AlertDialog dialog = builder.create();
now all that is left is to show and hide this dialog in our click events
like this
progress_dialog.show(); // to show this dialog
progress_dialog.dismiss(); // to hide this dialog
and thats it, it should work, as you can see it is farely simple and easy to implement ProgressBar (like ProgressDialog) instead of deprecated ProgressDialog.
now you can show/dismiss this dialog box in either Handler or ASyncTask, its up to your need, hope you can use this to solve your problems, cheers
Yes, API level 26 it's deprecated, Better you can use progressbar only.
Use this code snippet for creating programmatically:
ProgressBar progressBar = new ProgressBar(activity, null, android.R.attr.progressBarStyleSmall);
Just for future reference, change the android.R.attr.progressBarStyleSmall to android.R.attr.progressBarStyleHorizontal.
Maybe this guide could help you.
I hope this may help you.
This class was deprecated in API level 26.
ProgressDialog is a modal dialog, which prevents the user from
interacting with the app. Instead of using this class, you should use
a progress indicator like ProgressBar, which can be embedded in your
app's UI. Alternatively, you can use a notification to inform the user
of the task's progress.
https://developer.android.com/reference/android/app/ProgressDialog.html
You need to create a custom XML layout file with ProgressBar on it and show that instead. I've been using a library like https://github.com/Q115/DelayedProgressDialog to get this simple behavior.
Usage:
DelayedProgressDialog progressDialog = new DelayedProgressDialog();
progressDialog.show(getSupportFragmentManager(), "tag");
You can use ProgressBar instead of ProgressDialog.
Create a ProgressBar inside a custom dialog with TextView and other widgets you need.
If anyone insists on having a progress dialog, in my case I opted for a progress bar inside an alert dialog. You can use the following code to get started.
My case was simple because I just needed an indeterminate progressbar. For a full fledged version you'll have to encapsulate it into a class and access the Bar.
private AlertDialog Create_Indeterminate_HorizontalProgressBar_AlertDialog(
Context context, String title, String message)
{
final ProgressBar progressBar =
new ProgressBar(
context,
null,
android.R.attr.progressBarStyleHorizontal);
progressBar.setLayoutParams(
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
progressBar.setIndeterminate(true);
final LinearLayout container =
new LinearLayout(context);
container.addView(progressBar);
int padding =
getDialogPadding(context);
container.setPadding(
padding, (message == null ? padding : 0), padding, 0);
AlertDialog.Builder builder =
new AlertDialog.Builder(context).
setTitle(title).
setMessage(message).
setView(container);
return builder.create();
}
private int getDialogPadding(Context context)
{
int[] sizeAttr = new int[] { android.support.v7.appcompat.R.attr.dialogPreferredPadding };
TypedArray a = context.obtainStyledAttributes((new TypedValue()).data, sizeAttr);
int size = a.getDimensionPixelSize(0, -1);
a.recycle();
return size;
}
Note: If you're wondering why the Bar is in a container: I just couldn't get the padding to work on the Bar having to put in on the container instead.
This is what i managed to put together since the class has been deprecated in Android Oreo (API 26 +).
In the Xml File (whatever layout file):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:padding="13dp"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ProgressBar
android:visibility="gone"
android:layout_marginTop="5dp"
android:id="#+id/top_progressBar"
style="#style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_height="wrap_content"
android:indeterminateTintMode="src_in"
android:indeterminateTint="#color/white"
android:indeterminate="true" />
<TextView
android:layout_width="wrap_content"
android:text="Loading..."
android:textAppearance="?android:textAppearanceSmall"
android:layout_gravity="center_vertical"
android:id="#+id/loading_msg"
android:layout_toEndOf="#+id/loader"
android:layout_height="wrap_content" />
<ProgressBar
android:visibility="gone"
android:layout_marginTop="2dp"
android:id="#+id/down_progressBar"
style="#style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_height="wrap_content"
android:indeterminateTintMode="src_in"
android:indeterminateTint="#color/white"
android:indeterminate="true" />
</LinearLayout>
in the sample above, i have thought of a scroll situation say your view is long hence the two progress bars.
in the Java file sample :
public class GetUserDetails extends AppCompatActivity {
private ProgressBar topProgressBar, downProgressBar;
private ProgressDialog progressDialog;
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_get_user_details );
//initilise the progressbar views and progress dialog object
topProgressBar = findViewById ( R.id.top_progressBar );
downProgressBar = findViewById ( R.id.down_progressBar );
if ( Build.VERSION.SDK_INT < 26 ) {
progressDialog = new ProgressDialog ( this );
} else {
topProgressBar.setIndeterminate ( true );
downProgressBar.setIndeterminate ( true );
}
}
private void showProgressDialog (final boolean isToShow) {
if ( Build.VERSION.SDK_INT < 26 ) {
if ( isToShow ) {
progressDialog.setMessage ( "Processing ...Please wait." );
progressDialog.setCancelable ( false );
if ( ! progressDialog.isShowing () ) {
progressDialog.show ();
}
} else {
if ( progressDialog.isShowing () ) {
progressDialog.dismiss ();
}
}
} else {
/* this is Android Oreo and above*/
if ( isToShow ) {
topProgressBar.setVisibility ( View.VISIBLE );
downProgressBar.setVisibility ( View.VISIBLE );
getWindow ().setFlags ( WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE );
} else {
topProgressBar.setVisibility ( View.GONE );
downProgressBar.setVisibility ( View.GONE );
getWindow ().clearFlags ( WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE );
}
}
}
}
Well , this is my hack so i hope it helps.
The ProgressDialog in your example won't ever be visible because you call dismiss() right after show(). The creation of an Intent and call to startActivity() are not blocking: Basically you just schedule a switch to the other activity to be performed "soon".
You have to move the dismiss() call to your activity's onStop:
#Override
protected void onStop()
{
super.onStop();
mProgressDialog.dismiss();
}
Furthermore one might ask: Why does switching from one activity to the other take so long in this case? I guess that your MainActivity does some heavy work in its onCreate / onStart / onResume methods. A better way of handling that might be to put all that work into a separate thread.
I am working on an app using geolocation and I want to set a layout VISIBLE when close to a point and GONE when too far from the point.
This is my xml :
<LinearLayout
android:visibility="gone"
android:id="#+id/slidePane"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
Then I use the ID in the activity as usual :
declare it in the class attribute :
private LinearLayout slidepane;
set it in the onCreate() method :
slidepane = (LinearLayout) findViewById(R.id.slidePane);
And then I try to update it in onLocationChanged() method as follows :
if (InterestPoint.CalculationByDistance(location, new LatLng(47.247801, -1.551883)) < 0.03) {
Toast.makeText(MainActivity.mContext, "InterestPoint close", Toast.LENGTH_SHORT).show();
slidepane.setVisibility(View.VISIBLE);
} else {
slidepane.setVisibility(View.GONE);
}
So the condition is correct since the Toast appears but the layout does not become visible, why?
Thank you.
EDIT : I am using https://github.com/umano/AndroidSlidingUpPanel and try to set the visibility of the second child (the sliding panel). I didn't see anything in the documentation about visibility. I can still access the children of the LinearLayout but not the container itself (and especialy the visibility).
Replace this:
slidepane.setVisibility(View.VISIBLE);
with this:
runOnUiThread(new Runnable() {
#Override
public void run() {
slidepane.setVisibility(View.VISIBLE);
}
});
Make sure you set the visibility of view at the time of initialization.
#Override
protected void onCreate(Bundle savedInstanceState) {
........
slidepane = (LinearLayout) findViewById(R.id.slidePane);
slidepane.setVisibility(View.GONE);
.......
//onLocationChanged() add ->
if (InterestPoint.CalculationByDistance(location, new LatLng(47.247801, -1.551883)) < 0.03) {
Toast.makeText(MainActivity.mContext, "InterestPoint close", Toast.LENGTH_SHORT).show();
slidepane.setVisibility(View.VISIBLE);
} else {
slidepane.setVisibility(View.GONE);
}
I have a need to show a minimally-intrusive non-blocking notification which is not tied to the activity it was shown in (like a Toast) and which is clickable. Anyone have any idea whether or not this is possible? Unfortunately, it appears that Toast notifications (custom or otherwise) are not clickable (i.e. setting an OnClickListener on its views has no effect). All the alternatives that I'm aware of (i.e. AlertDialog, PopupWindow and Crouton) seem to show a notification which is tied to the activity it was shown in (i.e. they won't continue showing when the activity finishes). Any suggestions?
You can use PopupWindow, add an onClickListener and add a handler to auto cancel it after n times (just like the behavior of a toast). Something like this:
public static void showToast(Activity a, String title, String message) {
// inflate your xml layout
LayoutInflater inflater = a.getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
(ViewGroup) a.findViewById(R.id.toast_layout_root));
// set the custom display
((TextView) layout.findViewById(R.id.title)).setText(title);
((TextView) layout.findViewById(R.id.message)).setText(message);
// initialize your popupWindow and use your custom layout as the view
final PopupWindow pw = new PopupWindow(layout,
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, true);
// set windowType to TYPE_TOAST (requires API 23 above)
// this will make popupWindow still appear even the activity was closed
pw.setWindowLayoutType(WindowManager.LayoutParams.TYPE_TOAST);
pw.showAtLocation(layout, Gravity.CENTER | Gravity.TOP, 0, 500);
// handle popupWindow click event
layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// do anything when popupWindow was clicked
pw.dismiss(); // dismiss the window
}
});
// dismiss the popup window after 3sec
new Handler().postDelayed(new Runnable() {
public void run() {
pw.dismiss();
}
}, 3000);
}
xml layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toast_layout_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:orientation="vertical"
android:elevation="10dp"
android:padding="20dp">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#FFF"
android:textStyle="bold"/>
<TextView
android:id="#+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#FFF"/>
</LinearLayout>
You are right, a Toast object has no way to be interacted with, but there are many libraries out there that will give you the same look and feel as a toast, but with some interactivity. The one I use is https://github.com/JohnPersano/SuperToasts
I think what you need is in fact a PopupWindowwhich can be seen here "http://developer.android.com/reference/android/widget/PopupWindow.html".
Toasts have a very specific task, which is to inform the user, without any input from them. So instead of trying to extend the purpose of the Toast, use the PopupWindow which can be interacted with by the user.
A 'Dialog' type of activity will probably be your best bet.
In manifest:
<activity android:name=".ToastLikeActivity"
android:theme="#android:style/Theme.Dialog"
android:label="#string/label"
></activity>
And timeout the activity within the onCreate():
class ToastLikeActivity extends Activity {
#Override
public void onCreate(Bundle state)
// auto-kill activity after X seconds <-------------------------
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
ToastLikeActivity.this.finish(); // kill after X seconds
}
}
}, VisibleTimeSecs*1000);
}
To display the dialog start it as with any other activity:
Intent i = new Intent(this, ToastLikeActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
And it will show up and automatically go away after X seconds.
Such a popup will not be tied to the caller activity. In fact - it will not even require a caller activity. You
can activate it (bad idea, but possible) even from a service.
You can implement basically any kind of sensitive (i.e. accepting user's clicks) interface you want to
the ToastLikeActivity. Especially: you can make its exteriors transparent, giving it a dialog-likke looks.
how can I catch the event when click occurs somewhere on the app screen?
It doesn't matter if there is a button or something else. I just need to apply an onClick() event listener on the whole application screen.
How to do this?
setonclicklistner for main layout of your layout file....
Like....main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/mainlayout">
<!- Your other view->
</Relativelayout>
and set click listener for mainlayout...
RelativeLayout rlayout = (RelativeLayout) findViewById(R.id.mainlayout);
rlayout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
You should just override Activity's dispatchTouchEvent(ev: MotionEvent) method
Look at this example in Kotlin. This approach will not affect any onClickListenters in your app
/**
* On each touch event:
* Check is [snackbar] present and displayed
* and dismiss it if user touched anywhere outside it's bounds
*/
override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
// dismiss shown snackbar if user tapped anywhere outside snackbar
snackbar?.takeIf { it.isShown }?.run {
val touchPoint = Point(Math.round(ev.rawX), Math.round(ev.rawY))
if (!isPointInsideViewBounds(view, touchPoint)) {
dismiss()
snackbar = null // set snackbar to null to prevent this block being executed twice
}
}
// call super
return super.dispatchTouchEvent(ev)
}
Or check out the full gist
I believe the easiest way is to override onUserInteraction in your Activity
Just be aware that it won't fire when users focus on/off EditTexts and Spinners (probably other Widgets too). But it will fire every time the user touches the screen or presses a Button.
But this makes it unnecessary to build listeners into your layouts or write additional methods to handle those listeners, so I believe it's the most trouble-free way to get what you want.
If you want to know, that the user clicked anywhere on the screen
then the trick is to override OnUserUnteraction():
"Called whenever a key, touch, or trackball event is dispatched to the activity.Implement this method if you wish to know that the user has interacted with the device in some way while your activity is running. This callback and {#link #onUserLeaveHint} are intended to help activities manage status bar notifications intelligently; specifically, for helping activities determine the proper time to cancel a notification...."
public abstract class BaseActivity extends Activity {
...
#Override
public void onUserInteraction() {
Log.d(DEBUG_TAG,"Touch anywhere happened");
super.onUserInteraction();
}
I am using it to set up app screen block after some inactivity.
Give your main layout an id, then
LinearLayout root = (LinearLayout) findViewById(R.id.main_layout);
root.setOnClickListener(new OnClickListener() {
public void onClick()
{
}
});
You can also directly set a callback from the onClick xml attribute of the root layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:onClick"myMethod">
<!- Your other view->
</Relativelayout>
And in your activity:
public void myMethod(View pView) {
//XXX do something
}
Less bloated code this way :)
Maybe you can add a listener to the Layout instance, getting the layout with
RelativeLayout l = (RelativeLayout)findViewById(R.id.mylayout);
l.setOnClickListener(click);
I need to display progress icon in button so that user can interact other GUI elements while background task is processing.
I have searched in Android developer site and found that we can use animated drawables but don't know how to use them. Please advise on the same.
The very simple way to do this without using the animated drawable is to use "PregressBar" component in the design layout xml. When u need to show it, just set it's visibility property to visible and when you need to hide it, u can set it's visibility property to GONE. But remember this is UI task so when u need to do this with non-UI thread, u need to use Handler to set the status of "ProgressBar" component at runtime.
Below id the component in the layout file.
<ProgressBar
android:id="#+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ProgressBar>
Below is the code written in java file
ProgressBar prg;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
prg=(ProgressBar)findViewById(R.id.ProgressBar1);
prg.setVisibility(ProgressBar.GONE);
}
public void start_background_process()
{
// starting the process
prg.setVisibility(ProgressBar.VISIBLE);
new Thread(new Runnable()
{ public void run()
{
// Do your background stuff here which takes indefinite time
mHandlerUpdateProgress.post(mUpdateUpdateProgress);
}
} ).start();
}
final Handler mHandlerUpdateProgress= new Handler();
final Runnable mUpdateUpdateProgress = new Runnable() {
public void run() {
// ending the process
prg.setVisibility(ProgressBar.GONE);
}
};
If the default progress indicator is good enough for you (i.e. the spinning wheel), then you can just use ProgressBar. To change it from a normal progress bar to a spinning wheel, use progressBar.setIndeterminate(true).