I am developing an android application. I am using the alertDialog class. Where I have a custom listview and 2 buttons. In my custom listview I have an EditText Field. But when the dialog pops and I touch the EditText while the cursor starts flashing in the Textbox the softKeyboard doesn't appear. Is it because of the alertdialog?
public class ProgressReport extends ListActivity {
private ResultSet receivedData;
private StudentProgressAdapter ca;
private Button btnRtn;
ArrayList<String> content =new ArrayList<String>();
ArrayList<String> subContent = new ArrayList<String>();
AlertDialog dialog ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(android.R.style.Theme_Holo_Light);
setContentView(R.layout.examslayout);
for(int i =0;i<20;i++)
content.add("ELEMENTARY: "+i);
for(int i =0;i<20;i++)
subContent.add("ΒΑΣΙΚΗ ΚΑΤΗΓΟΡΙΑ");
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Student Stats").setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
//MainActivity.this.finish();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});;
ListView modeList = new ListView(this);
modeList.setAdapter(new ProgressReportAdapter2(this, new int[]{R.layout.arrowlistview}, new int[]{R.id.textView1,R.id.textView2},content,20,subContent));
builder.setView(modeList);
dialog = builder.create();
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
Intent intent = getIntent();
receivedData = (ResultSet) intent.getSerializableExtra("SentData");
ArrayList<String> content = new ArrayList<String>(MainPagerActivity.receivedData.getStudents().length);
for (int i=0; i <receivedData.getStudents().length; i++)
content.add(receivedData.getStudents()[i].getProperty(3).toString()+" "+receivedData.getStudents()[i].getProperty(2).toString());
ca= new StudentProgressAdapter(this, new int[]{R.layout.stdprgrlistview}, new int[]{R.id.textView1}, content,content.size());
setListAdapter(ca);
btnRtn= (Button) findViewById(R.id.btn);
btnRtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
dialog.show();
}
Try this:
AlertDialog alertToShow = alert.create();
alertToShow.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
alertToShow.show();
Update:
you can modify your code like this
dialog = builder.create();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
dialog.show();
Hope this helps.
After the creation of the dialog add :
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);`
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();
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
I have added one button "btn". I have set onclicklistener, inside the button "btn" I have added another button "btnYes" to show display custom dialog when I add these "btnYes" apps get crash.
When I remove the "btnyes" button app is working.
Can we add onclicklistener for two button inside one button for different work?
Java Code
public class MainActivity extends AppCompatActivity {
private Button btn, btnYes, btnNo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.click);
btnYes = findViewById(R.id.yes);
btnNo = findViewById(R.id.no);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder dialogBox = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
View myView = inflater.inflate(R.layout.custom_dialogbox, null);
dialogBox.setView(myView);
final AlertDialog mybuilder = dialogBox.create();
mybuilder.setCancelable(false);
btnYes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mybuilder.dismiss();
}
});
}
});
}
}
Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.customdialog.MainActivity$1.onClick(MainActivity.java:33)
If btnYes and btnNo are on the dialog, then you should initialize these buttons using the AlertDialog's View object.
You have to modify your code like following.
public class MainActivity extends AppCompatActivity {
private Button btn, btnYes, btnNo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.click);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder dialogBox = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
View myView = inflater.inflate(R.layout.custom_dialogbox, null);
// these button should be initialize here.
btnYes = myView.findViewById(R.id.yes);
btnNo = myView.findViewById(R.id.no);
dialogBox.setView(myView);
final AlertDialog mybuilder = dialogBox.create();
mybuilder.setCancelable(false);
btnYes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mybuilder.dismiss();
}
});
}
});
}
}
Hope it helps you.
if you want to display Dialog with choices yes or no, use this
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(message)
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// action for yes
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// action for no
}
});
AlertDialog alert = builder.create();
alert.show();
But, if you want to use a custom dialog layout, you should do it like this.
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.YOUR_LAYOUT_HERE);
Button btnyes= dialog.findViewById(R.id.btnyes);
btnyes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.cancel();
}
});
I have a problem about my listview i am populating my listview with what the user enters in the dialog box when the add new button is clicked. When this button is clicked a dialog appears and asks the user to input something when the user presses done what he/she enters does not appear in the view from the list even when i put the notifyDataSetChanged. What the user inputs only appear when the fragment has been restarted. I think it may involved the saving in sharedpreferences which is the tinyDB in the code. How do i make sure the notifyDataSetChanged is noticed by the list that is stored. Thank you
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_year_two_my_courses_first_semester, container, false);
btnAddNew = (Button)rootView.findViewById(R.id.btnAddNewYearTwoFirstSemester);
btnSave = (Button)rootView.findViewById(R.id.btnSaveAddedSubjectsFirstSemesterYr2);
listView = (ListView)rootView.findViewById(R.id.listViewSubjectsMyCoursesFirstSemesterYr2);
tinyDB = new TinyDB(getContext());
storedList = new ArrayList<>(tinyDB.getListString("storedList1stYr2"));
storedArray = new ArrayList<>();
generalList = new ArrayList<>();
generalList.addAll(storedArray);
generalList.addAll(storedList);
getActivity().setTitle("Year Two");
setHasOptionsMenu(true);
btnAddNew.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btnSave.setVisibility(View.VISIBLE);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getContext(),R.style.DialogeTheme);
// Setting Dialog Title
alertDialog.setTitle("Add new");
// Setting Dialog Message
String getUsername = tinyDB.getString("Username");
alertDialog.setMessage("Hello "+ getUsername + ", please write the new subject");
final EditText input = new EditText(getContext());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
input.setLayoutParams(lp);
input.setTextColor(Color.parseColor("#f06292"));
alertDialog.setView(input);
alertDialog.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
String getSubjectInput = input.getText().toString();
storedList.add(getSubjectInput);
tinyDB.putListString("storedList1stYr2", storedList);
arrayAdapter.notifyDataSetChanged();
}
});
alertDialog.setNegativeButton("CANCEL",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.create();
alertDialog.show();
}
});
arrayAdapter = new CustomListAdapter(getContext(),generalList);
listView.setAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();
return rootView;
}
Your CustomListAdapter is observing generalList, which does not change when you add a new String in storedList.
You could do this :
public void onClick(DialogInterface dialogInterface, int i) {
String getSubjectInput = input.getText().toString();
storedList.add(getSubjectInput);
tinyDB.putListString("storedList1stYr2", storedList);
generalList.add(getSubjectInput);
arrayAdapter.notifyDataSetChanged();
}
or clear and reset your list.
Like the title says. I have coded onClickListener to my AlertDialog but i don't know how to put there onLongClickListener.
This is my code:
private void addRecipeMethod() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title");
adapter = new ArrayAdapter<>(getBaseContext(), android.R.layout.simple_list_item_1, getArrayList("ListOfRecipes"));
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
List<String> list = new ArrayList<>(getArrayList("ListOfRecipes"));
getArrayListRecipes(list.get(which));
List<String> listMain = new ArrayList<>(getArrayList("ListMain"));
listMain.addAll(getArrayListRecipes(list.get(which)));
saveList(listMain, "ListMain");
adapter = new ArrayAdapter<>(getBaseContext(), android.R.layout.simple_list_item_1, getArrayList("ListMain"));
listView.setAdapter(adapter);
//Toast.makeText(getApplicationContext(), "you have clicked " + list.get(which) , Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
PS. void addRecipeMethod is called when Menu Item is clicked
Create AlertDialog with custom layout like this
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
// ...Irrelevant code for customizing the buttons and title
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.alert_label_editor, null);
dialogBuilder.setView(dialogView);
Button button = (Button)dialogBuilder.findViewById(R.id.btnName);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Commond here......
}
});
button.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return false;
}
});
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
Add Button in alert_label_editor xml and add setOnLongClickListener for that Button
Button button = (Button)dialogBuilder.findViewById(R.id.btnName);
button.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return false;
}
});
I need some help to run an AlertDialog from my application. It won't compile while there are some errors. Especially at the line alertDialog.setItems(items, new DialogInterface.OnClickListener() with the setItems().
Please take a look.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nastavenie_casu);
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
final CharSequence[] items = {"30 minút", "45 minút", "60 minút", "75 minút", "90 minút"};
Button tlacidlo = (Button) findViewById(R.id.spusti);
EditText pripomienka = (EditText) findViewById(R.id.upozornit_za);
pripomienka.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Intent myIntent = new Intent(view.getContext(), agones.class);
// startActivityForResult(myIntent, 0);
AlertDialog alertDialog = new AlertDialog.Builder(NastavenieCasu.this).create(); //Read Update
alertDialog.setTitle("hi");
alertDialog.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do something
}
alertDialog.show();
});
}
}
}
setItems() is part of the AlertDialog.Builder class, not AlertDialog. Try:
AlertDialog.Builder builder = new AlertDialog.Builder(NastavenieCasu.this);
builder.setTitle("hi");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//
// do something
}
AlertDialog alertDialog = builder.create();
alertDialog.show();