70

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.

lorraine batol
  • 6,001
  • 16
  • 55
  • 114

5 Answers5

78

For example you can find any textView:

TextView textView = (TextView) ((Activity) context).findViewById(R.id.textView1);
KuzyaTheBrownie
  • 804
  • 6
  • 8
67

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);
samus
  • 6,102
  • 6
  • 31
  • 69
  • 3
    Remember that you should cast `View v = rootView.findViewById(R.id.your_view_id);` to `View v = (View) rootView.findViewById(R.id.your_view_id);` and the class _View_ can be any derivated class of View. – Joaquin Iurchuk Jan 02 '15 at 22:24
  • 1
    When trying this I get "Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity". – drynyn Apr 06 '16 at 11:35
  • 4
    @drynyn that's because this example is assuming an Activity is being passed for the context, which is not a safe assumption. You can use instanceof keyword to check if the cast is safe to perform or not. (google "instanceof java" for examples). – eimmer May 17 '16 at 13:10
7

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);
Augiwan
  • 2,392
  • 18
  • 22
Marcin S.
  • 11,161
  • 6
  • 50
  • 63
  • What should be I placed on root parameter? – lorraine batol Oct 29 '12 at 06:38
  • root parameter is a root View of the inflated hierarchy. In above example I have shown how to inflate a view that is already a root therefore you can keep this parameter false. If your inflated view is not a root, then you place your root view as a third parameter. – Marcin S. Oct 29 '12 at 15:52
  • 3
    Shouldn't that be `null` instead of `false`? – Roberto Aug 15 '14 at 23:36
  • 2
    I don't think (re)inflating the entire view is a very efficient or reliable approach, seeing that it is already inflated with a call to `setContentView()`. However, if you omit the call to 'setContentView()` and manually attached it after a manual inflation then you may be ok (I forgot exactly how to do this, but manual view attachment can be done in any `Activity` or `Fragment`). – samus Jan 29 '15 at 20:54
1

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);
Fahad Anjum
  • 1,246
  • 1
  • 10
  • 19
-2

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);
Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
MacLiamor
  • 1
  • 1
  • 3
  • 3
    Nice try, but unfortunately this will introduce a memory leak since `ClassicSingleton` will hold a reference to Activity. – Dmitry Zaytsev Jan 24 '19 at 19:12