need help

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

need help

Postby mamimaye » Fri Feb 11, 2011 2:16 pm

Hello, hello; i'm a new android developer, I want to make an app where a user enter a matrix A and a vector b of the same lenght and my app will print x vector, for Ax=b! But I don't know how to collect the values typed by the user, nor how to save variables in main.xml! Can someone help me please?
mamimaye
Freshman
Freshman
 
Posts: 5
Joined: Tue Feb 08, 2011 8:56 pm

Top

Re: need help

Postby Shattered9 » Sat Feb 12, 2011 7:46 pm

If you're looking to keep this as simple as possible, you will probably do something like have the user put the data in through EditText boxes and print out via a TextView somewhere on the screen. I've thrown together an absolute bare minimum app that simply adds two integers together as an example.

The XML is fairly obvious :

Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Sum Here"
    android:gravity="center_horizontal"
    android:id="@+id/SumText"/>
<EditText android:id="@+id/Num1Field"
   android:layout_height="wrap_content"
   android:layout_gravity="center_horizontal"
   android:layout_width="150dp"
   android:hint="Num 1 Here"></EditText>
<EditText android:id="@+id/Num2Field"
   android:layout_height="wrap_content"
   android:layout_gravity="center_horizontal"
   android:layout_width="150dp"
   android:hint="Num 2 Here"></EditText>
<Button android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="center_horizontal"
   android:text="Add!"
   android:id="@+id/AddButton"></Button>
</LinearLayout>


And the actual Java code is simply :

Code: Select all
package net.shattered9.EditTextTest;

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

public class EditTextTest extends Activity {
   
   TextView sumView;
   EditText number1;
   EditText number2;
   int num1;
   int num2;
        int sum;

   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        Button addButton = (Button)findViewById(R.id.AddButton);
        addButton.setOnClickListener(new OnClickListener() {
         
         @Override
         public void onClick(View v) {
            sumView = (TextView)findViewById(R.id.SumText);
            number1 = (EditText)findViewById(R.id.Num1Field);
            number2 = (EditText)findViewById(R.id.Num2Field);
            num1 = Integer.parseInt(number1.getText().toString());
            num2 = Integer.parseInt(number2.getText().toString());
            sum = num1 + num2;
            sumView.setText(num1 + " + " + num2 + " = " + sum);
         }
      });     
    }
}


Obviously this is over simplified, and even a tad buggy (Never checks to see that what was entered were ints), but it gives the most basic way to get the values out. Of course in yours you will not simply be adding two numbers, and if I recall anything about working with matrices and vectors (Probably not, it's been a while :P ) you will also be using Floats instead of Integers. Hopefully this gives you some basic idea though.
Shattered9
Developer
Developer
 
Posts: 39
Joined: Sat Feb 12, 2011 4:41 pm

Re: need help

Postby mamimaye » Wed Feb 23, 2011 12:11 pm

Oh so sorry, i didn't know a that an answer for my question had been post!! i'll try to understand all this code and will make u know soon! Thanks a loooot!!
mamimaye
Freshman
Freshman
 
Posts: 5
Joined: Tue Feb 08, 2011 8:56 pm

Re: need help

Postby mamimaye » Wed Feb 23, 2011 12:45 pm

great, so helpful! i have seen a matrix android object in developer.android.com maybe I wont need to put as many textView and EditText as my matrix size! Else it seems to be laborious!!! Thanks again...
Regards
mamimaye
Freshman
Freshman
 
Posts: 5
Joined: Tue Feb 08, 2011 8:56 pm

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest