As we have seen dialog boxes are used for displaying alerts and warnings. But the drawback is that the user has to interact with them every time to hide those pop-ups.
So one alternative is there which will not require any user interaction.
It is known as Toast which displays the message for a particular time duration and goes away.
This example displays a simple toast and a custom toast.
So one alternative is there which will not require any user interaction.
It is known as Toast which displays the message for a particular time duration and goes away.
This example displays a simple toast and a custom toast.
Please Note: The images are taken from Pixabay.
--------------------- custom_toast_xml ---------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@color/colorPrimary">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:layout_marginRight="5dp"
android:id="@+id/cta_simpleToastBtn"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark"
android:text="Simple Toast"
android:textStyle="bold"
android:textColor="#ffffff"/>
<Button
android:layout_marginLeft="5dp"
android:id="@+id/cta_customToastBtn"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark"
android:text="Custom Toast"
android:textStyle="bold"
android:textColor="#ffffff"/>
</LinearLayout>
</LinearLayout>
package com.app.yourappname;------------------------------------------------------------------------------
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class CustomToastActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_toast_activity);
Button simpleToastBtn = findViewById(R.id.cta_simpleToastBtn);
simpleToastBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Simple Toast!", Toast.LENGTH_LONG).show();
}
});
Button customToastBtn = findViewById(R.id.cta_customToastBtn);
customToastBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customToast();
}
});
}
private void customToast() {
LinearLayout ll = new LinearLayout(getApplicationContext());
ll.setOrientation(LinearLayout.VERTICAL);
ll.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
ll.setBackgroundResource(R.drawable.wood_panel);
ImageView imgView = new ImageView(getApplicationContext());
imgView.setImageResource(R.drawable.alert_icon);
ll.addView(imgView);
TextView textView = new TextView(getApplicationContext());
textView.setText("Custom Toast!");
textView.setTextColor(Color.YELLOW);
textView.setTextSize(40);
textView.setGravity(Gravity.CENTER);
ll.addView(textView);
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(ll);
toast.show();
}
}
--------------- Declare Activity name in Manifest File -----------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.yourappname">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".CustomToastActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
No comments:
Post a Comment