0

I have created a TextView in the UI designer, but I can't figure out how I should access it from the code. I have tried Go To Declaration but that just brings me to the XML file where the TextView is 'made'. Does anyone know how to do this? Help is very much appreciated!

user2687781
  • 325
  • 2
  • 11

2 Answers2

1

This is independent of the IDE. First you need to "find" the TextView, then you can modify its properties:

TextView myTextView = (TextView) findViewById(R.id.yourid); // The ID is declared in the XML file as android:id atrribute.
myTextView.setText("New Text");
Lennart
  • 9,657
  • 16
  • 68
  • 84
0

What do you mean by "access it from the code"? If you're talking about navigating from where it's referenced in the code to viewing it in the UI designer, newer versions of Intellij with Android support enabled put tabs at the bottom of the editor when you're editing XML files to let you switch between a text representation and a visual representation of layout files.

If you're talking about how to instantiate the view in code, post some samples of what you've been trying (the most common way is to use a LayoutInflater).

Edit:

Changing the actual text that's displayed in the TextView isn't an IDE-specific issue. You have two ways to do this (well, three if you count the visual and text views of the XML file as separate methods). You can set the text either in the XML file by setting the android:text attribute on the TextView widget, or in the code by calling setText(). Whichever way you decide to do it, you should consider not referring to your text as a raw string but as a String resource as described here.

Edit 2:

OK, you're looking for instructions on how to inflate the view in the first place to get access to it. This is what I answered initially, but here's a little more code. In your Activity (you do have an Activity set up, right?):

LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.<your layout ID>, null);
RelativeLayout item = (RelativeLayout) view.findViewById(R.id.<your TextView's id>);
Josh
  • 1,563
  • 11
  • 16
  • The thing I want to do is get a code reference to the TextView. Do you know how to do this? – user2687781 Oct 11 '13 at 14:32
  • OK, check the second edit. If this still doesn't make sense, we'll need to see some sample code of what you have so far. – Josh Oct 11 '13 at 15:32