Android Button Onclick

Android button onClickListener

This task can be accomplished using one of the android's main building block named as Intents and One of the methods public void startActivity (Intent intent) which belongs to your Activity class.

An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.

An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.

Refer the official docs -- http://developer.android.com/reference/android/content/Intent.html

public void startActivity (Intent intent) -- Used to launch a new activity.

So suppose you have two Activity class --

  1. PresentActivity -- This is your current activity from which you want to go the second activity.

  2. NextActivity -- This is your next Activity on which you want to move.

So the Intent would be like this

Intent(PresentActivity.this, NextActivity.class)

Finally this will be the complete code

public class PresentActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);

setContentView(R.layout.content_layout_id);

final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Intent activityChangeIntent = new Intent(PresentActivity.this, NextActivity.class);

// currentContext.startActivity(activityChangeIntent);

PresentActivity.this.startActivity(activityChangeIntent);
}
});
}
}

Android: how to handle button click

Question 1:
Unfortunately the one in which you you say is most intuitive is the least used in Android. As I understand, you should separate your UI (XML) and computational functionality (Java Class Files). It also makes for easier debugging. It is actually a lot easier to read this way and think about Android imo.

Question 2:
I believe the two mainly used are #2 and #3. I will use a Button clickButton as an example.

2

is in the form of an anonymous class.

Button clickButton = (Button) findViewById(R.id.clickButton);
clickButton.setOnClickListener( new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
***Do what you want with the click here***
}
});

This is my favorite as it has the onClick method right next to where the button variable was set with the findViewById. It seems very neat and tidy that everything that deals with this clickButton Button View is located here.

A con that my coworker comments, is that imagine you have many views that need onclick listener. You can see that your onCreate will get very long in length. So that why he likes to use:

3

Say you have, 5 clickButtons:

Make sure your Activity/Fragment implement OnClickListener

// in OnCreate

Button mClickButton1 = (Button)findViewById(R.id.clickButton1);
mClickButton1.setOnClickListener(this);
Button mClickButton2 = (Button)findViewById(R.id.clickButton2);
mClickButton2.setOnClickListener(this);
Button mClickButton3 = (Button)findViewById(R.id.clickButton3);
mClickButton3.setOnClickListener(this);
Button mClickButton4 = (Button)findViewById(R.id.clickButton4);
mClickButton4.setOnClickListener(this);
Button mClickButton5 = (Button)findViewById(R.id.clickButton5);
mClickButton5.setOnClickListener(this);

// somewhere else in your code

public void onClick(View v) {
switch (v.getId()) {
case R.id.clickButton1: {
// do something for button 1 click
break;
}

case R.id.clickButton2: {
// do something for button 2 click
break;
}

//.... etc
}
}

This way as my coworker explains is neater in his eyes, as all the onClick computation is handled in one place and not crowding the onCreate method. But the downside I see is, that the:

  1. views themselves,
  2. and any other object that might be located in onCreate used by the onClick method will have to be made into a field.

Let me know if you would like more information. I didn't answer your question fully because it is a pretty long question. And if I find some sites I will expand my answer, right now I'm just giving some experience.

android button onClick don't do anything

Dialog fragment has separate set of life cycles.If you want to create a class adddialog,then extend dialog fragment and in oncreate of adddialog use setcontent view.

If you want to create simple dialog try this line of code.This will solve your problem.

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final Dialog addDialog = new Dialog(this);
addDialog.setContentView(R.layout.dialogedit);
final Button butEditAdd =(Button)addDialog.findViewById(R.id.btn);
//final Button butEditAdd=addDialog.getAdd();
addDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
butEditAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//addDialog.dismiss();
Toast.makeText(getApplicationContext(),"It's working", Toast.LENGTH_SHORT).show();
}
});
addDialog.show();
}
});

Android Studio: How to change a button's text onClick

You can try something like this: (You need to fill in your button id.)

public class MainActivity extends Activity implements View.OnClickListener {
private Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.yourbuttonid);
btn.setOnClickListener(this);
}

@Override
public void onClick(View v) {
btn.setText("X");
}
}

If you want to do this with multiple buttons you need to check which button is clicked, like this:

@Override
public void onClick(View v) {
if(v.getId() == R.id.yourbuttonid) {
btn.setText("X");
}else if(v.getId() == R.id.yourbuttonid2){
btn2.setText("X");
}
}

Hope that solves your problem. If you have any questions, feel free to ask ;)

Button on click listener

You have three options:

common in 1,2) You need to assign an id to each of your buttons in the layout XML file

<Button android:id="@+id/my_button1"
..........
/>
<Button android:id="@+id/my_button2"
..........
/>

1) In the activity's onCreate() method after setContentView() you need to set a new OnClickListener for each button.

 public class MyActivity extends Activity  {
int a;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button myButton1 = (Button) findViewById(R.id.my_button1);
Button myButton2 = (Button) findViewById(R.id.my_button2);

myButton1.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
// Do what you want here
a = 1;
}
});

myButton2.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
// Do what you want here
a = 2;
}
});
}

2) As you see in the first approach, we need to make a new Object from the OnClickListener for each button. We can mix all that into one OnClickListener for performance and readability reasons.

public class MyActivity extends Activity implements View.OnClickListener {
int a;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button myButton1 = (Button) findViewById(R.id.my_button1);
myButton1.setOnClickListener(this);
Button myButton2 = (Button) findViewById(R.id.my_button2);
myButton2.setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.my_button_1:
a = 1;
break;
case R.id.my_button_2:
a = 2;
break;
}
}
...
}

3) You don't need to assign an id for this option you just need to assign the method name in the XML and then implement the same method in the activity with the exact same name but it must take a View object as an argument.

<Button 
...
android:onClick="button1Click" />

<Button
...
android:onClick="button2Click" />

and then in your activity just write the methods.

public class MyActivity extends Activity implements View.OnClickListener {
int a;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void button1Click(View v) {
a = 1;
}

public void button2Click(View v) {
a = 2;
}
}

That's all your options. I personally prefer number 2.

Android programmatically created button onClick from class

You cannot assign a Context from a field. You probably "can't assign a right", because you're actually getting a nullpointerexception and editing the wrong location. That being said, getApplicationContext() isn't needed.

Also, might I suggest that you return the content layout rather than casting the context to an Activity?

public Navigation nav;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
nav = new Navigation(this);
setContentView(nav.mainMenu());

}

@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.btnGame:
nav.game();
break;
}
}

At first glance, I see that you never set a click listener on the button, so you should probably do that.

For example

Context mContext;
View.OnClickListener mClickListener;

Navigation(Context context) {
this.mContext = context;
if (context instanceof View.OnClickListener) {
this.mClickListener = (View.OnClickListener) context ;
}
}

Now use this

if (mClickListener!=null) {
button.setOnClickListener(mClickListener);
}

I'm also not sure what I should be expecting from v.getId() in MainActivity.java R.id.btnGame?

The ID doesn't match the variable name. Without explicitly giving the button an ID, it'll be -1

Android button onClick handler not found

Your xml: android:onClick="onSaveIdeaButtonClick" and your method is public void onSaveIdeaButtonClicked(View v).

Change your method for:

public void onSaveIdeaButtonClick(View v) 


Related Topics



Leave a reply



Submit