2Questions i. Purpose of Context parameter in TextView ii. layout visibility ambiguity

  • Replies:1
Danish ALI
  • Forum posts: 1

Apr 25, 2016, 9:52:46 PM via Website

Hi! I just started learning Android Development from "developer . android . com / develop / index . html" (Sorry new users not allowed to post links so I had to put a spaces)
I reached on this part "Starting Another Activity (developer . android . com / training / basics / firstapp / starting-activity . html)". Here I have a part of code which is working fine but I don't understand how it works and want to know about it. The code is following:

   TextView textView = new TextView(this);
   textView.setTextSize(40);
   textView.setText(message);

   RelativeLayout layout = (RelativeLayout) findViewById(R.id.content);
   layout.addView(textView);

Now I have 2 Questions:

  1. In first line of this code; why does the TextView(this) constructor need a Context as it's parameter ?
  2. This code is in an Activity Class and R.id.content identifies a RelativeLayout in same Activity's xml file now what I understand is that the identifier "layout" refers to a copy of R.id.content casted to RelativeLayout
    And the question is
    i. if my understanding is correct then "layout" is just a copy of RelativeLayout so adding textView to this copy should not make textView visible as it's not added to actual RelativeLayout in the xml file but only to it's copy
    ii. if my understanding is not correct then please correct me and tell me how this works and makes textView visible

Reply
Tommi Chou
  • Forum posts: 6

Aug 4, 2016, 11:05:45 PM via Website

1.it's fundamental, rule.
2.not a copy, it's an instance.

Reply