2

I don't know how to find the value from the EditTextPreference. Like when i write a number in Edittextpreference I want the number shows up in my Textview "numTxt" in my fragment firstlayout xml :(.

Blockquote The base for the settings menu is the "Settings Activity" where you get in Android studio, new>Activity>Settings Activity.so in the xml pref_general.xml is where the EditTextPreference is.

Please look at the imgur photos where the textfield is going to be edited. Image-imgur

This is the preference xml file.

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <SwitchPreference
        android:defaultValue="true"
        android:key="example_switch"
        android:summary="@string/pref_description_social_recommendations"
        android:title="@string/pref_title_social_recommendations" />

    <!-- NOTE: EditTextPreference accepts EditText attributes. -->
    <!-- NOTE: EditTextPreference's summary should be set to its value by the activity code. -->




    <EditTextPreference
        android:capitalize="words"
        android:defaultValue="@string/pref_default_display_name"
        android:inputType="textCapWords"
        android:key="example_text"
        android:id="@+id/textnrisettings"
        android:maxLines="1"
        android:selectAllOnFocus="true"
        android:singleLine="true"
        android:title="@string/pref_title_display_name" />

    <!-- NOTE: Hide buttons to simplify the UI. Users can touch outside the dialog to
         dismiss it. -->
    <!-- NOTE: ListPreference's summary should be set to its value by the activity code. -->
    <ListPreference
        android:defaultValue="-1"
        android:entries="@array/pref_example_list_titles"
        android:entryValues="@array/pref_example_list_values"
        android:key="example_list"
        android:negativeButtonText="@null"
        android:positiveButtonText="@null"
        android:title="@string/pref_title_add_friends_to_messages" />

</PreferenceScreen>

And here is the java file of the first layout xml

public class FirstFragment extends Fragment {



    private View view;

    public FirstFragment(){


    }
    Button sendSMS;
    Button sendSMSaon;
    Button sendSMSaoff;
    Button sendSMSrela1;
    Button sendSMSrela2;
    EditText msgTxt;
    EditText aonTxt;
    EditText aoffTxt;
    EditText rela1txt;
    EditText rela2txt;
    Button taframnummer;

//Down below in Textview numTxt is where the value from the Edittextpreference will be shown

    TextView numTxt;
    



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.first_layout, container, false);



        sendSMS = (Button)view.findViewById(R.id.skicka);
        sendSMSaon = (Button)view.findViewById(R.id.skickaaon);
        sendSMSaoff = (Button)view.findViewById(R.id.skickaaoff);
        sendSMSrela1 = (Button)view.findViewById(R.id.skickarela1);
        sendSMSrela2 = (Button)view.findViewById(R.id.skickarela2);

        msgTxt = (EditText)view.findViewById(R.id.Textmeddelande);
        numTxt = (TextView)view.findViewById(R.id.nummer);
        aonTxt = (EditText)view.findViewById(R.id.aon);
        aoffTxt = (EditText)view.findViewById(R.id.aoff);
        rela1txt = (EditText)view.findViewById(R.id.rela1txt);
        rela2txt = (EditText)view.findViewById(R.id.relä2txt);
        taframnummer = (Button) view.findViewById(R.id.taframnummer);




        msgTxt.setVisibility(View.INVISIBLE);
        aonTxt.setVisibility(View.INVISIBLE);
        aoffTxt.setVisibility(View.INVISIBLE);
        rela1txt.setVisibility(View.INVISIBLE);
        rela2txt.setVisibility(View.INVISIBLE);




        sendSMSaoff.setOnClickListener(new View.OnClickListener() {
                                           @Override
                                           public void onClick(View v) {
                                               String mymsgaoff = aoffTxt.getText().toString();
                                               String theNumber = numTxt.getText().toString();
                                               sendMsg(theNumber, mymsgaoff);
                                           }

                                       }

        );

        sendSMSaon.setOnClickListener(new View.OnClickListener() {
                                          @Override
                                          public void onClick(View v) {
                                              String mymsgaon = aonTxt.getText().toString();
                                              String theNumber = numTxt.getText().toString();
                                              sendMsg(theNumber, mymsgaon);
                                          }

                                      }







        );
        sendSMS.setOnClickListener(new View.OnClickListener() {
                                       @Override
                                       public void onClick(View v) {
                                           String myMsg = msgTxt.getText().toString();
                                           String theNumber = numTxt.getText().toString();
                                           sendMsg(theNumber, myMsg);
                                       }

                                   }
        );
        sendSMSrela1.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                String myMsgrela1 = rela1txt.getText().toString();
                                                String theNumber = numTxt.getText().toString();
                                                sendMsg(theNumber, myMsgrela1);
                                            }

                                        }
        );
        sendSMSrela2.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                String mymsgrela2 = rela2txt.getText().toString();
                                                String theNumber = numTxt.getText().toString();
                                                sendMsg(theNumber, mymsgrela2);
                                            }

                                        }







        );

        return view;
    }



    private void sendMsg(String theNumber, String myMsg)
    {
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(theNumber, null, myMsg, null, null);

    }


}
Tim
  • 547
  • 1
  • 7
  • 17
  • Really hope som1 can link a tutorial or something. This is almost the last part of my app! The project has been on and off for four months... Just because of the setting menu. Thing is i only need 1 option where the user can "save" a number to the textfield in the settings menu... :( – Tim Sep 13 '16 at 19:00
  • Which specific value/variable in your code you want to save and write to textField? Also check: http://stackoverflow.com/questions/5821051/how-to-display-the-value-of-a-variable-on-the-screen http://stackoverflow.com/questions/23024831/android-shared-preferences-example – Fr333du Sep 14 '16 at 06:44
  • Hi thanks for your answer. This is going to be an SMS app so the value is going to be numbers (phone number). And i can write the number in the edit text preference but i just want it to be shown to a textfield in the "normal layout" the mainscreen/fragment. – Tim Sep 14 '16 at 10:34
  • I do not see any text field in your code, if you want to copy the value to the other activity (screen) you have to save the number using `SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit(); editor.putString("numTxt", numTxt.getText()); editor.commit();` and retrieve the number in other activity using `SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); String restoredText = prefs.getString("numTxt", null); TextView textView = (TextView) findViewById(R.id.textViewName); //your textField? textView.setText(restoredText);` – Fr333du Sep 14 '16 at 10:44
  • I will try the code! Sorry! for now the textfield is actually, " EditText numTxt; " . but i will change it to textview. – Tim Sep 14 '16 at 15:55
  • Try it and please write in comments if you have any problems. Added my comment as a potential answer. – Fr333du Sep 15 '16 at 06:30

1 Answers1

2

I do not see any text field in your code, if you want to copy the value to the other activity (screen) you have to save the number using

SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit(); 
editor.putString("numTxt", numTxt.getText()); 
editor.commit(); 

and retrieve the number in other activity using

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); String restoredText = prefs.getString("numTxt", null); 
TextView textView = (TextView) findViewById(R.id.textViewName); //your textField?
textView.setText(restoredText);    
//or with EditText 
//EditText editText = (EditText) findViewById(R.id.editTextName);
//editText.setText(restoredText);
Fr333du
  • 191
  • 13
  • Okay @Fr33du I updated the question, and the imgur image!. think im getting a little more wiser, How can i save the value from a Edittextpreference? I wanna put the value from the EditTextPreference to the TextView. Thank you for helping me! – Tim Sep 15 '16 at 18:55
  • You can thank me by accepting the answer if it proved useful :) – Fr333du Sep 16 '16 at 05:49
  • Ofcourse! I tried but I am a new member so I cant upvote it yet! But I will in time! :) – Tim Sep 16 '16 at 07:12
  • You can accept it by clicking "tick" icon under upvote if you found my answer most helpful :) – Fr333du Sep 16 '16 at 07:20
  • The more you know ;) Thanks again! :) – Tim Sep 16 '16 at 11:35