andro prabu
andro prabu

Reputation: 364

Screen switching in android eclipse

Image_1

The above emulator image is just summing up the given two values and showing the result on the below textview which is on the same screen.

Here my need is i want to show only the result on another screen's textview.How to achieve this? what source i have to add on my source?

public class CheckingActivity extends Activity {
 Button     button1;
 EditText   txtbox1,txtbox2;
 TextView tv;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);


     txtbox1=  (EditText) findViewById(R.id.editText1);
     button1 = (Button) findViewById(R.id.button1);
     tv = (TextView) findViewById(R.id.textView5);
     txtbox2=  (EditText) findViewById(R.id.editText2);
     button1.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) 
    {
         String a,b;
         Integer vis;
         a =  txtbox1.getText().toString();
         b =  txtbox2.getText().toString();    
         vis =  Integer.parseInt(a)+Integer.parseInt(b);
         tv.setText(vis.toString());
        }
    });

Thanks a lot!.

Upvotes: 1

Views: 368

Answers (2)

GAMA
GAMA

Reputation: 5996

1.) Replace your main activity with this:

public class CheckingActivity extends Activity {
 Button     button1;
 EditText   txtbox1,txtbox2;
 TextView tv;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);

 txtbox1=  (EditText) findViewById(R.id.editText1);
 button1 = (Button) findViewById(R.id.button1);
 tv = (TextView) findViewById(R.id.textView5);
 txtbox2=  (EditText) findViewById(R.id.editText2);
 button1.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) 
{
     String a,b;
     Integer vis;
     a =  txtbox1.getText().toString();
     b =  txtbox2.getText().toString();    
     vis =  Integer.parseInt(a)+Integer.parseInt(b);
     //tv.setText(vis.toString());
     Intent i = new Intent(getApplicationContext(),ResultActivity.class);
     i.putExtra("sum",vis.toString());
     startActivity(i);
    }

3.) Add one xml layout file resultview

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tvsum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
            android:text="Sum : " />

    <TextView
        android:id="@+id/tvres"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/tvsum"/>
</RelativeLayout>

4.) Finally add in your manifest:

<activity android:name=".ResultActivity" />

Hope it helps!!

EDIT

public class ResultActivity extends Activity {
TextView tv;
String result;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.resultview);
Bundle extras = getIntent().getExtras();
if (extras != null) {
    result = extras.getString("sum");
    }
tv=(TextView) findViewById(R.id.tvres);    
tv.setText(result);
  }
 }

Upvotes: 4

Ofir A.
Ofir A.

Reputation: 3162

If I understand you correctly you want to show a new activity with the result value. If so, you can do this by create a new activity and pass to this activity an integer value symbols that value.

You can do this like this:

In CheckingActivity activity:

public void onClick(View v) 
{
     String a,b;
     Integer vis;
     a =  txtbox1.getText().toString();
     b =  txtbox2.getText().toString();    
     vis =  Integer.parseInt(a)+Integer.parseInt(b);
     Intent in = new Intent(this, B.class);
     in.putExtra("output_value", vis);
     startActivity(in);

    }
});

Xml for your new activity:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        android:id="@+id/txt_sum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Output: "/>

    <TextView
        android:id="@+id/txt_value"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/txt_sum"/>
</RelativeLayout>

In the new activity:

    Bundle extras = getIntent().getExtras();
    int value = extras.getInt("output_value");

    TextView output =  (TextView) findViewById(R.id.txt_value);
    output.setText(value);

Upvotes: 2

Related Questions