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

Apps How to Put EditText Input Into a Variable?

BubbaD123

Lurker
For the weather app I am building, I have two EditText fields.. one that prompts for latitude, and another for longitude. I have the following XML code:

Java:
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:imeOptions="actionSend"
        android:ems="10"
        android:id="@+id/latInput"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="182dp"
        android:text="Please enter latitude"/>

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:imeOptions="actionSend"
        android:ems="10"
        android:id="@+id/longInput"
        android:layout_below="@+id/latInput"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="85dp"
        android:text="Please enter longitude"/>

As you can see, I have numberDecimal as the input type(because a proper latitude and longitude will need a '.' somewhere within the range of numbers) and imeOptions is set to ActionSend so the value is stored when the user enters their data and presses 'send' using the soft keyboard.

I am confused, though, on how to put the user input into a variable that I can use elsewhere in the app(I need it for appending it to a request to my weather API). I know that it will need to be stored in a double, but how do I exactly do that? If someone could tell me the proper code to put into the java file, and exactly where in the java file, I would greatly appreciate it. I will need to use those two values in a different activity, if that makes a difference.

Thank you guys! Have a great week!
 
So you're saying you don't know how to declare a variable in Java?

Please step away from the keyboard, and pick up the nearest Java for beginners book ;)

But seriously, if you don't know about variable visibility and scope, you would do yourself a big favour by reading up on these concepts. If you wish to pass a variable to another part of your code, this is usually done using a method parameter.
Passing information to other Activities is done using an Intent.
You can also make your variable available globally by declaring it as 'public static', although not really a good practice, as it breaks encapsulation.
 
Last edited by a moderator:
So you're saying you don't know how to declare a variable in Java?

Please step away from the keyboard, and pick up the nearest Java for beginners book ;)

But seriously, if you don't know about variable visibility and scope, you would do yourself a big favour by reading up on these concepts. If you wish to pass a variable to another part of your code, this is usually done using a method parameter.
Passing information to other Activities is done using an Intent.
You can also make your variable available globally by declaring it as 'public static', although not really a good practice, as it breaks encapsulation.

Thank you for your reply. I'm aware of visibility and scope, and am pretty comfortable in Java, I just don't know how to assign dynamically entered input from an EditText field in an Android application into a Java variable. They don't teach that in beginner Java books. I ordered a few beginner Android books from the library but they are currently on hold.

I only mentioned the 'using the two values in a different activity' part in case it was best practice to assign the EditText input to the variable in a slightly different way if it were to be used in another activity.
 
Thanks, LV! Tell a brotha how this looks:

Java:
public class DataInputActivity extends ActionBarActivity {

    public static double lat;
    public static double longitude;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data_input);

        EditText latNum = (EditText)findViewById(R.id.latInput);
        EditText longNum = (EditText)findViewById(R.id.longInput);

        lat = Double.parseDouble(latNum.getText().toString());

        longitude = Double.parseDouble(longNum.getText().toString());

        Button switchButton = (Button) findViewById(R.id.weatherButton);

        switchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(DataInputActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }

}

The switchButton is what takes the user to the next activity :-) I just hope the 'actionSend' attribute of my latInput and longInput will actually hold the input content without any added code. The documentation on that wasn't clear.

Wish me luck, LV!
 
Back
Top Bottom