java - How to implement a ClickListener for a button from a custom layout to use in a FragmentDialog -
i'm try set action imagebutton. when dialgofragment called , shown on screen, want preess button , action. when put action inside onclick in code below didn't work. i'm sure that's not right way of doing it.
i'm using class fragment:
public class generaldialogfragment extends dialogfragment { @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout use dialog or embedded fragment view v = inflater.inflate(r.layout.activity_dialog, container, false); imagebutton mimagebutton = (imagebutton) v.findviewbyid(r.id.image_button); mimagebutton.setonclicklistener(new view.onclicklistener(){ @override public void onclick(view v) { string name = ""; sharedpreferences prefs = getactivity().getsharedpreferences("my_prefs_name", context.mode_private); string yourmessage = prefs.getstring("msg", null); if (yourmessage != null) { name = prefs.getstring("msg", "no name defined");//"no name defined" default value. } scrolltextview txt = (scrolltextview) v.findviewbyid(r.id.scroll_text); txt.settext(name); txt.settextsize(220); txt.startscroll(); } }); return v; }
and in activity_dialog layout have imagebutton:
<imagebutton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/image_button" android:src="@drawable/ic_image_500px"/>
edited: action in class above. error in action. have mainactivity press send button:
public void send(view view){ edittext medittext = (edittext)findviewbyid(r.id.text_msg); string message = medittext.gettext().tostring(); sharedpreferences.editor editor = getsharedpreferences("my_prefs_name", mode_private).edit(); editor.putstring("msg", message ); editor.commit(); intent intent = new intent(this, messageactivity.class ); startactivity(intent); }
it calls messageactivity call generaldialogfragment class shows imagebutton code:
dialogfragment mdialog = new generaldialogfragment(); mdialog.show(getsupportfragmentmanager(), "dialog tag");
because of v
you've added before, it's working without errors:
view v = inflater.inflate(r.layout.activity_dialog, container, false); imagebutton mimagebutton = (imagebutton) v.findviewbyid(r.id.image_button);
Comments
Post a Comment