Monday, March 3, 2014

Pass Data Between two XML files in ANDROID using Java

In here i'm going to insert a text to edittext , and then click a button.Then the page move to another page and display the first text in the second page's text box

My first pages java file name is MainActivity.java and it's xml file name is activity_main.xml
in there I create a edittext(R.id.editText1) and a button (R.id.button1)

My secound pages java file name is Display.java it's xml file name is display.xml in there I creat a edittext(R.id.editText2)
----------------------------------------------------------------------------------------------------------
//this is the cord for the first java class (MainActivity.java)
package com.myapp.activity_01;


import android.inputmethodservice.ExtractEditText;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
    public static String st="com.myapp.activity_01";//story my package name
   
    EditText e1;
    Button b1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//
       
        e1=(EditText) findViewById(R.id.editText1);//get the  edittext xml  id  to java file
        b1=(Button) findViewById(R.id.button1);
     //get the  button xml  id  to java file
       b1.setOnClickListener(this);                           //set the on clicklistner
       
            }

    @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;
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Intent i = new Intent(MainActivity.this, Display.class);
        Toast.makeText(getApplicationContext(), "Button Clicked! success!!!! ", Toast.LENGTH_LONG).show();
                //to display massage as the button is click
        String s=e1.getText().toString();//get the edittext's content to string variable


        i.putExtra(st,s);//pass the string value to the new activity
        startActivity(i);
//start new activity
       
    }

}


-------------------------------------------------------------------------------------------------------------------
//this is the cord for the first java class (Display.java)

package com.myapp.activity_01;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;

public class Display extends Activity {
   
    EditText e1;
   
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display);
    //display the layout       
        e1=(EditText) findViewById(R.id.editText2);
//get the  edittext xml  id  to java file
        Intent i=getIntent();
        String s1=i.getStringExtra(MainActivity.st);
//get the pass string , to string varlable
        e1.setText(s1);//set the passed text value for the edit text
       
    }
   

}

No comments:

Post a Comment