DialogFragment with extra space below ListView - android

As you can see, below the bottom list element in my ListView, there is excess space I can't seem to be rid of. I've tried Relative and Linearlayout, both look like this. Here's the code:
public class ChooseDialog extends DialogFragment implements
DialogInterface.OnClickListener {
String URLhome;
String Title;
String type;
/* public static ChooseDialog newInstance() {
ChooseDialog dialog = new ChooseDialog();
Log.v("a", "shit runs");
Bundle bundle = new Bundle();
dialog.setArguments(bundle);
return dialog;
}*/
public ChooseDialog(String type) {
this.type = type;
}
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setCancelable(true);
int style = DialogFragment.STYLE_NORMAL, theme = 0;
setStyle(style, theme);
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(type);
builder.setNegativeButton("Cancel", this);
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialogLayout = inflater.inflate(R.layout.dialog, null);
builder.setView(dialogLayout);
final String[] items = {"Red", "Green", "Blue" };
builder.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.v("touched: ", items[which].toString());
}}
);
return builder.create();
}
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}
And the code that launches the dialog:
public OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
showNationalityDialog();
}
};
private void showNationalityDialog() {
FragmentManager fm = getSupportFragmentManager();
ChooseDialog nationalityDialog = new ChooseDialog("Nationality");
nationalityDialog.show(fm, "fragment_edit_name");
}

I know this question never drew much attention, but I finally solved the problem.
By using the listview that I created in XML rather than setting the builder's adapter, I managed to get rid of all the excess space.
Here's what the new code looks like:
switch (editText.getId()) {
case (0) :
ListView list = (ListView) dialogLayout.findViewById(R.id.listView1);
list.setAdapter(new ArrayAdapter<String>(activity, R.layout.dialoglist,
activity.getResources().getStringArray(R.array.ageArray)));
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
editText.setText(activity.getResources().getStringArray(R.array.ageArray)[arg2]);
dismiss();
}
});
builder = (Integer.parseInt(android.os.Build.VERSION.SDK) < 11)? new AlertDialog.Builder(activity) :
new AlertDialog.Builder(activity, android.R.style.Theme_Translucent);
builder.setNegativeButton("Cancel", this);
builder.setView(dialogLayout);
return builder.create();

If you are setting a custom view on the alert dialog (via setView()) that ONLY has a ListView then you don't need to use a custom view. The builder will automatically add a ListView into the view if set adapter is called.
The extra space at the end of the list view is probably your custom view with no content.
For example:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final AccountChooserListAdapter adapter = new AccountChooserListAdapter(getActivity(), R.layout.choose_account_list_item,
accountMetadataFactory.getAccountsAsList());
return new AlertDialog.Builder(getActivity())
.setCancelable(true)
.setTitle(getActivity().getString(R.string.title_add_account))
.setAdapter(adapter, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
listener.onAddAccount(which);
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create();
}

Related

How should I remove unselected element from ArrayList in Multiple Checkbox Selection?

I am passing an integer array-list that hold the selection and retrieve it based on selection but when I remove the item from check selection it is not remove from array list displayed in text. Here is my logic to handle multiple check.
public class MainFragment extends Fragment {
private Button buttonUser;
String[] listItems;
boolean[] checkedItems;
ArrayList<Integer> mUserItems = new ArrayList<>();
private Context context;
private AppCompatTextView compatTextView;
public static MainFragment newInstance() {
return new MainFragment();
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listItems = getResources().getStringArray(R.array.person_array);
checkedItems = new boolean[listItems.length];
context = getActivity();
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
buttonUser = (Button) view.findViewById(R.id.buttonUser);
compatTextView = (AppCompatTextView) view.findViewById(R.id.text_view_selection);
buttonUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("Select Person");
alert.setMultiChoiceItems(listItems, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
if (!mUserItems.contains(which)) {
mUserItems.add(which);
}
} else if (!mUserItems.contains(which)){
mUserItems.remove(which);
}
}
});
alert.setCancelable(false);
alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
String items = "";
for (int i = 0; i < mUserItems.size(); i++) {
items = items.concat(listItems[mUserItems.get(i)]);
if (i != mUserItems.size() - 1) {
items = items + ", ";
}
compatTextView.setText(items);
}
}
});
alert.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = alert.create();
dialog.show();
}
});
return view;
}
}
And I am not getting this part of logic how to handle
#Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
if (!mUserItems.contains(which)) {
mUserItems.add(which);
}
} else if (!mUserItems.contains(which)){
mUserItems.remove(which);
}
}
It's somewhat correct but when I deselects the choice from alert dialog the list is not update and display the previous check data.
I think the else if part would yield false if isChecked is false. May be it should be like this?: else if (mUserItems.contains(which))

Activate Phone call on ItemClick

In my app I am trying to activate phonecall on a Item click for a list item click. But this code is not working at all. After running this code whenever I click on phone call the app is crashing . I have attached my crash report here. As I am very new in android developing. I am not sure how to get rid of thi serror.
My code is-
public class AlertDialogFragment extends DialogFragment {
private ListView listView1;
private ListView listView2;
private Button cancelButton1;
private Button cancelButton2;
private String[] companyName;
private String[] actionName;
private ArrayAdapter<String> adapter;
protected FragmentActivity mActivity;
public AlertDialogFragment(){
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setCancelable(true);
setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogStyle);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.first_alertlist_contact, container, false);
//Set Title Dialog
getDialog().setTitle("Contact");
//Button,ListView1 Initialization
listView1=(ListView) rootView.findViewById(R.id.listView1);
cancelButton1=(Button) rootView.findViewById(R.id.cancel_button1);
// Defined Array values to show in ListView
companyName = getResources().getStringArray(R.array.company_name);
//Create and set Adepter TO ListView1
adapter=new ArrayAdapter<String>(getActivity(), R.layout.first_alertlist_textstyle,android.R.id.text1,companyName);
listView1.setAdapter(adapter);
cancelButton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dismiss();
}
});
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
if(itemPosition == 0)
{
dismiss();
showDialog2();
}
if(itemPosition == 1)
{
dismiss();
showDialog2();
}
if(itemPosition == 2)
{
dismiss();
showDialog2();
}
.....
}
});
return rootView;
}
private void showDialog2(){
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this.getActivity(), R.style.DialogStyle);
LayoutInflater inflater = this.getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.second_alertlist_contact, null);
dialogBuilder.setView(dialogView);
listView2 = (ListView) dialogView.findViewById(R.id.listView2);
cancelButton2=(Button) dialogView.findViewById(R.id.cancel_button2);
// Defined Array values to show in ListView
actionName = getResources().getStringArray(R.array.contact_way);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(),
R.layout.first_alertlist_textstyle, android.R.id.text1, actionName);
listView2.setAdapter(adapter);
listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
if(itemPosition == 0)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("+1234667");
//builder.setMessage("Are you sure you want to log out?");
builder.setPositiveButton("Call", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// close the dialog, go to login page
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:1234567"));
startActivity(callIntent);
dialog.dismiss();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
if(itemPosition == 1)
{
dismiss();
System.out.println("Hello");;
}
if(itemPosition == 2)
{
dismiss();
System.out.println("Hello");;
}
if(itemPosition == 3)
{
dismiss();
System.out.println("Hello");;
}
}
});
dialogBuilder.show();
}
}
07-10 15:40:49.134 12970-12970/com.testgrid E/InputEventReceiver: Exception dispatching input event.
07-10 15:40:49.134 12970-12970/com.testgrid E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
07-10 15:40:49.134 12970-12970/com.testgrid E/MessageQueue-JNI:
java.lang.NullPointerException: Attempt to invoke virtual method
'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference
at
android.support.v7.app.AlertDialog.resolveDialogTheme(AlertDialog.java:113)
at
android.support.v7.app.AlertDialog$Builder.<init>(AlertDialog.java:291)
at
com.testgrid.AlertDialogFragment$3.onItemClick(AlertDialogFragment.java:155)
at
android.widget.AdapterView.performItemClick(AdapterView.java:310)
You need some permissions forr making calls, put this in your manifest
<uses-permission android:name="android.permission.CALL_PHONE" />
maybe this tutorial helps https://www.mkyong.com/android/how-to-make-a-phone-call-in-android/
Use below code to make dialog and call button :
Put context youractivityname.this or if you are in a fragment getActivity().
AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AlertDialog.Builder(context, android.R.style.Theme_Material_Dialog_Alert);
} else {
builder = new AlertDialog.Builder(context);
}
builder.setTitle("Title")
.setMessage("some message")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// call specific number
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:1234567"));
startActivity(callIntent);
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do anything
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
source

Spinner from editext android

My Scenario:
When I click the top (+)icon there is a dialog displayed with editext and If I enter some text and click ok button the text should be added to my spinner which I am unable to do it.
Here is what I mean to say:
This is what I have done:
protected void showInputDialog() {
// get prompts.xml view
LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
View promptView = layoutInflater.inflate(R.layout.input_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainActivity.this);
alertDialogBuilder.setView(promptView);
// setup a dialog window
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Spinner element
listsp = (Spinner) findViewById(R.id.listspinner);
listtext = (EditText) findViewById(R.id.list_text);
list = new ArrayList<String>();
list.add(listtext.getText().toString());
listadapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_spinner_item, list);
listadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
listsp.setAdapter(adapter);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create an alert dialog
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
Try to update the adapter outside the onClick :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Whatever else
listsp = (Spinner) findViewById(R.id.listspinned);
list = new ArrayList<String>();
listadapter = new MyArrayAdapter(getApplicationContext(), android.R.layout.simple_spinner_item, list);
listadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
listsp.setAdapter(adapter);
}
protected void showInputDialog() {
// get prompts.xml view
LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
View promptView = layoutInflater.inflate(R.layout.input_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainActivity.this);
alertDialogBuilder.setView(promptView);
// setup a dialog window
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
listtext = (EditText) findViewById(R.id.list_text);
updateAdapter(listtext.getText().toString());
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create an alert dialog
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
protected void updateAdapter(String input) {
list.add(input);
listadapter.notifyDataSetChanged();
}
EDIT : Here's how to implement your custom adapter (I made it private so it'd use the same dataList. Therefore, you don't need to call any updateData() function, just to notify the adapter that the data has changed with notifyDataSetChanged()) :
private class MyArrayAdapter extends BaseAdapter implements SpinnerAdapter {
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
TextView text = new TextView(lexs);
text.setText(list.get(position).getName());
return text;
}
}

dialog.dismiss() doesn't work on item click inside alertDialog

Why dialog.dismiss() doesn't work after click on item inside the AlertDialog ?
I have also tried alert.dismiss but i get the error dialog cannot be resolved. How can i solve that?
Here's the dialog
protected void myMarkersDialog() {
final String name = prefs.getString("Name", "");
String nameTwoo = prefs.getString("NameTwoo", "");
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View convertView = (View) inflater.inflate(R.layout.my_markers_listview, null);
builder.setView(convertView);
builder.setTitle("List");
lv = (ListView) convertView.findViewById(R.id.listView1);
if (prefs.contains("Name") || prefs.contains("NameTwoo"))
{
String[] values = new String[] {name,nameTwoo};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,values);
lv.setAdapter(adapter);
}
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long arg) {
String selected;
selected = lv.getItemAtPosition(position).toString();
if(selected.equalsIgnoreCase(name))
{
mapView.getMapViewPosition().setCenter(myMarkerGeopoint);
dialog.dismiss();
}
}
});
builder.setNeutralButton(getResources().getString(R.string.no_dialog), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss(); // I get error!
}
});
AlertDialog alert = builder.create();
alert.show();
}
Declare the AlertDialog before build it and use it to dismiss.
AlertDialog alert;
protected void myMarkersDialog() {
final String name = prefs.getString("Name", "");
String nameTwoo = prefs.getString("NameTwoo", "");
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View convertView = (View) inflater.inflate(R.layout.my_markers_listview, null);
builder.setView(convertView);
...
builder.setNeutralButton(getResources().getString(R.string.no_dialog), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
alert.dismiss();
}
});
alert = builder.create();
alert.show();
}

AlertDialog doesn't display items from ListAdapter Hide options

When I try to set the Alert using ArrayAdaptor to display a set of
items, the list is displayed but the items' characters are invisible.
If the item is selected, then the characters are visible. Scratching
my head on why. Appreciate any advice. Below is the code and the
screenshot from the emulator.
public class MessageTest extends Activity implements
View.OnClickListener {
public final static String debugTag = "MessageDemo::";
Button alert;
Button toast;
String[] items={"item1", "item2", "item3", "item4", "item5" };
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.message);
alert=(Button)findViewById(R.id.alert);
alert.setOnClickListener(this);
}
public void onClick(View view) {
if (view==alert) {
ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice, items);
new AlertDialog.Builder(this)
.setTitle("MessageTest")
.setSingleChoiceItems(aa, 0, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dlg, int which) {
Log.d(MessageDemo.debugTag,
"DialogInterface.OnClickListener::onClick() is called -> which =
"+which);
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
Log.d(MessageDemo.debugTag, "OK button is clicked -> sumthin
= "+sumthin);
}
})
.setNeutralButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
Log.d(MessageDemo.debugTag, "Close button is clicked ->
sumthin = "+sumthin);
// do nothing -- it will close on its own
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dlg, int sumthin) {
Log.d(MessageDemo.debugTag, "Cancel button is clicked ->
sumthin = "+sumthin);
}
})
.show();
}
}
I know I'm a bit late at answering, but I had the same problem and I fixed it by simply changing
ArrayAdapter<String> aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, items);
to
ArrayAdapter<String> aa = new ArrayAdapter<String>(this, android.R.layout.select_dialog_singlechoice, items);
I hope it helps someone.
As Quintin already mentioned in the comment, the problems reason maybe that the text color and background of the list items are the same. Use another view template for your list items, eg. android.R.layout.select_dialog_item:
builder.setAdapter(
new ArrayAdapter<Object>(context, android.R.layout.select_dialog_item, my_array)
{
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row;
if (null == convertView)
{
row = inflater.inflate(android.R.layout.select_dialog_item, null);
}
else
{
row = convertView;
}
TextView tv = (TextView) row.findViewById(android.R.id.text1);
tv.setText(getItem(position).toString());
return row;
}
}, ...
The layout inflater can be grabbed over a context:
final LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

Categories

Resources