Explanation of findViewById(), ImageView, & TextView

Greetings Fellow Programmers (in-training)!

My question pertains to the article, “Programming View Objects”, w/in the Building Android Apps coursework.

I followed along with the video in building my own contact card and have the “final” code (in so far as this exercise is concerned) for this task. See below.

    private static final String CONTACT_1 = "Jay Gatsby";
    private static final String CONTACT_2 = "Daisy Buchanan";
    private static final String TAG = "Main Activity Tag";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        View contactCard1 = findViewById(R.id.incl_cardview_contact_card_main_1);
        Log.d(TAG, "Contact Card 1 is type: " + contactCard1.getClass().getName());
        TextView contactName1 = contactCard1.findViewById(R.id.tv_contact_card);
        contactName1.setText(CONTACT_1);

        View contactCard2 = findViewById(R.id.incl_cardview_contact_card_main_2);
        TextView contactName2 = contactCard2.findViewById(R.id.tv_contact_card);
        contactName2.setText(CONTACT_2);
    }

I’d like to better understand the relationship b/t the View object, contactCard1, and the TextView object, contactName1.

First, why is FindViewById() first called w/o any object (1), then, it is called on an instance(2)?

(1)

View contactCard1 = findViewById(R.id.incl_cardview_contact_card_main_1);

(2)

TextView contactName1 = contactCard1.findViewById(R.id.tv_contact_card);

It is to say, why do we need to access the contactCard1 View object in order to access the contactName1 TextView object?

I hope I’ve crafted this question clearly!
Mike

Hello! I believe (someone do correct me if I’m wrong) that the first one

is the entire view of the cardview activity (is it an activity, it’s been a while since I’ve done this). This means contactCard1 now holds the values for everything in the incl_cardview_contact_card_main_1 view.

The second one

Then selects the specific TextView within the incl_cardview_contact_card_main_1 view.

I hope this helps!

Hmm… yes and no. Conceptually, I understand what you’re communicating.

I may be overthinking this. I’ve been studying Java on the side for a year now and it’s fun seeing some of the “gaps” fill in that I’d taken for granted before. Maybe this particular “gap” will clear itself up with time. For whatever it’s worth at this point, in my head, any non-static method (which this one definitely is) needs to be called on an object. There is no object on which findViewById() is being called in (1). In (2), there clearly is. Again, I feel like there’s something just really small and obvious that isn’t clicking that will fill in the gap.

Thank you very much for your time @codeneutrino!

1 Like

This section from the android developer docs may be helpful. Also, see the link on that page Activity.findViewById. I think that may also be helpful.