0

I am trying to save the text entered in edittext from an activity and send it to another activity so that it can be displayed in a textview which is invisible at first. So pls help me..

This is my first xml. main.xml

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

        <EditText
        android:id="@+id/editText5"
        android:layout_width="70dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="12dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:inputType="number" />

        <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="49dp"
        android:text="@string/save" />

     </RelativeLayout>

This is my second xml.

next.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >          

    <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/imageView1"
    android:layout_toRightOf="@+id/imageView1"
    android:text="Medium Text"
    android:visibility="invisible"
    android:textAppearance="?android:attr/textAppearanceMedium" />
   </RelativeLayout>

This is my main actvity code. MainActivity.java

   public class MainActivity extends Activity {

    public SharedPreferences savedData;
    private Button mbtn_save;
    private EditText medit_currency;
    public String s1;
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
            mbtn_save=(Button)findViewById(R.id.button1);
            medit_currency=(EditText)findViewById(R.id.editText5);
            savedData=PreferenceManager.getDefaultSharedPreferences(this);

       mbtn_save.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            s1=medit_currency.getText().toString();
            savePreference(s1,s1);
            Intent i=new Intent(MainActivity.this,Next.class);
            startActivity(i);
        }
    });
       public void savePreference(String key,String value)
{
    savedData=getPreferences(MODE_PRIVATE);
    SharedPreferences.Editor e=savedData.edit();
    e.putString(key, value);
    e.commit();
    Toast.makeText(MainActivity.this, "Saved", Toast.LENGTH_SHORT).show();

}
      public void showPreference(String key)
{
    savedData=getPreferences(MODE_PRIVATE);
    String text=savedData.getString(key, "");
}
}}

This is my second activity

Next.java

public class Next extends Activity {
MainActivity ma;
private TextView mtext_one;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.next);
    mtext_one=(TextView)findViewById(R.id.textView1);
    ma=new MainActivity();
    ma.savedData=getPreferences(MODE_PRIVATE);
    mtext_one.setVisibility(CONTEXT_INCLUDE_CODE);
    mtext_one.setText(ma.savedData.getString("10", ""));
}



}

I cant get the text displayed in Next.java once i save the preference and move to another activity.. help me..

Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
dilip
  • 63
  • 2
  • 10

6 Answers6

1

In MainActivity.java, pass the value to second activity through the intent as below:

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    s1=medit_currency.getText().toString();
    savePreference(s1,s1);
    Intent i=new Intent(MainActivity.this,Next.class);
    // add below line
    intent.putExtra("s1", s1);
    startActivity(i);
}

In Next.java, in onCreate, you can use:

Bundle bundle = getIntent().getExtras();
String s1 = bundle.getString("s1");
Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
1

Use this one:

    SharedPreferences sp = getSharedPreferences("Key", Activity.MODE_PRIVATE);
    SharedPreferences.Editor edt = sp.edit();
    edt.putString("Name", "your Edit Text Value");
    edt.commit();

retrieve in your New Activity:

    SharedPreferences sp = getSharedPreferences("Key", Activity.MODE_PRIVATE);
    String name = sp.getString("Name", "");
Piyush
  • 18,895
  • 5
  • 32
  • 63
0

mtext_one.setText(ma.savedData.getString("10", ""));

Put key which u are using to save the prefrence. so, it would be mtext_one.setText(ma.savedData.getString(key, ""));

Bhoomika Brahmbhatt
  • 7,404
  • 3
  • 29
  • 44
0

It is better to use intent extras instead of sharedPreference.

Below is the example of using intent extras.

In your first activity(A):

Intent i = new Intent(A.this, B.class);
i.putExtra("someName", variableThatYouNeedToPass);
startActivity(i);

In your second activity(B):

Bundle extras = getIntent().getExtras();
int fetchedVariable = extras.getInt("someName");
Sanket
  • 3,094
  • 1
  • 16
  • 19
0

Open SharedPreference like this:

SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);

This will get the apps default shared preference.

Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64
0

For putting text entered to EditText in sharedPrefernces

SharedPreferences spppp = getSharedPreferences("tab", 0);
SharedPreferences.Editor editors = spppp.edit();
editors.putString("for", "you_value");
editors.commit();

For getting value on another activity

SharedPreferences spppp = getSharedPreferences("tab", 0);
String get_value = spppp.getString("for" , "");

Cheers

Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
AndroidHacker
  • 3,596
  • 1
  • 25
  • 45