Cannot resolve method "setMultichoiceItem" error - java

I am learning Android from some book and keep getting error on .setMultichoiceItem block : cannot resolve method .setMultichoiceItems .
I check it over multiple times and my code is all case sensitive, and no misspelled words.
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
public class MainActivity extends ActionBarActivity {
CharSequence[] items = {"Google","Safari","Yahoo"};
Boolean[] itemChecked = new Boolean[items.length];
Button btn ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showDialog(0);
}
});
}
protected Dialog onCreateDialog(int i) {
switch (i) {
case 0:
return new AlertDialog.Builder(this)
.setTitle("Test of Dialog")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplication(), "OK Clicked !", Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Cancel Clicked !", Toast.LENGTH_LONG).show();
}
})
.setMultiChoiceItems(items, itemChecked,
new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Toast.makeText(getApplicationContext(), items[which] + (isChecked ? " checked!" : " unchecked!"), Toast.LENGTH_SHORT).show();
}
}).create();
}
return null;
}
}
Logcat Error : Cannot resolve method 'setMultiChoiceItems(java.lang.CharSequence[], java.lang.Boolean[], android.content.DialogInterface.OnMultiChoiceClickListener)'
Any help would be awesome .
Thanks

try this one,
Change this line
Boolean[] itemChecked = new Boolean[items.length];
to
boolean[] itemChecked = new boolean[items.length];
because its second parameter accepts boolean[], not Boolean[] objects
setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, DialogInterface.OnMultiChoiceClickListener listener)

import android.content.DialogInterface.OnMultiChoiceClickListener;

I found the better solution:
public void alertMultipleChoiceItems(){
final CharSequence[] dialogList = Symbollist.toArray(new CharSequence[Symbollist.size()]);
HashSet<String> uniqueValues = new HashSet<>(Symbollist);
Symbollist.clear();
for (String value : uniqueValues) {
//... //Do something
Symbollist.add(value);
}
AlertDialog.Builder builder = new AlertDialog.Builder(AddPackageStep3.this);
selectedItems = new ArrayList<Integer>();
// set the dialog title
boolean[] itemChecked = new boolean[selectedItems.size()];
builder.setMultiChoiceItems(dialogList,null, new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
// if the user checked the item, add it to the selected items
selectedItems.add(which);
}
else if (selectedItems.contains(which)) {
// else if the item is already in the array, remove it
selectedItems.remove(Integer.valueOf(which));
}
// you can also add other codes here,
// for example a tool tip that gives user an idea of what he is selecting
// showToast("Just an example description.");
}
})
// Set the action buttons
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// user clicked OK, so save the mSelectedItems results somewhere
// here we are trying to retrieve the selected items indices
String selectedIndex = "";
for(Integer i : selectedItems){
selectedIndex += i + ", ";
}
//showToast("Selected index: " + selectedIndex);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// removes the AlertDialog in the screen
}
})
.show();
}

You can convert to boolean Array this way by using the Guava:
boolean[] listBoolean = Booleans.toArray(checkedList);

Related

How do I copy and paste text from EditText of alertdialog to EditText of my activity, on button click?

I searched but the questions I could see dealt with just copy, or copying to clipboard, or just pasting. Specifically what I want (in 1 button click, the PositiveButton in AlertDialog) is to copy the text entered by user in the EditText of my alertdialog to the EditText of my Activity.
Can you tell me how to do this please? Here is the code I am using and trying to fix:
//when user touches on "commentname" edittext we want the alertdialog to open
commentname.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
AlertDialog.Builder builder = new AlertDialog.Builder(NewContact.this);
builder.setTitle("Ur Comment:");
//start the following xml file/ layout
View viewInflated = LayoutInflater.from(NewContact.this).inflate(R.layout.comment_text_pop_up, null, false);
builder.setView(viewInflated);
// Set up the buttons
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
//we want to copy the text entered in "input", in the alertdialog,
//and paste it in commentname
commentname.setText(alertdialog_edittext.getText().toString());
}
});
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
alertdialog_edittext = (EditText) dialog.findViewById(R.id.input);
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
dialog.show();
return true;
}
return false;
}
});
i wrote this simple code example for you to do it.
just add method to setText on Your edittext in your Activity:
private void setTextFromDialog(final String textFromDialog){
myEditText.setText(textFromDialog);
}
when user click in dialog get text from edittext dialog and pass using this method:
setTextFromDialog(YouEditTextValueX);
here code example:
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText myEditText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button ShowDialog = findViewById(R.id.showdialog_id);
myEditText = findViewById(R.id.editText_id);
ShowDialog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
final EditText edittext = new EditText(MainActivity.this);
alert.setTitle("Title");
alert.setMessage("Message");
alert.setView(edittext);
alert.setPositiveButton("Set text", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String YouEditTextValueX = edittext.getText().toString();
if(YouEditTextValueX.length() > 0){
//this line for call method and pass the text
setTextFromDialog(YouEditTextValueX);
}
}
});
alert.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// what ever you want to do with No option.
}
});
alert.show();
}
});
}
private void setTextFromDialog(final String textFromDialog){
myEditText.setText(textFromDialog);
}
}
hope this help you

How to use the AlertDialog's return value for boolean condition

I want to build a java class file that create alertDialog and return a boolean value for sendSMS. But i dont know how to return a value from alertDialog java class. So my function return void result.
This is my first class that execute alertDialog java class
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class SMSActivity extends AppCompatActivity {
ListView mainListView;
String mainCommandStrings[] = {"Turn ON", "Turn OFF", "Parameters"};
private static final String LOG_TAG = SMSActivity.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms);
// Array Adapter
mainListView = findViewById(R.id.main_list_view);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this,
R.layout.sms_main_list_view,
R.id.command_text_view,
mainCommandStrings );
mainListView.setAdapter(arrayAdapter);
mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView parent, View view, int position, long id){
switch (position){
case 0: // turn on
Toast.makeText(getApplicationContext(),"Turn on",Toast.LENGTH_SHORT).show();
ConfirmSMS confirmSMS =
new ConfirmSMS(getApplicationContext(), position);
sendTextMessage(confirmSMS, position);
break;
case 1: // Turn off
Toast.makeText(getApplicationContext(),"Turn off",Toast.LENGTH_SHORT).show();
ConfirmSMS confirmSMS1=
new ConfirmSMS(getApplicationContext(), position);
sendTextMessage(confirmSMS1, position);
break;
case 2: //Setting Parameters
Toast.makeText(getApplicationContext(),"Parameters",Toast.LENGTH_SHORT).show();
intentParameter();
break;
default:
break;
}
}
});
}
private void sendTextMessage(ConfirmSMS confirmSMS, int itemPosition) {
int position = itemPosition;
String stat;
if(position==0){
stat = "SMS Sent";
} else{
stat = "Cancel";
}
Toast.makeText(getApplicationContext(),stat,Toast.LENGTH_SHORT).show();
}
private void intentParameter() {
Log.d(LOG_TAG, "SMS Method");
Intent parameterIntent = new Intent(this, ParameterActivity.class);
startActivity(parameterIntent);
}
and this is my alertDialog java class (ConfirmSMS)
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.Toast;
class ConfirmSMS {
ConfirmSMS(final Context applicationContext, int itemPosition){
String stat = null;
if(itemPosition==0){
stat = "Turn ON";
} else if(itemPosition==1) {
stat = "Turn OFF";
}
final String condition = stat;
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(applicationContext);
//Setting alert dialog
alertDialogBuilder.setTitle("Confirmation"); // set judul
alertDialogBuilder.setMessage("Are you sure to "+ condition+ " the AC?");
alertDialogBuilder.setCancelable(true);
alertDialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(applicationContext,
"Send instruction to "+condition+" the AC",
Toast.LENGTH_SHORT).show();
}
});
alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(applicationContext,
"Cancel",
Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
Refactor your code so that you don't need another class for creating an AlertDialog. Reorganize your code to keep it all in the Activity so that you can access the method sendTextMessage from the actions on the AlertBuilder.
Something like this:
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class SMSActivity extends AppCompatActivity {
ListView mainListView;
String mainCommandStrings[] = {"Turn ON", "Turn OFF", "Parameters"};
private static final String LOG_TAG = SMSActivity.class.getSimpleName();
private boolean myBoolean;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms);
// Array Adapter
mainListView = findViewById(R.id.main_list_view);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this,
R.layout.sms_main_list_view,
R.id.command_text_view,
mainCommandStrings);
mainListView.setAdapter(arrayAdapter);
mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView parent, View view, final int position, long id) {
String stat = null;
switch (position) {
case 0: // turn on
Toast.makeText(getApplicationContext(), "Turn on", Toast.LENGTH_SHORT).show();
stat = "Turn ON";
break;
case 1: // Turn off
Toast.makeText(getApplicationContext(), "Turn off", Toast.LENGTH_SHORT).show();
stat = "Turn ON";
break;
case 2: //Setting Parameters
Toast.makeText(getApplicationContext(), "Parameters", Toast.LENGTH_SHORT).show();
intentParameter();
break;
default:
break;
}
final String condition = stat;
if(condition != null){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getApplicationContext());
//Setting alert dialog
alertDialogBuilder.setTitle("Confirmation"); // set judul
alertDialogBuilder.setMessage("Are you sure to "+ condition+ " the AC?");
final AlertDialog.Builder builder = alertDialogBuilder.setCancelable(true);
alertDialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),
"Send instruction to "+condition+" the AC",
Toast.LENGTH_SHORT).show();
//Here is where you execute sendTextMessage
//passing the param from the AlertDialog
sendTextMessage(position);
}
});
alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),
"Cancel",
Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
});
}
private void sendTextMessage(int itemPosition) {
int position = itemPosition;
String stat;
if (position == 0) {
stat = "SMS Sent";
} else {
stat = "Cancel";
}
Toast.makeText(getApplicationContext(), stat, Toast.LENGTH_SHORT).show();
}
private void intentParameter() {
Log.d(LOG_TAG, "SMS Method");
Intent parameterIntent = new Intent(this, ParameterActivity.class);
startActivity(parameterIntent);
}

How to store added Items in Android

I recently wanted to develop a shopping list app and i followed this tutorial, but in this tutorial the tutor shows that his added items are stored but I followed the same process but when I close and reopen the app the added items are gone. what can be done to fix this?
here is my program code:
package com.dopenets.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import android.app.AlertDialog;
import android.widget.EditText;
import android.content.DialogInterface;
import android.content.Context;
import android.content.SharedPreferences;
import android.app.Activity;
import android.widget.Toast;
import android.widget.AdapterView;
import android.widget.TextView;
import android.view.View;
public class MainActivity extends AppCompatActivity {
ArrayList<String> shoppingList = null;
ArrayAdapter<String> adapter = null;
ListView lv = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
shoppingList = getArrayVal(getApplicationContext());
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, shoppingList);
lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView parent, View view, final int position, long id) {
String selectedItem = ((TextView) view).getText().toString();
if (selectedItem.trim().equals(shoppingList.get(position).trim())) {
removeElement(selectedItem, position);
} else {
Toast.makeText(getApplicationContext(),"Error Removing Element", Toast.LENGTH_LONG).show();
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_add){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Add Item");
final EditText input = new EditText(this);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
shoppingList.add(preferredCase(input.getText().toString()));
Collections.sort(shoppingList);
lv.setAdapter(adapter);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
return true;
}
return super.onOptionsItemSelected(item);
}
public static String preferredCase(String original)
{
if (original.isEmpty())
return original;
return original.substring(0, 1).toUpperCase() + original.substring(1).toLowerCase();
}
public static void storeArrayVal( ArrayList<String> inArrayList, Context context)
{
Set<String> WhatToWrite = new HashSet<String>(inArrayList);
SharedPreferences WordSearchPutPrefs = context.getSharedPreferences("dbArrayValues", Activity.MODE_PRIVATE);
SharedPreferences.Editor prefEditor = WordSearchPutPrefs.edit();
prefEditor.putStringSet("myArray", WhatToWrite);
prefEditor.commit();
}
public static ArrayList getArrayVal( Context dan)
{
SharedPreferences WordSearchGetPrefs = dan.getSharedPreferences("dbArrayValues",Activity.MODE_PRIVATE);
Set<String> tempSet = new HashSet<String>();
tempSet = WordSearchGetPrefs.getStringSet("myArray", tempSet);
return new ArrayList<String>(tempSet);
}
public void removeElement(String selectedItem, final int position){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Remove " + selectedItem + "?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
shoppingList.remove(position);
Collections.sort(shoppingList);
storeArrayVal(shoppingList, getApplicationContext());
lv.setAdapter(adapter);
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
}
It appears that you are not calling the storeArrayVal method when an item is added, so nothing is ever stored.

Android Dev: Something wrong with my function, but I've been stuck for two hours

so I am making my 2nd app, and I decided to build a menu which has multiple skins/icon sets and can randomize main menu buttons, I got everything working, then I started developing Introduction screen which is launched when user first launches app using Dialog boxes, now I got colors changing dynamically when user selects different scheme, which works fine, and when user comes to box which asks if they want to randomize colors of menu buttons every time:
If I click on Randomize it does change dynamically, but if I tick sequential then it doesn't change at all, I don't get it.
I have 3 main files:
ConversePrefs.java, I won't post it as I am 100% sure it's correct as it's just a set of shortcut functions for getting shared preferences.
MainMenu.java - main activity/main menu:
package com.arl.conversion;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainMenu extends Activity {
private static final OnClickListener PreSetupsColor = null;
final Context context = this;
ColorCollections cc = new ColorCollections(context);
List<String> colorList = null;
int currentIndex = 0;
public void LoadColorPalette(){
ConversePrefs cp = new ConversePrefs(this);
colorList = cc.GetColorList(cp.GetIntSetting("UsersColor", 0),cp.GetBolSetting("UsersRandomizer", false));
}
public void SetBtnColour(int btn){
Button bttn = (Button)findViewById(btn);
bttn.setBackgroundColor(Color.parseColor(colorList.get(currentIndex)));
currentIndex++;
}
public void GenerateButtonsDefault(){
ConversePrefs cp = new ConversePrefs(this);
LoadColorPalette();
SetBtnColour(R.id.DistanceBtn);
SetBtnColour(R.id.VolumeBtn);
SetBtnColour(R.id.AreaBtn);
SetBtnColour(R.id.ClothingBtn);
SetBtnColour(R.id.ElectricBtn);
SetBtnColour(R.id.TempBtn);
SetBtnColour(R.id.TimeBtn);
SetBtnColour(R.id.MoneyBtn);
SetBtnColour(R.id.SpeedBtn);
SetBtnColour(R.id.MathBtn);
SetBtnColour(R.id.CurrencyBtn);
SetBtnColour(R.id.OptionsBtn);
}
public void RefreshMenu(){
currentIndex = 0;
GenerateButtonsDefault();
}
//SECOND
public void PreSetuposColor(final View v){
final ConversePrefs cp = new ConversePrefs(this);
String[] ColorSchemes = {"Windows 8", "Modern Urban"};
new AlertDialog.Builder(this).
setTitle("Color Scheme").
setSingleChoiceItems(ColorSchemes, cp.GetIntSetting("UsersColor", 0), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
cp.SetIntSetting("UsersColor", which);
RefreshMenu();
}
}).
setPositiveButton("Continue", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
MainMenu.this.PreSetupRandoms(v);
}
}).setNeutralButton("Back", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
MainMenu.this.PreSetupsWelcome(v);
}
}).setNegativeButton("Quit", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
System.exit(0);
}
}).setCancelable(false).show();
}
// THIRD
public void PreSetupRandoms(final View v){
final ConversePrefs cp = new ConversePrefs(this);
String[] FalseAndTrue = {"Sequential", "Randomize"};
new AlertDialog.Builder(this).
setTitle("Randomize Colors?").
setSingleChoiceItems(FalseAndTrue, cp.GetBoolInt(cp.GetBolSetting("UsersRandomizer", false)), new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
cp.SetBooleanSetting("UsersRandomizer", cp.GetIntBool(which));
RefreshMenu();
}
}).setPositiveButton("Continue", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO
}
}).setNeutralButton("Back", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
MainMenu.this.PreSetuposColor(v);
}
}).setNegativeButton("Quit", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
System.exit(0);
}
}).setCancelable(false).show();
}
//FIRST
public void PreSetupsWelcome(final View v){
new AlertDialog.Builder(this).
setTitle("Welcome").
setMessage("It seems that this is the first time application was ran on this device, you will be now presented with app appearance setup of your own choice.").
setPositiveButton("Continue", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
MainMenu.this.PreSetuposColor(v);
}
}).
setNegativeButton("Quit", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
System.exit(0);
}
}).
setCancelable(false).show();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ConversePrefs cp = new ConversePrefs(this);
//LoadColorPalette();
setContentView(cc.GetTheme(cp.GetIntSetting("ThemeName", 0)));
GenerateButtonsDefault();
PreSetupsWelcome(new View(this));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
}
ColorCollections.java - file that has info about colors/and few functions for retrieval.
package com.arl.conversion;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
public class ColorCollections{
Context context;
public ColorCollections(Context context){
this.context = context;
}
//Color Palettes.
public static String[] Win8 = new String[]{"#008299","#00A0B1","#2672EC","#2E8DEF","#8C0095","#A700AE","#5133AB","#643EBF","#AC193D","#BF1E4B","#D24726","#DC572E","#008A00","#00A600","#094AB2","#0A5BC4"};
public static String[] ModernUrban = new String[]{"#47B6BE","#73CC3E","#FF7149","#FF5063","#FF6999","#233977","#4B9364","#8A8F1D","#A63B52","#B23265","#883E57","#888788","#87A087"};
public List<String> GetColorList(int colorindex, boolean randomizer){
List<String> colorList = Arrays.asList(Win8);
switch(colorindex){
case 0:
colorList = Arrays.asList(Win8);
break;
case 1:
colorList = Arrays.asList(ModernUrban);
break;
}
if(randomizer == true){
Collections.shuffle(colorList);
Collections.shuffle(colorList);
Collections.shuffle(colorList);
Collections.shuffle(colorList);
}
return colorList;
}
public int GetTheme(int theme){
int themeID = 0;
switch(theme){
case 0:
themeID = R.layout.activity_main_menu_light;
break;
case 1:
themeID = R.layout.activity_main_menu_light_bg;
break;
case 2:
themeID = R.layout.activity_main_menu_dark;
break;
case 3:
themeID = R.layout.activity_main_menu_dark_bg;
break;
}
return themeID;
}
}
Can somebody please identify problem? I've been stuck with this for past 2 hours, and tried out everything, even ended up having so much more efficient code (I know it may look bad to you, but it was worse, trust me).
Also I have 4 sets of icons, 12 icons in each set, each set has prefix for icon, it's an actual imported drawable .png. is there any better way to create XML list of them than manually type?
Thank You.
I can't find the exact location of the problem, but it would be beneficial for you to do debugging if you haven't, and step into the function that is setSingleChoiceItems(...) as a start. You can use a ListView to display a XML list. Here is an example generated from a strings.xml file: http://www.androidhive.info/2011/10/android-listview-tutorial/ . More complicated ListViews will require you to create a custom adapter class.

Return/save values from AlertDialog in Android?

So, I've got this code for an AlertDialog in an Android activity, and although it works and pops up at the correct moment, when I press the "OK" buttons it's supposed to save the 2 variables which I put in, into 2 Strings which after the activity should be able to use those strings.
private void showDialog(){
AlertDialog.Builder alertdg = new AlertDialog.Builder(this);
alertdg.setTitle("Choose page");
alertdg.setMessage("Choose episode/page");
final EditText page = new EditText(this);
final EditText episode = new EditText(this);
page.setWidth(210);
episode.setWidth(210);
LinearLayout layout = new LinearLayout(this);
layout.addView(episode);
layout.addView(page);
alertdg.setView(layout);
alertdg.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
episodeString = episode.getText().toString();
pageString = page.getText().toString();
}
});
alertdg.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
alertdg.show();
}
And yes if you're wondering I already declared the 2 strings "episodeString" and "pageString" somewhere in the start of the activity, and I know I declared them correctly. Now what I'm wondering is, why can't I return those values so that the rest of the activity can use them? I've tried many times but the Dialog just won't return/save values... What am I doing wrong?
You code absolutely works
package pete.android.study;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends Activity{
private String mEpisode = "";
private String mPage = "";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
showDialog();
}
private void showDialog(){
AlertDialog.Builder alertdg = new AlertDialog.Builder(this);
alertdg.setTitle("Choose page");
alertdg.setMessage("Choose episode/page");
final EditText page = new EditText(this);
final EditText episode = new EditText(this);
page.setWidth(210);
episode.setWidth(210);
LinearLayout layout = new LinearLayout(this);
layout.addView(episode);
layout.addView(page);
alertdg.setView(layout);
alertdg.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
mEpisode = episode.getText().toString();
mPage = page.getText().toString();
Toast.makeText(MainActivity.this, mEpisode + " | " + mPage, Toast.LENGTH_SHORT).show();
}
});
alertdg.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
alertdg.show();
}
}

Categories

Resources