android - How to get view from context?

AndroidAndroid IntentBroadcastreceiverAndroid Context

Android Problem Overview


I want to get the view or findViewById() from Context? Or from intent?

I'm trying to reach a specific view in my broadcast receiver and the parameter of onReceive are context and intent.

Well, I have a class and within it is my broadcast receiver. Now, I'm trying to separate the broadcast receiver from it, but I need a way so I can still communicate with the views on my class from my separated broadcast receiver class.

Thanks.

Android Solutions


Solution 1 - Android

For example you can find any textView:

TextView textView = (TextView) ((Activity) context).findViewById(R.id.textView1);

Solution 2 - Android

Starting with a context, the root view of the associated activity can be had by

View rootView = ((Activity)_context).Window.DecorView.FindViewById(Android.Resource.Id.Content);

In Raw Android it'd look something like:

View rootView = ((Activity)mContext).getWindow().getDecorView().findViewById(android.R.id.content)

Then simply call the findViewById on this

View v = rootView.findViewById(R.id.your_view_id);

Solution 3 - Android

In your broadcast receiver you could access a view via inflation a root layout from XML resource and then find all your views from this root layout with findViewByid():

View view = View.inflate(context, R.layout.ROOT_LAYOUT, null);

Now you can access your views via 'view' and cast them to your view type:

myImage = (ImageView) view.findViewById(R.id.my_image);

Solution 4 - Android

first use this:

LayoutInflater inflater = (LayoutInflater) Read_file.this
	            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

Read file is current activity in which you want your context.

View layout = inflater.inflate(R.layout.your_layout_name,(ViewGroup)findViewById(R.id.layout_name_id));

then you can use this to find any element in layout.

ImageView myImage = (ImageView) layout.findViewById(R.id.my_image);

Solution 5 - Android

Why don't you just use a singleton?

import android.content.Context;


public class ClassicSingleton {
    private Context c=null;
    private static ClassicSingleton instance = null;
    protected ClassicSingleton()
    {
       // Exists only to defeat instantiation.
    }
    public void setContext(Context ctx)
    {
    c=ctx;
    }
    public Context getContext()
    {
       return c;
    }
    public static ClassicSingleton getInstance()
    {
        if(instance == null) {
            instance = new ClassicSingleton();
        }
        return instance;
    }
}

Then in the activity class:

 private ClassicSingleton cs = ClassicSingleton.getInstance();

And in the non activity class:

ClassicSingleton cs= ClassicSingleton.getInstance();
        Context c=cs.getContext();
        ImageView imageView = (ImageView) ((Activity)c).findViewById(R.id.imageView1);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlorraineView Question on Stackoverflow
Solution 1 - AndroidKuzyaTheBrownieView Answer on Stackoverflow
Solution 2 - AndroidsamusView Answer on Stackoverflow
Solution 3 - AndroidMarcin S.View Answer on Stackoverflow
Solution 4 - AndroidFahad AnjumView Answer on Stackoverflow
Solution 5 - AndroidMacLiamorView Answer on Stackoverflow