# Android Widget Usage

**URL:** <https://discourse.processing.org/t/android-widget-usage/4302>\
**Category:** Processing for Android\
**Created:** [October 9, 2018, 10:37pm UTC](https://discourse.processing.org/t/android-widget-usage/4302 "2018-10-09T22:37:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![macarrony00](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/macarrony00/32/1830_2.png) [@macarrony00](https://discourse.processing.org/u/macarrony00)\
**Post date:** [October 9, 2018, 10:37pm UTC](https://discourse.processing.org/t/android-widget-usage/4302/1 "2018-10-09T22:37:43Z")

</div>

Hy everyone, I am trying to use android widgets in my app, however I don’t know exactly how activity, context, ect. works. At the moment I have this code:

```auto
//imports
import android.app.Activity;
import android.content.Context;
import android.widget.NumberPicker;

Activity act;
NumberPicker numberPicker;

void setup() {
   fullScreen();
   frameRate(60);

   act = this.getActivity();

   numberPicker = new NumberPicker(act.getApplicationContext());
    numberPicker.setMaxValue(20);
    numberPicker.setMinValue(1);
    numberPicker.setX(200);
    numberPicker.setY(200);
    //numberPicker.setDisplayedValues( **** );
}

void draw(){
}

```

I can compile and run the app in my phone, however it does not show the numberPicker, what do I miss?  
And there is any good guide on how to use this widgets in processing android?

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [October 10, 2018, 7:29am UTC](https://discourse.processing.org/t/android-widget-usage/4302/2 "2018-10-10T07:29:58Z")

</div>

@macarrony00===  
numberPicker is a view; your code creates the object but never add it using addView: so you cannot see it; in the previous forum i have given snippets for doing that (for buttons, textView, editText etc).

---

<div class="post-metadata">

**Author:** ![macarrony00](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/macarrony00/32/1830_2.png) [@macarrony00](https://discourse.processing.org/u/macarrony00)\
**Post date:** [October 10, 2018, 9:06am UTC](https://discourse.processing.org/t/android-widget-usage/4302/3 "2018-10-10T09:06:52Z")

</div>

Before my post I saw your reply here:  
https://forum.processing.org/two/discussion/16539/how-to-manually-create-android-textfield-in-processing

It makes sense to need addView(numberPicker), however I need to have a frameLayout and use:  
`fl = (FrameLayout)act.findViewById(` `0x1000` `)`  
Most of the time I see the input like: “R.id.example” but for that I need the XML right? you use 0x1000 why?

```auto

```

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [October 10, 2018, 9:12am UTC](https://discourse.processing.org/t/android-widget-usage/4302/4 "2018-10-10T09:12:52Z")

</div>

@macarrony00===  
fl is the frameLayout created by P5; 0x1000 was working when i have posted this code but now it does not work anymore because this id is generated randomly by the mainActivity: solution is to get fl with another way, that is:

`fl = (FrameLayout)act.getWindow().getDecorView().getRootView();`

---

<div class="post-metadata">

**Author:** ![macarrony00](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/macarrony00/32/1830_2.png) [@macarrony00](https://discourse.processing.org/u/macarrony00)\
**Post date:** [October 10, 2018, 10:08am UTC](https://discourse.processing.org/t/android-widget-usage/4302/5 "2018-10-10T10:08:03Z")

</div>

Ok, now I can run the code:

```auto
Activity act;
NumberPicker numberPicker;
FrameLayout fl;

void setup() {
   fullScreen();
   frameRate(60);

   act = this.getActivity();

   numberPicker = new NumberPicker(act.getApplicationContext());
    numberPicker.setMaxValue(20);
    numberPicker.setMinValue(1);
    numberPicker.setX(200);
    numberPicker.setY(200);
    
   fl = (FrameLayout) act.getWindow().getDecorView().getRootView();
}

void draw(){
   fl.addView(numberPicker);
}

```

but I got this error:

```auto
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

```

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [October 10, 2018, 10:25am UTC](https://discourse.processing.org/t/android-widget-usage/4302/6 "2018-10-10T10:25:19Z")

</div>

@macarrony00===  
that is normal with android; in order to solve that you have to use a runnable:

```auto
runOnUiThread(new Runnable() {

    @ Override
    public void run() {

      //here you can update your uithread

    }
});

```

more details here:[https://developer.android.com/reference/android/app/Activity#runOnUiThread(java.lang.Runnable)](https://developer.android.com/reference/android/app/Activity#runOnUiThread(java.lang.Runnable))

---

<div class="post-metadata">

**Author:** ![macarrony00](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/macarrony00/32/1830_2.png) [@macarrony00](https://discourse.processing.org/u/macarrony00)\
**Post date:** [October 10, 2018, 4:58pm UTC](https://discourse.processing.org/t/android-widget-usage/4302/7 "2018-10-10T16:58:38Z")

</div>

Thank you, it’s working now 🙂
