Android: How to only hear when my custom class is clicked - android

when I make a custom view class and add a clickListener it fires anywhere on the screen I click, even where the custom view is not. If I use the same code with a button from a layout it only fires when I click the button not anywhere on screen. Any ideas how to just only listen for when my custom class is directly clicked?
button only fire when pressed
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d(DEBUG_TAG, "button clicked");
}
});
stroke object fires when you press anywhere on screen, even outside of stroke's bounding box
Stroke stroke = new Stroke(this);
mainLayout.addView(stroke);
stroke.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// fires on every screen click :>(
Log.d(Main.DEBUG_TAG, this.toString()+"shape clicked");
}
});

I think your custom view just fills the whole screen. That's why it reacts on every click on the screen. You need to make it smaller and everything will work fine.

Related

HavingListeners on top of other listeners - android

I have a RelativeLayout that conatins a number of ImageViews and Buttons etc. Each have their own listener allowing the user to set up various things.
After the user presses the start Button, I would like some of these listeners to go inactive while the whole screen has an onClickListener() that will listen for clicks anywhere on the screen.
How can this be done?
Get your parent layout an add the new listener to it. something like:
frameLayout = ((FrameLayout) findViewById(R.id.mylayout));
screenClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
}
};
frameLayout.setOnClickListener(screenClickListener);
stop listeners:
either
myClickListener = null
or
myClickableButton.setClickable(false);

Button wont become invisible after being clicked - Android

I have a button that is set to VISIBLE under certain circumstances, then once its clicked its suppose to make the button INVISIBLE again but for some reason its not working. Here is my code,
if(variable == 2){
testButton.setVisibility(View.VISIBLE);
testButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
testButton.setVisibility(View.INVISIBLE);
test2Button.setVisibility(View.VISIBLE);
}
});
}
Have you tried displaying a toast when the button is clicked, just to see if that block of code is even executing? I don't see it, but I'm assuming you've actually declared a View associated with that button via 'findViewById'
EDIT:1
Do this
public void onClick(View view) {
view.setVisibility(View.INVISIBLE);
findViewById(R.id.<your test2Buttons ID>).setVisibility(View.VISIBLE);
}
Note: If you do View.GONE it will leave all area acquired by it and the other control will capture this area
where is with View.INVISIBLE it will maintain its acquired area

Is it possible to click on two buttons at the same time, if one covers the other one?

Sorry for the weird question, but is it possible to click on two buttons at the same time in android(having two logs, "clicked on b1" and "clicked on b2"), if one totally covers the other one?
This is not ordinarily possible; the top button will absorb the button click event and not pass it on to the one behind it. It is not clear whether or not you want to obtain this behaviour or avoid it, nonetheless, you can force it by propagating the event manually across the click listeners.
Here is one way (there are a few); assume buttonOne is on top of buttonTwo:
final Button buttonOne = (Button) findViewById(...);
final Button buttonTwo = (Button) findViewById(...);
buttonOne.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ButtonTest", "ButtonOne clicked");
buttonTwo.performClick();
}
});
buttonTwo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ButtonTest", "ButtonTwo clicked");
}
});
The click event enters the listener on button one, which then causes a click event on button two.
Here is another way which would work (and could be changed to support long click events easily):
final Button buttonOne = (Button) findViewById(...);
final Button buttonTwo = (Button) findViewById(...);
final OnClickListener listenerTwo = new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ButtonTest", "ButtonTwo clicked");
}
};
final OnClickListener listenerOne = new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ButtonTest", "ButtonOne clicked");
listenerTwo.onClick(buttonTwo);
}
};
buttonOne.setOnClickListener(listenerOne);
buttonTwo.setOnClickListener(listenerTwo);
Yes, it is possible. You will need to pass the click event that occurs on the foreground view to the background view. You can do this by checking where the click occurs and if it occurs within the view's bounds.

Layout in android

I have three buttons in one activity.when each of the button is clicked ,different layout should be shown with in the same activity.for example if first button is clicked,edit boxes and button should be shown.if second buttojn is clicked listview should be shown etc..
Define different layout files for each layout.
Then after each click event have the intent call this particular activity recalled.
Have setContentView() called conditionally ie determining the particular clickevent and vice versa.
This you can do if you want complete activity to be layuot in diffrent manner. Otherwise if you want some widgets to be displayed on button click then it is pretty easy to show them on click event.
You might wanna consider a "TabWidget" for this. It actually does what you need.
A sample tutorial here.
Why don't you just include the all the layout elements in your single layout, then use the setVisibility attribute to turn them on and off, depending on which button is pressed.
Something like this pseudo code:
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
view1.setVisibility(View.GONE);
view2.setVisibility(View.GONE);
view2.setVisibility(View.VISIBLE);
}
});
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
view1.setVisibility(View.VISIBLE);
view2.setVisibility(View.GONE);
view2.setVisibility(View.GONE);
}
});

passing click event to underlying view

I have a Layout with a TextView. The TextView has
android:autoLink="all"
How can I achieve the following:
if user clicks a link, an action associated with that link is executed (i.e., click on phone number invokes dialer, etc.)
if user clicks anywhere else within Layout boundaries, Layout's onClick is called.
Thanks.
Attach an oclicklistener to the TextBox layout, and handle it from there.
TextView txt = (TextView) findViewById(R.id.yourTextBoxId));
txt.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// USer Clicked the textBox
}
});
The same apply for the Layout, find it and ...
layout.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// USer Clicked the layout
}
});
I hope it helps.

Categories

Resources