Saving text from TextView

Filippo Foladore

I'm new in programming on Android. What I want is to add to a LinearLayout some TextView by clicking a Button and saving the Text I've inserted in the TextView.

Here's my Java code:

public class MainActivity extends AppCompatActivity {
private LinearLayout mLayout;
private EditText mEditText;
private Button mButton;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mLayout = (LinearLayout) findViewById(R.id.linearLayout);
    mEditText = (EditText) findViewById(R.id.editText);
    mButton = (Button) findViewById(R.id.button);
    mButton.setOnClickListener(onClick());
    TextView textView = new TextView(this);
    textView.setText("New text");
}

private View.OnClickListener onClick() {
    return new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            mLayout.addView(createNewTextView(mEditText.getText().toString()));
        }
    };
}

private TextView createNewTextView(String text) {
    final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    final TextView textView = new TextView(this);
    textView.setLayoutParams(lparams);
    textView.setText("New text: " + text);
    return textView;
}

public void onDestroy(Bundle savedInstanceState) {
}

And my XML code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/linearLayout">
<EditText
    android:id="@+id/editText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Add+"
    />

The creation of the TextView works. So, when I insert text in the top EditText I want that the text is saved "somewhere" for being readable at the opening of the app. Obviously this for every TextView created.

Arsal Imam

You can simply use SharedPreferences to store and retrive text, You can use the below code to acheive your task,

I made a small changed in your createNewTextView method,

package com.package.name;

import java.util.ArrayList;

import org.json.JSONArray;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;

public class MainActivity extends Activity 
{
    private LinearLayout mLayout;
    private EditText mEditText;
    private Button mButton;

    private View.OnClickListener onClick() {
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mLayout.addView(createNewTextView(mEditText.getText().toString()));
            }
        };
    }

    //Create a field variable
    ArrayList<String> mCreatedText = new ArrayList<String>();

    private TextView createNewTextView(String text) {
        final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        final TextView textView = new TextView(this);
        textView.setLayoutParams(lparams);
        textView.setText("New text: " + text);
        mCreatedText.add(text);
        return textView;
    }

    private void savingText() 
    {
        SharedPreferences.Editor mEditor = PreferenceManager.getDefaultSharedPreferences(MainActivity.this).edit();
        mEditor.putString("created_text", new Gson().toJson(mCreatedText)).commit();
    }

    private ArrayList<String> getSavedContent()
    {
        ArrayList<String> strArray = new ArrayList<String>();
        SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
        try
        {
            String jsonArrayString = mPrefs.getString("created_text","");
            if (!jsonArrayString.equalsIgnoreCase(""))
            {
                JSONArray jsonArray = new JSONArray(jsonArrayString);
                for (int i = 0; i < jsonArray.length(); i++) {
                    strArray.add(jsonArray.getString(i));
                }
            }
        }
        catch(Exception exception)
        {
            exception.printStackTrace();
        }
        return strArray;
    }

    //Call this method from onCreate()
    private void createSavedTextViews()
    {//nhj
        this.mCreatedText = getSavedContent();
        for(int i = 0; i < this.mCreatedText.size(); i++)
        {
            mLayout.addView(restoreAllTextViews(this.mCreatedText.get(i)));
        }
    }
    private TextView restoreAllTextViews(String text)
    {
        final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        final TextView textView = new TextView(this);
        textView.setLayoutParams(lparams);
        textView.setText("New text: " + text);
        return textView;
    }
    @Override
    protected void onStop() {
        super.onStop();
        savingText();
    }
    //Here is your new onCreate
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rela);
        mLayout = (LinearLayout) findViewById(R.id.linearLayout);
        mEditText = (EditText) findViewById(R.id.editText);
        mButton = (Button) findViewById(R.id.button);
        mButton.setOnClickListener(onClick());
        createSavedTextViews();
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Saving text from TextView

From Dev

Saving a text from UIAlertView

From Dev

Get the text from a TextView

From Dev

Saving words from text into an array

From Java

Saving Info from CSV to Text

From Dev

Saving data from Text Field

From Dev

Saving text file from code

From Dev

Saving a text file from flash

From Dev

Get Selected Text from TextView

From Dev

Saving text input from shiny permanently?

From Dev

Saving list from python to text file in format

From Dev

Python : saving variables from a text file created

From Dev

Saving formatted text from textarea to database

From Dev

Text from EditText not saving in Shared Preferences

From Dev

Saving TextView contents in TableView

From Dev

Transferring text on overflow from one TextView to another TextView

From Dev

how to get text from textview using espresso

From Dev

Android: Adding text from textView to status bar

From Dev

Stopping ClickableSpan on a TextView from changing text color

From Dev

How to get TextView text from a custom Spinner

From Dev

getting text from a textview to display in a string in android

From Dev

Set Text from header textview widget

From Dev

How to change TextView Text from static method?

From Dev

How to change TextView Text from static method?

From Dev

How to get text from a textView in Haskell

From Dev

how to set a text of a textview from another activity

From Dev

Android: Adding text from textView to status bar

From Dev

How to get the text from a random set textview?

From Dev

Get text from Html styled TextView