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 7845717

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:20:15+00:00 2026-06-02T17:20:15+00:00

I have a simple Activity with its main.xml attached. main.xml has a custom layout

  • 0

I have a simple Activity with its main.xml attached. main.xml has a custom layout (customLinearLayout) and a simple TextView. The custom layout also have its own layout-file (linearlayout.xml) with a simple Button. The layout is inflated by the class properly.

The Button in linearlayout.xml shall change the text of the textView lying in main.xml, but I can’t get access to that textView. What am I doing wrong? I can’t inflate the main.xml either.

Here’s the customLinearLayout:

public class CustomLinearLayout extends LinearLayout {

LayoutInflater mInflater;
View ContainerView;

Button button1;
TextView tv;

public CustomLinearLayout(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    init();
}

public CustomLinearLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
    init();
}

private void init() {
    mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ContainerView = mInflater.inflate(R.layout.linearlayout, this, true);

    button1 = (Button) ContainerView.findViewById(R.id.button1);

    // trying to get the TextView from main.xml
    tv = (TextView) ContainerView.findViewById(R.id.textview1); // doesn't work

    // these tries doesn't work either
    //tv = (TextView) ContainerView.getRootView().findViewById(R.id.textview);  
    //tv = (TextView) this.getRootView().findViewById(R.id.textview);
    //tv = (TextView) this.findViewById(R.id.textview);

    button1.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(tv != null)
                tv.setText("works");
            else
                Log.d("CustomLinearLayout", "TextView not found");
        }
    });
}
}

the layout-file (linearlayout.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="press" />

</LinearLayout>

The Activity:

public class TestLayoutViewsv2Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}
}

The Activity’s main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<test.layoutviewsv2.CustomLinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/textview1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>
  • 0 0 Answers
  • 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-02T17:20:20+00:00Added an answer on June 2, 2026 at 5:20 pm

    The method findViewById is scoped – your custom view is looking inside itself for a text field that doesn’t exist. But that’s ok! your views should be their own little world – They shouldn’t need to know anything about the world outside itself. Sadly you’ll have to change your design to accomodate this (like with a callback to the activity, perhaps) to make this do what you want to.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an AdView in the main activity with its requestInterval set to (the
I have an app that has 2 tabs. Each tab loads an xml file
I have a fragment embedded in a layout. This fragment has a very simple
I have a very simple Activity with the following layout... <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=fill_parent android:layout_height=fill_parent
I have a simple game activity. I start it and than press the back
So I have a very simple android activity that starts a timer when you
friends, i have a EditText on simple activity with a button. when every i
This seems like a simple problem: I have a WF4 activity that guides the
I'm building an app that has a main Activity that takes advantage of the
My situation is following. I have one Activity that contains three main parts. At

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.