I'd like to pass back user typed string in the dialog fragment: "AddFriendDialogFragment.java" back to the activity that had called it: "HomeActivity.java". I'm doing this thru an interface declared inside "AddFriendDialogFragment.java": "EditNameDialogListener". However for some reason, HomeActivity is not seeing this interface, so I'm getting a "Cannot resolve symbol: "EditNameDialogListener" error.
"HomeActivity.java":
package tutorial.com.example.jerryhou.dialogactionbartutorial;
import android.app.Activity;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class HomeActivity extends Activity implements EditNameDialogListener
{
@Override
public void onFinishEditDialog(String inputText)
{
Toast.makeText(this, "Hi, " + inputText, Toast.LENGTH_SHORT).show();
}
public void showUsernameSearchDialog(View v)
{
FragmentManager fragmentManager = getFragmentManager();
DialogFragment newFragment = new AddFriendDialogFragment();
newFragment.show(fragmentManager, "AddFriendDialog");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_home, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
"AddFriendDialogFragment.java":
package tutorial.com.example.jerryhou.dialogactionbartutorial;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
public class AddFriendDialogFragment extends DialogFragment
{
private static final String TAG = "AddFriendDialogFragment";
public interface EditNameDialogListener
{
void onFinishEditDialog(String inputText);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
View addFriendDialogView = inflater.inflate(R.layout.fragment_add_friend_dialog, null);
// Set an EditText view to get user input
final EditText usernameEditText = (EditText) addFriendDialogView.findViewById(R.id.username);
builder.setView(addFriendDialogView)
// Add action buttons
.setPositiveButton(R.string.search, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id)
{
//Retrieve Username typed in
String username_querystr = usernameEditText.getText().toString();
//Correctly retrieving query str
Log.v(TAG, "Going to search " + username_querystr);
//Pass back query str to search in HomeActivity
EditNameDialogListener activity = (EditNameDialogListener) getActivity();
activity.onFinishEditDialog(usernameEditText.getText().toString());
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
AddFriendDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
}
}