tags:

views:

86

answers:

2

This is going to be a bit lame question. I have the following code:

..............
 public void onCreate (Bundle bundle)
 {
  super.onCreate(bundle);
  this.setContentView(R.layout.main2);
  Button bnt = (Button) this.findViewById(R.id.browser);
  bnt.setOnClickListener(new ButtonListener());

 }
..............
class ButtonListener implements android.view.View.OnClickListener
{

 public void onClick(View v) 
 {
  // I have a TextView in my xml layout file. 
  // I'd like to get it and change my text when I click this button.
  // But I can't get it (the TextView) unless I make it as a value of a static member of this class and pass it to the constructor. 
  //I believe I am missing a big point here, so i'd be very thankful if you could explain how this is meant to be done ? 

 }
}

Any help is appreciated.

A: 

EDIT I dont think it will be possible. The view V only has the buttons view in it....

You will know if you typecast the same

  class ButtonListener implements android.view.View.OnClickListener
    {

     public void onClick(View v) 
     {

        Button vg=(Button)v;
        Log.e("", "views are "+vg.getText());

//However u can set the test here for ue textview txt.setText("change text");

     }
    }

I did not understand your question properly. But if you just want to get the text out from ur TextView you can try like this

TextView txt;

public void onCreate (Bundle bundle)
 {
  super.onCreate(bundle);
  this.setContentView(R.layout.main2);
  Button bnt = (Button) this.findViewById(R.id.browser);
  txt=(TextView)this.findViewById(R.id.urtextview);
  bnt.setOnClickListener(new ButtonListener());

 }
..............
class ButtonListener implements android.view.View.OnClickListener
{

 public void onClick(View v) 
 {
  // I have a TextView in my xml layout file. 
  // I'd like to get it and change my text when I click this button.
  // But I can't get it (the TextView) unless I make it as a value of a static member of this class and pass it to the constructor. 
  //I believe I am missing a big point here, so i'd be very thankful if you could explain how this is meant to be done ? 
//I think this should get u the string...
System.out.println("text is "+txt.getText().toString());

 }
}
Rahul
See, I have a button, and when I click it, I'd like to change the text of a TextView. So my question is "Is there a way to do that when only coding in the onClick method ?" I did say it would be a lame question ...Imagine I only have the (View v) information and I'd like to interact with a UI element in the layout.Can I get all the UI elements in a layout if I only have a reference to one of them?
George
In your case i don't think it would be possible. If you are getting a layouts view probably u can get the children from it in ur case it will be just a button. Check my updated post
Rahul
A: 

You could try this:

class ButtonListener implements android.view.View.OnClickListener {
    public void onClick(View v) {
        View parent = v.getParent();
        if (parent != null) {
            TextView txtView = parent.findViewById(R.id.mytextview);
            txtView.setText(...);
        }
    }
}

the usage depends on your layout. Its possible, that the parent of your button is not the parent of your textview so be careful...

WarrenFaith
My point exactly. View parent = (View) v.getParent() - I wrote it this way though - .getParent() returns a ViewParent object. Still, this was exactly what I was looking for. Thanks.
George
the more robust way would be to give your root layout element an id and do a `findViewById()` in your `onCreate()`. So you have full easy access to your layout and all views inside. So Rahul has given the correct answer... just use the root element instead of the textview
WarrenFaith