Hello.

Help, I'm new to Java :)

It is necessary to break down by concepts: TextView myTextView = (TextView)findViewById(R.id.tvCat) ;

TextView -
myTextView -
(TextView) -
R.id.tvCat -

Mostly clear, but confused, because have the same values.

  • 2
    It is worth reading the manuals before trying to gash a program on an android, this is elementary. At least read a couple of lectures :) - jessez
  • @jessez I’m reading, but they didn’t dwell on this method) - ikerya
  • one
    It is worthwhile to first learn the basics of Java to understand what a class is, what an instance of a class is, what type casting, etc. And then take on Android. - Eugene Krivenja

2 answers 2

Maybe it will be clearer, I don’t know, TextView myTextView is that you created a link, then, using the equal sign, you specify a link to the object to which it will refer. A simple example can be shown as follows. String string = new String(); Now you can access the previously created new String() object via the string reference. Those. calling methods referring to the link string.replace() , etc.

In your situation, you do not create a new object, but so to speak, request it by calling a method that returns (return) an object of type View, you pass in the argument of the findViewById(ТУТ) method to the ID of the requested findViewById(R.id.tvCat) . But since you need an object of type TextView, you cast ( article: casting ) the View object in TextView, if you can do this, otherwise there will be an error.

ps I myself a novice, for inaccuracies, please do not kick))

  • no big deal, thanks a lot)) - ikerya
  • @ikerya don't forget to mark the answer as correct, which came up to you - jessez
  • both answers are good, I don't even know :( - ikerya
  • @ikerya choose any, this is not very significant, if the question is exhausted, it is better to choose the answer so that the question does not hang in the unanswered questions - jessez
  • TextView - class
  • myTextView is a TextView type variable
  • (TextView) - casting an object to a successor type (it only works if findViewById () actually returns an instance of the TexView class, written into a variable of type View)
  • R.id.tvCat - the unique identifier of the View object in a separate XML markup
  • thank you, very grateful) can you show with an example of some element? is there a declaration of myTextView , etc. and what is the TextView class? what does he do? - ikerya
  • Here is a sample code that creates a text box with red text color. The field takes up the entire screen. public static TextView createTextViewWith (final Context ctx, final String text) {final TextView tw = new TextView (ctx); tw.setText (text); tw.setLayoutParams (new FrameLayout.LayoutParams (ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); tw.setTextColor (Color.RED); return tw; } - P. Ilyin
  • I will understand, thank you) - ikerya
  • The @ikerya TextView class belongs to a group of Android widgets , that is, classes that display information on the device screen — everything you see on the screen is implemented with their help — buttons, switches, pictures, text, sliders, and everything else. That class TextView is responsible for displaying text on the screen. - pavlofff
  • understood thanks. and here it is still not clear about the same class of widgets wrapped in brackets - (TextView) - ikerya