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 8785511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:22:57+00:00 2026-06-13T21:22:57+00:00

i am relatively new to Android programming. i have gone through a lot of

  • 0

i am relatively new to Android programming. i have gone through a lot of threads on this topic but none of the solutions mentioned work for me. Here is what i am trying.
i am trying to create a layout resource with an XML file and in my MainActivity, i do a setContentView(R.layout.activity_main). i then have a custom view class that extends View in which i try to modify the default layout defined in the activity_main.xml file.
However, any time i try to obtain a View Id with the findViewById(R.id.) (from the layout file activity_main.xml) in the Custom view implemented in a separate file, i always get null. If i try to get the id in the MainActivity class with the findViewById(…), i get a proper value.
What am i doing wrong here ?

Here are all the code snippets

thanks

Here is the file “MainActivity.java”

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);



    setContentView(R.layout.activity_main);

    MyView myView = new MyView(this, null);

      ....

Here is the file MyView.java

public class MyView extends View {

public static final String DEBUG_TAG = "MYVIEW" ;

public MyView(Context context, AttributeSet attrs) {
    super(context, attrs);


    View tablerow1 = (TableRow) findViewById (R.id.tableRow) ;

    Log.v(DEBUG_TAG," tablerow1 = " + tablerow1 + "\n");



}

And here is the file activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/top_layout"  
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"  >
<TableLayout  
android:id="@+id/tableLayout1"  
android:layout_width="match_parent"  
android:layout_height="0dp"
android:layout_weight="2"
android:background="@color/red"
android:shrinkColumns="*"  
android:stretchColumns="*" >  
<TableRow
    android:id="@+id/tableRow1"  
    android:layout_height="wrap_content"  
    android:layout_width="match_parent"  
    android:gravity="center_horizontal">  

    <Button  
        android:id="@+id/button_00_1"  
        android:text="@string/button_1"
        android:textStyle="bold"
        android:background="@color/green"
        android:layout_margin="0.5dp"
        android:layout_width="0dp"
        android:layout_weight="0.5"           
        android:typeface="serif"></Button> 
    </TableRow>  
  </TableLayout>
</LinearLayout>

The findViewById(…) call in the MyView.java always returns null, whereas if i include it in the MainActivity.java file, i get proper non-null values.

Can someone point out what is wrong here ?

thanks

  • 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-13T21:22:59+00:00Added an answer on June 13, 2026 at 9:22 pm

    You can easily access any view/layout from the layout xml of your activity. The only thing you are missing in your code is that you are trying to execute findViewById() in the MyView and trying to access the Activity’s other views. This call will work only on the view that are enclosed in your MyView and not outside that. You have to call that activity’s findViewByID() of which you are trying to access the views. For example:-

    ((Activity)context).findViewById(R.id.abc).setVisibility(View.VISIBLE);
    

    In your case you can do it like this:-

    public class MyView extends View {
       public static final String DEBUG_TAG = "MYVIEW" ;
    
       public MyView(Context context, AttributeSet attrs) {
       super(context, attrs);
       View tablerow1 = (TableRow) ((Activity)context).findViewById (R.id.tableRow) ;
       Log.v(DEBUG_TAG," tablerow1 = " + tablerow1 + "\n");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm relatively new to Android but I just cant google this. I have following
Im relatively new to the Android world but I have a quick question. I
I am new to Java programming / Android development - but I have been
Hi I'm relatively new to Android programming but I would be grateful of some
I am relatively new to Android development but I do have a pretty good
I am a relatively new Android developer but have gotten pretty familiar with the
I'm relatively new to Android programming, and am having a problem with inflation. I'm
I am relatively new to Android, but I am looking to use an interactive
I am relatively new to android. I have two applications A and B. I
I'm relatively new to java and android development so sorry if this is a

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.