5

I've created a new .xml file in my layout folder called log.xml. It only contains one TextView.

Is it possible to set the text on the textview located in the log.xml from my main activity? Or can it only be set when in an activity which uses the log.xml as view? Hope you get what i mean here, otherwise ill elaborate.

Thanks

Johan
  • 35,120
  • 54
  • 178
  • 293
  • your mainActivity has a main layout which is called main.xml , and you have added a new layout log.xml , soo you should create a new Activity , and you can change of course its value , from the first Activity ( MainActivity ) by using intents – Houcine Jun 23 '12 at 23:40
  • When do you plan on displaying log.xml; in a new Activity, as a row in a ListView (or Spinner, etc), as a new view in your current Activity? – Sam Jun 23 '12 at 23:54
  • @Sam As a new activity when clicking on a button in my main.xml. – Johan Jun 24 '12 at 00:00

4 Answers4

11

If you don't set the xml you are talking about on "setContentView()" you can always get it with layout inflater. You'll have to add the tv to the current layout using addView() though.

LayoutInflater inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View vi = inflater.inflate(R.layout.log, null); //log.xml is your file.
TextView tv = (TextView)vi.findViewById(R.id.tv); //get a reference to the textview on the log.xml file. 
Nuno Gonçalves
  • 6,202
  • 7
  • 46
  • 66
  • Hi Nuno, The above inflating is working fine. I want to update textview value in particular position. any help.? Because,inside layout, i hvae button, when click button, its get response from server and show the update in particular textview – harikrishnan Feb 10 '18 at 10:18
0

Unless log.xml is included in the current, visible layout findViewById() will return null.

Since you want to set the TextView's text when you load it in a new activity, you can pass the new String in the Intent used to start your Activity.

In the appropriate onClick() from your First Activity:

Intent intent = new Intent(this, Second.class);
intent.putExtra("myTextViewString", textString);
startActivity(intent);

In your Second Activity's onCreate():

setContentView(R.layout.log);

TextView textView = (TextView) findViewById(R.id.textView);
Bundle extras = getIntent().getExtras();
if(extras != null) {
    String newText = extras.getString("myTextViewString");
    if(newText != null) {
        textView.setText(newText);
    }
}
Sam
  • 86,580
  • 20
  • 181
  • 179
0

Below solution worked for me -

  1. Get View object of layout XML file (ex. toast_loading_data) -

    View layout = inflater.inflate(R.layout.toast_loading_data,
            (ViewGroup) findViewById(R.id.toast_layout_root));
    
  2. Get the TextView element from this View (ex. TextView id - toast_text)-

    TextView tvToast = (TextView) layout.findViewById(R.id.toast_text);
    
  3. Set text of the TextView -

    tvToast.setText("Loading data for " + strDate + " ...");
    
  4. Below is snippet for customized Toast message from Main Activity -

    View layout = inflater.inflate(R.layout.toast_loading_data,
            (ViewGroup) findViewById(R.id.toast_layout_root));
    TextView tvToast = (TextView) layout.findViewById(R.id.toast_text);
    tvToast.setText("Loading data for " + strDate + " ...");
    Toast toast = new Toast(getApplicationContext());
    toast.setGravity(Gravity.CENTER, 0, 0); //Set toast gravity to bottom
    toast.setDuration(Toast.LENGTH_LONG);   //Set toast duration
    toast.setView(layout);  //Set the custom layout to Toast
    

Hope this helps

Halvor Holsten Strand
  • 19,829
  • 17
  • 83
  • 99
-1

I think I understand what you are trying to say. If you do this:

TextView tv = (TextView) findViewById(R.id.textView2);    
tv.setText(output);

where textView2 is the id of the text view that you want to set the text of, you can set it to any string value using the setText() function. Hope this helps!

eaglzfn37
  • 56
  • 1
  • 6
  • If log.xml is not currently displayed, `tv` will be null. – Sam Jun 23 '12 at 23:55
  • Could he do this? `TextViewActivity.findViewById(R.id.textView)` where `TextViewActivity` is the activity with the text view? – eaglzfn37 Jun 24 '12 at 00:02
  • I don't believe so, findViewById() only works on the current Activity or an inflated View. – Sam Jun 24 '12 at 00:22
  • This answer is incorrect. findViewById() does not work if the 'id' you are looking for is in a different activity. Please refer to @nuno-gonçalves answer which is correct. – SoVinceble Jul 30 '16 at 16:40