java - Android : How to Store the Value for a Variable in Strings.xml through coding -



java - Android : How to Store the Value for a Variable in Strings.xml through coding -

requirement : need store values of user id , password string values in string.xml, indeed taken user.

problem : far have seen solutions utilize shared preferences. though utilize shared preferences , values don't stored in string.xml

here, strings.xml

<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">sp</string> <string name="hello_world">hello world!</string> <string name="menu_settings">settings</string> <string name="id" ></string> <!-- fill in value java code, how ? --> <string name="pwd"></string> <!-- fill in value java code, how ? --> </resources>

layout page : activity_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" tools:context=".mainactivity" > <edittext android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_margintop="178dp" android:ems="10" android:text="id" > <requestfocus /> </edittext> <button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/edittext1" android:layout_centerhorizontal="true" android:layout_margintop="44dp" android:text="send" /> <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/text" android:layout_alignparentleft="true" android:layout_marginbottom="40dp" android:layout_marginleft="22dp" android:text="id" /> </relativelayout>

edtext.java

import android.app.activity; import android.content.sharedpreferences; import android.content.sharedpreferences.editor; import android.os.bundle; import android.util.log; import android.view.view; import android.widget.button; import android.widget.edittext; public class edtext extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); final edittext et=(edittext)findviewbyid(r.id.text); button = (button) findviewbyid(r.id.button); but.setonclicklistener(new view.onclicklistener(){ public void onclick(view arg0) { string s1 = ((edittext)findviewbyid(r.id.text)).gettext().tostring().trim(); //saving strings sharedpreferences prefs = getsharedpreferences("myprefskey", context.mode_private); editor editor = preferencemanager.getdefaultsharedpreferences(context).edit(); editor.putstring("s1", s1.gettext().tostring()); editor.commit(); //retrieving strings preferences sharedpreferences prefs2 = getsharedpreferences("myprefskey",context.mode_private); string s12 = prefs.getstring("s12", ""); //empty string default value } }); } }

thank time

i dont know thought static , cannot changed on run time.

you create work around here

first create class , add together public static string that

e.g

public class stringgen { public static string username public static string password }

set info perhaps log in

stringgen.username = *yourstring*

and phone call on app (activities , etc)

string myusername = stringgen.username

cheers

java android xml

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -