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.
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.
Comments