On Android here is for example an excellent code fragment,
showing how to achieve five buttons on a dialog fragment...
in your DialogFragment, you have to setOnClickListener(this) for all of your buttons/imageviews etc.
Then you implement View.OnClickListener and have a routine like this...
public void onClick(View v)
{
Utils.Log("Fucking 'A' sort of... ");
switch (v.getId())
{
case R.id.postfragment_send:
break;
etc etc etc
default:
break;
}
}
That's all fantastic. BUT.
Over in my main activity, where I have a ListView. The custom cells have five buttons. Very simply, in the main activity, I have five routines named whatever I like...
public void clickedComments(View v)
{
int position = feed.getPositionForView(v);
...etc etc
}
public void clickedExplosions(View v)
{
int position = feed.getPositionForView(v);
...etc etc
}
public void clickedTanks(View v)
{
int position = feed.getPositionForView(v);
...etc etc
}
Then you just do this which is unbelievably easy ("screw Xcode!") ...
Amazing!
My question, why can't I use the 'onClick system' in dialog fragments?
What am I doing wrong? Can an android expert explain what the fundamental difference is between the two? For the record my projects are 4.1+ only.
Thanks!!
Here I am pasting in a full example of a fragment, using the first method described above.
public class HappyPopupFragment extends DialogFragment implements View.OnClickListener
{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
return dialog;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.happy_popup, container);
_setupButtons(view);
return view;
}
public void onClick(View v)
{
Utils.Log("Fucking 'A' sort of... ");
switch (v.getId())
{
case R.id.button_a:
Utils.Log("tanks !!");
break;
case R.id.button_b:
Utils.Log("bombs !!");
break;
case R.id.button_c:
Utils.Log("guns !!");
break;
case R.id.button_d:
Utils.Log("ammo !!");
break;
default:
break;
}
}
private void _setupButtons(View view)
{
((ImageView)view.findViewById(R.id.button_a)).setOnClickListener(this);
((ImageView)view.findViewById(R.id.button_b)).setOnClickListener(this);
((ImageView)view.findViewById(R.id.button_c)).setOnClickListener(this);
((TextView)view.findViewById(R.id.button_d)).setOnClickListener(this);
}
}