Just Simple Info

Pages

Android RelativeLayout Example

RelativeLayout
Black is created using xml
Green is created using java code
RelativeLayout in Android is most flexible layout. Children can be position almost anywhere or children view can position to the right, left, top and bottom of another children view. This layout is useful if you want to organize the content of your application the way you want. There are two way of creating RelativeLayout, by Xml and Java code.

So,  I going to show on how :
  1. Create RelativeLayout using xml.
  2. Create RelativeLayout programmatically.
  3. Arrange children view in RelativeLayout programmatically. 

Here is the relative_layout.xml
file location: res/layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100px"
    android:background="#000000">
</RelativeLayout>

Load RelativeLayout from xml to interface

RelativeLayout relativeLayoutFromXml =  (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.relative_layout, contentnView, false);  
// ADD LAYOUT FROM XML TO mainLayout  
contentnView.addView(relativeLayoutFromXml);  

Create RelativeLayout programmatically

 /** 
  * Create relative layout programmically 
  * @return 
  */  
RelativeLayout createRelativeLayoutProgrammatically(){  
   
   RelativeLayout relativeLayout = new RelativeLayout(this);  
   
   // CREATE PARAM FOR SIZE   
   LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 100);  
   
   // APPLY PARAM  
   relativeLayout.setLayoutParams(params);  
   
   // SET BACKGROUND COLOR  
   relativeLayout.setBackgroundColor(Color.GREEN);  
   
   return relativeLayout;  
   
}  

Arrange child views in RelativeLayout programmatically
/** 
  * Arranged child views  
  */  
  private void arrangedChildView(RelativeLayout relativeLayoutWithView){  
     
    Button buttonA = new Button(this);  
    Button buttonB = new Button(this);  
    Button buttonC = new Button(this);  
    Button buttonD = new Button(this);  
    Button buttomE = new Button(this);  
     
    EditText editText = new EditText(this);  
     
    // SET TEXT  
    buttonA.setText("BUTTON A");  
    buttonB.setText("BUTTON B");  
    buttonC.setText("BUTTON C");  
    buttonD.setText("BUTTON D");  
    buttomE.setText("BUTTON E");  
     
    // SET ID. This is important  
    buttonA.setId(1);  
    buttonB.setId(2);  
    buttonC.setId(3);  
    buttonD.setId(5);  
    buttomE.setId(6);  
    editText.setId(7);  
     
     
    // CREATE PARAM  
     
    RelativeLayout.LayoutParams paramsA = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);  
    
     
    RelativeLayout.LayoutParams paramsB = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);  
    paramsB.addRule(RelativeLayout.RIGHT_OF, buttonA.getId());  
     
    RelativeLayout.LayoutParams paramsC = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);  
    paramsC.addRule(RelativeLayout.BELOW, buttonA.getId());  
     
    RelativeLayout.LayoutParams paramsD = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);  
    paramsD.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM );  
     
    RelativeLayout.LayoutParams paramsE = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);  
    paramsE.addRule(RelativeLayout.ABOVE, buttonD.getId());  
     
    RelativeLayout.LayoutParams editTextParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);  
    editTextParams.addRule(RelativeLayout.RIGHT_OF, buttomE.getId());  
    editTextParams.addRule(RelativeLayout.ABOVE,buttonD.getId());  
     
    // ADD CHILDREN VIEW  
     
    relativeLayoutWithView.addView(buttonA, paramsA);  
    relativeLayoutWithView.addView(buttonC, paramsC);  
    relativeLayoutWithView.addView(buttonB, paramsB);  
    relativeLayoutWithView.addView(buttonD,paramsD);  
    relativeLayoutWithView.addView(buttomE, paramsE);  
    relativeLayoutWithView.addView(editText, editTextParams);  
     
  }  

Here is the complete code

package com.example.relativelayout;  
  
import android.app.Activity;  
import android.graphics.Color;  
import android.os.Bundle;  
import android.text.Editable;  
import android.view.LayoutInflater;  
import android.widget.Button;  
import android.widget.EditText;  
import android.widget.LinearLayout;  
import android.widget.LinearLayout.LayoutParams;  
import android.widget.RelativeLayout;  
  
public class MainActivity extends Activity {  
  
 LinearLayout contentnView;  
   
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
          
        contentnView = new LinearLayout(this);  
          
        // SET ORIENTATION TO VERTICAL  
        contentnView.setOrientation(LinearLayout.VERTICAL);  
          
        // SET CONTENT VIEW  
        setContentView(contentnView);  
          
        // GET LAYOUT FROM XML   
        RelativeLayout relativeLayoutFromXml =  (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.relative_layout, contentnView, false);  
        // ADD LAYOUT FROM XML TO mainLayout  
        contentnView.addView(relativeLayoutFromXml);  
         
        // RELATIVE LAYOUT PROGRAMMATICALLY CREATED  
        RelativeLayout relativeLayoutCreatedProgrammatically = this.createRelativeLayoutProgrammatically();  
          
        // ADD LAYOUT TO mainLayout  
        contentnView.addView(relativeLayoutCreatedProgrammatically);  
          
          
        // RELATIVE LAYOUT WITH VIEWS  
        RelativeLayout relativeLayoutWithView = this.withChildRelativeLayout();  
          
        // ADD VIEWS IN RELATIVE LAYOUT AND ARRANGE  
        arrangedChildView(relativeLayoutWithView);  
          
        // ADD LAYOUT TO mainLayout  
        contentnView.addView(relativeLayoutWithView);  
    }  
      
    /** 
     * Create relative layout programmically 
     * @return 
     */  
   RelativeLayout createRelativeLayoutProgrammatically(){  
      
    RelativeLayout relativeLayout = new RelativeLayout(this);  
      
    // CREATE PARAM FOR SIZE   
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 100);  
      
    // APPLY PARAM  
    relativeLayout.setLayoutParams(params);  
      
    // SET BACKGROUND COLOR  
    relativeLayout.setBackgroundColor(Color.GREEN);  
      
    return relativeLayout;  
      
   }  
     
   /** 
    * RelativeLayout with child views 
    * @return 
    */  
   RelativeLayout withChildRelativeLayout(){  
      
    RelativeLayout relativeLayout = new RelativeLayout(this);  
      
    // CREATE PARAM FOR SIZE   
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT );  
      
    // APPLY PARAM  
    relativeLayout.setLayoutParams(params);  
      
    // SET BACKGROUND COLOR  
    relativeLayout.setBackgroundColor(Color.LTGRAY);  
      
    return relativeLayout;  
   }  
   /** 
    * Arranged child views  
    */  
   private void arrangedChildView(RelativeLayout relativeLayoutWithView){  
      
    Button buttonA = new Button(this);  
    Button buttonB = new Button(this);  
    Button buttonC = new Button(this);  
    Button buttonD = new Button(this);  
    Button buttomE = new Button(this);  
      
    EditText editText = new EditText(this);  
      
    // SET TEXT  
    buttonA.setText("BUTTON A");  
    buttonB.setText("BUTTON B");  
    buttonC.setText("BUTTON C");  
    buttonD.setText("BUTTON D");  
    buttomE.setText("BUTTON E");  
      
    // SET ID. This is important  
    buttonA.setId(1);  
    buttonB.setId(2);  
    buttonC.setId(3);  
    buttonD.setId(5);  
    buttomE.setId(6);  
    editText.setId(7);  
      
      
    // CREATE PARAM  
      
    RelativeLayout.LayoutParams paramsA = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);  
     
      
    RelativeLayout.LayoutParams paramsB = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);  
    paramsB.addRule(RelativeLayout.RIGHT_OF, buttonA.getId());  
      
    RelativeLayout.LayoutParams paramsC = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);  
    paramsC.addRule(RelativeLayout.BELOW, buttonA.getId());  
      
    RelativeLayout.LayoutParams paramsD = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);  
    paramsD.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM );  
      
    RelativeLayout.LayoutParams paramsE = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);  
    paramsE.addRule(RelativeLayout.ABOVE, buttonD.getId());  
      
    RelativeLayout.LayoutParams editTextParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);  
    editTextParams.addRule(RelativeLayout.RIGHT_OF, buttomE.getId());  
    editTextParams.addRule(RelativeLayout.ABOVE,buttonD.getId());  
      
    // ADD CHILDREN VIEW  
      
    relativeLayoutWithView.addView(buttonA, paramsA);  
    relativeLayoutWithView.addView(buttonC, paramsC);  
    relativeLayoutWithView.addView(buttonB, paramsB);  
    relativeLayoutWithView.addView(buttonD,paramsD);  
    relativeLayoutWithView.addView(buttomE, paramsE);  
    relativeLayoutWithView.addView(editText, editTextParams);  
      
   }  
}  

Download Source Code
Have a good day!!!

24 comments:

  1. Thanks bro. Struggled with many other tutorial but this is clear.

    ReplyDelete
  2. I think this should be on google developers website. Very clear. Comments are excellent.
    I have struggled with many other sources and nothing worked, but this one the only one that really worked.
    Thanks.

    ReplyDelete
  3. Excellent blog, I wish to share your post with my folks circle. It’s really helped me a lot, so keep sharing post like this
    Click here:
    angularjs training in chennai
    Click here:
    angularjs2 training in chennai
    Click here:
    angularjs4 Training in Chennai
    Click here:
    angularjs5 Training in Chennai

    ReplyDelete
  4. Some us know all relating to the compelling medium you present powerful steps on this blog and therefore strongly encourage contribution from other ones on this subject while our own child is truly discovering a great deal. Have fun with the remaining portion of the year.
    Click here:
    Microsoft azure training in velarchery
    Click here:
    Microsoft azure training in sollinganallur
    Click here:
    Microsoft azure training in btm
    Click here:
    Microsoft azure training in rajajinagar

    ReplyDelete
  5. I am really happy with your blog because your article is very unique and powerful for new reader.
    Click here:
    Selenium Training in Chennai | Selenium Training in Bangalore | Selenium Training in Pune
    Click here:
    Selenium Training in Bangalore

    ReplyDelete
  6. Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
    Devops Training in Chennai

    Devops Training in Bangalore

    Devops Training in pune

    ReplyDelete
  7. Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.


    rpa training in electronic-city | rpa training in btm | rpa training in marathahalli | rpa training in pune

    ReplyDelete
  8. A good blog always comes-up with new and exciting information and while reading I have feel that this blog is really have all those quality that qualify a blog to be a one.I wanted to leave a little comment to support you and wish you a good continuation. Wishing you the best of luck for all your blogging efforts read this.
    java training in chennai | java training in bangalore

    java interview questions and answers | core java interview questions and answers

    ReplyDelete
  9. Really great post, Thank you for sharing This knowledge.Excellently written article, if only all bloggers offered the same level of content as you, the internet would be a much better place. Please keep it up!

    angularjs Training in marathahalli

    angularjs interview questions and answers

    angularjs Training in bangalore

    angularjs Training in bangalore

    angularjs Training in chennai

    ReplyDelete
  10. I likable the posts and offbeat format you've got here! I’d wish many thanks for sharing your expertise and also the time it took to post!!
    Devops Training in Chennai | Devops Training Institute in Chennai

    ReplyDelete
  11. The post is written in very a good manner and it entails many useful information for me. I am happy to find your distinguished way of writing the post. Now you make it easy for me to understand and implement the concept.

    angularjs online training

    apache spark online training

    informatica mdm online training

    devops online training

    aws online training

    ReplyDelete
  12. This is beyond doubt a blog significant to follow. You’ve dig up a great deal to say about this topic, and so much awareness. I believe that you recognize how to construct people pay attention to what you have to pronounce, particularly with a concern that’s so vital. I am pleased to suggest this blog.
    Microsoft Azure online training
    Selenium online training
    Java online training
    Python online training
    uipath online training

    ReplyDelete
  13. Hi,
    Good job & thank you very much for the new information, i learned something new. Very well written. It was sooo good to read and usefull to improve knowledge. Who want to learn this information most helpful. One who wanted to learn this technology IT employees will always suggest you take python training in bangalore. Because Python course in Bangalore is one of the best that one can do while choosing the course.

    ReplyDelete
  14. I truly like this post..
    Thanks for sharing with us,
    We are again come on your website,
    Thanks and good day,
    If you need any logo then,
    Please visit our site,
    buylogo

    ReplyDelete
  15. really enjoy reading and also appreciate your work.

    BEST ANGULAR JS TRAINING IN CHENNAI WITH PLACEMENT

    https://www.acte.in/angular-js-training-in-chennai
    https://www.acte.in/angular-js-training-in-annanagar
    https://www.acte.in/angular-js-training-in-omr
    https://www.acte.in/angular-js-training-in-porur
    https://www.acte.in/angular-js-training-in-tambaram
    https://www.acte.in/angular-js-training-in-velachery

    ReplyDelete
  16. The post is written in very a good manner and it entails many useful information for me. I am happy to find your distinguished way of writing the post. Now you make it easy for me to understand and implement the concept.
    IELTS Coaching in chennai

    German Classes in Chennai

    GRE Coaching Classes in Chennai

    TOEFL Coaching in Chennai

    Spoken english classes in chennai | Communication training

    ReplyDelete