I create a whole layout setup in XML then attempt to attach listeners to the buttons using findViewById(). The problem I am having now is that the View parameter I receive in the method does not contain the ID of the view I clicked: 830009633920 vs 2131099657.
Button btnNext = (Button)findViewById(R.id.btnNext);
btnNext.setOnClickListener(this);
#Override
public void onClick(View view) {
if (view.getId() == R.id.btnNext) {
...
}
}
How about this:
Button btnNext = (Button)findViewById(R.id.btnNext);
btnNext.setOnClickListener(this);
#Override
public void onClick(View view)
{
switch (view.getId())
{
case R.id.btnNext:
...
break;
case R.id.foo:
...
break;
}
}
That should be working, so my only guess without seeing the rest of the code or layout is that you have set click handlers on two items that overlap.
If you plan on making an Android Library project switch like this will not work due to a recent change in the tool chain.
http://tools.android.com/tips/non-constant-fields
In general I find it is much simpler to use an anonymous class the handle clicks rather than a large single handler.
btnNext.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view)
{
...
}
});
You might try this approach just to debug that the item you expect to get the click is what you are pressing, to make sure you don't have layout issues.
Try skipping the IDs, and just compare the actually view object. so something like this:
#Override
public void onClick(View view) {
if (view.equals(btnNext)) {
...
}
}
Related
I am validating the EditText value by clicking anywhere inside the window. But for some buttons, I want to do something different from the validation functionality. How can I get the id of the item from the layout action. Here my layout is ConstraintLayout. Tried the below code but it's not working.
window_layout.setOnClickListener(new
View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_tech:
setButtonsVisibility();
break;
}
}
});
Please suggest a better way to implement that.
What you're doing right now is adding the listener to the parent view. You need to set the listener to the individual views.
You can add a listener variable and add it to the different button views.
You can also iterate your constraint layout child views and setting the listener individually.
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_tech:
setButtonsVisibility();
break;
}
}
}
for(int i = 0; i <= windowLay.getChildCount(); i++) {
rec.getChildAt(i).setOnClickListener(listener);
}
I was filling inside a getView() of an adapter while defining the click listeners, this got me wondering:
Performance-wise, is there a difference between these two implementations:
Defining onClickListener separately:
View.OnClickListener mClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) { //... }
};
mView.setOnClickListener(mClickListener);
and
Defining onClickListener as the argument:
mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) { //... }
});
As getView() is frequently called, my though is that even small differences in its implementation would have big effect in app's performance. But I'm not sure.
So, which of the above is recommended regarding the performance/memory? Or aren't they that different to matter?
Well that actually depends on how many OnClickListeners do you have as anonymous classes.
it would be better to implement one click listener and set it to all of the views on which you want to get click event and sort the clicks out with view id's in OnClick(View v)
public void onClick(View v) {
switch (v.getId()) {
case R.id.some_view_id:
doSomething();
break;
}
}
Inside my Activity I start a simple dialog.
final Dialog myDialog = new Dialog(this);
myDialog.setContentView(R.layout.testing);
...
My testing.xml Layout consists of nothing but 10 ImageViews, id`s are '1' to '10'.
I want every ImageView to be clickable and to do something.
The define the onclick() methode in the .xml file isn`t working, as the methode can't be found when the dialog is viewed.
The only way I got it work is following: define 10 onclick-listeners:
ImageView img_1 = (ImageView) myDialog.findViewById(R.id.1);
ImageView img_2 = (ImageView) myDialog.findViewById(R.id.2);
...
img_1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
execute_funtion(1);
myDialog.cancel();
}
});
img_2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
execute_funtion(2);
myDialog.cancel();
}
});
...
However, that's really bad code, I have 10 times nearly the same lines.
So my question: How can I make that work with clean code?
I thought about a multiple onclicklistener (overwride the onClick() function and make a switch/case in the functions or something like that), but it's not working.
I'm happy about every idea!
Thanks
/EDIT
Here a snippet of the .xml file
<ImageView
android:id="#+id/1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:onClick="myFunction"
android:src="#drawable/ic_launcher" />
Make your Activity implement OnClickListener and then process the onClick event like below:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.img1:
...
break;
case R.id.img2:
...
break;
}
}
You should let your Activity/Fragment implement the OnClickListener.
When you do that you will have to override the onClick method in that particular activity/fragment.
Set the onClickListeners on the images as follows:
img_1.setOnClickListener(YourActivity.this);
Then in that onClick method you can put a switch case or an if else if case as follows
#Override
public void onClick(View v)
{
if(v==img_1) {
//do this
} else if(v==img_2) {
//do that
}...
}
or
#Override
public void onClick(View v)
{
switch (v.getId()) {
case img_1.getId(): // do this
break;
case img_2.getId(): // do that
break;
.
.
.
default : break;
}
}
I'm having a button in a sliding drawer in a Android Application. The problem is it does not seem to react to any clicks as normal buttons do.
I'm guessing the problem is that it's a different view than buttons on the normal view.
If I implement a button the normal way like this
myAgenda = (Button)findViewById(R.id.BtnMyAgenda);
myAgenda.setOnClickListener(this);
public void onClick(View v) {
switch(v.getId()){
case R.id.BtnMyAgenda:
test.setAnimation(leftLeft);
test.startAnimation(leftLeft);
break;
}
I'm guessing there is something wrong with the above code since the button is in a SlidingDrawer and not in the "normal" view.
Any ideas how to fix the problem?
Here is the code
Register with event listner like below code
button.setOnClickListener(clickButtonListener);
and create this listner for button
private OnClickListener clickButtonListener= new OnClickListener()
{
#Override
public void onClick(View v)
{
if(v == button)
{
}
}
}
I actually found the solution to the problem, I simply created a new view.onclicklistener specific to that button.
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
Is there a way to have 1 onClick Lister for many buttons where I can toss a case statement to do things based on what buttons were clicked.
I know I can make 100 different listeners for 100 buttons but I have to think I can create some nifty variables to do it in less lines of code.
Button btn1, btn2;
public void onCreate(Bundle b)
{
// here you do normal things like assigning a
// content view to the activity, initiate buttons, etc.
// then you assign the same listener to both buttons
btn1.setOnClickListener(yourListener);
btn2.setOnClickListener(yourListener);
}
// declare a OnClickListener that will execute different actions
// depending on the view that was clicked
View.OnClickListener yourListener = new View.OnClickListener(){
public void onClick (View v){
if( v == btn1 ){
// do something
}
elseif( v == btn1 ){
// do another thing
}
}
};
If you are using 1.6+ version of the SDK you can use android:onClick to set the onClick handler of a view. In your activity you must have a method with the following signature. The view is the view that was clicked.
void onClick(View v) {
switch(v.getId()) {
case R.id.button1:
//do something fantastic;
break;
}
}
public class MainActivity extends Activity implements View.OnClickListener{
btnXXX.setOnClickListener(this);
public void onClick(View v) {
if (v.getId()==R.id.btnXXX){
dialog.show();
} else {
handleOtherViews(v);
}
}
Alternatively, you can specify the method to call in xml:
<Button android:id="#id/button" android:text="#string/button" android:onClick="someMethod" />