Just Simple Info

Pages

Create view programmatically (TextView, Button, RelativeLayout and LinearLayout )

TextView, Button, RelativeLayout and LinearLayout
without the use of xml
Sometimes we want to create our view programmatically or without the use of xml.
So, I decide to share a simple code on how to create views like TextView, Button, LinearLayout and RelativeLayout written in java code.
Let's start!!!

Below code is on how to create Textview.
TextView textView(){

     TextView textView = new TextView(this);
     textView.setText("Textview");
     return textView;
    }

Below code is on how to create Button.
    Button button(){

     Button button = new Button(this);
     button.setText("Button");
     return button;
    }
    

Below code is on how to create RelativeLayout
    RelativeLayout relativeLayout(){

     RelativeLayout relativeLayout = new RelativeLayout(this);
     
     //*** SET THE SIZE
     relativeLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 100));
     
     //*** SET BACKGROUND COLOR JUST TO MAKE LAYOUT VISIBLE
     relativeLayout.setBackgroundColor(Color.GREEN);
     return relativeLayout;
    }
    

Below code is on how to create LinearLayout
    LinearLayout linearLayout(){

     LinearLayout linearLayout = new LinearLayout(this);
     linearLayout.setOrientation(LinearLayout.VERTICAL);
     
     return linearLayout;
    }
    

If you want to create like layout in not activity. Just replace "this" keyword by activity context.

Here is the complete code.
package com.example.createviewprogrammatically;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

 LinearLayout linearLayout;
 RelativeLayout relativeLayout;
 TextView textView;
 Button button;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        
        // INITIALIZE VIEW'S
        
        linearLayout = this.linearLayout();
        relativeLayout = this.relativeLayout();
        textView = this.textView();
        button = this.button();
        
        // ADD VIEW'S TO linearLayout
        
        linearLayout.addView(textView);
        linearLayout.addView(button);
        linearLayout.addView(relativeLayout);
        
        // SET linearLayout AS CONTENT VIEW
        
        setContentView(linearLayout);
    }
    /*
     * Create textview
     */
    TextView textView(){
     TextView textView = new TextView(this);
     textView.setText("Textview");
     return textView;
    }
    /**
     * Create button
     * @return
     */
    Button button(){
     Button button = new Button(this);
     button.setText("Button");
     return button;
    }
    
    /**
     * Create relative layout
     * @return
     */
    RelativeLayout relativeLayout(){
     RelativeLayout relativeLayout = new RelativeLayout(this);
     
     // SET THE SIZE
     relativeLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 100));
     
     // SET BACKGROUND COLOR JUST TO MAKE LAYOUT VISIBLE
     relativeLayout.setBackgroundColor(Color.GREEN);
     return relativeLayout;
    }
    
    /**
     * Create linear layout
     * @return
     */
    LinearLayout linearLayout(){
     LinearLayout linearLayout = new LinearLayout(this);
     linearLayout.setOrientation(LinearLayout.VERTICAL);
     
     return linearLayout;
    }
}


Download Source Code Here

Good luck and happy coding!!!

No comments:

Post a Comment