Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8881999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:28:05+00:00 2026-06-14T20:28:05+00:00

I don’t fully understand LayoutInflater functions though I used it in my projects. For

  • 0

I don’t fully understand LayoutInflater functions though I used it in my projects. For me it’s just a way to find view when I cannot call findViewById method. But sometimes it does not work like I expect.

I have this really simple layout (main.xml)

<?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/layout">
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Hello World, MyActivity"
            android:id="@+id/txt"/>

    <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Change text"
            android:id="@+id/btn"/>
</LinearLayout>

What I want is very simple – just to change text inside TextView when pressing the button. Everything works fine like this

public class MyActivity extends Activity implements View.OnClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        TextView txt = (TextView) findViewById(R.id.txt);
        double random = Math.random();
        txt.setText(String.valueOf(random));
    }
}

But I want to understand what would be the equivalent using LayoutInflater? I tried this, but without success, TextView didn’t change its value

@Override
public void onClick(View view) {
    LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View main = inflater.inflate(R.layout.main, null);
    TextView txt = (TextView) main.findViewById(R.id.txt);
    double random = Math.random();
    txt.setText(String.valueOf(random));
}

But when debugging I can see that each variable populates with right value. I mean txt variable actually contains TextView which value is “Hello World, MyActivity”, and after setText method it contains some random number but I couldn’t see this changes on UI. It’s the main problem I faced with LayoutInflater in my projects – for some reason I can’t update inflated views. Why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:28:06+00:00Added an answer on June 14, 2026 at 8:28 pm

    For me it’s just a way to find view when I cannot call findViewById
    method.

    That is incorrect. The LayoutInflater is used to inflate(build) a view hierarchy from the provided xml layout file. With your second code snippet you build the view hierarchy from the layout file(R.layout.main), find the TextView from that inflated view and set the text on it. The problem is that this inflated view isn’t attached to the visibile UI of the Activity. You could see the changes, for example, if you call again setContentView this time giving it the inflated view. This will make the content of your Activity to be the newly inflated View:

    LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View main = inflater.inflate(R.layout.main, null);
    TextView txt = (TextView) main.findViewById(R.id.txt);
    double random = Math.random();
    txt.setText(String.valueOf(random));
    setContentView(main);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't be scared of the extensive code. The problem is general. I just provided
Don't know how to google for such, but is there a way to query
Don't ask why, but is there any way to suppress a failed linking error?
Don't overlook the 'date AND TIME' part though.
Don't think that I'm mad, I understand how php works! That being said. I
Don't be frightened, its a very basic code. Just wanted to check with you
Don't overthink this - there's a very commonly used term and I ... have
Don't have much to say, just can get into the event handler. XAML: <Grid>
Don't understand this simple code: def main(): print (This program illustrates a chaotic function)
don't understand: in my controller: @json = User.all.to_gmaps4rails do |user| \Title\: \#{user.email}\ end in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.