Can't get Double output in TextView - java

The main purpose of this Android app is to learn how to use spinners and parse data. I have tried a lot of things and it still is not working. I don't get the output of the resultFuel on the screen.
Here's the code in my MainActivity:
public class MainActivity extends Activity implements OnItemSelectedListener {
Spinner spinFrom,spinTo;
String[] paths ={"liters/100km","liters/100 miles","(US)gallons/100 miles","(US)gallons/100km","(UK)gallons/100miles",
"(UK)gallons/100km","kilometers/liter","miles/liter", "miles/gallon", "kilometers/gallon"
};
EditText etfrom;
TextView textview;
double fromFuel,resultFuel;
Button clik;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinFrom = (Spinner) findViewById(R.id.fromSpinner);
spinTo = (Spinner) findViewById(R.id.toSpinner);
etfrom = (EditText) findViewById(R.id.fromET);
textview = (TextView) findViewById(R.id.Results);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item,paths);
spinFrom.setAdapter(adapter);
spinTo.setAdapter(adapter);
spinFrom.setOnItemSelectedListener(this);
spinTo.setOnItemSelectedListener(this);
if( etfrom.length() == 0 || etfrom.equals("") || etfrom == null){
}
clik = (Button) findViewById(R.id.button1);
clik.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(selectedFrom.equals(selectedTo)){
//Do nothing
}else if(selectedFrom.equals("liters/100km")&& selectedTo.equals("liters/100 miles")){
fromFuel= Double.parseDouble(etfrom.getText().toString());
resultFuel = fromFuel* 1.61;
// String result = String.valueOf(resultFuel);
textview.setText("" +resultFuel );
}else{
}
}
});
}
String selectedFrom;
String selectedTo;
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
selectedFrom = spinFrom.getItemAtPosition(arg2).toString();
selectedTo = spinTo.getItemAtPosition(arg2).toString();
Toast.makeText(getBaseContext(),"From: " + selectedTo, Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}

Please try :
textview.setText(Double.toString(resultFuel) );

Related

If else use in spinners

I want to take the value in spinner and user press the ok Button and then start new activity from against the value of spinner. The string array is just like that. and I use two spinners value get and new activity start against the spinners
Spinner s, s1;
Button ok;
Object _globalString, _globalString2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
s = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, array_spinner);
s.setAdapter(adapter);
s1 = (Spinner) findViewById(R.id.spinner01);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, array_spinner01);
s1.setAdapter(adapter1);
//
// s2=(Spinner)findViewById(R.id.spinner02);
// ArrayAdapter<String> adapter2=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_dropdown_item,array_spinner02);
// s2.setAdapter(adapter2);
ok = (Button) findViewById(R.id.btn_ok);
ok.setOnClickListener(this);
s.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
_globalString = parent.getItemAtPosition(position);
}
//#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
s1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
_globalString2 = parent.getItemAtPosition(position);
}
// #Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.btn_ok) {
if (_globalString.equals("Honda") || _globalString2.equals("Civic")) {
startActivity(new Intent(getApplicationContext(), Civic.class));
} else if (_globalString.equals("Toyota") || _globalString2.equals("Gli")) {
startActivity(new Intent(getApplicationContext(), Toyota.class));
}
}
}
}
here is the code this can error generate. Sorry application has been stopped
so i stuck on this code.
use setOnItemSelectedListener instead of setOnItemClickListener:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Spinner s, s1;
Button ok;
Object _globalString, _globalString2;
String[] array_spinner = {"Honda", "Toyota"};
String[] array_spinner01 = {"Civic", "Gli"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
s = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, array_spinner);
s.setAdapter(adapter);
s1 = (Spinner) findViewById(R.id.spinner01);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, array_spinner01);
s1.setAdapter(adapter1);
//
// s2=(Spinner)findViewById(R.id.spinner02);
// ArrayAdapter<String> adapter2=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_dropdown_item,array_spinner02);
// s2.setAdapter(adapter2);
ok = (Button) findViewById(R.id.btn_ok);
ok.setOnClickListener(this);
s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
_globalString = parent.getItemAtPosition(position);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
_globalString2 = parent.getItemAtPosition(position);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.btn_ok) {
if (_globalString.equals("Honda") || _globalString2.equals("Civic")) {
startActivity(new Intent(getApplicationContext(), Civic.class));
} else if (_globalString.equals("Toyota") || _globalString2.equals("Gli")) {
startActivity(new Intent(getApplicationContext(), Toyota.class));
}
}
}
}

get edit text value from view

I want to get the editext value and bitmap image from the every view that has bean created dynamically.I am not getting how to use it.I have tried to get the details from view vx.But it's not happening.Here is my code.
Please help...
public class MainActivity extends Activity {
EditText textIn;
Button buttonAdd;
LinearLayout container;
View addView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<String> value=new ArrayList<String>();
textIn = (EditText)findViewById(R.id.textin);
buttonAdd = (Button)findViewById(R.id.add);
Button button = (Button)findViewById(R.id.all);
container = (LinearLayout)findViewById(R.id.container);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0, c = container.getChildCount(); i < c; i++) {
View vx = container.getChildAt(i);
}
}
});
buttonAdd.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
addView = layoutInflater.inflate(R.layout.row, null);
EditText textOut = (EditText)addView.findViewById(R.id.textout);
textOut.setText(textIn.getText().toString());
textOut.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
}
});
final ImageView buttonimg = (ImageView)addView.findViewById(R.id.remove);
buttonimg.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//((LinearLayout)addView.getParent()).removeView(addView);
buttonimg.setImageResource(R.drawable.logo);
}});
container.addView(addView);
}});
}
}
For retrieving the text:
((EditText)vx.findViewById(R.id.textout)).getText()
I am posting the answer since I haven't got the answer about how to get the imagebitmap. This code is working
ImageView v2=((ImageView) vx.findViewById(R.id.remove));
Bitmap bm=((BitmapDrawable)v2.getDrawable()).getBitmap();

how to assign different activities to each item of the spinner?

I have a page which consist of a spinner and a submit button. What I want to achieve is when user selects an item in the list and click on submit, it should take him to an other layout having a webview. Each item in the spinner should open different .html page in the layout.
What I have now is the item is being selected from the spinner, but I'm not sure how to perform onclick listener to it...
code for main activity is here
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.beef);
addListenerOnButton();
addListenerOnSpinnerItemSelection();
}
public void addListenerOnSpinnerItemSelection(){
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
//get the selected dropdown list value
public void addListenerOnButton() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
btnSubmit = (ImageButton) findViewById(R.id.imageButton1);
btnSubmit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
/*Nothing as of now*/
//I need to call the ID of the selected item from the spinner here and start new activity
}
});
}
}
code of CustomOnItemSelectedListener is here
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
if (arg2 == 0) // First item selected
{
//Here I need to give an id for the .html file
}
else if (arg2 == 1) // Second
{
//Here I need to give an id for the .html file
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String selItem = arg0.get(arg2); // String representation of the selected item
if (arg2 == 0) // First item selected
{
}
else if (arg2 == 1) // Second
{
}
// etc
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}

Time/Date dialog. trying to add button. Eclipse Android

So I made an Activity with textViews, editTexts, date dialog and time dialog picker. plus a button.
I used this to get the Date/Time dialogs to work:
public class RainbowBookActivity extends Activity {
private static final int DATE_DIALOG_ID=1; // for date
private static final int TIME_DIALOG_ID=2; // for month
private int d,mo,y,h,m; // for date & month variables
Button b1,b2; // button objects
TextView e1,e2; // textview objects
// execution starts from here
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rainbowbook); // calling activity_rainbowbook.xml
e1=(TextView)findViewById(R.id.textview1); // getting textview1 id from activity_rainbowbook.xml
b1=(Button)findViewById(R.id.button1); // getting button id from activity_rainbowbook.xml
b1.setOnClickListener(new OnClickListener() // setting listener for button one
{
public void onClick(View arg0) {
// TODO Auto-generated method stub
showDialog(DATE_DIALOG_ID); // generating dialog box
}
});
final Calendar cal=Calendar.getInstance(); // allocating memory for calendar instance
d=cal.get(Calendar.DAY_OF_MONTH); // present date
mo=cal.get(Calendar.MONTH); // present month
y=cal.get(Calendar.YEAR); // present year
updateDate(); // update date
b2=(Button)findViewById(R.id.button2); // getting listener for button2
e2=(TextView)findViewById(R.id.textview2);
b2.setOnClickListener(new OnClickListener() // setting listener for button2
{
public void onClick(View arg0) {
// TODO Auto-generated method stub
showDialog(TIME_DIALOG_ID);
}
});
h=Calendar.getInstance().get(Calendar.HOUR); // getting present hour & minute
m=Calendar.getInstance().get(Calendar.MINUTE);
updateTime(); // updating time
}
public void updateTime()
{
e2.setText(new StringBuilder().append(h).append(':').append(m));
}
public void updateDate()
{
e1.setText(new StringBuilder().append(d).append('/').append(mo+1).append('/').append(y));
}
private DatePickerDialog.OnDateSetListener datelistener=new DatePickerDialog.OnDateSetListener()
{
public void onDateSet(DatePicker view,int year, int monthofyear, int day)
{
y=year;
mo=monthofyear;
d=day;
updateDate();
}
};
private TimePickerDialog.OnTimeSetListener timelistener=new TimePickerDialog.OnTimeSetListener()
{
public void onTimeSet(TimePicker view, int hourofday, int minute)
{
// TODO Auto-generated method stub
h=hourofday;
m=minute;
updateTime();
}
};
protected Dialog onCreateDialog(int id)
{
switch(id)
{
case DATE_DIALOG_ID:
return new DatePickerDialog(this,datelistener , y, mo, d);
case TIME_DIALOG_ID:
return new TimePickerDialog(this,timelistener,h,m,false);
}
return null;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_rainbowbook, menu);
return true;
}
}
Now I would some how like to get this in there so I can have a button that transferes info from the editTexts:
EditText inputName;
EditText inputRoom;
public void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rainbowbook);
inputName = (EditText) findViewById(R.id.editText1);
inputRoom = (EditText) findViewById(R.id.editText2);
Button btnNextScreen = (Button) findViewById(R.id.button3);
//Listening to button event
btnNextScreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//Starting a new Intent
Intent nextScreen = new Intent(getApplicationContext(), BookingsActivity.class);
//Sending data to another Activity
nextScreen.putExtra("name", inputName.getText().toString());
nextScreen.putExtra("room", inputRoom.getText().toString());
Log.e("n", inputName.getText()+"."+ inputRoom.getText());
startActivity(nextScreen);
}
});
}
I cant seem to figure this out and am hoping someone can help me. Thanks.
*
BookingsActivity:
public class BookingsActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bookings);
TextView Name = (TextView) findViewById(R.id.textView2);
TextView Room = (TextView) findViewById(R.id.textView3);
Button btnClose = (Button) findViewById(R.id.btnClose);
Intent i = getIntent();
// Receiving the Data
String name = i.getStringExtra("name");
String room = i.getStringExtra("room");
Log.e("Bookings", name + "." + room);
// Displaying Received data
Name.setText(name);
Room.setText(room);
// Binding Click event to Button
btnClose.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//Closing SecondScreen Activity
finish();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_bookings, menu);
return true;
}
}
maybe this in your BookingActivity
Bundle b = i.getExtras();
String name = b.getString("name", "default name");
String room = b.getString("room", "default room");
edit:
also, launche BookingActivity with startActivityForResult() so you can get data back from it later when you're done with it.
EDIT:
examples of startActivityForResult can be found How to return a result (startActivityForResult) from a TabHost Activity? and http://android.rahulblogs.com/android-startactivityforresult-example/

Android Dynamic Spinner Update

I'm working with Android and Spinners and I need some help. I have a class that creates two spinners and a button. The first spinner is for my category, my second is for my sub-category. What I am trying to do is dynamically update the second spinner (spinner2). I've been trying to use adapter2.clear() but that crashes android, with an error "unable to start activity componentinfo unsupported operation"
Here is my code:
public class MyClass extends MyBaseClass
{
int category;
int sub_category;
ArrayAdapter<String> adapter2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quizes);
//CATEGORY INFO
final String[] items1 = new String[] {"One", "Two", "Three"};
final Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, items1);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
//SUBCATEGORY INFO
final String[] items2 = new String[] {"SOne", "STwo", "SThree"};
final Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
adapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, items2);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
// Capture our button from layout
Button button = (Button)findViewById(R.id.button1);
// Register the onClick listener with the implementation above
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
startActivity(new Intent(MyClass.this, GoToOtherClass.class));
}
});
//SELECTOR CONTROL FOR SPINNER1 {CATEGORY}
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
MyClass.this.category = spinner1.getSelectedItemPosition();
//OTHER STUFF
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
//SELECTOR CONTROL FOR SPINNER2 {SUB-CATEGORY}
spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
MyClass.this.sub_category = spinner2.getSelectedItemPosition();
//OTHER STUFF
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
return true;
}
}
I understand about the .clear()/.add() methods, but anytime I try clear()/add() my program crashes, what do I need to do to fix things so I can change the spinner2 contents for my sub-category list? Any advice would help, as I have spent hours doing things such as:
Object t=adapter2.getitem(0);
spinner2.remove((String) t);
or adapter2.clear() and a few other tricks and I have no further ideas left. I am still learning android. I've tried looking at some other posts here on stackoverflow and google but was not sure how to get their ideas working.
After you change the contents of the second Spinner, you need to call adapter2.notifyDataSetChanged(). Without that call, the UI won't update with the new contents of the Spinner, and you could also have problems referencing things that don't exist anymore.
Try this Code..
public class MainActivity extends Activity {
Spinner sp1,sp2;
ArrayAdapter<String> adp1,adp2;
List<String> l1,l2;
int pos;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l1=new ArrayList<String>();
l1.add("A");
l1.add("B");
sp1= (Spinner) findViewById(R.id.spinner1);
sp2= (Spinner) findViewById(R.id.spinner2);
adp1=new ArrayAdapter<String> (this,android.R.layout.simple_dropdown_item_1line,l1);
adp1.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
sp1.setAdapter(adp1);
sp1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
pos=arg2;
add();
}
private void add() {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), ""+pos, Toast.LENGTH_SHORT).show();
switch(pos)
{
case 0:
l2= new ArrayList<String>();
l2.add("A 1");
l2.add("A 2");
adp2=new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_dropdown_item_1line,l2);
adp2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
sp2.setAdapter(adp2);
select();
break;
case 1:
l2= new ArrayList<String>();
l2.add("B 1");
l2.add("B 2");
adp2=new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_dropdown_item_1line,l2);
adp2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
sp2.setAdapter(adp2);
select();
break;
}
}
private void select() {
// TODO Auto-generated method stub
sp2.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Test "+arg2, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}

Categories