Alertdialog with checkbox ( Don't show again ) - java

I need to show an AlertDialog with "Don't Show Again" checkbox. I searched, but I couldn't find a working solution :/
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
PackageManager pk = getPackageManager();
Drawable icon;
alertDialogBuilder
.setTitle(R.string.confirm)
.setPositiveButton(R.string.close, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
//Do something
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();

If i'm not wrong then just Make one class extends with View.
public class DialogShow extends View {
SharedPreferences dialogPreferences;
String prefrencestring;
CheckBox nevershowagain;
Button closedialog;
Dialog dialog;
View view;
public DialogShow(final Context context) {
super(context);
dialog = new Dialog(context);
view = View.inflate(context, R.layout.startdialog, null);
dialog.setContentView(view);
nevershowagain = (CheckBox) view.findViewById(R.id.nevershowagain);
closedialog = (Button) view.findViewById(R.id.closedialog);
closedialog.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (nevershowagain.isChecked()) {
prefrencestring = "1";
dialogPreferences = PreferenceManager
.getDefaultSharedPreferences(context);
Editor editprefrences = dialogPreferences.edit();
editprefrences.putString("showdialog", prefrencestring);
editprefrences.commit();
}
dialog.dismiss();
}
});
dialogPreferences = PreferenceManager
.getDefaultSharedPreferences(context);
String check = dialogPreferences.getString("showdialog", "");
if (check.equals("1")) {
} else {
dialog.show();
}
}
}
Now call this class in your splash Activity on onCreate() method..
DialogShow d = new Dialog(this);

You can try this for AlertDialog :
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.myDialogLayout, null));
Dialog d = builder.create();
Edit: Please look into HERE FOR DETAILS EXPLANATION

Pass your view to the setView() method and it will set your view to the dialog body.
alertDialogBuilder.setView(your_view);

Since the accepted answer is not a good one in 2023 (IMO) and this post is the first one on google search, here is my solution:
private void showAlertWithCheck() {
boolean infoShowed = PreferenceManager.getDefaultSharedPreferences(this).
getBoolean(Const.PREF_KEY_INFO_SHOWN, false);
if (!infoShowed) {
View view = getLayoutInflater().inflate(R.layout.dialog_remember_check, null);
final CheckBox chk_drc_Remember = view.findViewById(R.id.chk_drc_Remember);
chk_drc_Remember.setText(R.string.dont_show_again);
chk_drc_Remember.setChecked(true);
new AlertDialog.Builder(MainActivity.this).
setIcon(android.R.drawable.ic_dialog_info).
setMessage(R.string.fixed_info).
setView(view).
setNeutralButton(R.string.caption_ok, null).
setOnDismissListener(dialog ->
PreferenceManager.getDefaultSharedPreferences(this).edit().
putBoolean(Const.PREF_KEY_INFO_SHOWN,
chk_drc_Remember.isChecked()).apply()).
show();
}
}
requied view XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical"
android:padding="#dimen/dialog_padding"
tools:context=".dialogs.PrayTimeDetailDialog"
tools:ignore="RtlHardcoded">
<CheckBox
android:id="#+id/chk_drc_Remember"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:paddingLeft="#dimen/dialog_padding"
android:paddingRight="#dimen/dialog_padding"
android:text="#string/remember_method" />

Related

Trying to change custom dialog Background color

Ok so I am trying change my background of my dialog box from white to a dark blue. However when I long press on one of the grid elements the dialog box looks like this:
I am trying to make it look something like this (this is a photoshop):
Here is snipet of my XML code for the edit dialog
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:background="#color/customBG">
Java code for custom dialog
#Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.edit_game_dialog,null);
editTitle = view.findViewById(R.id.editTitle);
editTitle.setText(currentTitle);
imageView = view.findViewById(R.id.item_image_dialog);
imageView.setImageResource(currentImage);
changeImageBt = view.findViewById(R.id.change_image);
changeImageBt.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
}
});
builder.setView(view).setTitle("Edit game")
.setPositiveButton("Apply Changes", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialogInterface, int i)
{
String title = editTitle.getText().toString();
int image = R.drawable.blank; //PLACE HOLDER CODE
editGameDialogListener.applyChanges(pos,title,image);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialogInterface, int i)
{
}
});
return builder.create();
}
I think you should use Dialog instead of AlertDialog.
Alert Dialog has its own Title and Button.
With Dialog you will have the benefit of defining your Title and Buttons.
Create a Layout as your design needs and set it in Dialog.
class ABC(context: Context) : Dialog(context) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.your_custom_layout)
}
}
When you create your dialog, you can pass theme as a second param
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.MyDialogTheme);
and set the custom theme to override anything you need. For background color something like this should work:
<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:background">#color/customBG</item>
</style>

Android Theme.AppCompat theme [duplicate]

This question already has answers here:
Cannot create AlertDialog: AppCompat error
(2 answers)
Closed 6 years ago.
My app crashes when I invoke alert dialogue within a list item. My app lists cars, every item has 2 buttons, each button calls alertdialog for yes/no answer. the app crashes with "You need to use a Theme.AppCompat theme (or descendant) with this activity." everytime I press the button. App code is as follows:
AndroidManifest:
<activity android:name=".SplashScreen" android:theme="#style/generalnotitle">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".CarActivity" android:theme="#style/generalnotitle"></activity>
</application>
Style:
<resources>
<style name="generalnotitle" parent="Theme.AppCompat.Light">
<item name="android:windowNoTitle">true</item>
<item name="android:screenOrientation">portrait</item>
</style>
</resources>
CarActivity.java
public class CarActivity extends AppCompatActivity {
String[] car_tag, car_makemodel, car_owner_id;
TypedArray car_pic;
String[] aa_owner_id, aa_owner_name, aa_owner_tlf;
String tlf, name;
List<CarItem> carItems;
ListView mylistview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("list start", "list start" );
getSupportActionBar().hide();
setContentView(R.layout.activity_list);
mylistview = (ListView) findViewById(R.id.LList);
setUpListView();
}
public void setUpListView() {
carItems = new ArrayList<CarItem>();
// gets car info
car_pic = getResources().obtainTypedArray(R.array.a_carpic);
car_tag = getResources().getStringArray(R.array.a_mat);
car_makemodel = getResources().getStringArray(R.array.a_makemodel);
car_owner_id = getResources().getStringArray(R.array.a_owner);
//defines items
for (int i = 0; i < car_tag.length; i++){
CarItem item = new CarItem(car_pic.getResourceId(i, -1), car_tag[i], car_makemodel[i], car_owner_id[i]);
Log.e("CarActivity", car_tag[i] );
carItems.add(item);
}
//gets available owners
aa_owner_id = getResources().getStringArray(R.array.a_id);
aa_owner_name = getResources().getStringArray(R.array.a_name);
aa_owner_tlf = getResources().getStringArray(R.array.a_tlf);
//populates the list
CarAdapter adapter = new CarAdapter(getApplicationContext(), carItems, aa_owner_id , aa_owner_name, aa_owner_tlf);
mylistview.setAdapter(adapter);
}
}
CarAdapter.java
public class CarAdapter extends BaseAdapter {
Context context;
List<CarItem> carItems;
String [] a_owner_id, a_owner_names, a_owner_tlf;
String name, tlf, owner_id;
CarAdapter(Context context, List<CarItem> carItems, String [] a_owner_id, String [] a_owner_names, String [] a_owner_tlf){
this.context = context;
this.carItems = carItems;
this.a_owner_id = a_owner_id;
this.a_owner_names = a_owner_names;
}
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null){
convertView = mInflater.inflate(R.layout.row_car,null);
holder = new ViewHolder();
holder.car_pic = (ImageView) convertView.findViewById(R.id.car_pic);
holder.tag = (TextView) convertView.findViewById(R.id.tag);
holder.makemodel = (TextView) convertView.findViewById(R.id.make_model);
holder.owner = (TextView) convertView.findViewById(R.id.owner);
holder.blck = (Button) convertView.findViewById(R.id.block);
holder.mov = (Button) convertView.findViewById(R.id.move);
CarItem row_pos = carItems.get(position);
holder.car_pic.setImageResource(row_pos.getCar_pic());
holder.tag.setText(row_pos.getTag());
holder.makemodel.setText(row_pos.getMakemodel());
owner_id = row_pos.getOwner();
getOwner(owner_id);
holder.owner.setText(name);
holder.blck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBlockClick();
}
});
holder.mov.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e("CarAdapter move click", owner_id );
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
private void onBlockClick() {
Log.e("CarActivity", "CLICK block button" );
AlertDialog.Builder alertDlg = new AlertDialog.Builder(context);
alertDlg.setMessage("Do you wish to inform " + name + "?");
alertDlg.setCancelable(false);
alertDlg.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Get owner phone number
getNumber(owner_id);
Log.e("CarActivity YES DIALOG", tlf );
//Toast.makeText(getApplicationContext(),"Afasta-me o teu cangalho...", Toast.LENGTH_SHORT).show();
}
});
alertDlg.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.e("CarActivity", "CLICK NO DIALOG" );
}
});
alertDlg.create().show();
}
}
SOLUTION: when I set adapter, i was sending "getApplicationContext()" but instead i sent "this" and this way i could send the context to the adapter.
CarActivity.java:
before:
CarAdapter adapter = new CarAdapter(getApplicationContext(), carItems, aa_owner_id , aa_owner_name, aa_owner_tlf);
after:
CarAdapter adapter = new CarAdapter(this, carItems, aa_owner_id , aa_owner_name, aa_owner_tlf);
Have you tried using the constructor that takes the dialog's theme as well?
Try this:
AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.generalnotitle);
Then put the rest of the relevant code for the builder.
Note, the theme should point to a dialog theme (Theme.AppCompat.Light.Dialog.Alert). Check out this post for more details.
http://www.materialdoc.com/alerts/
There are two AlertDialog classes: the regular android.app.AlertDialog, and the support/appcompat version android.support.v7.app.AlertDialog. I can't tell which one you are importing, but the latter one requires you to be using an appcompat theme for the given Context, which should not be a problem if you are already using the AppCompat library and your activities extend from AppCompatActivity. (If you aren't using AppCompat, then you should make sure you are using the regular AlertDialog.)

Inflated layout to alertdialog doesn't work

I have the following method where I must confirm the user's password before he is allowed to access the Setup menu. To do so, I make him login again because Parse (my cloud storage) doesn't allow me to confirm passwords. So far so good. The problem is when I inflate the view to my AlertDialog to use a custom button and EditText. You can see in my code below that I have commented the Button section, because when I try to get the onClickListener it doesn't work. It just does nothing. So I'm currently using the alertDialogPositiveButton and it's ok. The second problem and the real one is that I cannot retrieve the data insert into the editText. The SUpassword3is always equals to blank, not null. Can someone please help?
method:
public void confirmPassword(){
final LayoutInflater inflateSetup = getLayoutInflater();
AlertDialog.Builder alertSetup = new AlertDialog.Builder(this);
alertSetup.setView(inflateSetup.inflate(R.layout.inflate_setup, null));
alertSetup.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
System.out.println("User has clicked the [confirm] button");
View viewInflate_setup = inflateSetup.inflate(R.layout.inflate_setup, null);
edtPassword3 = (EditText)viewInflate_setup.findViewById(R.id.edtPassword3);
SUpassword3 = edtPassword3.getText().toString();
final ParseUser beforeConfirmPassword = ParseUser.getCurrentUser();
ParseUser.logInInBackground(beforeConfirmPassword.getUsername(), SUpassword3, new LogInCallback() {
#Override
public void done(ParseUser parseUser, ParseException e) {
if(e==null){
//password confirmed, logout last user and go to next setup
System.out.println("Password [" +SUpassword3+ "] and user [" +beforeConfirmPassword.getUsername()+ "] confirmed, logout last user and go to setup");
beforeConfirmPassword.logOut();
}else{
//passwords don't match
System.out.println("Password don't match: [" +SUpassword3 + "] USER [" +beforeConfirmPassword.getUsername()+ "]");
}
}
});
}
});
final AlertDialog dialogSetup = alertSetup.create();
dialogSetup.show();
/**
btnConfirmPassowod = (Button)viewInflate_setup.findViewById(R.id.btnConfirm);
btnConfirmPassowod.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
System.out.println("User has clicked the [confirm] button");
final ParseUser beforeConfirmPassword = ParseUser.getCurrentUser();
ParseUser.logInInBackground(beforeConfirmPassword.getUsername(), SUpassword3, new LogInCallback() {
#Override
public void done(ParseUser parseUser, ParseException e) {
if(e==null){
//password confirmed, logout last user and go to next setup
System.out.println("Password confirmed, logout last user and go to setup");
beforeConfirmPassword.logOut();
}else{
//passwords don't match
dialogSetup.dismiss();
Snackbar.make(v, "Wrong password, try again.", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
}
});
}
}); */
}
inflate_setup.xml(the xml file I inflate on the alertDialog)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/edtPassword3"
android:layout_gravity="center_horizontal"
android:hint="#string/EN_txt.confirmPassword"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/btnConfirm"
android:layout_gravity="center_horizontal" />
</LinearLayout>
Final version(working):
public void confirmPassword(){
final LayoutInflater inflateSetup = getLayoutInflater();
final View viewInflate_setup = inflateSetup.inflate(R.layout.inflate_setup, null);
AlertDialog.Builder alertSetup = new AlertDialog.Builder(this);
alertSetup.setView(viewInflate_setup);
alertSetup.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
System.out.println("User has clicked the [confirm] button");
edtPassword3 = (EditText)viewInflate_setup.findViewById(R.id.edtPassword3);
SUpassword3 = edtPassword3.getText().toString();
...
You inflate another view inside your onClickListener. That's why you actually check text of another edtPassword3, not that one which visible for user. It should be something like that:
View view = inflateSetup.inflate(R.layout.inflate_setup;
final EditText edtPassword3 = (EditText)viewInflate_setup.findViewById(R.id.edtPassword3);
alertSetup.setView(view, null));
alertSetup.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
System.out.println("User has clicked the [confirm] button");
SUpassword3 = edtPassword3.getText().toString();
....
Try to use: Dialog class
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");
Get a reference of the inflated view:
View dialogView = inflateSetup.inflate(R.layout.inflate_setup, null);
In dialog use:
alertSetup.setView(dialogView);
Then, access button using:
Button myButton = (Button) dialogView.findViewById(R.id.btnConfirm);

Trouble with my custom layout for an Alert Dialog

Making a to-do list. Having 2 issues, The not so important issue is that for some reason I can't see the TextView's that I set up as "labels". The more important thing is that when I click the create new task button, my alert pops up, I can put values in my EditText boxes, but when I hit create, it crashes and I get a NullPointer exception saying I'm trying to call getText() on a null object reference. I can't figure out if I'm inflating incorrectly or if I'm not linking the EditTexts to the alert properly. The annoying thing is that my edittext alert box works just fine editing existing list items(that I hardcoded for testing). Here's my layout and activity, I commented the line in which it breaks. Sorry about all the Log.d's I'm really trying to visualize how all this works.
The Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<EditText
android:layout_width="55dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/txtCreatePriority"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="25dp"
android:hint="1"
android:textAlignment="center" />
<EditText
android:layout_width="235dp"
android:layout_height="wrap_content"
android:id="#+id/txtCreateItemContent"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/txtCreatePriority"
android:layout_toEndOf="#+id/txtCreatePriority"
android:layout_marginLeft="15dp"
android:hint="Do Laundry"
android:textAlignment="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Priority"
android:id="#+id/lblPriority"
android:layout_above="#+id/txtCreatePriority"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="17dp"
android:textStyle="bold"
android:textSize="23dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Task to-do"
android:id="#+id/lblItemContent"
android:layout_above="#+id/txtCreateItemContent"
android:layout_toRightOf="#+id/lblPriority"
android:layout_toEndOf="#+id/lblPriority"
android:layout_marginLeft="65dp"
android:textStyle="bold"
android:textSize="23dp" />
</RelativeLayout>
The Activity
public class MainActivity extends AppCompatActivity {
private ListDataSource ds;
private ListView listViewToDo;
private Button btnAddNew;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Context context = this;
Log.d("MainActivity","Attempting to create data source");
try {
ds = new ListDataSource();
}
catch(Exception e)
{
e.printStackTrace();
Log.d("MainActivity","Failed to create data source");
}
btnAddNew = (Button)findViewById(R.id.btnAddNew);
Log.d("Main Activity","Attempting to link empty list view to on screen view");
listViewToDo = (ListView)findViewById(R.id.listOfLists);
Log.d("Main Activity", "Views linked, Attempting to set adapter to listView");
listViewToDo.setAdapter(new ListDataSourceAdapter(this, ds));
Log.d("Main Activity", "Successfully set Adapter");
btnAddNew.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("addItem", "Entered onclick, Attempting to create AlertDialog");
AlertDialog.Builder addItem = new AlertDialog.Builder(context);
Log.d("addItem", "AlertDialog Built, attempting to create inflater");
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
addItem.setView(inflater.inflate(R.layout.create_item_layout, null));
Log.d("addItem", "inflater built linking text boxes");
final TextView txtCreatePriority = (TextView)findViewById(R.id.txtCreatePriority);
final TextView txtCreateCellContent = (TextView)findViewById(R.id.txtCreateItemContent);
final TextView lblPriority = (TextView)findViewById(R.id.lblPriority);
final TextView lblItemContent = (TextView)findViewById(R.id.lblItemContent);
addItem.setTitle("Create new item");
addItem
.setCancelable(false)
.setPositiveButton("Create", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.d("editText onClick", "in onClick method, preparing to add entry");
// This is where the code breaks
ds.getList().add(Integer.valueOf(txtCreatePriority.getText().toString()), new CellContent(Integer.valueOf(txtCreatePriority.getText().toString()) + 1, txtCreateCellContent.getText().toString().trim()));
Log.d("editText onClick", "added new entry");
ListDataSourceAdapter adapter = new ListDataSourceAdapter(context, ds);
Log.d("editText onClick", "reestablished link to adapter");
listViewToDo.setAdapter(adapter);
Log.d("editText onClick", "adapter set");
adapter.notifyDataSetChanged();
Log.d("editText onClick", "DataSetChanged");
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = addItem.create();
alertDialog.show();
}
});
// add button listener
listViewToDo.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id)
{
AlertDialog.Builder editItem = new AlertDialog.Builder(context);
final EditText edittext = new EditText(context);
editItem.setTitle("Change item");
editItem
.setMessage("Set new todo item")
.setView(edittext)
.setCancelable(false)
.setPositiveButton("Submit", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
Log.d("editText onClick","in onClick method, preparing to remove previous entry");
ds.getList().remove(position);
Log.d("editText onClick", "removed previous entry");
ds.getList().add(position, new CellContent(position + 1, edittext.getText().toString().trim()));
Log.d("editText onClick", "added new entry");
ListDataSourceAdapter adapter = new ListDataSourceAdapter(context,ds);
Log.d("editText onClick","reestablished link to adapter");
listViewToDo.setAdapter(adapter);
Log.d("editText onClick", "adapter set");
adapter.notifyDataSetChanged();
Log.d("editText onClick", "DataSetChanged");
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = editItem.create();
alertDialog.show();
}
});
}
}
The Error
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
You're calling "(EditText)v.findViewById" on the view v.
The view v is the view passed back in the onClickListener, which is the button itself.
Since that button does not contain the EditTexts within it, those views are null. And crash when you try to access them.
I'm a little uncertain where the layout with the edit texts is in this code. Is it in the same layout as the listview, or in create_item_layout?
If its create_item_layout, that needs to be inflated before getting the EditTexts. Use the view you inflate to findViewById.

Parse XML and display it in an AlertDialog in Android

I have this .xml file with countries and their countrycodes in them. This is how it looks like:
<?xml version="1.0" encoding="UTF-8"?>
<landen>
<land>
<naam>Afghanistan</naam>
<code>AF</code>
</land>
<land>
<naam>Albani�</naam>
<code>AL</code>
</land>
<land>
<naam>Algerije</naam>
<code>DZ</code>
</land>
<land>
</landen>
Now I want people to choose one country out of an list. I though an AlertDialog would be nice to display everything.
The way i get the values out of my xml-file is like this:
protected ArrayList<Land> getLanden() {
ArrayList<Land> lijst = new ArrayList<Land>();
try {
DocumentBuilder builder =DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(getAssets().open("landenlijst.xml"));
NodeList nl = doc.getElementsByTagName("land");
for (int i=0;i<nl.getLength();i++) {
Node node = nl.item(i);
Land land = new Land();
land.land = Xml.innerHtml(Xml.getChildByTagName(node, "naam"));
land.landcode = Xml.innerHtml(Xml.getChildByTagName(node, "code"));
lijst.add(land);
}
Log.d("Gabug","Klaar met parsen");
Log.d("Gabug","Landen: " + lijst);
} catch (Exception e) {
e.printStackTrace();
}
return lijst;
}
And I use this to make my AlertDialog:
public void KiesLandMenu(){
ArrayList<Land> alleLanden = getLanden();
final CharSequence[] items = alleLanden.toArray(new CharSequence[alleLanden.size()]);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Kies land");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
switch (item){
case 0:
break;
case 1:
break;
case 2:
break;
}
}
});
AlertDialog alert = builder.create();
alert.show();
}
I don't know if this works as DDMS returns some bytecode or something when i Log it. And after that it Force Closes because of ArrayStoreException..
Now my question is; is this the best way to do this? if yes, how can I fix the ArrayStoreException? If no, what are better ways to let my user choose a country (a whole new view maybe)?
Furthermore, how can I register what country someone tapped?
EDIT:
I slightly changed the sample code below and I get an NullPointerException now..
public void KiesLandMenu(){
ArrayAdapter<Land> arrAdapter;
ArrayList<Land> alleLanden = getLanden();
arrAdapter = new ArrayAdapter<Land>(this, android.R.layout.simple_list_item_single_choice, alleLanden);
ListView list = (ListView)findViewById(R.layout.lijstview);
list.setAdapter(arrAdapter);
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> list, View view, int position,
long id) {
Log.e("item clicked", String.valueOf(position));
}
});
}
The NullPointerException is at list.setAdapter(arrAdapter);
Make a layout with a ListView, then set that layout in your onCreate. To make the list, you can do something like:
public class RunTestProject extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); //whatever you want your layout to be
}
// getLanden() implementation goes here
public void KiesLandMenu(){
ArrayList<Land> alleLanden = getLanden();
arrAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, alleLanden);
Dialog dialog = new Dialog(this);
dialog.setTitle("Kies land");
dialog.setContentView(R.layout.withList); // The dialog layout
ListView list = (ListView) dialog.findViewById(android.R.id.list); //note that it's not simply findViewById
list.setAdapter(arrAdapter);
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> list, View view, int position,
long id) {
Log.e("item clicked", String.valueOf(position));
}
});
dialog.show();
}
}
When the user chooses on an item, you can see in the log that the item's position in the array is shown.
Your layout file can be something like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
/>
</LinearLayout>
You could probably extend AlertDialog and give it a ListView as a view. Then bind the ListView to an ListAdapter which uses your ArrayList.
Edit:
ListView lv = new ListView(context);
ArrayAdapter aa = new ListAdapter(context, viewid, lijst);
lv.setAdapter(aa);
AlertDialog ad = new AlertDialog(context);
ad.setView(lv);
There is a bit more work than that though. You need to specify viewid which is the View representing each item in the ListView.
The sdk reference is very good you know.
new AlertDialog.Builder(this)
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(R.string.alert_dialog_single_choice)
.setSingleChoiceItems(<ListAdapter> or CharaSequnce[] , 0, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked on a radio button do some stuff */
}
})
.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked Yes so do some stuff */
}
})
.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked No so do some stuff */
}
})
.create();
Note :
Please see This link to Api for Bold text mentioned below
.setSingleChoiceItems(CharacterSequnce[] , 0, new DialogInterface.OnClickListener()....
Hope this helps . Thanks :)

Categories

Resources