Search code examples
javaandroidheaderdevelopment-environment

Hello world button?


I am beginning android studio. I need to know simply how to make a button have a toast notification that says hello world. I can not find a working way to do so. Can I use the header of it for other functions?


Solution

  • @Display Word: When user clicks on the button, directly inside the XML like that:

    <Button
         android:id="@+id/button"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="TextButton"
         android:onClick="buttonClickFunction"/>
    

    Using the attribute android: onClick we declare the method name that has to be present on the parent activity. So i have to create this method inside our activity like that:

        public void buttonClickFunction(View v)
               {
                Toast.makeText(getActivity(), "Button Clicked", Toast.LENGTH_SHORT).show();
              }
    

    For details information please visit
    https://developer.android.com/reference/android/widget/Button.html