Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8693233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:30:14+00:00 2026-06-13T00:30:14+00:00

I have an activity setup with an edittext field and an add button. Essentially

  • 0

I have an activity setup with an edittext field and an add button. Essentially when the user makes changes to the edittext and hits Add, a new textview should be created with the edittext field. In addition i want the textviews created to be available even after i leave the screen or activity. The problem i have is that when i click add, a default values shows in the newly created textview each time, and when i leave the activity, all the textviews are gone altogether. Any suggestions?

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.adddocnote);
    etDocNote = (EditText) findViewById(R.id.editDocNote);
    btnAdd1 = (Button) findViewById(R.id.btnAdd1);
    nLayout = (LinearLayout) findViewById(R.id.linearLayout);
    TextView tvNote = new TextView(this);
    tvNote.setText("");
    btnAdd1.setOnClickListener(onClick());

        etDocNote.setOnKeyListener(new View.OnKeyListener() {
           public boolean onKey(View v1, int keyCode1, KeyEvent event1) {
                if ((event1.getAction() == KeyEvent.ACTION_DOWN) &&
                        (keyCode1 == KeyEvent.KEYCODE_ENTER)) {
                      getSharedPreferences("myprefs", 0).edit().putString("etDocNote", etDocNote.getText().toString()).commit();
                      return true;
                }
                return false;
            }
        });
}

private OnClickListener onClick() {
    // TODO Auto-generated method stub
    return new OnClickListener(){

        public void onClick(View v) {
            // TODO Auto-generated method stub
            String newDocNote = getSharedPreferences("myprefs", 0).getString("etDocNote", "" );
            nLayout.addView(createNewTextView(newDocNote));
        }

    };
}
private TextView createNewTextView(String newText) {
    // TODO Auto-generated method stub

    LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    TextView tvNote = new TextView(this);
    tvNote.setLayoutParams(lparams);
    tvNote.setText(newText);
    return tvNote;
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:30:15+00:00Added an answer on June 13, 2026 at 12:30 am

    So your question has two parts:

    1. How do you correctly add the textView with the text from the editText:
      Right now you have a timing issue. You are trying to save in shared preferences the text when someone punched keys. This is not necessary since you can retrieve the editText contents when the add button is clicked.

    Here is what you need to do: Remove setOnKeyListener and change your onClick() function to be

    return new OnClickListener(){
    
        public void onClick(View v) {
            // TODO Auto-generated method stub
    
            String value = etDocNote.getText().toString();
    
            nLayout.addView(createNewTextView(value));
    
            // Save to shared preferences
           getSharedPreferences("myprefs", 0).edit().putString("etDocNote", value).commit();
        }
    
    };
    
    1. How do you restore your view when you return to the activity.
      Since your text is now saved in shared preferebnces. In your oncreate, you need to retrieve and restore your TextViews.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So i have this test Activity with 3 elements: TextView, EditText, Button. When user
I have a tab setup as an Activity extension using API8 max. In this
I have a setup view where the user can enter their name and email
I have a a listview with each row having a text field and edittext
I have an Activity (not a ListActivity, although I should probably convert my code
In my app I have couple activities. How to setup that every time user
I have a simple activity with only one edittext which was set to http://
I've setup my activity to have a listview-> Listview-> listview-> table layout -> etc.
I have 2 Activity files. 1 activity is for Setup and other is for
I have a three-page/Activity setup/configuration piece to my app. I want to have two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.