Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7046749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:41:15+00:00 2026-05-28T02:41:15+00:00

This is going to be a real noob question, so please have mercy. I

  • 0

This is going to be a real noob question, so please have mercy. I am trying to create a message box on a button click event in Android. I have read some examples on StackOverflow, but I can’t seems to grasp the concept. In my main.xml file, I have defined the button xml as follows:

<Button
android:id="@+id/btnOK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display Message"
android:onClick="onBtnClicked" />

I read on one of the posts that I need to register the onClick event in the XML layout. So that is what I thought I did in the XML code above. Then, in my java code file, I have written the following code:

package com.example.helloandroid;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class HelloAndroid extends Activity 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        
    }   

    public void onBtnClicked(View v)
    { 
        if(v.getId() == R.id.btnOK)
        {
            MessageBox("Hello World");
        }       
    }

    public void MessageBox(String message)
    {
       Toast.makeText(this, message, Toast.LENGTH_SHORT);
    }   
}

To me, this makes sense. But the message box does not display when I click on the button. From the code imports above, you can see that I have already tried a few solutions without success. Am I perhaps missing a listener? I thought that the definition in the XML code would create this for me?

Thanks in advance 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:41:16+00:00Added an answer on May 28, 2026 at 2:41 am

    Change

    Toast.makeText(this, message, Toast.LENGTH_SHORT);
    

    To

    Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    

    The show() makes sure you actually display the Toast, else you are only creating the Toast.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a noob question. I'm trying to learn how to use SWIG to
This is going to be a very simple question, I have code that looks
Hey this is going to be one of those dumb questions. I am trying
This is going to be very simple I know. I have seen so many
I know this is not a real programming question. But, it relates to programming
This is going to take a bit to explain. I'm creating my first real-world
Dear all, this is going to be tough: I have created a game object
Hi guys i am trying an sample where i have a button which downloads
New to databasing, so please let me know if I'm going about this entirely
I'm trying to create a real-time charting utility in android, and for the most

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.