When I click an Android button the Android's Software Keys are shown (hidden before) instead of firing the onClick() method for the actual button that I click.
I have two methods for showing or hiding the system UI:
1.
// Set up the user interaction to manually show or hide the system UI.
contentView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (TOGGLE_ON_CLICK) {
mSystemUiHider.toggle();
} else {
mSystemUiHider.show();
}
}
});
and 2:
/**
* Touch listener to use for in-layout UI controls to delay hiding the
* system UI. This is to prevent the jarring behavior of controls going away
* while interacting with activity UI.
*/
View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (AUTO_HIDE) {
delayedHide(AUTO_HIDE_DELAY_MILLIS);
}
return false;
}
};
And I also have another method which a switch to get the onClick() event on buttons
public void onClick(View v) { switch() { } }
Is it possible to fix this behaviour and when I click the actual button will be fired and the System UI will be shown?
There is no workaround to overcome this behaviour until you have both touch listener and also a click listener inside the same view. Try using
View.SYSTEM_UI_FLAG_LOW_PROFILE|View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
instead. Add the below code before setcontentview() method.
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_LOW_PROFILE|View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
decorView.setSystemUiVisibility(uiOptions);
Related
When my app starts, the user needs to touch on the screen before the real action starts. I have a textView which gives the hint to touch the screen.
After the screen is touched, I want the text to get invisible. Right now the textView never disappears and always stays in the front.
public class MainActivity extends Activity implements OnGestureListener
{
public boolean touched = false;
TextView mMyView;
public void onTouch()
{
mMyView.setVisibility(View.GONE);
touched = true;
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
mMyView = (TextView)findViewById(R.id.textView6);
if(touched == true)
{
}
}
}
1.Always use if(something) if you want to see if it's true/false instead of writing if(something == true) [something is a boolian assigned with value true.]
2.If you point your views xml to a method using android:onClick like below,
<Button android:id="#+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:onClick="onTouch" />
.
What's the point of implementing OnGestureListener?
If i do this onCreate i initialize my view
View myView = findViewById(R.id.my_view);
3.If i really want a touch i will do this
myView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// ... Respond to touch events --> tv.setVisibility(View.INVISIBLE);
return true; // if you return false with this then the listener will not be called for the subsequent ACTION_MOVE and ACTION_UP string of events.
}
});
Now you can see in the 3rd ones parameter there is a MotionEvent, you can identify the motion ACTION_DOWN , ACTION_MOVE and ACTION_UP
Now think have you ever used them. You got an idea in your head about a touch so tried to use touch events .. But you don't use them. So it does the same as what onClickListner does in your case. If you want motions use that 3rd one i gave.
But simply you can use
// view is the background layout
myView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Do something here --> Hide your text tv.setVisibility(View.INVISIBLE);
}
});
Those view onClickListner or setOnTouchListener you can directly use them inside onCreate or keep them inside a method and you can call that method from onCreate. Why to keep a boolean? It's nothing major
Note i considered myView as the background layout not your textView , background is the one you click / touch
So now you changed the questions code several times and I hope it´s the final change. Only than my answer could help.
You have done this in your onCreate():
if(touched == true)
{
tv.setVisibility(View.INVISIBLE);
}
But this is executed directly and has nothing to do with you onTouch() method. Let´s assume your onTouch() works correctly. Make the TextView global:
TextView mMyView;
initialize it in onCreate():
mMyView = (TextView)findViewById(R.id.textView6);
and then hide it in onTouch():
onTouch(View view){
mMyView.setVisibility(View.GONE);
}
But you have to be sure that your method onTouch() works. You can make a Toast or a Log to check. You have to be sure that:
-The TextView is inside your layout xml that you set with setContentView(R.layout.activity_game);
-The onTouch() method is declared in your TextView's xml attribute
android:onClick="onTouch"
and set clickable of your TextView to true:
android:clickable="true";
EDIT
If you implement onGestureListener() I guess the touch event is consumed by the listener and your TextView did not recognize onTouch(). If you don´t do any gesture detection in your activity, then remove this implementation.
You are checking if screen was touched in onCreate() which is called only once at the start of the activity. Initialize TextView globally and set its visibility inside onTouch(View v, MotionEvent event)
Also your onTouch() isn;t correct. You should override public boolean onTouch(View v, MotionEvent event)
public class MainActivity extends Activity
{
public boolean touched = false;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
TextView tv = (TextView) findViewById(R.id.textView6);
tv.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
touched = true;
tv.setVisibility(View.INVISIBLE);
return true;
}
});
}
}
Instead of implementing OnGestureListener add a setOnTouchListener in your root view of your activity layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rlTestView"/>
For example rlTestView is your activity's root layout id, then use below code in your oncreate method
((RelativeLayout)findViewById(R.id.rlTestView)).setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
tv.setVisibility(View.GONE);
return true;
}
});`
Use the code below on the onCreate method and yes set the visibility as GONE instead of invisible. Also state the current visibilty of the TextView in the onTouch then set it to
tv.setVisibility(View.GONE);
I have a custom dialog which extends the Dialog Class, I would like to bind an event to execute some code after the Dialog is closed when the user presses the BACK button of the device. How can I do that? I found a post where someone says that the .addWindowListener() should be used when working with Dialogs and other Window widgets. But the dialog class doesn't have the addWindowListener method, so I cannot use it. Is there another way without using fragments cause I shouldn't re-write the MyCustomDialog class?
This is the code:
public class MyCustomDialog extends Dialog {
public MyCustomDialog(Context context, int layoutResourceId) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(layoutResourceId);
}
}
Thanks for the attention!
EDIT: i found this on the android developers site, is there a way to use it with MyCustomDialog class?
onDismiss DialogInterface
Since you are extending android Dialog class you can implement a Dismiss Listener in your Activity's and set it when you create the Dialog, and then in the listener implement any functionality you want depending on the button that was used to dismiss the Dialog.
Hope this will solve your problem.
Edit You can use dialog.setCanceledOnTouchOutside(false); which will stop closing the dialog if you touch outside of the dialog.
Something like,
Dialog dialog = new Dialog(context)
dialog.setCanceledOnTouchOutside(false);
OR Alternatively
Override onTouchEvent() of dialog and check for action type. if the action type is
'MotionEvent.ACTION_OUTSIDE' means, user is interacting outside the dialog region. So in this case, you can dimiss your dialog or decide what you wanted to perform.
view plainprint?
dialog.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_OUTSIDE){
Toast.make(getApplicationContext(), "TOuched outside the dialog", Toast.LENGTH_LONG).show();
this.dismiss();
}
return false;
}
});
And for back press you can do dialog.setCancelable(false); which will prevent dialog getting cancelled from backpress event.
OR you can alternatively override setOnKeyListener event and put your own code into it.
Edit
dialog.setOnKeyListener(new Dialog.OnKeyListener() {
#Override
public boolean onKey(DialogInterface arg0, int keyCode,
KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
dialog.dismiss();
}
return true;
}
});
Happy Coding!!!
You need to Override onBackPressed inside Dialog class. Also make sure to close dialog after override OnBackPressed .
Try this
public class MyCustomDialog extends Dialog {
public MyCustomDialog(Context context, int layoutResourceId) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(layoutResourceId);
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
dismiss(); // make sure to call dismiss to close dialog
// put your code here
}
}
If you want to trigger an event if user clicked/touched outside the dialog and close it or used the back button to close then use
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
//your trigger goes here
Toast.makeText(IntroductoryActivity.this, "on cancel", Toast.LENGTH_SHORT).show();
}
});
But if you want to trigger an event if something dismisses the dialog, like some other event then use
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
//your trigger goes here
Toast.makeText(IntroductoryActivity.this, "on dismiss", Toast.LENGTH_SHORT).show();
}
});
I have a simple AlertDialog that displays a list of some items and upon clicking one of them, the clicked item is passed back to the enclosing Activity. I also want to perform some default handling when the user cancels the dialog (using the back button) - more specifically, I want to pass an empty string to the activity in such case.
However, if I put the dialog in a DialogFragment (from the compatibility package), the OnCancelListener is not called when I close the dialog with the back button. What am I doing wrong?
public class SelectItemDialog extends DialogFragment {
public interface Callback {
void onItemSelected(String string);
}
private static final String ARG_ITEMS = "items";
private Callback callback;
public static SelectItemDialog fromItems(Collection<String> items) {
SelectItemDialog fragment = new SelectItemDialog();
fragment.setArguments(newArguments(items));
return fragment;
}
private static Bundle newArguments(Collection<String> items) {
Bundle arguments = new Bundle();
arguments.putStringArray(ARG_ITEMS, items.toArray(new String[items.size()]));
return arguments;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
callback = (Callback) activity;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final String[] items = getArguments().getStringArray(ARG_ITEMS);
return new AlertDialog.Builder(getActivity())
.setTitle(R.string.dialog_select_email_title)
.setItems(items, new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
callback.onItemSelected(items[which]);
}
})
.setOnCancelListener(new OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
// this code is not executed
callback.onItemSelected("");
throw new RuntimeException("dialog cancelled");
}
})
.create();
}
}
It might have to do with the fact that there is no explicit call to cancel() from your code.
The OnCancelListener documentation says:
This will only be called when the dialog is canceled
Which probably needs an explicit cancel() call.
Either make a positive/negative button with a OnClickListener that calls DialogInterface#cancel() or use a OnDismissListener() with an extra check to see if a list item was clicked.
Also, to listen for a back keypress and cancel the dialog, you can set up an OnKeyListener, like outlined in this SO answer
Also, once you have the Dialog set up, it would also be a good idea to use Dialog#setCanceledOnTouchOutside() in case the the user taps outside the Dialog.
Edit: The below part is the easy way to handle cancel events in a DialogFragment.
Since you are using a DialogFragment, this class has a very handy method, DialogFragment#onCancel() which gets called when the DialogFragment is cancelled. Do your logic in there.
DialogFragments are more complex, with a slightly different lifecycle than normal dialogs. Therefore, first check the documentation if you have a certain Dialog-based approach that you are trying to port to a DialogFragment, some methods may exist that allow your new implementation to function properly!
If you are using DialogFragment and want to listen back button then use this -
this.getDialog().setOnKeyListener(new Dialog.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (****) {
your logic
}
return true;
}
return false;
}
});
Note: DialogFragment own the Dialog.setOnCancelListener and Dialog.setOnDismissListener callbacks. You must not set them yourself.
To find out about these events, override onCancel(DialogInterface) and onDismiss(DialogInterface).
public class SelectItemDialog extends DialogFragment {
#Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
//your code hear
dialog.cancel();
}
}
And you should remove .setOnCancelListener()
Actually if you want to use DialogFragment, you can never add OnCancelListener or OnDismissListener to it, since the Dialog Fragment owns callbacks to these methods!
You have 3 options here:
1- go with regular dialogs.2- set your dialog fragment to cancellable(false) and add a cancel button to the dialog.3- check #Nikhil Pingle answer.
this is from the documentation of the Dialog Fragment
* <p><em>Note: DialogFragment own the {#link Dialog#setOnCancelListener
* Dialog.setOnCancelListener} and {#link Dialog#setOnDismissListener
* Dialog.setOnDismissListener} callbacks. You must not set them yourself.</em>
* To find out about these events, override {#link #onCancel(DialogInterface)}
* and {#link #onDismiss(DialogInterface)}.</p>
Cancel Listener or Dismiss listener in DialogFragment can achieve by onDismiss
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
newFragment.onDismiss(new DialogInterface(){
#Override
public void cancel() {
// TODO Auto-generated method stub
}
#Override
public void dismiss() {
// TODO Auto-generated method stub
}
});
// Set up the user interaction to manually show or hide the system UI.
contentView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (TOGGLE_ON_CLICK) {
mSystemUiHider.toggle();
((ZooView)contentView).editmode = mSystemUiHider.isVisible();
} else {
mSystemUiHider.show();
}
}
});
This is my code, I am trying to update a variable in a custom view (ZooView) to know whether or not the view is in which mode (editmode a custom variable changing OnDraw method primarily)... I tried to invalidate the view when it toggles on click but that's not it, because the logcat showed it wasn't even getting to this function regularly. (only sporadically)
Any ideas?
mSystemUiHider
.setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
// Cached values.
int mControlsHeight, mControlsWidth;
int mShortAnimTime;
#Override
#TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
public void onVisibilityChange(boolean visible) {
((ZooView)contentView).editmode = visible;
putting the change here fixed it totally! :)
The java.lang.RuntimeException is "Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead," but that is not correct. I am using setOnItemClickListener to do some stuff based on the new selection, but I also need to do some stuff before the user changes the selection. Specifically, I am collecting data for each selection that needs to be saved to a file before moving to another selection, since the other selection is associated with different set of data. Is there a way to use setOnClickListener with an Android Spinner?
spinner.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do some stuff before the user changes the selection
...
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
// Do some stuff based onItemSelected
...
You can replicate the an onclick event using ontouch events
this.spinner=(Spinner)findViewById(R.id.spinner);
this.spinner.setClickable(false);
this.spinner.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.v(TAG, "spinner touch");
//replicating a click
if(event.getAction() == MotionEvent.ACTION_UP){
v.playSoundEffect(android.view.SoundEffectConstants.CLICK);
}
return true;
}
});
You will have to set the Click listener on the underlying view (normally a TextView with id: android.R.id.text1) of the spinner. To do so:
Create a custom Spinner
In the constructor (with attributes) create the spinner by supplying the layout android.R.layout.simple_spinner_item
Do a findViewById(android.R.id.text1) to get the TextView
Now set the onClickListener to the TextView