Android Testing: Inflate and Display a custom Toast from Instrumentation

帅比萌擦擦* 提交于 2019-12-13 05:04:15

问题


I am trying to display a custom Toast but doing so from my automated test rather than from the application itself.

The layout inflation does not work though. Is it even possible to inflate views and from the test project and display those?

What does work is a standard Toast:

final Activity targetActivity = Solo.getCurrentActivity(); // Using Robotium to get current displayed Activity
Toast.makeText(targetActivity, "Hello from Instrumentation", Toast.LENGTH_SHORT).show();

What does NOT work is the following:

final Activity targetActivity = Solo.getCurrentActivity(); // Using Robotium to get current displayed Activity

LayoutInflater inflater = targetActivity.getLayoutInflater();                         
View layout = inflater.inflate(test.my.package.R.layout.my_custom_toast, null); // resource is located in test project
TextView text = (TextView) layout.findViewById(test.my.package.R.id.textToShow); // textview within the layout

text.setText("Hello from Instrumentation"); // here I get the NullPointerException

Toast toast = new Toast(targetActivity);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();

Solution

Get new LayoutInflater without reference to targetActivity

final Activity targetActivity = Solo.getCurrentActivity(); // Using Robotium to get current displayed Activity

// *** !!! ***
LayoutInflater inflater = 
(LayoutInflater) getInstrumentation().getContext().getSystemService  (Context.LAYOUT_INFLATER_SERVICE); 
 // getContext() , NOT getTargetContext()

View layout = inflater.inflate(test.my.package.R.layout.my_custom_toast, null); // resource is located in test project
TextView text = (TextView) layout.findViewById(test.my.package.R.id.textToShow); // textview within the layout

text.setText("Hello from Instrumentation"); // here I get the NullPointerException

Toast toast = new Toast(targetActivity);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();

回答1:


Hi Use the below codes:

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.example.R;

public class DoToast extends Toast {

    /** The Constant TOAST_DURATION. */
    private static final int TOAST_DURATION=4000;

    /** The layout. */
    View layout; 

    /**
     * Instantiates a new do toast.
     *
     * @param context the context
     * @param text the text
     */
    public DoToast(Context context, CharSequence text) {
        super(context);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
        layout = inflater.inflate(R.layout.toast, null);
        TextView textView = (TextView) layout.findViewById(R.id.text);
        textView.setText(text);
        DoToast.this.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
        DoToast.this.setDuration(TOAST_DURATION);
        DoToast.this.setView(layout);
        DoToast.this.show();
    }
}

Layout xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/toast_layout_root"
     android:orientation="horizontal"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:padding="8dp"
     android:layout_marginLeft="10dp"
     android:layout_marginRight="10dp"
     android:background="@drawable/toast_bg">

     <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:textColor="#000000" />
</LinearLayout>

And Style file:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="rectangle" >
   <gradient
        android:angle="90"
        android:endColor="#ffffff"
        android:startColor="#F2F2F2"
        android:type="linear" />
    <stroke
        android:width="3dp"
        android:color="#000000" />
</shape>

If you need to show toast then just use the below line:

new DoToast(this,"Testing");  

Let me know if you have any queries..




回答2:


I believe the issue is to do with the context, you are trying to show a toast from your test applications R files but using the applications context via an activity, what you will want to do is get the test context from instrumentation and then create a layout inflater from that using the following.

 Context context = instrumentation.getContext();
 LayoutInflater li = LayoutInflater.from(context);


来源:https://stackoverflow.com/questions/24904445/android-testing-inflate-and-display-a-custom-toast-from-instrumentation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!