Add a button and handle button click in Android Studio

preview_player
Показать описание
Add a button to a new Android Studio project. Set the onClick property of the button, then make a method to handle that button click.

Рекомендации по теме
Комментарии
Автор

Dear Prof. Jones,

The way you conduct the video attracts me so much. Thank you for your fruitful video series.

As far as I knew, there are two methods to respond button click event, say,

Method 1
public void btnPauseGPSClicked(View v) {
// a toast is a way to show a message that will go away its own.
Toast.makeText(this, "Button Clicked", Toast.LENGTH_LONG).show();
}

Method 2
@Override
protected void onCreate(Bundle savedInstanceState) {
Button btnPauseGPS;


btnPauseGPS =
View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getBaseContext(), "Button Clicked", Toast.LENGTH_LONG).show();
}
});
}

Your video adopts the said method 1, but it refuse to work, as is told to you in my post yesterday. Fortunately, the second method works well. I'll be grateful if you are kind enough to offer me a technical analysis on the reason.

Thank you in advance.

REGARDS
BB

bereshyitbara
Автор

Just what I needed, when I need it... thanks for helping me bootstrap my way into Andriod development.

kadgx
Автор

Hi Sir,

First of all, 100s of thanks for your kind offer of such a helpful tutorial series. I love the way you explain programming skills technically.

1 As far as I knew that the difference between “ActionBarActivity” and “AppCompatActivity” is that AppCompatActivity is a new, more generic implementation which uses AppCompatDelegate class internally. At 5:12, I saw your “ActionBarActivity” is crossed out with strikethrough. My AS 2.2.1 provides "AppCompatActivity" automatically. Do I need to cross it out?

2 The source codes of your activity_gpsaplant.xml have two errors in my AS2.2.1. Both "menu" and "action_settings" are red words. To run the app, it is obvious to delete these two methods. But the emulator says: "Unfortunately, PlantPlaces15FS has stopped." when the Pause button is pressed. How to fix it remains a trouble to me. The two methods with two errors are as follows:
@Override
public boolean onCreateOptionsMenu(Menu menu){
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_gpsaplant, menu);
return true;
}
@Override
public boolean item){
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
// noinspection SimplifiableIfStatement
if (id == R.id.action_settings){
return true;
}
return
}

Regarding "menu", there are four solutions available according to my AS 2.2.1, say,
a. Create menu resource file 'menu gpsaplant.xml'
b. Create field 'menu'
c. Rename reference, and
d. Insert App Indexing API Code

Regarding "action_settings", there are nearly 10 solutions available according to my AS 2.2.1, including
a. Create id value resource 'action_settings'
b. Create field 'action_settings'
c. Rename reference, and more...

I really hope your future vides may fix such problems, so as to make the Pause button of this video workable.

Thank you in advance.

REGARDS
BB

bereshyitbara
welcome to shbcf.ru