---
title: "Code execution after activity creation in android"  
description: "Code execution after activity creation in android"  
author: "Lillian Martin"  
published: 2014-11-19  
updated: 2014-11-19  
canonical: https://www.mindstick.com/forum/12638/code-execution-after-activity-creation-in-android  
category: "android"  
tags: ["java", "android activity"]  
reading_time: 1 minute  

---

# Code execution after activity creation in android

I want to [execute](https://www.mindstick.com/interview/49/how-can-i-execute-a-php-script-using-command-line) some [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) immediately after the [form](https://www.mindstick.com/forum/6/multi-form-mfc-application) is shown.

I want to [check](https://yourviews.mindstick.com/story/2248/never-forget-to-check-these-specifications-before-buying-a-mobile-phone) the [size](https://www.mindstick.com/forum/159799/how-can-you-manage-the-size-of-mdf-and-ldf-files) of a [button](https://www.mindstick.com/articles/63/how-to-add-button-in-datagridview-in-csharp-dot-net) on the form and use the same size to create a new button at runtime.

I tried onStart() and onResumed(), but they do not work.

## Replies

### Reply by Anonymous User

You can add a globallayout listener. Add the listener at onActivityCreated. Check this example:

```
button1.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
{                @Override                public void onGlobalLayout() {                    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {                      
button1.getViewTreeObserver().removeOnGlobalLayoutListener(this);                    } else
{                       
button1.getViewTreeObserver().removeGlobalOnLayoutListener(this);                    }                   //here the size is already available. create new button2 here with the size of button1                }            });
```


---

Original Source: https://www.mindstick.com/forum/12638/code-execution-after-activity-creation-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
