# After port to AS, adding a button

**URL:** <https://discourse.processing.org/t/after-port-to-as-adding-a-button/11634>\
**Category:** Processing for Android\
**Created:** [May 27, 2019, 4:44pm UTC](https://discourse.processing.org/t/after-port-to-as-adding-a-button/11634 "2019-05-27T16:44:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![lve0200](https://avatars.discourse-cdn.com/v4/letter/l/aeb1de/32.png) [@lve0200](https://discourse.processing.org/u/lve0200)\
**Post date:** [May 27, 2019, 4:44pm UTC](https://discourse.processing.org/t/after-port-to-as-adding-a-button/11634/1 "2019-05-27T16:44:18Z")

</div>

Hi,  
I have ported my Proc4A app to AS. The App works “full screen”, I guess what’s called “match parent”. The App only draws Graphics elements in there. The related XML file, which was created by AS during the importation is:

```auto
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment"
    android:name=".MiniEFIS"
    tools:layout="@layout/fragment_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

```

I would like now to create 2 buttons in the area, where nothing is drawn yet, and later use something like mouse over or pressed to increment/decrement a local variable in my code.  
If I try to drag a button into the frame area, using the AS drag&Drop design tool, but I cannot place the button; hence apparently this is not allowed.  
How to?  
Thanks for helping  
hk

---

<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:** [May 28, 2019, 1:37pm UTC](https://discourse.processing.org/t/after-port-to-as-adding-a-button/11634/2 "2019-05-28T13:37:38Z")

</div>

@lve0200 ===  
you have to add your buttons by code; the xml you have put is the “general” fragment created by p5 which creates also a frameLayout (by code) and adds also by code the applet to the fragment and the fragment to the frameLayout. As for more details look inside this forum or the previous one, i have put code for that. Guide line: onStart() you get the frameLayout, you create your buttons and give attributes to them (x, y, background…) - then (but it can be inside a runnable, depends if your buttons change) you have to add them to the view, that’s all.

---

<div class="post-metadata">

**Author:** ![lve0200](https://avatars.discourse-cdn.com/v4/letter/l/aeb1de/32.png) [@lve0200](https://discourse.processing.org/u/lve0200)\
**Post date:** [May 28, 2019, 2:36pm UTC](https://discourse.processing.org/t/after-port-to-as-adding-a-button/11634/3 "2019-05-28T14:36:02Z")

</div>

Hi,  
tried hard in both Forums, but couldn’t find the right 1 among 1000s.

> 

> [@akenaton](#):
>
> which creates also a frameLayout (by code) and adds also by code the applet to the fragment and the fragment to the frameLayout.

I guess you mean this:

```auto
public class MainActivity extends AppCompatActivity {
  private PApplet sketch;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FrameLayout frame = new FrameLayout(this);
    frame.setId(CompatUtils.getUniqueViewId());
    setContentView(frame, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
                                                     ViewGroup.LayoutParams.MATCH_PARENT));
    
    sketch = new MiniEFIS();
    new Thread(new listen()).start();
    PFragment fragment = new PFragment(sketch);
    fragment.setView(frame, this);

```

Could you give me a hint, where to find this code snippet, you’ve been talking about? I’ll try to adopt it, if I can see it.  
Rgds  
hk

---

<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:** [May 28, 2019, 2:42pm UTC](https://discourse.processing.org/t/after-port-to-as-adding-a-button/11634/4 "2019-05-28T14:42:39Z")

</div>

@lve0200 ===

Yes, that is exactly what i have described; as for the second question sorry, as for now i am working and cannot get time to look at that, but the principle is always the same (except for some kind of views, like texture or surface): you get the framelayout and add to it (with the addView() method from android ) your buttons or textView or imageView and so on.

---

<div class="post-metadata">

**Author:** ![lve0200](https://avatars.discourse-cdn.com/v4/letter/l/aeb1de/32.png) [@lve0200](https://discourse.processing.org/u/lve0200)\
**Post date:** [May 28, 2019, 5:50pm UTC](https://discourse.processing.org/t/after-port-to-as-adding-a-button/11634/5 "2019-05-28T17:50:08Z")

</div>

I’m stuck! Its not urgent, hence leave yourself time, but eventually I could use some help.  
I tried this:

```auto
public class MainActivity extends AppCompatActivity {
  private PApplet sketch;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FrameLayout frame = new FrameLayout(this);
    frame.setId(CompatUtils.getUniqueViewId());
    setContentView(frame, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
                                                     ViewGroup.LayoutParams.MATCH_PARENT));
    
    sketch = new MiniEFIS();
    new Thread(new listen()).start();
    PFragment fragment = new PFragment(sketch);
    fragment.setView(frame, this);
    
    Button newButton = new Button(this);
    newButton.setText("Hello");
    fragment.addView(newButton);
  }

```

but in the last line addView cannot be solved.  
Rgds  
hk

---

<div class="post-metadata">

**Author:** ![lve0200](https://avatars.discourse-cdn.com/v4/letter/l/aeb1de/32.png) [@lve0200](https://discourse.processing.org/u/lve0200)\
**Post date:** [May 28, 2019, 6:23pm UTC](https://discourse.processing.org/t/after-port-to-as-adding-a-button/11634/6 "2019-05-28T18:23:10Z")

</div>

```auto
 Button newButton = new Button(this);
    newButton.setText("Hello");
    frame.addView(newButton);

```

compiles OK, but no button.  
really trying hard. All explanations I can find in INET are with (R.layout.activity\_main);

---

<div class="post-metadata">

**Author:** ![lve0200](https://avatars.discourse-cdn.com/v4/letter/l/aeb1de/32.png) [@lve0200](https://discourse.processing.org/u/lve0200)\
**Post date:** [May 28, 2019, 7:02pm UTC](https://discourse.processing.org/t/after-port-to-as-adding-a-button/11634/7 "2019-05-28T19:02:53Z")

</div>

Hi,  
sorry to bother,  
its working. The button just got overpainted by the draw() function.  
Thanks anyway.  
hk
