Android Creating Layouts Programatically

Android Creating Layouts Programatically

In Android sometimes you need to create layouts programatically or dynamically according to your need. So today’s is about how to create layouts programatically in android.

Below is a method that I am going to use in this article. In this i am creating 5 Buttons dynamically and setting their ID’s for further use. After creating add this Button to Layout.


In this tutorial, we are going to learn how to create layouts programatically and do something when they clicked.


1. Create a new project in Android Studio by navigating to File ⇒ New Android ⇒ Application Project and fill required details. By default my activity is

2. Open res=>values=strings.xml and add the following strings to it.

3. Create a xml layout naming activity_main.xml and add the following code. In this  layout there are three Buttons for showing different layouts and a fragment holder for placing layouts.

4. Now come to your and add the following code. In this class there is a Button[] and TextView[] array for holding a reference of Button and TextView respectively.

Note: Set the TextView/Button ID for using this view like setting click listener.

5. In above class in customHorizontalLayout() i am using xml layout to add over LinearLayout, so for that we need to create xml naming customlayout.xml and add the following code.

6. Finally, all done you can use above steps for using custom font in your app. You can use RelativeLayout, FrameLayout, etc. also.

Thanks. :)

Download Source Code


About the author

I am an Android developer for whom Android is the one and only love. I work as a freelancer which gets me and Android closer. I always love to share my learnings and love to learn from others knowledge too.


Thank you very much. very useful article.

Leave a Comment

Comment (required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Name (required)
Email (required)