How to get and set string Resource values in android

52,984

Solution 1

You cant set a string, its static, and cant be changed at run-time, what you want to do is something like this

        Calendar cal = Calendar.getInstance();
        String date = cal.get(Calendar.DAY_OF_MONTH) + "/" + cal.get(Calendar.MONTH) + "/" + cal.get(Calendar.YEAR);
        setTitle(date);

Your trying to set the title as the date right?

To have two titles it is a bit harder, first you need a layout, call this custom_title_1

This is the xml code for that layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/screen"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView android:id="@+id/left_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="@string/custom_title_left" />
    <TextView android:id="@+id/right_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="@string/custom_title_right" />
</RelativeLayout>

Then in your code before you call the UI you need to call this:

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

then set you content view

then call this which will set the title:

getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_1);

Now you can find the two textViews and set the text:

TextView leftText = (TextView) findViewById(R.id.left_text);
TextView rightText = (TextView) findViewById(R.id.right_text);

Calendar cal = Calendar.getInstance();
String date = cal.get(Calendar.DAY_OF_MONTH) + "/" + cal.get(Calendar.MONTH) + "/" + cal.get(Calendar.YEAR);
String app_name = getString(R.string.app_name);
leftText.setText(app_name);
rightText.setText(date);

Solution 2

Try like this

String temp = getResources().getString(R.string.Please);

Solution 3

Try this.

String value = getResources().getString(R.string.Please);

Solution 4

 String str = getResources().getString(R.string.Please);

Solution 5

String resources are not views. To use a string resource you would do

@string/Please
Share:
52,984
Trikaldarshiii
Author by

Trikaldarshiii

No Me, No Life Know Me, Know Life Coding is my passion, whether it is hardware or software. I am for sustainability, trying as best as possible to create and innovate to make how I live productive so the future generations can live without fear of a non sustainable future. Stack Overflow makes me a better programmer while I can offer my own help to make others better programmers. Last but not the least I am a common man. Just, another common man. And now, I wont sit quiet, I will do whatever I can... I cannot reach publications, I will create my own way of publicity, I cannot reach their government, I will make sure, my message is reached. I cannot make my future anymore but, this is not the future, I will give to my future generation. I am a hacker and nothing can compromise me, not any more... I will fight, I will destroy, I will hit and hit it hard, where it hurts the most; I am not a Warrior with Guns but Brains, I am called, an Indian Cyber Warrior

Updated on August 12, 2020

Comments

  • Trikaldarshiii
    Trikaldarshiii over 3 years

    I've declare a String:

    <string name="Please">Please Select</string>
    

    in the res/values/Strings.xml. Now I want to access this String value but what I can get can go till id not value like:

     int i = findViewById(R.string.Please);
    

    But what I want is just the value of the string Please.

    Thanks for all of your answer but my problem is a little bigger

    What I wanted to do is just to display date with app_name

    from all your answers i can get the app_name String but whether I can set This app_name with current date

    <string name="app_name">My app</string>
    

    and also I'm to set my in this way

    My app(in Left alignment)             Date (in right aligment)
    

    and also on start of first activity i want to set app_name and not on every activity;

    I think it may work let me try

    setResource().setString(R.string.Please);
    

    but no setResource() method sorry.

  • Trikaldarshiii
    Trikaldarshiii about 12 years
    you got wrong i did like this but what i actually wanted to set the strings stored in strings.xml under res/strings/ there you have found string app_name what i wanted to set this from my code don't hurry read whole question
  • FabianCook
    FabianCook about 12 years
    Did you see the rest of it? I dont think I got anything wrong?
  • FabianCook
    FabianCook about 12 years
    You cant change strings in the strings.xml file, the best you can do is this, every time the application is run it will take half a ms to load this, also you get the alignment, can you explain more in a comment here if you wanted something different? This is what I understood is what you want.
  • FabianCook
    FabianCook about 12 years
    Btw I was editing it because I was reading through what I already had, saving it as I went along
  • Trikaldarshiii
    Trikaldarshiii about 12 years
    you must read thisline and also on start of first activity i want to set app_name and not on every activity;
  • Trikaldarshiii
    Trikaldarshiii about 12 years