Detecting touch by user at any point of time - android

I have a child view in my parent view.
Now I am using animation to show child view for 10 sec and hide it after that.
I want to show the child view even after 10 sec only if user touches it while animating.
So how can I determine if the child view is touched by user or not?
Does android provide any API to determine if view is touched by user at any instance of time?

You can set the "clickable" property to true and setOnClickListener(). like this:
exampleView.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
//TODO
}
});
You can also use the setOnTouchListener for more information on the "click".
Take a look here.

Related

Android Have View Block/Swallow Click Event

I have a view on top of another view. The bottom view is supposed to fire a callback when clicked. The top view is not supposed to fire any callback when clicked.
The problem I am facing is that when there is a click on the top view, the bottom view fires its callback. I want to prevent this.
I've read a number of posts on disabling clicks and they all suggest the same thing:
view.setEnabled(false);
and
view.setClickable(false);
Neither of these prevent the top view from swallowing/blocking the click event.
Do you know how I can have a view prevent passing clicks through?
Its a little hacky but the best solution I came up with was adding an empty onClickListener to swallow the event.
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// HACK disables click through events by swallowing click
}
});
I couldn't understand what you are trying to do without an example.
But if you want to disable click for a particular view & you don't have any click functionality for this view, then try
view.setOnClickListener(null);
Also, did you try view.setFocusable(false); ??

Repeatable process where image can be added when a button is click

I am trying to attempt the below :
What i am trying to do is whenever a user click on a button, it will add a image to the layout(which the user can scale and move to any position within the layout). The user can repeat this process hence if the user click 6 times on the button, there should be 6 image in the layout.
i have google around, looking for some sample but ain't sure if its the correct one to use. maybe i did not use the correct keyword to search. So far i have seen is endless adapter and RecyclerView but both seen to be mainly for "ListView" type.
Anyone can suggest or share a link/example that i can read up and learn from? It will be a great help.
If you want to add image on button click set on-click listener on it. You need to get how many time the user have clicked the button.
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//get adapter and view
//get image to add
addImage(i); //to desire view
}
});

Open button's image on Android when button is visible

I have a large canvas where I placed multiple buttons. Each button has an image which is opened based on the button click event. I want to change it such a way so that when a button enters to the screen area, it will automatically open the button image.
I guess I need to find the current button view (that is visible on the screen) and then use functions to simulate the button click event (View.performClick();). As I am not entirely sure, any suggestion would be highly appreciated.
Can you try to take both view button view and image at the same position, when click on button view then this view hide and show image.
I think the simplest way is to add ImageButton and change the image/background by click
Something like that:
boolean isShown;
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(isShown){
//set empty bg
}else{
//set right content
}
isShown = !isShown;
}});

ListView row is not clickable after adding button to the row

Currently, I have a ListView row. Clicking any area on the row, will yield ListView click events.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
this.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
this.getListView().setMultiChoiceModeListener(new ModeCallback());
this.getListView().setOnItemClickListener(new ListViewOnItemClickListener());
} else {
// http://stackoverflow.com/questions/9754170/listview-selection-remains-persistent-after-exiting-choice-mode
this.getListView().setOnItemLongClickListener(new ListViewOnItemLongClickListener());
this.getListView().setOnItemClickListener(new ListViewOnItemClickListener());
}
Now, for each row, I would like to add a small button. Clicking on the small button will yield button click event, which is different from ListView original click event.
In my ArrayAdapter, I use to have
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = activity.getLayoutInflater();
rowView = inflater.inflate(R.layout.watchlist_row_layout, null);
...
}
Button button = (Button)rowView.findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Utils.showLongToast("BUTTON CLICK");
}
});
Clicking on the button will yield button click event. However, clicking on area other than button, will not yield any ListView click event anymore.
May I know why is it so? Is there any way to resolve this?
If you want to make both Rows and Buttons clickable, set
android:focusable="false"
for your Button. The same goes for ImageButtons, RadioButtons, ...
Android has been primarily designed for a large set of input methods.
The entire system is completely capable of working with no touch
screen. To navigate through the UI, the user can use a directional pad
which focuses Views after Views if and only if those Views are
focusable. By default, all Android controls are focusable. In order to
prevent having controls that are not focus-reachable, the ListView
will simply prevent the selection (and click) of an itemview. By
design, the ListView blocks clicks of itemview containing at least one
focusable descendant but it doesn’t make the content focus-reachable
calling setItemsCanFocus(true).
Here is the whole great explaination: Having several clickable areas in a ListView
It's worth noting that the preferred way (even according to the link provided in the accepted answer) is to set the android:descendantFocusability attribute to block focus from the root layout you're inflating within the adapter. There can be potential situations where simply setting the focusable attribute to false on an individual element won't solve the problem, but the descendantFocusability attribute will.

android: How to set a listener that fires when my ViewFlipper shows a new child

I have a ViewFlipper for which I want a listener to fire when the child displayed is changed. I have set an OnFocusChangeListener to the ViewFlipper but it never fires when I flip from child 0 to child 1 or vice-versa. The ViewFlipper contains two RelativeLayouts and I have tried setting OnFocusChangeListeners for those but I get a ClassCastException when I try to set it. Here's my code:
RelativeLayout songsLayout = (RelativeLayout) findViewById(R.id.song_page_layout);
songsLayout.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View view, boolean hasFocus) {
showPopUp("View " + view.getId() + " now has focus: " + hasFocus);
}
} );
R.is.song_page_layout is one of my RelativeLayouts and showPopUp() is a function I use to show, well, popups.
Does anyone have working code for some sort of trigger that fires when a ViewFlipper changes which child is displayed?
I think you don't understand what "focus" means on Android; it's when a user selects that View, for example when they select an EditText by pressing on it, or they use the keyboard to select a button. When a ViewFlipper switches Views, it does not gain or lose focus.
As far as I know, you can either:
Don't use ViewFlipper's automatic flipping, instead manually calling showNext() when you want it to flip (then setting the interval yourself in code).
Subclass or reimplement ViewFlipper and add a change listener to your liking.

Categories

Resources