Meet your new best friend, the Android on click listener. Buttons are one of the most important building blocks in any application. Thus, their implementation is a key milestone in learning any new language that deals with GUIs. The following should get you through making any View, Button, or Image respond to touch in Android Studio.
System:
Android Studio 3.3
macOS
Project Setup
I started off with an empty activity.
File > New Project > Empty Activity
Click Next > Finish and accept all defaults on the way.
Android Studio will now create a bunch of files and folders. Luckily we will only need two of the files to get our button to work. Navigate to your activity_main.xml file using the explorer on the Left of Android. XML files define how your Android app looks and Java files contain the application logic.
XML
Double clicking activity_main.xml will open a user interface for editing your app’s look. Let’s go ahead and click Text on the bottom so we can manually edit the xml.
Let’s delete the default TextView and replace it with the following.
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Toast"
android:id="@+id/toast"
/>
layout_width=”match_parent” sets the button width to the screen’s width since it’s directly placed into the root view. Similarly layout_height=”match_parent” sets button height to the screen’s height. text=”Toast” sets the button text to Toast. If layout_width and layout_height are not defined then we will not see our button and will not be able to click it! Finally, we assign a distinct ID to the button using android:id=”@+id/toast” so we can easily find it amongst everything else in the app.
Java
Let’s tack on the following directly after setContentView(R.layout.activity_main) to set up our Android on click listener.
setContentView(R.layout.activity_main); // This pulls up the xml document
Button button = findViewById(R.id.toast); // Find the button and assign to variable of type Button
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Yay, our button works!", Toast.LENGTH_SHORT).show();
}
});
Button button = …. blah blah blah is where we find our button and assign it to a variable of type button. We could have cut out the middle man and said the following if we had wanted to.
findViewById(R.id.toast).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Yay, our button works!", Toast.LENGTH_SHORT).show();
}
});
And that’s pretty much it. If you had an image on screen that you wanted to make clickable change Button button = findView…. to Image button = findView…, or for a Text View do TextView button = findView….. Go ahead and run your first button on an emulator (by clicking the green play button shown below) and you’ll see a message appear when you click anywhere on the screen excluding the top bar.
Our app in action!
Feel free to leave questions or comments. Ready for something a little more complicated? Head over to our Google Translate API Android article. Cheers!
Finished Product: https://github.com/cvcaban/firstButton
Hey this is somewhat of off topic but I was wondering if blogs use WYSIWYG editors or if you have to manually code with HTML.
I’m starting a blog soon but have no coding experience so I
wanted to get guidance from someone with experience.
Any help would be enormously appreciated!
Look into WordPress. You can host it on a server. 0 coding experience needed. You do need to be kinda tech literate. There are tons of how-tos online. Cost is somewhere around 5ish bucks a month for the hosting.
My coder is trying to convince me to move to .net from PHP.
I have always disliked the idea because of the expenses.
But he’s tryiong none the less. I’ve been using WordPress on a number of websites for about a year and am nervous about switching
to another platform. I have heard great things about blogengine.net.
Is there a way I can transfer all my wordpress content into it?
Any help would be really appreciated!
WordPress is written in PHP. I’d stay with what you’re comfortable with.