Android/Java Help

Associate
Joined
8 Sep 2011
Posts
525
Location
Hiding from Google
Hello folks, need some help with a program I am trying to make in android

I hate programming so my skills are pretty null in it.

Anyways the App in question is a very basic app which converts kilometres to miles

So my GUI atm contains two text boxes, one to input kilometres, and the other to output the result as miles

I have a button called convert which when pressed I guess should have an on click listener, take the value from box 1 and multiply it by 1.6 and output it in the 2nd text box.

It's really simple only that I am crap at programming. if anyone has any pointers they could give me to implementing this it would be great. cheers.
 
Not Android, but Java code using Swing would be (I'm sure Android has it's own versions of this, just look through the API)

Code:
final JTextField inputField = new JTextField();
final JTextField outputField = new JTextField();

JButton convert = new JButton();

convert.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        String miles = inputField.getText();
        double km = convertToKM(Integer.parseInt(miles));
        outputField.setText(String.valueOf(km));
    }
});
...
...
private double convertToKM(int miles) {
    return miles * 1.6;
}

Obviously missed out a few steps here like adding the components to your container, error checking etc.
 
Last edited:
woops just noticed I was returning an int instead of a double in the convert method, two options in that case really

a) cast the return value like so, you will lose your decimal point

Code:
 return (int) miles * 1.6;

b) change the return type of the method to double, which is obviously better for a conversion like this.

It also assumed that the miles weren't a decimal value
 
Cheers PaulStat[/b}

Nearly there I think could anyone help me with the last bit?


I think i've got the code sorted that it reads from the kilometer box and does the calculation, I need to display it in the miles box but it's not working.

Code:
package org.uuj;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Activity2<ActionEvent> extends Activity {
	
	  /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);  //calling my layout from R
        
        
        final EditText kilo = (EditText) findViewById(R.id.editText3); 
      
        
        
        /*instigating my XML buttons and edit 
        text boxes*/
        
        
        
        
        final EditText mile = (EditText) findViewById(R.id.editText2);
        
        final Button convert = (Button) findViewById(R.id.button1);
        Button back = (Button) findViewById(R.id.button2);
        
        
        // creating my back button's action listener to take you back to the start page
        back.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent myIntent = new Intent (v.getContext(),AnroidsimpleappActivity.class);
				startActivityForResult(myIntent, 0);
				
				// TODO Auto-generated method stub
        
        
       // the onclick listener for the convert button, it takes values from the kilo box and 
			//	converts it to miles and outputs it to the miles box 
				
        convert.setOnClickListener(new View.OnClickListener() {
        	
    	   @SuppressWarnings("unused")
		public void actionPerformed(ActionEvent e) {
    		 
    		   Editable km1 = (Editable)kilo.getText();
    		   String km = km1.toString();
    		 
    		 int km2 = (Integer.parseInt(km));
    		 
    		 double answer = (km2/1.6);
    		 String answer1 = Double.toString(answer);
    		 
    		   mile.setText(answer1);
    		   
    		  
    		  
    		  
    	  }

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			
		}
    
    

    	  
    	  
       
    		 
    		  
    		  
    	  
    	   
       });
    }
    
    
        });
        
    }
    
}

I had to convert editable to string, then to an int, and then make the answer a double due to 1.6. then convert that to a string and then setText as the answer, but when I press the convert button nothing happens :(

Theres no syntax errors so I dunno :(

Anyone in Android development care to lend a hand? I'm stumped and my Lecturer isn't much help either.



That's what the app looks like.


Cheers for any input, its due in at 12PM tomorrow so any advice would be appreciated!
 
I'd be adding some debugging in around this code, i.e. print out the values it thinks it's got to the console

Code:
Editable km1 = (Editable)kilo.getText();
String km = km1.toString();
    		 
int km2 = (Integer.parseInt(km));
    		 
double answer = (km2/1.6);
String answer1 = Double.toString(answer);
    		 
mile.setText(answer1);
 
Back
Top Bottom