I need a clickable user interface element instead of Toast (can I create a custom toast with a click option?)

I am developing an RSS news app for Android and use Toast.makeText() to show news or the current update. It should go online at the click of this message / newsletter. The problem is that Toast does not have any API / functions that I can click on it and go online to read the full article.

Most importantly, I need to display the news one by one, so I can not use the list or any other widget.

Can someone suggest which user interface element I should use to solve this problem?

+4
source share
4 answers

As you have discovered, there is absolutely no interaction with toasts. This is unlikely to change given their purpose.

Another thing I can think of is to use Notifications .

+5
source

If the amount of news is very low, you can use the notifications as spoken, but, as I believe, the RSS feed can have a lot of things to notify, it would populate the list of notifications if you want to display them one at a time.

You can’t click toasts, you can create an action with a special theme and emulate Toast behavior.


Detailed instructions

Ok, here we go! =)

To make things clear, I ended up writing a complete example of an application that acts as a toast.

The first thing we need is a class that will act as our Toast. This class will do nothing but show itself and finish after a certain time. You can handle other events to suit your needs, but this is minimally necessary to reproduce Toast behavior.

 package com.rchiossi.popup; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.widget.TextView; public class MyPopup extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_popup); String message = getIntent().getStringExtra("message"); TextView messageView = (TextView) findViewById(R.id.message); messageView.setText(message); Handler handler = new Handler(); long delay = 1000; handler.postDelayed(new Runnable() { @Override public void run() { MyPopup.this.finish(); } }, delay); } } 

As you can see, this class gets a String that was passed as Extra for the intention that triggered the action, sets the text to display, and sets a 1000 ms timer for the finish () method to be called. The display time can be adjusted to better suit the user interface.

The layout for this operation is very simple:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="@android:drawable/toast_frame"/> </RelativeLayout> 

This layout is a blank screen with text in the center. The trick here is to set the TextView background as "@android: drawable / toast_frame", which is the default background for toasts.

And now, to give it the final touch, you need to set the theme in AndroidManifest.xml to set the background of the application as transparent and remove the title.

 <activity android:name=".MyPopup" android:label="@string/app_name" android:theme="@android:style/Theme.Translucent.NoTitleBar"> </activity> 

The trick here is the android: theme property. Theme.Translucent.NoTitleBar will give us an action with a transparent background and no title. Thus, when displaying activity, only Toast text and background are displayed.

Ok, now you have a fake toast, locked and loaded. But how to use it ?! First, we create a dummy application for our popup. You will not need this in your project, since your own class will run Toast.

 package com.rchiossi.popup; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class Main extends Activity { /** Called when the activity is first created. */ private int mCount = 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent intent = new Intent(Main.this,MyPopup.class); intent.putExtra("message", "My popup number " + mCount); mCount++; startActivity(intent); } }); } } 

This application is basically just a button that, when clicked, shows you a toast with a message. As you can see, to tell your Toast application which message to display, we use the Intent.putExtra(name,message) method. This data is saved when MyPopup starts using the Intent.getStringExtra(name) method.

You can get the source code of my example: http://www.mediafire.com/file/7vmuy8244vwh4dk/PopItUp.zip

I hope this helps you. =)

+4
source

How about a custom dialog? You can make your dialogue more or less look like a toast.

http://developer.android.com/guide/topics/ui/dialogs.html

+2
source

You can simulate toast-like behavior with a clickable element, but it does require a bit of work.

The easiest way to do this is to use a RelativeLayout in your activity. If you are not using RelativeLayout yet, you can simply wrap your current layout in one and simply set it to layout_alignParentTop = true and set layout_width and layout_height to fill_parent.

Inside your RelativeLayout (below the rest of the content so that it is not hidden by the other elements in your activity), add your interactive element with layout_alignParentBottom to true and use layout_marginBottom to add a placeholder. This element can be a simple TextView or it can be as complex as you want. Set the visibility to "gone" in xml and where your code will usually show a toast, you can set the visibility to "visible". To get rid of it, you can use Handler.postDelayed to set the visibility to “leave”, or if you want to be a fantasy, you can use AlphaAnimation to step on / off.

0
source

Source: https://habr.com/ru/post/1340030/


All Articles