Reputation: 15
Well, I'm testing "Dialog Fragment" and I've done a little program that when it starts show a Dialog Fragment with 3 options, the TextView on the main activity will show which option was selected. So basicly it's communication between Dialog and Activity. I was following an example with 2 buttons(positive and negative) but now I'm testing with my own layout with 3 button and I don't know how to continue...
Let's see the code:
3 Button in dialog_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3" />
</LinearLayout>
Then the DialogFragment class
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.webkit.WebView.FindListener;
import android.widget.Button;
public class TwoActionButtonsDialog extends DialogFragment {
private DialogListener listener;
public interface DialogListener {
public void onDialogOption1(DialogFragment dialog);
public void onDialogOption2(DialogFragment dialog);
public void onDialogOption3(DialogFragment dialog);
}
// Override the Fragment.onAttach() method to instantiate the
// NoticeDialogListener
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Verify that the host activity implements the callback interface
try {
// Instantiate the NoticeDialogListener so we can send events to the
// host
listener = (DialogListener) activity;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString()
+ " must implement DialogListener");
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setTitle("Test").setView(
inflater.inflate(R.layout.dialog_layout, null));
Button btn1 = ((Button) findViewById(R.id.button1));
Button btn2 = (Button) findViewById(R.id.button2);
Button btn3 = (Button) findViewById(R.id.button3);
// Create the AlertDialog object and return it
return builder.create();
}
}
And the main activity
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends FragmentActivity implements
TwoActionButtonsDialog.DialogListener {
private static final String TAG = "dialog";
private TextView texto = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
texto = (TextView) findViewById(R.id.textView1);
showTwoActionButton();
}
public void showTwoActionButton() {
DialogFragment dialog = new TwoActionButtonsDialog();
dialog.show(getSupportFragmentManager(), TAG);
}
// The dialog fragment receives a reference to this Activity through the
// Fragment.onAttach() callback, which it uses to call the following methods
// defined by the NoticeDialogFragment.NoticeDialogListener interface
@Override
public void onDialogOption1(DialogFragment dialog) {
// User touched the dialog's positive button
texto.setText("1");
}
@Override
public void onDialogOption2(DialogFragment dialog) {
// User touched the dialog's negative button
texto.setText("2");
}
@Override
public void onDialogOption3(DialogFragment dialog) {
// TODO Auto-generated method stub
texto.setText("3");
}
}
I'm not sure how to manage the buttons beacause I can't do:
Button btn1 = ((Button) findViewById(R.id.button1));
Button btn2 = (Button) findViewById(R.id.button2);
Button btn3 = (Button) findViewById(R.id.button3);
it throws an error: "The method findViewById(int) is undefined for the type TwoActionButtonsDialog". If I've inflated the layout why I can't acces to them?
What should I do?
Upvotes: 0
Views: 398
Reputation: 11234
After you inflate your view for a Dialog, save a result:
LayoutInflater inflater = getActivity().getLayoutInflater();
View v = inflater.inflate(R.layout.dialog_layout, null)
builder.setTitle("Test").setView(v);
After that you can traverse your view for buttons:
Button btn1 = (Button) v.findViewById(R.id.button1);
Upvotes: 1