LayoutInflater is a class used to instantiate layout XML file into its corresponding view objects which can be used in java programs.
In simple terms there are two ways to create UI in android. One is static way and another is dynamic or programmatically.
Suppose we have a simple layout main.xml having one textview and one edittext as follow.
In simple terms there are two ways to create UI in android. One is static way and another is dynamic or programmatically.
Suppose we have a simple layout main.xml having one textview and one edittext as follow.
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/layout1"
>
<TextView
android:id="@+id/namelabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter your
name"
android:textAppearance="?android:attr/textAppearanceLarge"
>
</TextView>
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="14dp"
android:ems="10">
</EditText>
</LinearLayout>
We can display this layout in static way by
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Dynamic way of creating a view means the view is not
mentioned in our main.xml but we want to show with this in run time. For
example We have another xml in layout folder as footer.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Add your record"
android:textSize="24sp"
>
</TextView>
We want to show this textbox in run time within our main UI.
So here we will inflate text.xml . See how
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TextView t = (TextView)inflater.inflate(R.layout.footer,null);
lLayout =
(LinearLayout)findViewById(R.id.layout1);
lLayout.addView(t);
Here I have used getSystemService
(String)
to retrieve a LayoutInflater instance. I can use
getLayoutInflator() too to inflate instead of using getSystemService (String) like below
LayoutInflator inflater = getLayoutInflater();
TextView
t = (TextView) inflater.inflate(R.layout.footer, null);
lLayout.addView(t);
We have inflated the view footer using inflater and converted it
Textview object. In this example we have inflated a simple textview. If you want to know how to inflate a listview , check this Inflating Listview post.
Before Inflation |
You can download the complete source code from here.
You might be interested to know
How to inflate a listview?
You might be interested to know
How to inflate a listview?
Thanks.. Your code helped to understand better..
ReplyDeletekeepup the good work..
Nice tutorial
ReplyDeleteGood explanation!
ReplyDeleteThank you for your interest in helping others.
ReplyDeleteSuperb!!! Thanks for your code.
awsome.!
ReplyDeleteNice Tutorial. Easy to understand.
ReplyDeletenice ! thanks.
ReplyDeleteAwesome!
ReplyDeleteThank a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support about this area.
ReplyDeletefull stack developer training in pune
full stack developer training in annanagar
full stack developer training in tambaram
We are a group of volunteers and starting a new initiative in a community. Your blog provided us valuable information to work on.You have done a marvellous job!
ReplyDeleteClick here:
python training in chennai
Click here:
python training in chennai
Great content thanks for sharing this informative blog which provided me technical information keep posting.
ReplyDeleteData Science Training in Chennai
Data science training in bangalore
Data science online training
Data science training in pune
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.
ReplyDeletejava course in tambaram | java course in velachery
java course in omr | oracle course 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!!
ReplyDeleteangularjs Training in chennai
angularjs-Training in pune
angularjs-Training in chennai
angularjs Training in chennai
angularjs-Training in tambaram
Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging.
ReplyDeletePython training in marathahalli | Python training institute in pune | Python training in IndiraNagar
Does your blog have a contact page? I’m having problems locating it but, I’d like to shoot you an email. I’ve got some recommendations for your blog you might be interested in hearing.
ReplyDeleteAWS Interview Questions And Answers
Best AWS Training in Chennai | Amazon Web Services Training in Chennai
Amazon Web Services Training in OMR , Chennai | Best AWS Training in OMR,Chennai
AWS Training in Chennai |Best Amazon Web Services Training in Chennai
AWS Training in Chennai | AWS Training Institute in Chennai Velachery, Tambaram, OMR
Great so we can inflate layout every activity where we want
ReplyDeleteThis is a terrific article, and that I would really like additional info if you have got any. I’m fascinated with this subject and your post has been one among the simplest I actually have read.
ReplyDeletedevops online training
aws online training
data science with python online training
data science online training
rpa online training
Thank You! For sharing such a great article, It's been a amazing article.
ReplyDeleteIt's provide lot's of information, I really enjoyed to read this,
i hope, i will get these kinds of information on a regular basis from your side.
Keyword: Best Plumbing Service
Visit: Cheap hotel room
Read More: Buy New Home
very nicely explained !!!
ReplyDeleteThank You! For sharing such a great article, It's been a amazing article.
ReplyDeleteIt's provide lot's of information, I really enjoyed to read this.
I hope, i will get these kinds of information on a regular basis from your side.
I follows your site, and share your's content because it is so much useful.
Source: Play Sports Free
Keyword: Way to Earn Money
Read: Wrestle Mania 36
Watch: Wrestling Video
Very well explained, specially it is easy to understand for beginners
ReplyDeleteNice and good article. It is very useful for me to learn and understand easily. Thanks for sharing your valuable information. keep it up.
ReplyDeleteAi & Artificial Intelligence Course in Chennai
PHP Training in Chennai
Ethical Hacking Course in Chennai Blue Prism Training in Chennai
UiPath Training in Chennai
This is good site and nice point of view.
ReplyDeleteDigital Marketing Training in Chennai | Certification | SEO Training Course | Digital Marketing Training in Bangalore | Certification | SEO Training Course | Digital Marketing Training in Hyderabad | Certification | SEO Training Course | Digital Marketing Training in Coimbatore | Certification | SEO Training Course | Digital Marketing Online Training | Certification | SEO Online Training Course
very nice post!!!
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
very nice blog!!
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
Very interesting blog Thank you for sharing such a nice and interesting blog and really very helpful article.I have recently visited your blog profile. I am totally impressed by your blogging skills and knowledge.Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. After reading your article I was amazed. I know that you explain it very well. And I hope that other readers will also experience how I feel after reading your article
ReplyDeleteData Science Training In Chennai
Data Science Online Training In Chennai
Data Science Training In Bangalore
Data Science Training In Hyderabad
Data Science Training In Coimbatore
Data Science Training
Data Science Online Training