Skip to content Skip to sidebar Skip to footer

Android: Get Access To View Elements In Onclicklistener

I have a Fragment container and some buttons outside the fragment which interact with the content of the fragment. When I click on the button, I need to get the information inside

Solution 1:

The v in your onCLickListener is referring to the Button itself, and calling findViewByID on the button means that the button is looking through its children (it doesn't have any) to find an EditText element. if this code is running in an activity, you may be able to do:

Buttonbm= (Button) findViewById(R.id.bm);
bm.setOnClickListener(newOnClickListener() {
  publicvoidonClick(View v) {
    EditTexteditTextIn1= (EditText) findViewById(R.id.editTextIn1);
  }
});

alternately, if you have a reference to the fragment's container (like a framelayout or something), you can do

Buttonbm= (Button) findViewById(R.id.bm);
bm.setOnClickListener(newOnClickListener() {
  publicvoidonClick(View v) {
    EditTexteditTextIn1= (EditText) myFragmentContainer.findViewById(R.id.editTextIn1);
  }
});

Post a Comment for "Android: Get Access To View Elements In Onclicklistener"