How to get and set string Resource values in android

帅比萌擦擦* 提交于 2019-12-04 13:37:46
FabianCook

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);

Try like this

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

Try this.

String value = getResources().getString(R.string.Please);
Mohammed Azharuddin Shaikh
 String str = getResources().getString(R.string.Please);
Amit

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

@string/Please
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!