In the Previous post we learned to use Intent to pass Data between Activities.Now we are going to learn about Toasts.A toast provides simple feedback about an operation in a small popup. It only fills the amount of space required for the message and the current activity remains visible and interactive. For example, navigating away from an email before you send it triggers a "Draft saved" toast to let you know that you can continue editing later. Toasts automatically disappear after a timeout.Its very easy to use Toasts and very useful while debugging your application since it provide visual feedback.
We are going to make a button to show the Toast message on clicking on it.So let us do it in few steps.See more about toast here
STEP 1:
This is our layout.We have a button in layout with onClick value "onclick"
activity_main.xml
Let us add a small code in the onclick method of MainActivity.java to show our Toast message
So this is our MainActivity.java
Now we are ready to click the RUN button and open your application.Press your button in MainActivity to see your Toast message.
Hope you have enjoyed this tutorial.Let us learn more about android programming in next tutorials.
We are going to make a button to show the Toast message on clicking on it.So let us do it in few steps.See more about toast here
STEP 1:
This is our layout.We have a button in layout with onClick value "onclick"
activity_main.xml
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onclick"
android:text="Next Activity"/>
</RelativeLayout>
Let us add a small code in the onclick method of MainActivity.java to show our Toast message
Toast.makeText(getApplicationContext(), "This is my toast message", Toast.LENGTH_LONG).show();
So this is our MainActivity.java
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onclick(View v)
{
Toast.makeText(getApplicationContext(), "This is my Toast message", Toast.LENGTH_LONG).show();
}
}
Now we are ready to click the RUN button and open your application.Press your button in MainActivity to see your Toast message.
Hope you have enjoyed this tutorial.Let us learn more about android programming in next tutorials.

No comments:
Post a Comment