android – How to get view from context?
For example you can find any textView: TextView textView = (TextView) ((Activity) context).findViewById(R.id.textView1);
For example you can find any textView: TextView textView = (TextView) ((Activity) context).findViewById(R.id.textView1);
Yes its different for different cases, It depends on the scope. Suppose if you are creating a method in a global class that extends Application to create a Toast that is used in every class of your Application you can use getApplicationContext() to create it. If you want to create a view that is restricted … Read more
getApplicationContext () returns the application context of the entire application life cycle,when application will destroy then it will destroy also. this the context returns the current context of the activity, belong to the activity, the activity is destroyed then it will destroy also.but in your case it will refers to the Spinner instance because we … Read more
@Module public class MainActivityModule { private final Context context; public MainActivityModule (Context context) { this.context = context; } @Provides //scope is not necessary for parameters stored within the module public Context context() { return context; } } @Component(modules={MainActivityModule.class}) @Singleton public interface MainActivityComponent { Context context(); void inject(MainActivity mainActivity); } And then MainActivityComponent mainActivityComponent = DaggerMainActivityComponent.builder() … Read more
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
You can reference an outer context when you define your DialogInterface.OnClickListener as an anonymous class. If you’re in an activity you can use MyActivity.this as the context. Edit – since your Activity is implementing DialogInterface.OnClickListener, you should be able to just use this as the context.
Update: 06-Mar-18 Use MyApplication instance instead of Context instance. Application instance is a singleton context instance itself. public class MyApplication extends Application { private static MyApplication mContext; @Override public void onCreate() { super.onCreate(); mContext = this; } public static MyApplication getContext() { return mContext; } } Previous Answer You can get the the application context … Read more
For Android Architecture Components View Model, Edit 1 : It’s not a good practice to pass your Activity Context to the Activity’s ViewModel as its a memory leak. You shouldn’t be need your context this way as there are better ways to write your code and there are better ways to manage the reference to … Read more
If your class is non-activity class, and creating an instance of it from the activiy, you can pass an instance of context via constructor of the later as follows: class YourNonActivityClass{ // variable to hold context private Context context; //save the context recievied via constructor in a local variable public YourNonActivityClass(Context context){ this.context=context; } } … Read more
It is better to set private mode for the file. App needs to be signed with same set of certificates to share this file. Set sharedUserId in both apps to be same. <manifest xmlns:android=”http://schemas.android.com/apk/res/android” package=”com.example.hello” android:versionCode=”1″ android:versionName=”1.0″ android:sharedUserId=”com.example”> …. Get Context from other package: mContext = context.createPackageContext( “com.example.otherapp”, Context.MODE_PRIVATE); mPrefs = mContext.getSharedPreferences(“sameFileNameHere”, Activity.MODE_PRIVATE); Get items … Read more