• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps getAltitude() help

Marker88

Newbie
i want to have it so when you press a button it puts it into a textview so far i got this
Code:
package Android.sdk;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.location.Location;

public class Android extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final Button button = (Button) findViewById(R.id.Button);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                TextView tv = new TextView(this);
                double altitude = 0.0;
                Location.getAltitude() altitude = new Location.getAltitude();
               tv.setText(altitude);
                // Perform action on click
            }
        });

    }
}

yeah, what do i need to change?
 
add a textview to the main layout and refer to it the same way you did with the button
TextView tv = (TextView) findViewById(R.id.text);

then you gotta convert your value you wanna post to a String and have
tv.setText(string_value);
 
im kinda new so you have to go slow, how do i turn getaltitude() into a string? heres my new script
Code:
package Android.sdk;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.location.Location;

public class Android extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    	final TextView tv = new TextView(this);
        final Button button = (Button) findViewById(R.id.Button);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                tv.setText("hi");
                setContentView(tv);
                // Perform action on click
            }
        });

    }
}
it runs, but how do i add it so "hi" shows up as your altitude?
 
I've never used getAltitude() but assuming it returns a a normal data type you can do this

Code:
tv.setText(altitude + "");
 
It also depends on your Location Service. First of all this doesn't make much sense

Code:
Location.getAltitude() altitude = new Location.getAltitude();

You should do something like this instead:

Code:
LocationProvider locationProvider = LocationManager.GPS_PROVIDER;

Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);

lastKnownLocation.getAltitude();

This requires that your device's GPS is turned on! Which is required anyway, as you cannot get any real altitude readings from a network based location.

To transfer the return value of .getAltitude() , which returns a Double value, you could also use this:

Code:
alt = Double.toString(lastKnownLocation.getAltitude());

Way more useful stuff on this here:

Obtaining User Location | Android Developers
 
so i put in
Code:
        String locationProvider = LocationManager.GPS_PROVIDER;

Location lastKnownLocation = LocationManager.getLastKnownLocation(locationProvider);

lastKnownLocation.getAltitude();
but no matter where i put it it gives the error non-static content cant be referenced in static content
 
>it gives the error non-static content cant be referenced in static content
Maybe reading and learning the basic of Java Programming for Android will help you a lot.
 
Back
Top Bottom