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

  • Home
  • SEARCH
  • 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 5992647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:33:44+00:00 2026-05-22T23:33:44+00:00

So I have a custom view set up in code (no XML layout defined

  • 0

So I have a custom view set up in code (no XML layout defined for it) and am wondering how to get the child Views ID’s defined correctly. I DO have an xml file defining id’s similar to this.. But how I understand it, at least, since I don’t have an xml layout there’s no AttribSet to pass .. so my constructors are all just (Context context) types.

<resources>
    <item type="id" name="textview1"/>
    <item type="id" name="textview2"/>
</resources>

And my view looks something along these lines:

public class MyView extends View {

    protected RelativeLayout baseLayout;
    protected TextView textView1;
    protected TextView textView2;

    public MyView(Context context) {
        super(context);
        LayoutParams fill = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        setLayoutParams(fill);

        Resources res = getResources();

        baseLayout = new RelativeLayout(context);
        baseLayout.setLayoutParams(fill);

        textView1 = new TextView(context);
        textView1.setId(R.id.textview1);
        // other init stuff here

        textView2 = new TextView(context);
        textView2.setId(R.id.textview2);
        // other init stuff here

        baseLayout.addView(textView1);
        baseLayout.addView(textView2);
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // *snip lots of stuff*

        baseLayout.draw(canvas);
    }

    // This is only here because findViewById() returns NULL when I search
    public TextView getTextView1() {
        return textView1;
    }

    // This is only here because findViewById() returns NULL when I search
    public TextView getTextView2() {
        return textView2;
    }

}

Here’s the Activity that uses the view..

public class MyActivity extends Activity {

    // This is only here because findViewById() returns NULL when I search
    private MyView myView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Resources res = getResources();

        myView = new myView(this);
        setContentView(myView);

        // This returns NULL.  :/       
        // final TextView textView1 = (TextView) findViewById(R.id.textView1);

        // This is only here because findViewById() returns NULL when I search..
        final TextView textView1 = myView.getTextView1();
        final Animation anim = AnimationUtils.loadAnimation(this, R.anim.my_animation);
        textView1.setAnimation(anim);
        anim.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationEnd(Animation _anim) {
                Log.v("InsideAnimation", "Animation ended");
                textView1.setVisibility(View.INVISIBLE);
            }
            @Override
            public void onAnimationRepeat(Animation _anim) {
                Log.v("InsideAnimation", "Animation repeated");
            }
            @Override
            public void onAnimationStart(Animation _anim) {
                Log.v("InsideAnimation", "Animation started");
            }
        });
        anim.reset();
        textView1.startAnimation(anim);
    }
}

The main problem is that the animation I load (which is defined in xml) and start does absolutely nothing. I get a Log statement of “animation started” but that’s it. It never ends and it never does anything when I emulate it. It’s like it starts, but doesn’t actually RUN. I think the issue is that maybe the animation is expecting to use xml id layout definitions to do it’s work on, but since findViewById() returns NULL, I’m assuming that part is broken (and thus the animation doesn’t work). So .. at least for right now, I want to find out what I’m doing wrong with setting the ID so that I can get findViewById() to return the correct view and not NULL… And then possibly the animation will work. (or if it still doesn’t work after that point, I’ll have to look at maybe defining the animation in code also instead of xml)

Any help or suggestions are appreciated.

  • 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-05-22T23:33:45+00:00Added an answer on May 22, 2026 at 11:33 pm

    My guess is, that your TextView is not instantiated at this point. try to trigger your animation outside of the oncreate-method.

    Additionally, your MyView is extended from View and you´re creating a RelativeLayout inside. If you do this, you could also directly extend your class from RelativeLayout and then do something like

    mRelativeLayout = new MyRelativeLayout(context)
    mRelativeLayout.findViewById(R.id.textview1)
    

    could also be the problem, because you haven´t got the reference to your RelativeLayout in your Activity. Although it´s more likely that my first guess is the solution…

    Oh, and you could simply design your RelativeLayout in xml. I see no real point in using a programmatically Layout here…

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

Sidebar

Related Questions

I have a custom view (an extension of a TextView) that I want to
I am struggling to get my custom drawing code to render at the proper
Is there anything else that the code must do to sanitize identifiers (table, view,
Im opening a ListActivity with an xml called list with a ListView named list.
I've implemented a custom annotation derived from MKAnnotation called ContainerAnnotation and a custom annotation
I have a navigation based iPhone app. When you press on a cell in
I have a ListView with a CheckBox as one of the columns, bound to
I have recently started using the MVVM-Light toolkit and I am stuck on the
I was trying to use IB in a slightly different way that I am
I've just ran into an unexpected behaviour for Entity framework entities in use with

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.