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 :
- Create RelativeLayout using xml.
- Create RelativeLayout programmatically.
- Arrange children view in RelativeLayout programmatically.
Here is the relative_layout.xml
file location: res/layout
Load RelativeLayout from xml to interface
Create RelativeLayout programmatically
Arrange child views in RelativeLayout programmatically
Here is the complete code
Download Source Code
Have a good day!!!
<?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!!!
Thanks bro. Struggled with many other tutorial but this is clear.
ReplyDeletethankz bro.
DeleteI think this should be on google developers website. Very clear. Comments are excellent.
ReplyDeleteI have struggled with many other sources and nothing worked, but this one the only one that really worked.
Thanks.
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
ReplyDeleteClick here:
angularjs training in chennai
Click here:
angularjs2 training in chennai
Click here:
angularjs4 Training in Chennai
Click here:
angularjs5 Training in Chennai
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.
ReplyDeleteClick 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
I am really happy with your blog because your article is very unique and powerful for new reader.
ReplyDeleteClick here:
Selenium Training in Chennai | Selenium Training in Bangalore | Selenium Training in Pune
Click here:
Selenium Training in Bangalore
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.
ReplyDeleteDevops Training in Chennai
Devops Training in Bangalore
Devops Training in pune
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.
ReplyDeleterpa training in electronic-city | rpa training in btm | rpa training in marathahalli | rpa training in pune
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.
ReplyDeletejava training in chennai | java training in bangalore
java interview questions and answers | core java interview questions and answers
Well somehow I got to read lots of articles on your blog. It’s amazing how interesting it is for me to visit you very often.
ReplyDeleteData Science course in rajaji nagar | Data Science with Python course in chenni
Data Science course in electronic city | Data Science course in USA
Data science course in pune | Data science course in kalyan nagar
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!
ReplyDeleteangularjs Training in marathahalli
angularjs interview questions and answers
angularjs Training in bangalore
angularjs Training in bangalore
angularjs Training in chennai
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!!
ReplyDeleteDevops Training in Chennai | Devops Training Institute in Chennai
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.
ReplyDeleteangularjs online training
apache spark online training
informatica mdm online training
devops online training
aws online training
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.
ReplyDeleteMicrosoft Azure online training
Selenium online training
Java online training
Python online training
uipath online training
Hi,
ReplyDeleteGood 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.
very informative...
ReplyDeleteinplant training in chennai
inplant training in chennai
inplant training in chennai for it.php
Australia hosting
mexico web hosting
moldova web hosting
albania web hosting
andorra hosting
australia web hosting
denmark web hosting
excellent...!
ReplyDeleteinternship in chennai for ece students
internships in chennai for cse students 2019
Inplant training in chennai
internship for eee students
free internship in chennai
eee internship in chennai
internship for ece students in chennai
inplant training in bangalore for cse
inplant training in bangalore
ccna training in chennai
I truly like this post..
ReplyDeleteThanks 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
really enjoy reading and also appreciate your work.
ReplyDeleteBEST 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
I really enjoyed lot by reading your post.
ReplyDeleteAngularJS training in chennai | AngularJS training in anna nagar | AngularJS training in omr | AngularJS training in porur | AngularJS training in tambaram | AngularJS training in velachery
I really enjoyed lot by reading your post.
ReplyDeleteAngularJS training in chennai | AngularJS training in anna nagar | AngularJS training in omr | AngularJS training in porur | AngularJS training in tambaram | AngularJS training in velachery
First You got a splendid weblog .I could be interested in more comparable topics.thanks!!!
ReplyDeleteandroid training in chennai
android online training in chennai
android training in bangalore
android training in hyderabad
android Training in coimbatore
android training
android online training
Thank you for sharing the article. The data that you provided in the blog is informative and effective.
ReplyDeleteAngular js Training in Chennai
Angular js Training in Velachery
Angular js Training in Tambaram
Angular js Training in Porur
Angular js Training in Omr
Angular js Training in Annanagar
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.
ReplyDeleteIELTS Coaching in chennai
German Classes in Chennai
GRE Coaching Classes in Chennai
TOEFL Coaching in Chennai
Spoken english classes in chennai | Communication training