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.
Related
My page is activity page and now i would like to change to fragment but it crashed,
Caused by: java.lang.ClassCastException: com.mac.Activity cannot be cast to android.app.Activity
I dont know which part of code is crashed. So I put my code in below.
Code:
public class Activity extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.activity, container, false);
final TextView nameTxt = (TextView) view.findViewById(R.id.nameTxtDetail);
final TextView descTxt = (TextView) view.findViewById(R.id.descDetailTxt);
final Button btn = (Button) view.findViewById(R.id.btn);
//RECEIVE
Intent i = getActivity().getIntent();
String name = i.getExtras().getString("NAME_KEY");
String desc = i.getExtras().getString("DESCRIPTION_KEY");
//BIND
nameTxt.setText(name);
descTxt.setText(desc);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
getContext());
alertDialogBuilder.setTitle("Do you want to login?");
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setNeutralButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent i = new Intent(getActivity(), FacebookLogin.class);
startActivity(i);
}
})
.setPositiveButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, close
// current activity
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
return view;
}
#Override
public void onResume() {
super.onResume();
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
TextView toolbar_title = (TextView) getActivity().findViewById(R.id.toolbar_title);
toolbar_title.setText("DETAIL");
}
#Override
public void onStop() {
super.onStop();
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(false);
}
}
First I though crashed because forget put final for textview and button but i already put now, still crashed. I am using android studio. Hope somebody help thanks.
There are a lot of factors here that could cause this. But firstly did you remove it from Android Manifest, because fragments are not shown in Android Manifest, and if you didn't it would try opening it as an activity.
Override another method onViewCreated(). Get all the codes you put in onCreateView(), except the first line that inflates, put them in onViewCreated(). This way you are sure the view has been created and is ready to be used.
#Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity, container, false);
}
#Override
public void onViewCreated (View view, Bundle savedInstanceState){
super.onViewCreated (view, savedInstanceState);
final TextView nameTxt = (TextView) view.findViewById(R.id.nameTxtDetail);
final TextView descTxt = (TextView) view.findViewById(R.id.descDetailTxt);
final Button btn = (Button) view.findViewById(R.id.btn);
//RECEIVE
Intent i = getActivity().getIntent();
String name = i.getExtras().getString("NAME_KEY");
String desc = i.getExtras().getString("DESCRIPTION_KEY");
//BIND
nameTxt.setText(name);
descTxt.setText(desc);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( getContext());
alertDialogBuilder.setTitle("Do you want to login?");
// set dialog message alertDialogBuilder .setCancelable(false)
.setNeutralButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent i = new Intent(getActivity(), FacebookLogin.class); startActivity(i);
}
})
.setPositiveButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, close //
current activity dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it alertDialog.show();
}
});
}
I'm trying to make an Activity that deletes the item pressed on the Spinner using an AlertDialog. I'm not deleting anything yet, I just added a Toast to make sure it will work.
This is my code:
public class SpinnerActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner);
final Spinner spinner = (Spinner) findViewById(R.id.spinner);
final Context context = getApplicationContext();
// Create an ArrayAdapter using the string array and a default spinner layout
final ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
int selectionCurrent = spinner.getSelectedItemPosition();
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (selectionCurrent != position) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
// set title
alertDialogBuilder.setTitle(R.string.dialogtitle);
//set dialog message
alertDialogBuilder.setMessage(R.string.dialogtext).setCancelable(false)
.setPositiveButton(R.string.si,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked,
Toast.makeText(context, "Eliminar", Toast.LENGTH_SHORT).show();
}
}) .setNegativeButton(R.string.no
, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, do nothing
dialog.cancel();
}
});
alertDialogBuilder.setView(spinner);
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
selectionCurrent = position;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
When I run the code, the following error is displayed: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
I tried to use ((ViewGroup)spinner.getParent()).removeView(spinner); before the alertDialog.show(); but it still not working.
It says: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
Anyone knows how to solve the problem?
First create a variable to inflate the ArrayAdapter:
String[] mTestArray;
Get the data from resources:
mTestArray = getResources().getStringArray(R.array.planets_array);
Inflate the ArrayAdapter with this array:
final ArrayList<String> list =new ArrayList<String>(Arrays.asList(mTestArray));
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, list);
And finally remove it in your dialog:
final ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
mTestArray , android.R.layout.simple_spinner_item);
String item = list.get(position);
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (selectionCurrent != position) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
// set title
alertDialogBuilder.setTitle(R.string.dialogtitle);
//set dialog message
alertDialogBuilder.setMessage(R.string.dialogtext).setCancelable(false)
.setPositiveButton(R.string.si,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
list.remove(position);
adapter.notifyDataSetChanged();
// if this button is clicked,
Toast.makeText(context, "Eliminar", Toast.LENGTH_SHORT).show();
}
}) .setNegativeButton(R.string.no
, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, do nothing
dialog.cancel();
}
});
alertDialogBuilder.setView(spinner);
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
selectionCurrent = position;
}
I have two fragment (AddAddressFragment,AddressFragment) and one DialogFragment. In my app i show alert box with radio button. If user choose anyone option, i want to pass that item to two fragment(AddressFragment and also AddAddressFragment). I can passed only one fragment(AddressFragment). How to pass the same value to another fragment(AddAddressFragment). I want to make list view in that AddAddressFragment
My code here:
RadioListAlert.java:
public class RadioListAlert extends DialogFragment {
CharSequence[] tag = { "Home", "Office", "Pg", "Others" };
private AddressListener addressListener;
private String itemClicked;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(true);
builder.setTitle("Please Tag Your Address").setSingleChoiceItems(tag, -1,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(getActivity(), tag[which],Toast.LENGTH_SHORT).show();
itemClicked = (String) tag[which];
}
}).setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
if (addressListener != null)
addressListener.itemClicked(itemClicked);
//to dismiss the dialog after user choose an item and click ok, you can also add some validation before dismissing the dialog
dismiss();
}
}).setNegativeButton("Cancel",new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
dismiss();
}
});
return builder.create();
}
public void setListener(AddressListener addressListener)
{
this.addressListener = addressListener;
}
public interface AddressListener
{
void itemClicked(String text);
}
}
AddressFragment.java:
public class AddressFragment extends Fragment implements RadioListAlert.AddressListener {
int position = 0;
EditText line1;
EditText line2;
EditText landmark;
AutoCompleteTextView cityText;
EditText zipcode;
Spinner country;
Spinner state;
RadioGroup tag;
Button savaddr;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout_address, container, false);
savaddr = (Button) view.findViewById(R.id.addrsave);
tag = (RadioGroup) view.findViewById(R.id.radioGroup);
line1 = (EditText) view.findViewById(R.id.line1);
line2 = (EditText) view.findViewById(R.id.line2);
cityText = (AutoCompleteTextView) view.findViewById(R.id.city_autoCompleteTextView);
zipcode = (EditText) view.findViewById(R.id.zipcode);
country = (Spinner) view.findViewById(R.id.countrySpinner);
state = (Spinner) view.findViewById(R.id.stateSpinner);
landmark = (EditText) view.findViewById(R.id.landmark);
// Get a reference to the AutoCompleteTextView in the layout
AutoCompleteTextView textView = (AutoCompleteTextView) view.findViewById(R.id.city_autoCompleteTextView);
// Get the string array
String[] city = getResources().getStringArray(R.array.city);
// Create the adapter and set it to the AutoCompleteTextView
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, city);
textView.setAdapter(adapter);
savaddr.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String addr1 = line1.getText().toString();
String addr2 = line2.getText().toString();
String addr_city = cityText.getText().toString();
String addr_zipcode = zipcode.getText().toString();
String addr_country = country.getSelectedItem().toString();
String addr_state = state.getSelectedItem().toString();
//Field Validation
if (Utility.isNotNull(addr1) && Utility.isNotNull(addr2) && Utility.isNotNull(addr_city) && Utility.isNotNull(addr_zipcode) && Utility.isNotNull(addr_country) && Utility.isNotNull(addr_state)) {
if (Utility.line2_validate(addr2)) {
if (Utility.line2_validate(addr_city)) {
//Show alert box with radio button option
RadioListAlert objRadioListAlert=new RadioListAlert();
objRadioListAlert.setListener(AddressFragment .this);
objRadioListAlert.show(getActivity().getFragmentManager(), "Radio Alert");
Toast.makeText(getActivity().getApplicationContext(), "Success.", Toast.LENGTH_SHORT).show();
} else {
cityText.setError("Enter valid City");
}
} else {
line2.setError("Enter valid Address");
}
} else {
Toast.makeText(getActivity().getApplicationContext(), "Please fill the form, don't leave any field blank", Toast.LENGTH_SHORT).show();
}
}
});
return view;
}
#Override
public void itemClicked(String text) {
((AccountActivity) getActivity()).navigatetoAddAddressActivity(text);
}
}
AddAddressFragment.java:
public class AddAddressFragment extends Fragment implements RadioListAlert.AddressListener {
ImageView add;
ListView addressListView;
private ArrayList<String> strArr;
private ArrayAdapter<String> adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout_add_address, container,
false);
addressListView = (ListView) view.findViewById(R.id.address_list);
add = (ImageView)view.findViewById(R.id.add_address);
add.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
//((AccountActivity) getActivity()).navigatetoAddAddressActivity();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
AddressFragment fragment = new AddressFragment();
transaction.replace(R.id.account_frame, fragment);
transaction.commit();
}
});
return view;
}
#Override
public void itemClicked(String text) {
Log.d("Welcome","Its worked");
Toast.makeText(getActivity().getApplicationContext(), "Item Clicked:" + text, Toast.LENGTH_SHORT).show();
strArr = new ArrayList<String>();
for (int i = 0; i < 2; i++) {
strArr.add(text + i);
adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(),
android.R.layout.simple_list_item_1, strArr);
addressListView.setAdapter(adapter);
}
}
}
Please anyone help me!!
Thanks in advance!!!
You can use localbroadcast to send data between fragments
this is also handy when you want to send data from service to fragment or activity
how to use LocalBroadcastManager?
I have a Settings Activity in Android Studio and a password List item that when clicked prompts an EditTextPreference dialogue with InputType password. How can I make it so that when the user enters the password, another Dialog pops up asking the user to confirm the password change.
Or even better, how can I make the EditTextPreference multi line and prompt for old password, new password, confirm new password within the same Dialog?
I added the following in my main activity's onCreate
ListView lv;
Context ctx=this;
onCreate() {
lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 1) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(ctx);
alertDialog.setTitle("Values");
final EditText oldPass = new EditText(ctx);
final EditText newPass = new EditText(ctx);
final EditText confirmPass = new EditText(ctx);
oldPass.setTransformationMethod(PasswordTransformationMethod.getInstance());
newPass.setTransformationMethod(PasswordTransformationMethod.getInstance());
confirmPass.setTransformationMethod(PasswordTransformationMethod.getInstance());
LinearLayout ll = new LinearLayout(ctx);
ll.setOrientation(LinearLayout.VERTICAL);
ll.addView(oldPass);
ll.addView(newPass);
ll.addView(confirmPass);
alertDialog.setView(ll);
alertDialog.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
alertDialog.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = alertDialog.create();
alert11.show();
}
}
});
}
But there is no change in the behavior of the app. The listItem I'm focusing on is second from the top so presumably position == 1.
I created a LinearLayout and added two textBoxes into it and then gave it to the alertBox.
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog.setTitle("Values");
final EditText oldPass = new EditText(MainActivity.this);
final EditText newPass = new EditText(MainActivity.this);
final EditText confirmPass = new EditText(MainActivity.this);
oldPass.setTransformationMethod(PasswordTransformationMethod.getInstance());
newPass.setTransformationMethod(PasswordTransformationMethod.getInstance());
confirmPass.setTransformationMethod(PasswordTransformationMethod.getInstance());
oldPass.setHint("Old Password");
newPass.setHint("New Password");
confirmPass.setHint("Confirm Password");
LinearLayout ll=new LinearLayout(MainActivity.this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.addView(oldPass);
ll.addView(newPass);
ll.addView(confirmPass);
alertDialog.setView(ll);
alertDialog.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
alertDialog.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = alertDialog.create();
alert11.show();
Check the passwords with the object references oldPass and newPass.
If you want to add anymore objects on to it, just create and add to the view.
To deal with the problem that when I cancel or pressed yes in this new dialog, the old dialog would appear, I changed "EditTextPreference" to simply "Preference" in my pref_general.xml file. Now the old dialogue does not appear or show up at all and the problem is fixed.
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);`