I am new to Android. I am trying to select the spinner choice and multiply it by the user input. (it's an app that multiples the number of pizzas by the cost of each pizza) Does that make sense? So, I am able to process one spinner option and get the correct answer, but i can't figure out how do code more than one spinner option.
Heres the Java:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import java.text.DecimalFormat;
public class MainActivity extends AppCompatActivity {
//below is where I have placed the values associated with the Pizza Choices//
//each one is under the txtPizza string array and is referenced//
double costCheese = 4.50;
double costPepperoni = 5.75;
double costVeggie = 4.75;
double costGluten = 5.30;
double costPersonal = 3.15;
int txtnumber;
double txtresult;
String txtGroup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText amount = (EditText)findViewById(R.id.txtnumber);
final Spinner type =(Spinner)findViewById(R.id.txtGroup);
Button cost = (Button)findViewById(R.id.OrderBtn);
cost.setOnClickListener(new View.OnClickListener() {
final TextView result = ((TextView)findViewById(R.id.txtResult));
#Override
public void onClick(View v) {
txtnumber = Integer.parseInt(amount.getText().toString());
txtresult = costCheese * txtnumber;
DecimalFormat currency = new DecimalFormat("$###,###.##");
result.setText("Cost for this order is " + currency.format(txtresult)); }
});
}}
Well you need an adapter to illustrate the options that you desire.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
array_spinner=new String[5];
array_spinner[0]="option1";
array_spinner[1]="option2";
array_spinner[2]="option3";
array_spinner[3]="option4";
array_spinner[4]="option5";
Spinner s = (Spinner) findViewById(R.id.Spinner01);
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, array_spinner);
s.setAdapter(adapter);
}
Check this out for more about spinners.
Related
I'm working on a project that allows the user to send a string from one activity to a list view in another activity with a button. However, when I try to send data from one activity to the next, the receiving activity replaces the last item sent instead of adding to the list view. Here's my code:
EnterInfoActivity.java:
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import static com.example.kevin.myapplication.R.id.Done;
import static com.example.kevin.myapplication.R.id.EnterInfo;
public class EnterInfoActivity extends AppCompatActivity {
ArrayAdapter<CharSequence> yearAdapter;
Button Done;
EditText Name;
EditText PName;
Spinner YearSpinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enter_info);
Name = (EditText) findViewById(R.id.Name);
PName = (EditText) findViewById(R.id.PName);
Done = (Button) findViewById(R.id.Done);
YearSpinner = (Spinner) findViewById(R.id.Year);
yearAdapter = ArrayAdapter.createFromResource(this,R.array.year_sequence, android.R.layout.simple_spinner_item);
yearAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
YearSpinner.setAdapter(yearAdapter);
Done.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences sharedPreferences = getSharedPreferences("ViewData", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("newName", Name.getText().toString());
editor.putString("newPName", PName.getText().toString());
editor.putString("Year", YearSpinner.getSelectedItem().toString());
editor.commit();
finish();
}
});
}
}
and heres ViewActivity.java:
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ListViewCompat;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class ViewActivity extends AppCompatActivity {
ListView lv;
ArrayList<String> arrayList;
ArrayAdapter<String> adapter;
public static final String DEFAULT = " ";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
SharedPreferences sharedPreferences = getSharedPreferences("ViewData", Context.MODE_PRIVATE);
String Name = sharedPreferences.getString("newName",DEFAULT);
String PName = sharedPreferences.getString("newPName",DEFAULT);
String Year = sharedPreferences.getString("Year",DEFAULT);
String PhotoInfo =(Name + " " + PName + " " + Year);
lv = (ListView) findViewById(R.id.LV_View);
arrayList = new ArrayList<String>();
arrayList.add(PhotoInfo);
adapter = new ArrayAdapter<String>(ViewActivity.this, android.R.layout.simple_list_item_1,
arrayList);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
In ur arrayList you always have the last one . There is no existing list that you are adding to, hence its only showing you the last one
ArrayList<String> arrayList;
---
arrayList = new ArrayList<String>();
arrayList.add(PhotoInfo);
From the last 2 lines u can see, u are creating a new list and adding just that one String to it.Hence u can just see the last one.
you must set your ArrayList as static and don't renew it every time! you must just create it for one time and each time just add new item to it and call notifyDatasetChanged().
public class ViewActivity extends AppCompatActivity {
ListView lv;
//set arraylist as static and eager loading for it.
public static ArrayList<String> arrayList=new ArrayList<String>();
ArrayAdapter<String> adapter;
public static final String DEFAULT = " ";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
SharedPreferences sharedPreferences = getSharedPreferences("ViewData", Context.MODE_PRIVATE);
String Name = sharedPreferences.getString("newName",DEFAULT);
String PName = sharedPreferences.getString("newPName",DEFAULT);
String Year = sharedPreferences.getString("Year",DEFAULT);
String PhotoInfo =(Name + " " + PName + " " + Year);
lv = (ListView) findViewById(R.id.LV_View);
//remove renew arraylist
arrayList.add(PhotoInfo);
adapter = new ArrayAdapter<String>(ViewActivity.this, android.R.layout.simple_list_item_1,
arrayList);
lv.setAdapter(adapter);
//its ok
adapter.notifyDataSetChanged();
}
In the line of code marked by comment I want to put townHallLvl but it needs to be string so i tried to use .toString but it wasn't available.
Is there another solution or I made something wrong.
package com.example.android.planmyclash;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
//set buildings' values
int townHallLvl = 1;
//declare ids
TextView townHallText = (TextView) findViewById(R.id.town_hall_lvl);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setValues();
}
public void setValues(){
townHallText.setText(); //this line <----
}
}
Use
townHallText.setText(String.valueOf(townHallLvl));
in order to convert the integer to a String.
package com.example.android.planmyclash;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
//set buildings' values
int townHallLvl = 1;
TextView townHallText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//declare ids
townHallText = (TextView) findViewById(R.id.town_hall_lvl);
setValues();
}
public void setValues(){
townHallText.setText(townHallLvl + ""); //this line <----
}
}
Happy Coding
Try this:
townHallText.setText(townHallLvl+"");
I hope it will work perfectly.
I am trying to get the value of inputEditText and multiply it by the value of convertNum.
The error I'm getting is: Operator '*' cannot be applied to 'android.widget.EditText','double'
I believe I need to convert the value of inputEditText into a number, but I'm not sure. Any help would be appreciated. Here is my code:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Declare variables
final Button calculateButton = (Button) findViewById(R.id.button);
final TextView resultTextView = (TextView) findViewById(R.id.textView2);
final TextView resultnumTextView = (TextView) findViewById(R.id.textView3);
final EditText inputEditText = (EditText) findViewById(R.id.editText);
final double convertNum = 0.125;
// Set resultTextView to be invisible on app start
resultTextView.setVisibility(View.INVISIBLE);
// When calculate button is pressed: Set resultTextView to visible and show result
calculateButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
resultTextView.setVisibility(View.VISIBLE);
resultnumTextView.setText(inputEditText * convertNum);
}
});
}
You get the error as you are trying to multiply EditText (which is a View instance) and double (which is a number).
Easiest fix is to replace
resultnumTextView.setText(inputEditText * convertNum);
with
resultnumTextView.setText(String.valueOf(
Float.valueOf(inputEditText.getText().toString()) * convertNum
));
Instead of this:
resultnumTextView.setText(inputEditText * convertNum);
Do this:
resultnumTextView.setText(Double.parseDouble(inputEditText.getText().toString()) * convertNum);
inputEditText is an EditText, a widget to write text. You need to extract the typed text and convert into a float.
inputEditText.getText().toString()
Will give you a String with the typed text. Then use the wrapper class Float to convert into a number
String s = inputEditText.getText().toString();
Float f= Float.parseFloat(s);
As you can see here
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Declare variables
final Button calculateButton = (Button) findViewById(R.id.button);
final TextView resultTextView = (TextView) findViewById(R.id.textView2);
final TextView resultnumTextView = (TextView) findViewById(R.id.textView3);
final EditText inputEditText = (EditText) findViewById(R.id.editText);
final double convertNum = 0.125;
// Set resultTextView to be invisible on app start
resultTextView.setVisibility(View.INVISIBLE);
// When calculate button is pressed: Set resultTextView to visible and show result
calculateButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String valueFromInput = inputEditText.getText().toString();
try{
double valueToDouble = Double.parseDouble(valueFromInput);
double result = convertNum * valueToDouble;
resultnumTextView.setText(result+"");
resultTextView.setVisibility(View.VISIBLE);
}catch(Exception e){
// print the exception.
}
}
});
}
I'm new to android development and Java programming. In my code (shown below), when the user clicks the search button, the value of a key is displayed in displayField TextView. But I want it so that the value is displayed when the user clicks (touches) an item on the AutoCompleteTextView dropdown items, without needing to click the search button.
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity
{
Map<String, String> map = new HashMap<String, String>();
private EditText autocomplete_searchField;
private TextView displayField;
String note;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
autocomplete_searchField = (AutoCompleteTextView) findViewById(R.id.autocomplete_searchField);
// Get a reference to the AutoCompleteTextView in the layout
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_searchField);
// Gets the string array
String[] music_note = getResources().getStringArray(R.array.music_notes_array);
// Creates the adapter and set it to the AutoCompleteTextView
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, musicNote);
textView.setAdapter(adapter);
displayField = (TextView) findViewById(R.id.displayField);
Button button = (Button)findViewById(R.id.button); // I want to completely remove this button eventually
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String note = autocomplete_searchField.getText().toString();
displayField.setText(map.get(note));
map.put("Doe", "a deer, a female deer");
map.put("Ray", "a drop of golden sun");
}
I didn't understand why you needed autocomplete_searchField in your code, anyway, if I understood well you just need to remove button.setOnClickListener and add the following code:
textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
displayField.setText(map.get(adapterView.getItemAtPosition(position).toString()));
}
});
You need to set ItemClickedListener to AutoCompleteTextView.
textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View arg1, int position, long arg3) {
String note = autocomplete_searchField.getText().toString();
displayField.setText(map.get(note));
}
});
Android - I am using intent to communicate between two activities. I used putExtra() in the calling activity and i am using getExtras in the called activity. But the problem is iam not able to set the edit text with the number i retrieved in the intent. below is my program`
// Calling activity
package com.example.androidtutorial2;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.text.InputType;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//EDIT TEXT
final EditText sharedata = (EditText) findViewById ( R.id.editText1 ) ;
sharedata.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
//BUTTON
Button sendbutton = (Button) findViewById (R.id.button2) ;
sendbutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int edittext_data = Integer.valueOf(sharedata.getText().toString());
Intent sendintent = new Intent(MainActivity.this,second_activity.class);
sendintent.putExtra("somedata", edittext_data);
startActivity(sendintent);
}
});
// Button button_to_call_activity = (Button) findViewById(R.id.button1);
// button_to_call_activity.setOnClickListener(new OnClickListener() {
//
// #Override
// public void onClick(View v) {
//
//
// }
// });
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
//CALLED ACTIVITY
package com.example.androidtutorial2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.widget.EditText;
public class second_activity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
EditText displaymessage = (EditText) findViewById ( R.id.editText1 );
Intent intent1 = getIntent();
Bundle bundle1 = intent1.getExtras();
int integer1 = bundle1.getInt("somedata");
Integer string1 = integer1;
displaymessage.setText(integer1); // HERE IS A RUNTIME ERROR
}
}
use
displaymessage.setText("" +integer1);
Or
displaymessage.setText(String.valueOf(integer1));
1. First of all if you are using Java 1.5 and above dont useInteger.valueOf() but instead use Integer.parseInt().
eg:
int edittext_data = Integer.parseInt(sharedata.getText().toString());
2. Your problem with the output is here.
Dont do this ... Integer string1 = integer1;
Use it like this...
String string1 = getIntent().getExtras().getString("somedata");
displaymessage.setText(string1);
Since you don't seem to be doing anything with the variable as an integer, you could also simply keep it as a string.
sendintent.putExtra("somedata", sharedata.getText().toString());
String integer1 = bundle1.getString("somedata");