So i have a dialog that shows up in the fragment. i need to change a textview with a dialog.
so i created a dialog with an editText, but i am not sure how do i pass the char sequence
from dialog to fragment.
rename_dialog_edit is EditText from dialog
GroupName is TextView from fragment
on positive click:
GroupName.setText((CharSequence) rename_dialog_edit);
after positive click my textview gets empty. how do i properly set it?
Update
protected static TextView GroupName;
protected static EditText rename_dialog_edit;
In the onCreateView i have:
TextView GroupName = (TextView) view.findViewById(R.id.group_details_name);
EditText rename_dialog_edit = (EditText) view.findViewById(R.id.groupdetails_rename);
here is my onCreateDialog:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
//int title = getArguments().getInt("title");
return builder.setView(inflater.inflate(R.layout.group_details_rename_dialog, null))
//.setIcon(R.drawable.logo)
//.setTitle(R.string.groupDetails_rename)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
doPositiveClick();
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
doNegativeClick();
}
})
.create();
}
and here is my positive click:
public static void doPositiveClick() {
GroupName.setText(rename_dialog_edit.getText().toString());
//Log.i("FragmentAlertDialog", "Positive click!");
}
Get the Text from the EditText like:
String str= ((EditText)findViewById(R.id.rename_dialog_edit)).getText().toString();
Set it to textView like:
TextView text = (TextView) findViewById(R.id.this_is_the_id_of_your_textview);
text.setText(str);
have you tried something like this?
Update:
Inside the button listener just do:
GroupName.setText(rename_dialog_edit.getText().toString())
Also try to be consistent with of camelCase variable names in Java. It might be a good practice.
Related
I am building an app in Android studio and basically I want a window to popup when a user clicks the add button. I used the setOnClickListener but when I run the app, nothing happens. Could there possibly something wrong with my code?
Here's my MainActivity code
public class MainActivity extends AppCompatActivity {
Button addBtn;
ListView itemListView;
DatePickerDialog.OnDateSetListener dateSetListener;
String dateString = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addBtn = (Button)findViewById(R.id.addBtn);
itemListView = (ListView)findViewById(R.id.itemListView);
addBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
View view = getLayoutInflater().inflate(R.layout.activity_popup_window, null);
EditText itemName = (EditText)view.findViewById(R.id.itemName);
Button expirationDateBtn = (Button)view.findViewById(R.id.expirationDateBtn);
builder.setView(view)
.setTitle("Add Item")
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (itemName.getText().toString().isEmpty() || dateString == null) {
Toast.makeText(MainActivity.this,
"Item name or expiration date is missing",
Toast.LENGTH_LONG).show();
}
else{
//do action
}
}
});
//when clicked on Expiration Date Btn
//display date on button
AlertDialogBuilder doesn't create and show a new AlertDialog implicitly. It only prepares the dialog before explicitly calling create() (or you can directly call show() if you need to display your dialog in the moment it gets built).
Your code misses the following lines at the end of onClick():
AlertDialog dialog = builder.create();
dialog.show();
or just:
builder.show();
Im trying to bulid a clickable RecyclerView that will open a dialog.
I cant figure out what wrong with my code, after I click the RV item the app carsh and this Eror is in the logcat:
android.util.AndroidRuntimeException: requestFeature() must be called before adding content
at com.android.internal.policy.PhoneWindow.requestFeature(PhoneWindow.java:317)
at com.android.internal.app.AlertController.installContent(AlertController.java:231)
at android.app.AlertDialog.onCreate(AlertDialog.java:423)
The Adapter is suppose to create the CustomDialog when the user clicks an item from the RV.
This is my Adapter relevant method:
#Override
public void onBindViewHolder(PartyViewHolder holder, final int position) {
SingleParty party=partyList.get(position);
String partyItemTitle= party.getPartyTitle()+ " at "+ party.getFourDigitTime()+ " in "+ party.getPlace();
holder.partyDescription.setText(partyItemTitle);
holder.linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
((PartiesActivity)context).showCustomDialog(partyList.get(position),position,partyList);
}
});
}
CustomDialog relevant code:
public class CustomDialog extends AppCompatDialogFragment {
private SingleParty editedParty;
private CustomDialogListener listener;
private RadioGroup eventPosted;
private RadioButton published,notPublished;
private EditText minAge, fourDigitTime, customerCost, originalCustomerCost, ticketsLeft, maxTickets, partyTitle, place, partyId, accountId, description;
private boolean selectedB,deleteB;
private String minAgeS, fourDigitTimeS, customerCostS, originalCustomerCostS, ticketsLeftS, maxTicketsS, partyTitleS, placeS, partyIdS, accountIdS, descriptionS;
ToggleButton deleteTB;
#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
final View view = inflater.inflate(R.layout.custom_dialog, null);
initializeVariables(view);
builder.setView(view)
.setTitle(R.string.dialog_title)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
selectedB=published.isChecked();
getStringDataFromDialog();
listener.applyTexts(selectedB,
deleteB,
minAgeS,
fourDigitTimeS,
customerCostS,
originalCustomerCostS,
ticketsLeftS,
maxTicketsS,
partyTitleS,
placeS,
partyIdS,
accountIdS,
descriptionS);
}
})
// Add action buttons
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//dont save changes
}
});
AlertDialog alertDialog = builder.create();
//gets the dialog to be rtl
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
alertDialog.getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
}
return alertDialog;
}
showCustomDialog:
public void showCustomDialog(SingleParty editedParty, int position, ArrayList<SingleParty> listEdited){
CustomDialog customDialog=new CustomDialog();
customDialog.setEditedParty(editedParty);
//check if he is super admin
//super admin- delete waiting for published and edit/delete published one
this.editedParty=editedParty;
this.editedPosition=position;
this.listEdited=listEdited;
customDialog.show(getSupportFragmentManager(),"");
// customDialog.setDialogValues();
}
Thanks !
I have been unsuccessful in getting the input from my EditText object inside my custom dialog.
public class SetCityDialog extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater factory = LayoutInflater.from(MainActivity.this);
final View view = factory.inflate(R.layout.city_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.city_dialog, null))
// Add action buttons
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int id) {
//This is the input I can't get text from
EditText inputTemp = (EditText) view.findViewById(R.id.search_input_text);
//query is of the String type
query = inputTemp.getText().toString();
newQuery();
getJSON newData = new getJSON();
newData.execute("Test");
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
SetCityDialog.this.getDialog().cancel();
}
});
return builder.create();
}
}
I don't get any exceptions, but the variable query is set to an empty string.
Any help would be fantastic.
I was trying to do the same thing and i get the same error. I don't no why. I already use AlertDialog.Builder in the past and get no trouble. But in your case change this code:
public void onClick(DialogInterface dialog,
int id) {
//This is the input I can't get text from
EditText inputTemp = (EditText) view.findViewById(R.id.search_input_text);
//query is of the String type
query = inputTemp.getText().toString();
newQuery();
getJSON newData = new getJSON();
newData.execute("Test");
}
By this one:
public void onClick(DialogInterface dialog,
int id) {
Dialog f = (Dialog) dialog;
//This is the input I can't get text from
EditText inputTemp = (EditText) f.findViewById(R.id.search_input_text);
query = inputTemp.getText().toString();
...
}
This solution works for me and it seems to be the same for you.
Found on stackoverflow
Use this instead :
View myLayout = nflater.inflate(R.layout.city_dialog, null);
EditText myEditText = (EditText) myLayout.findViewById(R.id.myEditText);
String valueOfEditText = myEditText.getText().toString();
No need to do that much coding. just change
builder.setView(inflater.inflate(R.layout.city_dialog, null))
to
builder.setView(view)
and access text of EditText using **view.findView.....
EditText inputTemp = (EditText) view.findViewById(R.id.search_input_text);
String xyz = inputTemp.getText().toString();
This is worked for me:
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
Dialog inDialog = (Dialog) dialog;
EditText emailAddress = (EditText) inDialog.findViewById(R.id.emailAddress);
email = emailAddress.getText().toString();
if(email.length() == 0) {
objPublicDelegate.showToast("Please fill Email Address.");
}else{
objLoadingDialog.show("Please wait...");
// Call a network thread Async task
mNetworkMaster.runForgetAsync(email);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
The topic explains what i'm after... I can't retrieve the EditText from my custom view in android. All I get is a Nullpointer Exception. :/
I've marked where the problems are in the code with comments.
The ID:s are correct and my XML layout is a simple RelativeLayout containing two EditText attributes.
Obviously I'm missing something trivial here, but I've now stared at the code for almost 2 hours without solving this, so I thought I'll give SO a try instead.
protected void showLoginDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Get the layout inflater
LayoutInflater inflater = this.getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.activity_login, null))
// Add action buttons
.setPositiveButton(R.string.login, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
/* ERROR HERE! */
EditText uName, passWord;
uName = (EditText) findViewById(R.id.login_username);
passWord = (EditText) findViewById(R.id.login_password);
Log.i(TAG, uName.getText().toString() + " " + passWord.getText().toString());
/* STOP */
if(the_view.getSocketTask().isConnected) {
the_view.getSocketTask().send_command("LOGIN ");
} else {
showToast("Not connected!");
}
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
builder.create().show();
}
EDIT:
After suggestions the following code is a working one! Thanks again!
protected void showLoginDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Get the layout inflater
LayoutInflater inflater = this.getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.activity_login, null))
// Add action buttons
.setPositiveButton(R.string.login, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Dialog f = (Dialog) dialog;
/* ERROR HERE! */
EditText uName, passWord;
uName = (EditText) f.findViewById(R.id.login_username);
passWord = (EditText) f.findViewById(R.id.login_password);
Log.i(TAG, uName.getText().toString() + " " + passWord.getText().toString());
/* STOP */
if(the_view.getSocketTask().isConnected) {
the_view.getSocketTask().send_command("LOGIN ");
} else {
showToast("Not connected!");
}
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
builder.create().show();
}
Thanks in advance!
Alex
Cast the Dialog as a View:
View v_iew=inflater.inflate(R.layout.activity_login, null)) ;
builder.setView(v_iew);
Then replace:
uName = (EditText) findViewById(R.id.login_username);
passWord = (EditText) findViewById(R.id.login_password);
with
uName = (EditText) v_iew.findViewById(R.id.login_username);
passWord = (EditText) v_iew.findViewById(R.id.login_password);
(Can't vote so creating new answer: Took me hours to figure this out; adding the qualifier finally fixed it - like you figure out already)
Doesnt work:
final Dialog dialog = new Dialog(this);
...
keyInput = (EditText) findViewById(R.id.key_input);
Works:
final Dialog dialog = new Dialog(this);
...
keyInput = (EditText) dialog.findViewById(R.id.key_input);
In the case of an EditText you should implement TextWatcher It is generally a very bad idea to use editText.getText()
Here is a very simple example code for Custom Dialog containing an EditText as part of the layout. There is also a button that needs to be on the same layout which when clicked will show you the text which you just entered. Have fun!
final String inputString = null;
final Dialog dialog = new Dialog(YourActivityName.this);
dialog.setContentView(R.layout.custom_dialog_layout);
EditText editText = (EditText) dialog.findViewById(R.id.id_of_edit_text);
Button done = (Button) dialog.findViewById(R.id.done);
dialog.show();
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
inputString = s.toString();
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
done.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(YourActivityName.this, inputString, Toast.LENGTH_SHORT);
dialog.dismiss();
}
});
I've got a View defined in an xml file. It contains two Edittext fields (amongt other things like text)
I use an AlertBuilder to trigger a dialog where a user enters text(such as username and pass) into both edittext fields. When I try to retrieve the strings and send them to Login(), both strings are just null. What is going on?
It seems like somehow the string data isn't saved?
Here's when I show the Dialog in my app:
SignInDialog.show(ScreenMain.this,
"Login",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
LayoutInflater inflater = (LayoutInflater) ScreenMain.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.screen_dialog_login, null);
LogIn(((EditText) layout.findViewById(R.id.screen_dialog_login_username_edit)).getText().toString(),
((EditText) layout.findViewById(R.id.screen_dialog_login_password_edit)).getText().toString());
}
},
"Cancel",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
Here's a class I use to instantiate a Dialog:
/* login dialog*/
static class SignInDialog {
public static void show(Context context, String positiveText, DialogInterface.OnClickListener positive, String negativeText, DialogInterface.OnClickListener negative){
AlertDialog.Builder builder;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.screen_dialog_login, null);
builder = new AlertDialog.Builder(context);
builder.setView(layout);
if(positive != null && positiveText != null){
builder.setPositiveButton(positiveText, positive);
}
if(negative != null && negativeText != null){
builder.setNegativeButton(negativeText, negative);
}
builder.create().show();
}
}
To inflate a layout is to create a new instance of it. (You're not receiving a reference to an existing instance.) So, in your onClick you are creating a new copy of the layout and your fields don't contain any text because they are not the same ones your user just entered text in.
Why not just completely subclass AlertDialog.Builder and add a method to retrieve the EditText values?
Do something like:
View layout = inflater.inflate(R.layout.screen_dialog_login, null);
layout.findViewById(R.id.*yourwidget*);
i tried it and it helped
Here is the method I am using:
private void showPopUp3() {
AlertDialog.Builder helpBuilder = new AlertDialog.Builder(AlarmReceiverActivity.this);
helpBuilder.setTitle("hi");
// helpBuilder.setMessage("This is a Simple Pop Up");
final EditText input = new EditText(this);
input.setHeight(20);
input.setText("");
LayoutInflater inflater = getLayoutInflater();
final View checkboxLayout = inflater.inflate(R.layout.alarm, null);
checkboxLayout.findViewById(R.id.Yes).setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
// setTitle("button2");
checkboxLayout.findViewById(R.id.note).setVisibility(View.VISIBLE);
}
});
checkboxLayout.findViewById(R.id.No).setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
// setTitle("button2");
checkboxLayout.findViewById(R.id.note).setVisibility(View.INVISIBLE);
}
});
helpBuilder.setView(checkboxLayout);
helpBuilder.setPositiveButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Do nothing but close the dialog
mMediaPlayer.stop();
finish();
}
});
helpBuilder.setNegativeButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Do nothing but close the dialog
mMediaPlayer.stop();
//showSimplePopUp();
}
});
// Remember, create doesn't show the dialog
AlertDialog helpDialog = helpBuilder.create();
helpDialog.show();
}