Question

I am just starting writing Android apps. I have done some beginner's tutorials, but I'm having trouble with the next step for which I could not find any helpful answer.

I want to write an app that computes the value of a complex function which depends on a three parameters. This works if I put everything on a single view: For each parameter one TextField (with the parameter name) plus one EditText field to enter the value. At the bottom there is one Button, and when this is clicked, the result is displayed in another TextField.

But now I want improve the layout (plus I want to learn how to deal with more complex structures): Two of the parameters are usually just set once and then kept at their values. So I decided to move these two parameters to a different view.

No I'm looking for recommendations (and hopefully links example code): Which structure should I use? It seems that I do not need to use different activities, but one activity with different views should do the job. ViewSwitcher sounds reasonable, since I only require two different views in this case. On the other hand ViewFlipper may be preferable, since I can reuse this later for other projects with multiple views. I also read that ViewPager allows to swipe between different views.

How can I read an EditField if this is on a different view? I tried to use ViewSwitcher, and it worked when I added an additional button to switch to a second view. But when I moved the TextField and EditField for parameters one and two to the second view, the app does not run on the emulator, just stating "error in app" (while Eclipse shows no errors). From this, I guess that I have to do some additional work to pass data in EditText between different views. I have not found any examples for this - can anybody give me some advice/examples?

Any help is appreciated


I figured that my initial description was maybe not too helpful without any code. Here is my layout

<?xml version="1.0" encoding="utf-8"?>
<ViewSwitcher  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/viewSwitcher1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

   <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" >

    <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:orientation="vertical" >
    <TextView android:id="@+id/pg1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1st view" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    <TextView android:id="@+id/v1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="value1" />
    <EditText android:id="@+id/val1" 
        android:text="2.0"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:inputType="numberDecimal" />
    </LinearLayout>

        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    <TextView android:id="@+id/v2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="value2" />
    <EditText android:id="@+id/val2" 
        android:text="3.0"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:inputType="numberDecimal" />
    </LinearLayout>

    <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal" >
    <Button android:layout_width="wrap_content" 
    android:id="@+id/calc"  
    android:layout_height="wrap_content" 
    android:text="2*Val1 + Val2 =" />
    <TextView android:id="@+id/res" 
        android:text="" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textStyle="bold" 
        android:textSize="24sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    <Button android:id="@+id/b1"
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content" 
        android:text="switch view 1" />    
    </LinearLayout>
  </LinearLayout>

<!--   next view     -->  

   <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" >

    <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:orientation="vertical" >
    <TextView android:id="@+id/pg2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="View 2" />
    </LinearLayout>

   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal" >
    <Button android:id="@+id/b2"  
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="switch view 2" />    
    </LinearLayout>  
   </LinearLayout>

</ViewSwitcher>

and here is my main activity:

package com.example.testas2;

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

public class MainActivity extends Activity {

    ViewSwitcher switcher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        switcher = (ViewSwitcher)findViewById(R.id.viewSwitcher1);
        initControls(); 
    }

      private void initControls()
      {  
          Button calculate=(Button)findViewById(R.id.calc);
          calculate.setOnClickListener(new Button.OnClickListener()
          {public void onClick (View  v) { calculate(); }});

          Button b1=(Button)findViewById(R.id.b1);
          b1.setOnClickListener(new Button.OnClickListener()
          {public void onClick (View  v) { switcher.showNext(); }});

          Button b2=(Button)findViewById(R.id.b2);
          b2.setOnClickListener(new Button.OnClickListener()
          {public void onClick (View  v) { switcher.showPrevious(); }});

          calculate();
      }

      private void calculate()
      {
          EditText val1=(EditText)findViewById(R.id.val1);
          EditText val2=(EditText)findViewById(R.id.val2);
          TextView res=(TextView)findViewById(R.id.res);
          Double Val1=Double.parseDouble(val1.getText().toString());
          Double Val2=Double.parseDouble(val2.getText().toString());
          Double result = 2*Val1+Val2;
          res.setText( String.format( "%.3f", result ) );
      }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

This version is working: - when pushing the first button, it computes: result = 2 * value1 + value2) - when pushing the button "switch view 1", it goes to the second view - on the second view, when pushing the button "switch view 2", it goes back to view 1

But I can't get the following two pieces to work: 1) I would like to move the "switch view 1" button to the top of the first view, but this gives an error in the emulator ("Unfortunately testas2 has stopped"). 2) I would like to move the input of the second value to the second view, but this gives the same error in the emulator.

What am I doing wrong? Why does the order of the elements in the layout matter, and how can I modify my code to make it work?

Was it helpful?

Solution 2

I just did "Project > Clean", restarted the app in the emulator, and now it works!
In other words, the solution for the original question is trivial (in fact there was no real problem). One can simply move elements in the layout file (TextView, EditText, Button) within a view or between views. The posted activity works always and the data (from the different TextView and EditView fields) is accessible everywhere in the code. Therefore, no explicit "transfer" of data between views is required.
Only after moving elements in the layout file one should do "Project > Clean".

OTHER TIPS

Example code -

TextView tField= (add cast here)findViewById(R.id.editText)

tfield.getText();

The above answer can be improved if you post some code of yours because findViewById at times depends on the container view and the above code will not work then as it will look for the textfield in the default view.

In your current Activity, create a new Intent:

Intent i = new Intent(getApplicationContext(), NewActivity.class);
i.putExtra("new_variable_name","value");
startActivity(i);

Then in the new Activity, retrieve those values:

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String value = extras.getString("new_variable_name");
}

For more info follow this link

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top