Saturday, 7 September 2013

AlertDialog to change ImageButton Background

AlertDialog to change ImageButton Background

I have the following code written but it crashes when I run it. I am
trying to use an AlertDialog to allow the background image of an
ImageButton to be changed based on the offered selection in the
AlertDialog:
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
public class PageTwoFragment extends Fragment {
int i = 0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
final Bundle savedInstanceState) {
final ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.page2_layout, container, false);
final ImageButton pp_btn1 = (ImageButton) rootView.findViewById(R.id.m1_btn);
final ImageButton m1_ts_btn = (ImageButton)
rootView.findViewById(R.id.m1_ts_btn);
final Context context = getActivity();
pp_btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i +=1;
if (i % 2 == 0) {
pp_btn1.setImageResource(R.drawable.pause);
} else {
pp_btn1.setImageResource(R.drawable.play);
}
}
});
m1_ts_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder alertDialogBuilder = new
AlertDialog.Builder(context);
LayoutInflater inflater = getLayoutInflater(savedInstanceState);
View view = inflater.inflate(R.layout.ts_alert, (ViewGroup)
rootView.findViewById(R.id.RelativeLayout1));
alertDialogBuilder.setTitle("My Title");
ImageButton ts_1_4 = (ImageButton) view.findViewById(R.id.m1_ts_btn);
alertDialogBuilder.setView(view);
alertDialogBuilder.show();
ts_1_4.setImageResource(R.drawable.image_2);
}
});
return rootView;
}
}
I currently only have the one image to show but once I get this working I
will then add more.
Many thanks

No comments:

Post a Comment