dynamic layout

How to Create Dynamic layout with relative layout, linear layout, buttons, textview etc

How to Create Dynamic relative, linear layout is big question for beginners. Some time in android app you need to create layout accordingly server response. Here I would like to share my experience. I was working on a project for Norway based company. This project is about to provide easy to access list of all the item that a user have subscribe or rented. There are about 2 lack different item on list with having different properties. So my task was to provide each user a different layout according to item and user subscription plan. Today i am discuss how to create dynamic layout which have relative layout, linear layout, checkbox, textview, button etc.




First of all i will create a new Project in android studio. Main layout of the project have buttons for creating TextView, EditText, Checkbox. Here are the step i will follow.

1) Create a Layout with 2 Button.
2) On Click Text button -add new Textview in mainLayout.
3) Dynamic add new Textview, EditTextView into main layout.

Login & Download source code

download-code350-100




Create a Layout with 2 Button

This is simple step as you already know how to create new project and place button inside layout. I used here Linear Horizontal layout with 2 Buttons. One for creating Textview and another one for creating EditText.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@color/colorPrimary"
    tools:context="blueappsoftware.dynamiclayout.MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:columnCount="2"
        android:id="@+id/HolderLayout"
        android:background="@color/colorAccent">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/CreateTextView"
            android:text="Create TextView"
            android:layout_margin="5dp"
            android:textColor="@color/colorPrimary"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/CreateEdittext"
            android:text="Create Edittext"
            android:layout_margin="5dp"
            android:textColor="@color/colorPrimary"/>


    </LinearLayout>

</ScrollView>

On Click Text button -add new Textview in mainLayout

Set OnClick event on create TextView. Look at Below code. There is 3 important point. First declare what you want to create like textview, edittext, checkbox, button etc. 2nd set property of item as “param”. set padding, color, margin, gravity etc. 3rd final step is to all item into main layout. Like this ” mainLinear.addView(editTextbox);”.

                TextView surveytitle = new TextView(MainActivity.this);
                
               /// set property of textview
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                // param.setMargins(int left,int top , int right,int bottom)
                params.setMargins(20, 10, 20, 10);
                //  params.weight = 1.0f;
                params.gravity = Gravity.CENTER_HORIZONTAL;  /// this is layout gravity of textview
                surveytitle.setLayoutParams(params);
                surveytitle.setBackgroundColor(Color.parseColor("#3F51B5"));
                surveytitle.setGravity(View.TEXT_ALIGNMENT_GRAVITY);
                surveytitle.setTextColor(Color.parseColor("#ffffff"));
                surveytitle.setTypeface(null, Typeface.BOLD);
                surveytitle.setTextSize(18);
                surveytitle.setText("TextView "+ textcount);
                //// add textview to main layout
                mainLinear.addView(surveytitle);

MainActivity.xml

package blueappsoftware.dynamiclayout;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private LinearLayout mainLinear;
    private Button createTextview, createEdittext;
    private int textcount=1, edittextcount =1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mainLinear = (LinearLayout) findViewById(R.id.HolderLayout);
        createTextview = (Button) findViewById(R.id.CreateTextView);
        createEdittext = (Button) findViewById(R.id.CreateEdittext);

        createTextview.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //Starting a new Intent
                TextView surveytitle = new TextView(MainActivity.this);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                // param.setMargins(int left,int top , int right,int bottom)
                params.setMargins(20, 10, 20, 10);
                //  params.weight = 1.0f;
                params.gravity = Gravity.CENTER_HORIZONTAL;  /// this is layout gravity of textview
                surveytitle.setLayoutParams(params);
                surveytitle.setBackgroundColor(Color.parseColor("#3F51B5"));
                surveytitle.setGravity(View.TEXT_ALIGNMENT_GRAVITY);
                surveytitle.setTextColor(Color.parseColor("#ffffff"));
                surveytitle.setTypeface(null, Typeface.BOLD);
                surveytitle.setTextSize(18);
                surveytitle.setText("TextView "+ textcount);
                textcount= textcount+1;
                mainLinear.addView(surveytitle);


            }
        });


        createEdittext.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //Starting a new Intent
                EditText editTextbox = new EditText(MainActivity.this);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                // param.setMargins(int left,int top , int right,int bottom)
                params.setMargins(20, 10, 20, 10);
                //  params.weight = 1.0f;
                params.gravity = Gravity.CENTER_HORIZONTAL;  /// this is layout gravity of textview
                editTextbox.setLayoutParams(params);
                editTextbox.setBackgroundColor(Color.parseColor("#3F51B5"));
                editTextbox.setGravity(View.TEXT_ALIGNMENT_GRAVITY);
                editTextbox.setTextColor(Color.parseColor("#ffffff"));
                editTextbox.setTypeface(null, Typeface.BOLD);
                editTextbox.setTextSize(18);
                editTextbox.setHint("Edittext "+ edittextcount);
                editTextbox.setMinimumWidth(140);
                edittextcount = edittextcount+1;
                mainLinear.addView(editTextbox);


            }
        });





    }
}

Login & Download source code

download-code350-100




SUBSCRIBE FOR FREE ANDROID COURSE, Inbox 15 lesson & Video

* indicates required



Posted in andorid.

I Am Post Graduation (M.Tech. C.S.E) from VNIT, Nagpur. I Have Published Two Research Paper On International Journal. My Apps on Google Play Store- Sanyukt Parivar(Mp Police ), NOTOtag- Digital Chhindwara, The Tubby-VoiceRecognizer & More.
Contact me at kamal.bunkar@blueappsoftware.in

33 Comments

  1. I love your blog.. very nice colors & theme. Did you create this
    website yourself or did you hire someone to do it for you?
    Plz respond as I’m looking to create my own blog and would like to find
    out where u got this from. appreciate it

  2. Pretty great post. I just stumbled upon your weblog and wanted to say
    that I’ve truly loved browsing your blog posts.
    In any case I’ll be subscribing for your feed and I hope you write once more very soon!

  3. Hi there! This post could not be written any better!
    Reading through this post reminds me of my good old room mate!
    He always kept talking about this. I will forward this article to him.
    Fairly certain he will have a good read. Thank you for sharing!

  4. Hi there! Quick question that’s totally off topic.
    Do you know how to make your site mobile friendly?
    My weblog looks weird when browsing from my iphone4.
    I’m trying to find a template or plugin that might
    be able to fix this issue. If you have any recommendations, please share.
    Thank you!

  5. This is really attention-grabbing, You are an overly skilled blogger.
    I have joined your feed and look forward to in the hunt
    for extra of your great post. Also, I have shared your site in my social networks

  6. I’m really enjoying the theme/design of your blog.

    Do you ever run into any browser compatibility issues?

    A few of my blog audience have complained about my site not
    working correctly in Explorer but looks great in Chrome.
    Do you have any solutions to help fix this issue?

  7. I was recommended this web site by my cousin. I’m
    not sure whether this post is written by him as no one else
    know such detailed about my problem. You are wonderful!

    Thanks!

  8. Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates.
    I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this.
    Please let me know if you run into anything. I truly enjoy reading your
    blog and I look forward to your new updates.

  9. Having read this I thought it was very informative. I appreciate you finding the time and
    energy to put this short article together. I once again find myself personally spending a lot of time both reading and posting comments.
    But so what, it was still worthwhile!

  10. hi! may i know is your code complete? because i run the program and does nothing, i have download the given source code.

    • Yes this code is working…You have to create file on php server to store FCM key into your database for future use. First check on your android studio log whether FCM token is printing OR not. If it is printing in log than send token to your web server.

  11. Good day! I could have sworn I’ve been to
    this website before but after going through
    a few of the articles I realized it’s new to me.

    Nonetheless, I’m certainly delighted I found it and I’ll be book-marking it and checking back regularly!

  12. Spot on with this write-up, I absolutely feel this website needs much more attention. I’ll probably be returning to read
    through more, thanks for the info!

  13. Hey very cool website!! Man .. Excellent ..
    Amazing .. I will bookmark your site and take the feeds additionally?
    I am glad to search out numerous useful info here
    in the submit, we need work out more strategies
    on this regard, thanks for sharing. . . . . .

  14. Pingback: ExoPlayer Android Example- Best Alternative of VideoView By Kamal

  15. Pingback: Simplest way to get WordPress Post in Android App- REST API

  16. I just like the helpful information you supply to your articles.

    I’ll bookmark your blog and test once more here frequently.
    I’m fairly sure I will learn many new stuff proper here!
    Good luck for the following!

Comments are closed.