android-context

How to start an activity from a dialog in Android

点点圈 提交于 2019-12-01 03:51:46
I created a custom dialog and I'd like to start a new activity when OK is clicked. How can I get the context to set it as first argument of my Intent constructor? I can create the intent using getContext() , but I can't call startActivity . Shall I pass the activity calling the dialog to the dialog's constructor? Is it the usual way to start an activity by clicking a dialog? public class CustomDialog extends Dialog implements OnClickListener { Button okButton, cancelButton; public CustomDialog(Context context) { super(context); setContentView(R.layout.custom_dialog); okButton = (Button)

SQLite Database “context” passed to adapter

走远了吗. 提交于 2019-12-01 03:30:39
I have followed this tutorial to use SQLite db in my android app. Since I am a beginner I'm having problems understanding "context" parameter used in the example. I want to call adapter and insert/update/delete records from a class that does not extend activity wich in this example stands for context. Now I don't know what to pass in the adapter as context, since im not calling adapter from activity. Can someone please explain this? Pass the ActivityName.this as class context as argument to the adapter class's constructor the ActivityName is the name of the Activityclass in which you are

Use layout editor effectively on layouts with non-static fragments

烈酒焚心 提交于 2019-12-01 02:27:25
问题 With all the Android development tools greatness, especially as of version 21, the graphical layout editor is a powerful tool to have visual control over a layout with fragments for every configuration and locale. I'm aware that a typical Activity 's layout XML will contain static fragment tags with info embedded for the layout editor, for example tools:layout="@layout/book_collection_view_window_list" . However, because I need to replace the Fragments in my window dynamically, I cannot use

Call activity method from broadcast receiver android?

你说的曾经没有我的故事 提交于 2019-12-01 01:45:49
In my application I am sending a port SMS to the mobile. And when the message is recieved I need to perform some task in my activity and update the UI. Manifest Declaration of receiver <receiver android:name="com.vfi.BinarySMSReceiver" > <intent-filter android:priority="10" > <action android:name="android.intent.action.DATA_SMS_RECEIVED" /> <data android:host="*" android:port="9512" android:scheme="sms" /> </intent-filter> </receiver> Receiver class public class BinarySMSReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Bundle bundle =

SQLite Database “context” passed to adapter

自古美人都是妖i 提交于 2019-11-30 23:52:56
问题 I have followed this tutorial to use SQLite db in my android app. Since I am a beginner I'm having problems understanding "context" parameter used in the example. I want to call adapter and insert/update/delete records from a class that does not extend activity wich in this example stands for context. Now I don't know what to pass in the adapter as context, since im not calling adapter from activity. Can someone please explain this? 回答1: Pass the ActivityName.this as class context as argument

Access to getString() in android.support.v4.app.FragmentPagerAdapter?

余生长醉 提交于 2019-11-30 19:19:00
In a class extending android.support.v4.app.FragmentPagerAdapter , is there any way to get access to the Context.getString(..) method without the extending class being an inner class of an activity or passing in some context from the outside? Thanks for any hint! From a fragment use : getActivity().getString(...) From an adapter use : getContext().getResources().getString(...) Yes, you need a context to access the resources. Abhay Bhusari From an Activity, use: this.getString(R.string.string_name); From a Fragment, use: getActivity.getString(R.string.string_name); From an adapter, use:

Is it a bad practice to hold application Context instance?

白昼怎懂夜的黑 提交于 2019-11-30 19:00:40
From my understanding, Application in Android is a singleton (correct me if I'm wrong) and we always have just one application Context instance. So, from this perspective, is it a bad practice to save the application Context in my Application class? Can it lead to a massive memory leak? Here is an example: public class MyApp extends Application { private static Context appContext = null; // <-- here is the thing! @Override public void onCreate() { appContext = this; } public static Context getApplicationContextSingleton () { return MyApp.appContext; } } The reason to do this is globally

Android: BitmapFactory.decodeResource returning null

放肆的年华 提交于 2019-11-30 18:23:30
I can't seem to figure this out. I have 2 java classes with different characteristics, each calling BitmapFactory.decodeResource to get the same image resource, one returns the bitmap while the other returns null. Both classes are in the same package. Here is the class that works, it calls BitmapFactory.decodeResource which returns the bitmap. I've only included relevant code. package advoworks.test; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Rect; import android.util.Log; import

How to use Calligraphy with multi-language support Oreo

旧城冷巷雨未停 提交于 2019-11-30 15:17:11
I'm developing an app that need to support multiple languages and if the language is RTL I have to apply a custom font. For the requirement I have created my Class that extends Application . Everything was perfect till I got Oreo version device (Before I have Marshmellow enabled device). In Oreo if we want to change the language we have to create a custom ContextWrapper class, here problem come in. To use Calligraphy we need to Override the attachBaseContext method. And To change language we need to Override the attachBaseContext too I tried to call super.attachBaseContext in the Overrided

How to mock Context using Mockito?

一笑奈何 提交于 2019-11-30 14:33:25
问题 I'm using Context to access system level services like WifiManager and BluetoothManager. How to mock this getApplicationContext() using Mockito? 回答1: Let's have a look at the following class: MockContext If you need more insight, check the Official Testing Fundamentals page 回答2: Context context = mock(Context.class); 回答3: If you want to get the context with Kotlin and Mockito , you can do it in the following way: mock(Context::class.java) 来源: https://stackoverflow.com/questions/34063848/how