开发者

I need a clickable UI element instead of Toast (can i create custom toast with clickable feature?)

开发者 https://www.devze.com 2023-02-11 08:37 出处:网络
I am developing a RSS news feed app for Android, and using Toast.makeText() to show the news or current update. It must go on web on the click of that message/news. The problem is that the Toast has n

I am developing a RSS news feed app for Android, and using Toast.makeText() to show the news or current update. It must go on web on the click of that message/news. The problem is that the Toast has no API/features that I can cli开发者_如何学编程ck on that and go on web to read the full article.

The prime requirement is that I need to display the news one by one so I can not use list or any other widget.

Can anyone suggest what UI element should I use to solve this problem?


As you've discovered, there is absolutely no user interaction with Toasts. This is not likely to change given their purpose.

About the only other thing I can think of is to use Notifications instead.


If the volume of news is very low you could use Notifications as sugested, but as I assume an RSS feed may have a lot of things to notificate, it would flood the notification list if you want to display them one by one.

Although Toasts can't be clicked, you could create an activity with a special theme and emulate the Toast behavior.


Detailed instructions

Ok, here we go! =)

To make things clear i ended up writing a full example of an application acting as a Toast.

The first thing we need is a class to act as our Toast. This class will do nothing but show itself and finish after a given time. You can handle other events to fit your needs as well, but this is the minimum necessary to reproduce the 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 has been passed as an Extra to the intent that started the activity, sets the text to be displayed and set a timer of 1000ms for the finish() method to be called. The display time can be adjusted to better fit user experience.

The layout for this activity 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 nothing but an empty screen with a text on 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 application background 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 in here is the android:theme property. Theme.Translucent.NoTitleBar will give us an Activity with transparent background and no Title bar. So, when the activity is displayed, just the text and the Toast background will be visible.

Good, now you have a working fake toast locked and loaded. But how to use it?! First, we create a dummy application to be our popup launcher. You won't need this in your project, as your own class will be launching the 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 pressed shows you a Toast with a message. As you can see, to tell your Toast application what message to display, we use the Intent.putExtra(name,message) method. This data is rescued when MyPopup starts using the Intent.getStringExtra(name) method.

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

I hope it will help you out. =)


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

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


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

The easiest way to do it would be to use a RelativeLayout in your Activity. If you are not already using a RelativeLayout, you can simply wrap your current layout in one, and just set it to layout_alignParentTop=true, and set layout_width and layout_height to fill_parent.

Inside your RelativeLayout (below all other content so it isn't hidden by the rest of the elements in your Activity), add your clickable element with layout_alignParentBottom to true, and use layout_marginBottom to add some padding. This element can be a simple TextView or it can be as complicated as you want. Set visibility to "gone" in the xml, and where your code would normally display a toast, you can set visibility to "visible". To get rid of it, you can use Handler.postDelayed to set visibility back to "gone", or if you want to be fancy you can use AlphaAnimation to fade in/out.

0

精彩评论

暂无评论...
验证码 换一张
取 消