getting context in AsyncTask

You need to do following things. when you want to use AsyncTask, extend that in other class say MyCustomTask. in constructor of new class, pass Context Example public class MyCustomTask extends AsyncTask<Void, Void, Long> { private Context mContext; public MyCustomTask (Context context){ mContext = context; } //other methods like onPreExecute etc. protected void onPostExecute(Long result) … Read more

Checking toast message in android espresso

This slightly long statement works for me: import static android.support.test.espresso.assertion.ViewAssertions.matches; import static android.support.test.espresso.matcher.RootMatchers.withDecorView; import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; import static android.support.test.espresso.matcher.ViewMatchers.withText; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; …. onView(withText(R.string.TOAST_STRING)).inRoot(withDecorView(not(is(getActivity().getWindow().getDecorView())))).check(matches(isDisplayed()));

java.lang.RuntimeException: Can’t create handler inside thread that has not called Looper.prepare(); [duplicate]

I got this exception because I was trying to make a Toast popup from a background thread. Toast needs an Activity to push to the user interface and threads don’t have that. So one workaround is to give the thread a link to the parent Activity and Toast to that. Put this code in the … Read more

Toast equivalent for Xamarin Forms

There is a simple solution for this. By using the DependencyService you can easily get the Toast-Like approach in both Android and iOS. Create an interface in your common package. public interface IMessage { void LongAlert(string message); void ShortAlert(string message); } Android section [assembly: Xamarin.Forms.Dependency(typeof(MessageAndroid))] namespace Your.Namespace { public class MessageAndroid : IMessage { public … Read more

Custom toast on Android: a simple example

Use the below code of a custom Toast. It may help you. toast.xml <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:id=”@+id/toast_layout_root” android:orientation=”horizontal” android:layout_width=”match_parent” android:layout_height=”match_parent” android:padding=”10dp” android:background=”#DAAA” > <ImageView android:id=”@+id/image” android:layout_width=”wrap_content” android:layout_height=”match_parent” android:layout_marginRight=”10dp” /> <TextView android:id=”@+id/text” android:layout_width=”wrap_content” android:layout_height=”match_parent” android:textColor=”#FFF” /> </LinearLayout> MainActivity.java LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root)); ImageView image = (ImageView) layout.findViewById(R.id.image); image.setImageResource(R.drawable.android); TextView text = … Read more

Displaying a stock iOS notification banner when your app is open and in the foreground?

iOS 10 adds the UNUserNotificationCenterDelegate protocol for handling notifications while your app is in the foreground. The UNUserNotificationCenterDelegate protocol defines methods for receiving notifications and for handling actions. When your app is in the foreground, arriving notifications are delivered to your delegate object instead of displayed automatically using the system interfaces. Swift: optional func userNotificationCenter(_ … Read more

difference and when to use getApplication(), getApplicationContext(), getBaseContext() and someClass.this

Toast and Intent, both requires reference to context. And getApplication, getApplicationContext, LoginActivity.this and getBaseContext, they all offer reference to the context. Now the thing confuses is the declaration of different contexts and their specific-usage. To make things simple, you should count two types of context available in the Android framework. Application Context Activity Context Application … Read more

Call getLayoutInflater() in places not in activity

You can use this outside activities – all you need is to provide a Context: LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE ); Then to retrieve your different widgets, you inflate a layout: View view = inflater.inflate( R.layout.myNewInflatedLayout, null ); Button myButton = (Button) view.findViewById( R.id.myButton ); EDIT as of July 2014 Davide’s answer on how … Read more