I'm making an app that I can input name and money and calculate together. Example i input like this in plain text
User 1 = $520
User 2 = $241
User 3 = $253
User 4 = $704
Total = $1718
and here are the id i declared for my plain texts
name1 = (EditText) findViewById(R.id.editName1);
name2 = (EditText) findViewById(R.id.editName2);
name3 = (EditText) findViewById(R.id.editName3);
name4 = (EditText) findViewById(R.id.editName4);
money1 = (EditText) findViewById(R.id.editMoney1);
money2 = (EditText) findViewById(R.id.editMoney2);
money3 = (EditText) findViewById(R.id.editMoney3);
money4 = (EditText) findViewById(R.id.editMoney4);
but how can I set the data from plain text to PieGraph?
Convert the text to numbers (integer, float, ...)
Add them like this:
float number1 = ...; // parse your EditText input here
// other numbers ...
ArrayList<Entry> vals = new ArrayList<Entry>();
vals.add(new Entry(number1, 0));
vals.add(new Entry(number2, 1));
vals.add(new Entry(number3, 2));
vals.add(new Entry(number4, 3));
String[] xVals = new String[] { "User1", "User2", "User3", "User4" };
PieDataSet dataSet = new PieDataSet(vals, "User Values");
PieData data = new PieData(xVals, dataSet);
pieChart.setData(data);
// refresh
pieChart.invalidate();
Also, the example project shows numerous use cases of how data can be added to the charts, you might wanna check it out.
Furthermore, the documentation is pretty good and also gives examples and instructions on how to add data.
Related
I have the sum total of the income column of my database displaying as a double in a textview but I can't get it to display as currency. When I debug, the results show it should display as "$ 10,432.18", but it just shows as "10432.18". Below is what I used in a listview for the individual income items that worked in the listview, but not in the textview:
TextView cmIncomeSumTextView = (TextView) findViewById(R.id.cm_income_sum_text_view);
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setGroupingSeparator(',');
symbols.setDecimalSeparator('.');
DecimalFormat decimalFormat = new DecimalFormat("$ #,###.00", symbols);
String cmIncomeDecimal = decimalFormat.format(cmIncomeSum());
cmIncomeSumTextView.setText(cmIncomeDecimal);
any help would be appreciated!
You can try this instead.
string pattern = "$###,###.###";
DecimalFormat decimalFormat = new DecimalFormat(pattern);
String format = decimalFormat.format(123456789.123);
System.out.println(format);
I am very new to android programming and am trying to complete my first app. It is a recipe converter.
I have stored my recipe details in a SQLite DB and the text for ingredients is just one multiline string separated by carriage returns. I have used a cursor to get the ingredient data into a textview which returns text like (could be numerous variants):
100ml Water
500 g Mince
2 x 400g can crushed tomatoes
etc.
I originally had each Qty, Unit and Ingredient Description stored separately in the database which made life easy when converting but I chose to store it in a multiline string to allow copying and pasting of ingredients from the internet or another source.
I am attempting to extract the numbers and then multiply them by a percentage, then return the new converted numbers, and the corresponding unit and description to get something like this:
(multiplied by 200%)
200ml Water
1000g Mince
4 x 400g can crushed tomatoes
I just don't know how to do it though. Can anyone help please?
Thanks
UPDATE:
I have tried to do something like this to get the numbers.
public void Split() {
TextView tvSplit = (TextView) findViewById(R.id.tvSplit);
final TextView tvTest = (TextView) findViewById(R.id.tvTest);
String s = tvTest.getText().toString();
for (int i =0;i <= tvTest.getLineCount();i++){
tvSplit.setText("");
String text = s.replaceAll("\\D+", ",");
tvSplit.append(text);
tvSplit.append("\n");
}
That shows me all of the numbers with a "," between them but it also includes all numbers in the string like in the above example prior to conversion it would show 100,500,2,400 when I only need 100,500,2. Then from that point I'm not sure how I would convert them all. My "fresh to programming mind" thought that I could store these in a temp SQL table by INSERT INTO tablename (id, originalvalues) VALUES (my string ie 100,500,2).
I could then pull them back out, do the calculation, update the table, then add them back into my textview with the remaining string. I haven't got that far yet, so I'm just wondering what the correct way to do it is.
UPDATE 2:
As per my comments, this is the code I used to show an alert dialog with each item listed on a separate line, I then used the selected line to find the number before any " " to then display the text on the screen.
public void PopUpSpinnerDialogue() {
final TextView tvTest = (TextView) findViewById(R.id.tvTest);
final TextView tv2 = (TextView) findViewById(R.id.tvTest2);
String s = tvTest.getText().toString();
final ArrayAdapter myAdapter = new ArrayAdapter<String>(this,
R.layout.my_dropdown_style, s.split("\n"));
android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
builder.setTitle("Please choose the key ingredient you need to scale your recipe by.")
.setCancelable(false)
.setAdapter(myAdapter, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
try {
String itemName = myAdapter.getItem(which).toString();
String[] parts = itemName.split(" ");
String itemNumStr = parts[0];
TextView tvLineName = (TextView) findViewById(R.id.tvIngredientSelect);
EditText et1 = (EditText) findViewById(R.id.etRecipeQtyConvert);
EditText et2 = (EditText) findViewById(R.id.etQtyHave);
tvLineName.setText(itemName);
String b4Space = itemNumStr.replaceAll("\\D+", "");
tv2.setText(b4Space);
et1.setText(b4Space);
et2.setText(b4Space);
calculateKeyIngredientPercent();
} catch (Exception e) {
Toast.makeText(SelectConvertMethod.this, "Your ingredient must have a QTY. eg. 100ml.", Toast.LENGTH_SHORT).show();
}
}
});
android.app.AlertDialog alert = builder.create();
alert.show();
}
It is this idea that I think I can use but I don't know how to code it and then display the results.
UPDATE 3:
The code or at least the idea of the code I am trying to use is this.
TextView tvSplit = (TextView) findViewById(R.id.tvSplit);
final TextView tvTest = (TextView) findViewById(R.id.tvTest);
String s = tvTest.getText().toString();
for (int i =0;i <= tvTest.getLineCount();i++){
String[] ingreds = s.split("\n");
tvSplit.setText("");
String[] parts = ingreds.split(" ");
String Qty = parts[0];
String Units = parts[1];
String Ingredients = parts[2];
Integer QtyInt = Integer.parseInt(Qty);}
ingreds.split doesn't work and also, I don't know how to specify splitting the parts for each i.
I ended up using regex. It allowed the data to be entered with or without a space. So I ended up using this code to pull out the Qty of each line, multiply it by a percentage, append the text (units,ingredient description) to the line, then add it to a string array, to add to my alert dialog.
Code is here.
public void Split() {
final TextView tv2 = (TextView) findViewById(R.id.tvTest2);
TextView tvTest = (TextView) findViewById(R.id.tvTest);
TextView tvPercent = (TextView) findViewById(R.id.tvPercent);
String tvP = tvPercent.getText().toString();
String tvNumOnly = tvP.replaceAll("\\D+", "");
Integer PercentVal = Integer.parseInt(tvNumOnly);
String s = tvTest.getText().toString();
StringBuilder sb = new StringBuilder();
Pattern p = Pattern.compile("((\\d*) ?(.*)(?:\\n*))");
Matcher m = p.matcher(s);
while (m.find()) {
String Qty = m.group(2) + ".00";
String Ingred = m.group(3);
Float QtyFloat = Float.parseFloat(Qty);
Float newQTY = (QtyFloat * PercentVal) / 100;
String newQTYstr = newQTY.toString();
sb.append(newQTYstr + " " + Ingred + "\n");
}
String[] lines = sb.toString().split("\n");
String[] IngredArray = Arrays.copyOfRange(lines, 0, lines.length - 1);
final ArrayAdapter myAdapter = new ArrayAdapter<String>(this,
R.layout.my_dropdown_style, IngredArray);
android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
builder.setTitle("Please choose the key ingredient you need to scale your recipe by.")
.setCancelable(false)
.setAdapter(myAdapter, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
try {
String itemName = myAdapter.getItem(which).toString();
String[] parts = itemName.split(" ");
String itemNumStr = parts[0];
TextView tvLineName = (TextView) findViewById(R.id.tvIngredientSelect);
EditText et1 = (EditText) findViewById(R.id.etRecipeQtyConvert);
EditText et2 = (EditText) findViewById(R.id.etQtyHave);
tvLineName.setText(itemName);
String b4Space = itemNumStr.replaceAll("\\D.\\D+", "");
tv2.setText(b4Space);
et1.setText(b4Space);
et2.setText(b4Space);
calculateKeyIngredientPercent();
} catch (Exception e) {
Toast.makeText(SelectConvertMethod.this, "Your ingredient must have a QTY. eg. 100ml.", Toast.LENGTH_SHORT).show();
}
}
});
android.app.AlertDialog alert = builder.create();
alert.show();
// Toast.makeText(SelectConvertMethod.this, sb, Toast.LENGTH_LONG).show();
}
I want to create a list of the double values. screen shot of my layout
When the (+) add button is pressed a minimum of three edit boxes are added.
i want to get the values of these edit boxes and be able to cross multiply them.
the values will be mostly +/- and with decimals.
How can i identify the edit boxes, then from the input Values, i set them in a List where i can be able to cross Multiply them.
I am trying this but i don't understand where am going wrong.
List<EditText> allNs = new ArrayList<EditText>();
List<EditText> allEs = new ArrayList<EditText>();
String[] northings = new String[allNs.size()];
String[] eastings = new String[allEs.size()];
double inputNorths, inputEasts = 0;
for(int i=0; i<allNs.size(); i++){
northings[i] = allNs.get(i).getText().toString();
inputNorths = Double.parseDouble(northings[i]);
northValues [1] = inputNorths;
}
resultN.add(northValues);
for(int e=0; e<allEs.size(); e++){
eastings[e] = allEs.get(e).getText().toString();
inputEasts = Double.parseDouble(eastings[e]);
eastValues[2] = inputEasts;
}
resultsE.add(eastValues);
calcArea();
To be honest I don't understand what are you doing here:
inputNorths = Double.parseDouble(northings[i]);
....
inputEasts = Double.parseDouble(eastings[e]);
Every loop you overwrite these variables. Maybe you forgot about adding?
inputNorths += Double.parseDouble(northings[i]);
northValues [1] += inputNorths;
I have added some EditTexts using a loop,
textFieldsLayout = (LinearLayout) findViewById(R.id.LinearLayout2);
for(int i=1; i <= 8; i++){
final EditText ed = new EditText(this);
ed.setText("" + i);
ed.setInputType(2);
ed.setLayoutParams(lparams);
textFieldsLayout.addView(ed);
}
}
After this I want to get the text that the user adds into the EditText fields, but I am stuck on how to do this. How would I get an Id for each of these EditText Fields?
Thanks, Oli
just add them to a collection when you're creating them that you can easily refer to by index or key or loop through or otherwise.
EditText[] etCollection = new EditText[8];
..........
textFieldsLayout = (LinearLayout) findViewById(R.id.LinearLayout2);
for(int i=1; i <= 8; i++){
final EditText ed = new EditText(this);
ed.setText("" + i);
ed.setInputType(2);
ed.setLayoutParams(lparams);
textFieldsLayout.addView(ed);
etCollection[i] = ed; <------ adding them to the collection
}
}
You'll have to do ed.setId( id ); on each one your create, and id should be unique for each one you want to find. When you go to find one do
EditText ed = (EditText)findViewById( id );
You'll have to keep track of your ids somewhere.
Alternatively you can just keep a list of the EditTexts that you create and run through the list to get the text of each one.
Is there a way to store EditText into a list array. For example,
n1I1 = (EditText) findViewById(R.id.etN1I1);
n1I2 = (EditText) findViewById(R.id.etN1I2);
n1I3 = (EditText) findViewById(R.id.etN1I3);
n2I1 = (EditText) findViewById(R.id.etN2I1);
n2I2 = (EditText) findViewById(R.id.etN2I2);
n2I3 = (EditText) findViewById(R.id.etN2I3);
into
FirstList[]={n1I1,n1I2,n1I3}
SecondList[]={n2I1,n2I2,n2I3}.
I would like to have it like this so that it is easy for me to keep track of number input by user. While at it, how can I store double(eg. 31.12) value into an arraylist?
Thanks.
You have 2 options:
//Array
EditText[] FirstList = {n1I1,n1I2,n1I3};
//or ArrayList
List<EditText> FirstList = new ArrayList<EditText>(){{
add(n1I1);
add(n1I2);
add(n1I3);
}};
use an ArrayList object, not a regular array:
ArrayList<EditText> firstList = new ArrayList<EditText>();
firstList.add(n1I1);
firstList.add(n1I2);
firstList.add(n1I3);
ArrayList<EditText> secondList = new ArrayList<EditText>();
secondList.add(n2I1);
secondList.add(n2I2);
secondList.add(n2I3);