Created
June 15, 2018 11:30
-
-
Save jesselima/b13593a3a447b099171066beb83f5625 to your computer and use it in GitHub Desktop.
Reuse Of Toast Object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="utf-8"?> | |
| <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| xmlns:app="http://schemas.android.com/apk/res-auto" | |
| xmlns:tools="http://schemas.android.com/tools" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" | |
| android:orientation="vertical" | |
| tools:context=".ReuseOfToastObjectActivity"> | |
| <Button | |
| android:id="@+id/btn_toast_it" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:layout_gravity="center" | |
| android:text="Toast with one string"/> | |
| <Button | |
| android:id="@+id/btn_toast_it_two_strings" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:layout_gravity="center" | |
| android:text="Toast two strings"/> | |
| </LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.udacity.myapplication; | |
| import android.support.v7.app.AppCompatActivity; | |
| import android.os.Bundle; | |
| import android.view.View; | |
| import android.widget.Button; | |
| import android.widget.Toast; | |
| public class ReuseOfToastObjectActivity extends AppCompatActivity { | |
| Toast toast; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_reuse_of_toast_object); | |
| Button btnToast = findViewById(R.id.btn_toast_it); | |
| btnToast.setOnClickListener(new View.OnClickListener() { | |
| @Override | |
| public void onClick(View v) { | |
| // passing one input. | |
| doToast("Toast one string"); | |
| } | |
| }); | |
| Button btnToastTwo = findViewById(R.id.btn_toast_it_two_strings); | |
| btnToastTwo.setOnClickListener(new View.OnClickListener() { | |
| @Override | |
| public void onClick(View v) { | |
| // passing 2 inputs. | |
| doToast("Toast with", " 2 strings"); | |
| } | |
| }); | |
| } | |
| /** | |
| * When the method is called just pass a string as input to show it in a toast. | |
| * | |
| * @param string is the string input to be shown in the toast. | |
| */ | |
| public void doToast(String string){ | |
| if (toast != null){ | |
| toast.cancel(); | |
| } | |
| toast = Toast.makeText(this, string, Toast.LENGTH_SHORT); | |
| toast.show(); | |
| } | |
| /** | |
| * When the method is called you can pass two strings as input to show it in a toast. | |
| * | |
| * @param stringOne is the first string of two that you can pass as input. | |
| * @param stringTwo is the second string of two that you can pass as input. | |
| */ | |
| public void doToast(String stringOne, String stringTwo){ | |
| if (toast != null){ | |
| toast.cancel(); | |
| } | |
| toast = Toast.makeText(this, stringOne + stringTwo, Toast.LENGTH_SHORT); | |
| toast.show(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment