I created a DialogFragment that looks like this:
package com.name.test;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.name.test.MainActivity.DialogType;
public class AlertDialogFragment extends DialogFragment {
public static DialogType type;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View v = getActivity().getLayoutInflater().inflate(R.layout.search_fragment, null);
final EditText searchField = (EditText)v.findViewById(R.id.searchEditTxt);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
switch(type) {
case SEARCH:
builder.setView(inflater.inflate(R.layout.search_fragment, null))
.setTitle("Search Dialog")
.setPositiveButton("Search", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// logic for the search button
// Toast.makeText(getActivity().getBaseContext(), "Search Dialog", Toast.LENGTH_SHORT).show();
String tempString = searchField.getText().toString();
System.out.println(tempString);
}
})
//.setView(searchField)
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// logic for the cancel button
AlertDialogFragment.this.getDialog().cancel();
}
});
break;
case PREFERENCES:
builder.setView(inflater.inflate(R.layout.preferences_fragment, null))
.setTitle("Preferences")
.setPositiveButton("Save", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// logic for the search button
Toast.makeText(getActivity().getBaseContext(), "Preferences", Toast.LENGTH_SHORT).show();
}
})
//.setView(searchField)
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// logic for the cancel button
AlertDialogFragment.this.getDialog().cancel();
}
});
break;
default:
break;
}
return builder.create();
}
public static AlertDialogFragment newInstance(DialogType dialogType) {
type = dialogType;
return new AlertDialogFragment();
}
}
In the XML file for this fragment I have this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="search_fragment"
android:orientation="vertical" >
<EditText
android:id="#+id/searchEditTxt"
android:hint="#string/search_menu_hint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
I'm trying to target that EditText in the DialogFragment so when the user types something and clicks on Save or OK I can store its value. I can't seem to be able to target the EditText properly. My System.out.println(tempString); is empty. What am I doing wrong? Thanks much.
try to change
builder.setView(inflater.inflate(R.layout.search_fragment, null));
to
builder.setView(v);
You just need to set your inflated view to your builder.
Related
So belove is my code, in the design i have one textview with the ID t. I am using android 5.0 to build and i have the required SDK(android 5 and 5.1).
package com.example.alertdialogbox;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AlertDialog;
import android.os.Bundle;
import android.content.DialogInterface;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView t1=findViewById(R.id.t);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlertDialog.Builder b1=new AlertDialog.Builder(this);
b1.setTitle("Alert Dialog");
b1.setMessage("Do you want to continue?");
b1.setCancelable(false);
b1.setPositiveButton("YES", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
t1.setText("Clicked YES");
}
});
b1.setNegativeButton("NO", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
t1.setText("Clicked NO");
}
});
b1.setNeutralButton("CANCEL", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
t1.setText("Clicked CANCEL");
}
});
AlertDialog d=b1.create();
d.show();
}
}
Sorry if my question looks silly :)
Thank you
call TextView t1=findViewById(R.id.t); after setContentView(R.layout.activity_main);
I searched but the questions I could see dealt with just copy, or copying to clipboard, or just pasting. Specifically what I want (in 1 button click, the PositiveButton in AlertDialog) is to copy the text entered by user in the EditText of my alertdialog to the EditText of my Activity.
Can you tell me how to do this please? Here is the code I am using and trying to fix:
//when user touches on "commentname" edittext we want the alertdialog to open
commentname.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
AlertDialog.Builder builder = new AlertDialog.Builder(NewContact.this);
builder.setTitle("Ur Comment:");
//start the following xml file/ layout
View viewInflated = LayoutInflater.from(NewContact.this).inflate(R.layout.comment_text_pop_up, null, false);
builder.setView(viewInflated);
// Set up the buttons
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
//we want to copy the text entered in "input", in the alertdialog,
//and paste it in commentname
commentname.setText(alertdialog_edittext.getText().toString());
}
});
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
alertdialog_edittext = (EditText) dialog.findViewById(R.id.input);
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
dialog.show();
return true;
}
return false;
}
});
i wrote this simple code example for you to do it.
just add method to setText on Your edittext in your Activity:
private void setTextFromDialog(final String textFromDialog){
myEditText.setText(textFromDialog);
}
when user click in dialog get text from edittext dialog and pass using this method:
setTextFromDialog(YouEditTextValueX);
here code example:
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText myEditText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button ShowDialog = findViewById(R.id.showdialog_id);
myEditText = findViewById(R.id.editText_id);
ShowDialog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
final EditText edittext = new EditText(MainActivity.this);
alert.setTitle("Title");
alert.setMessage("Message");
alert.setView(edittext);
alert.setPositiveButton("Set text", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String YouEditTextValueX = edittext.getText().toString();
if(YouEditTextValueX.length() > 0){
//this line for call method and pass the text
setTextFromDialog(YouEditTextValueX);
}
}
});
alert.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// what ever you want to do with No option.
}
});
alert.show();
}
});
}
private void setTextFromDialog(final String textFromDialog){
myEditText.setText(textFromDialog);
}
}
hope this help you
I'm new to java and I'm currently testing it in android studio.
I'm trying to access function showMessage() in TEST class from TEST2 class.
There is no problem if showMessage() function is called within the same class but the code stopped if I tried to access it from another class.
Maybe there is something wrong with my logic or syntax, but I'm not entirely sure which one is it. I already tried to search for some solution but there is no answer that I can find to help with my situation.
Here is my TEST.java code
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class TEST extends AppCompatActivity {
public TEST2 ab;
Button button;
private final String text ="Testing test test test testing test";
public void showMessage(String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(message)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setCancelable(false) // cancel with button only
.show();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
button = (Button) findViewById(R.id.buttontest);
ab = new TEST2(this);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showMessage(text);
}
});
}
}
class TEST2 extends View{
public TEST t = new TEST();
public TEST2(Context context) {
super(context);
//there is error if I tried this 2 line code
//onTest();
//t.showMessage("TESTING FROM TEST2 CLASS");
}
public void onTest(){
t.showMessage("TESTING FROM TEST2 CLASS");
}
}
and this is my activity_test.xml layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/buttontest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
You can do this by passing parameter context
public void showMessage(String message, Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(message)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setCancelable(false) // cancel with button only
.show();
}
Alert Dialog is a UI element it will not run from another class if activity is changed. if activity is not change try this...
public void showMessage(final String message,final Context mContext) {
runOnUiThread(new Runnable() {
#Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setMessage(message)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setCancelable(false) // cancel with button only
.show();
}
});
}
public void onTest(){
if (getContext() instanceof Test)
((Test)getContext()).showMessage("TESTING FROM TEST2 CLASS");
}
Just pass the Context of your TEST2 class.
public void showMessage(Context context, String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
.............
......................
}
TEST2:
public TEST2(Context context) {
super(context);
t.showMessage(context, "TESTING FROM TEST2 CLASS");
}
I am learning Android from some book and keep getting error on .setMultichoiceItem block : cannot resolve method .setMultichoiceItems .
I check it over multiple times and my code is all case sensitive, and no misspelled words.
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
public class MainActivity extends ActionBarActivity {
CharSequence[] items = {"Google","Safari","Yahoo"};
Boolean[] itemChecked = new Boolean[items.length];
Button btn ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showDialog(0);
}
});
}
protected Dialog onCreateDialog(int i) {
switch (i) {
case 0:
return new AlertDialog.Builder(this)
.setTitle("Test of Dialog")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplication(), "OK Clicked !", Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Cancel Clicked !", Toast.LENGTH_LONG).show();
}
})
.setMultiChoiceItems(items, itemChecked,
new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Toast.makeText(getApplicationContext(), items[which] + (isChecked ? " checked!" : " unchecked!"), Toast.LENGTH_SHORT).show();
}
}).create();
}
return null;
}
}
Logcat Error : Cannot resolve method 'setMultiChoiceItems(java.lang.CharSequence[], java.lang.Boolean[], android.content.DialogInterface.OnMultiChoiceClickListener)'
Any help would be awesome .
Thanks
try this one,
Change this line
Boolean[] itemChecked = new Boolean[items.length];
to
boolean[] itemChecked = new boolean[items.length];
because its second parameter accepts boolean[], not Boolean[] objects
setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, DialogInterface.OnMultiChoiceClickListener listener)
import android.content.DialogInterface.OnMultiChoiceClickListener;
I found the better solution:
public void alertMultipleChoiceItems(){
final CharSequence[] dialogList = Symbollist.toArray(new CharSequence[Symbollist.size()]);
HashSet<String> uniqueValues = new HashSet<>(Symbollist);
Symbollist.clear();
for (String value : uniqueValues) {
//... //Do something
Symbollist.add(value);
}
AlertDialog.Builder builder = new AlertDialog.Builder(AddPackageStep3.this);
selectedItems = new ArrayList<Integer>();
// set the dialog title
boolean[] itemChecked = new boolean[selectedItems.size()];
builder.setMultiChoiceItems(dialogList,null, new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
// if the user checked the item, add it to the selected items
selectedItems.add(which);
}
else if (selectedItems.contains(which)) {
// else if the item is already in the array, remove it
selectedItems.remove(Integer.valueOf(which));
}
// you can also add other codes here,
// for example a tool tip that gives user an idea of what he is selecting
// showToast("Just an example description.");
}
})
// Set the action buttons
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// user clicked OK, so save the mSelectedItems results somewhere
// here we are trying to retrieve the selected items indices
String selectedIndex = "";
for(Integer i : selectedItems){
selectedIndex += i + ", ";
}
//showToast("Selected index: " + selectedIndex);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// removes the AlertDialog in the screen
}
})
.show();
}
You can convert to boolean Array this way by using the Guava:
boolean[] listBoolean = Booleans.toArray(checkedList);
I made an application that checks for root using RootTools. Now, I added an option to check for BusyBox availability. When I click on "Check For BusyBox", nothing happens, though the "Check For Root" is working perfectly. I don't understand why is it happening. Please help!
package com.maverick.checkforroot;
import com.stericson.RootTools.RootTools;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView Manufacturer = (TextView) findViewById(R.id.Manufacturer);
String Manu = android.os.Build.MANUFACTURER;;
Manufacturer.setText(Manu);
TextView tv1 = (TextView) findViewById(R.id.tv1);
String Model = android.os.Build.MODEL;
tv1.setText(Model);
TextView Product = (TextView) findViewById(R.id.Product);
String Pro = android.os.Build.PRODUCT;;
Product.setText(Pro);
Button Root = (Button) findViewById(R.id.Root);
Root.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (RootTools.isAccessGiven()) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("Congratulations!");
builder.setMessage("You Have Root Access!");
builder.setPositiveButton("OKAY", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
else {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("Oops!");
builder.setMessage("No Root Access!");
builder.setPositiveButton("OKAY", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
Button BusyBox = (Button) findViewById(R.id.BusyBox);
BusyBox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (RootTools.isBusyboxAvailable()) {
Toast.makeText(MainActivity.this,"No BusyBox!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this,"BusyBox Is Available!", Toast.LENGTH_LONG).show();
}
}
});
}
});
}}
All the code concerning the BusyBox button (inclusive the definition of its OnClickListener) is written inside the Root buttons OnClickListener code. Move the code and it should work for you.