Setting TextView to new string? - java

I used the layout resource editor to modify the main.xml layout file to add a second
TextView control to my app. by default, its set to something like #+id/TextView01. How do I Set the text attribute of the TextView control to my newly created String resource? I tried going into main.xml and just editing the android:text to point to the name of my new string, but it didn't seem to work.

you define your strings inside resources/strings/values.xml
and you can reference them using "#string/name_of_string" syntax in your layouts.
android:text="#string/name_of_string"

Related

How to save Strings locally and then update them programmatically?

what is the best way to store some Strings(used in Textview as a text) locally and then update them programmatically? Because I think(researched) that data can not be changed during runtime from res/values/Strings.xml
for example, I have a Textview, EditText, and a button. I get TextValue from Strings.xml using SetText in JAVA. I want to change the single string value from Strings.xml the value that I get from EditText.
Tell me some alternative method to get and change strings value or to how to use custom strings.xml to change the value in runtime.
Values places in XML are immutable, so you cannot change them at runtime. If you want changed values then set them directly on TextView by adding TextWatcher to your Edittext or setValues from a button's click listener.
Despite the name, this is what SharedPreferences are there for.

Android show multiple values from input in one layout

i'm new to android development.
What i want to do is show the 5 variables in one single layout.
I get those variables from the user by input.
I can show up one variable for example ip by setContactView
setContentView(ipView);
but how is it possible to show 3-4-5 variables?
setContentView(R.layout.activity_login_attempt_view);
By using the above i understand that i'm calling the layout.
Does that mean i have to form the layout according to the items id's that i'm giving the inputs??
Thank you
I'm confused by your question but you design your layout according to your needs. If you need to show 5 different pieces of input then you might create 5 different TextViews in activity_login_attemp_view.xml (which is a very long name, BTW, but ok). Maybe something like
<LinearLayout
...>
<TextView android:id="#+id/tv1"
.../>
<TextView
.../>
<TextView
.../>
<TextView
.../>
<TextView
.../>
</LinearLayout>
where the "..." is your properties such as height, width, id, etc... Then you only call setContentView() one time. Then use something like
TextView tv1 = (TextView) findViewById(R.id.tv1);
tv1.setText(someString);
be sure to initialize your Views such as with tv1 = (TextView) findViewById(R.id.tv1); after calling setContentView() or it will return null.
You are on the right track, you create a layout which contains many textviews, and call setContentView(R.layout.your_layout); from your activity.
and then use (TextView)findViewById(R.id.ip_id).setText("some string");
As has already been stated, you can create a "View" with as many elements on it as required. Each of these elements will be given an id value, whether it be the default, or one set by yourself.
The call to setContentView(R.layout.your_layout); informs android that you want to display whatever is defined in the layout file you specify as the argument.
For further research, you may want to look at: findViewById() - as that is what you'll be using mostly in code, to update your views.
For example, if you wanted to update the text of a TextView, you'd use something like this:
TextView someText = (TextView)findViewById(R.id.myTextView);
someText.setText("Hello!");
Of course, you're not limited to string literals in TextView's:
int x = 10;
TextView someText = (TextView)findViewById(R.id.myTextView);
someText.setText(String.valueOf(x));
Hope this helps!

How to use textview in Android's PreferenceScreen?

I would like to use textview in PreferenceScreen, it's a longer text that explains some specific setting. If I use summary or title attribute on some versions it gets formatted weirdly, doesn't display correctly etc. (The text is rather long).
Therefor I find it would be the best to use textview, is it possible to create custom settings element?
Use this:
<Preference
android:selectable="false"
android:enabled="true"
android:key="example_key"
android:title="example_title"
android:summary="anything_you_want" />
The attributes selectable will decide the click action of the Preference.
You can assign layout resource for your preference in your xml file, using tag android:layout="#layout/your_pref_layout".
Don't forget to use proper ids in your layout (android:id="#+android:id/title", android:id="#+android:id/summary") to assign views to be used as title/summary views.
For more info see for example this: Creating a custom layout for preferences
or this: How to add a button to PreferenceScreen
Yes You can create a layout and use that layout file to inflate as your preference screen you can see a example here.
custom EditTextPreference: can't get TextView by id

Modify included layout in Android?

I am using an include tag in an Android layout file to include a relative layout row that I would like to repeat a few times. The problem is that after I include the row I cannot modify the text of a textview inside the layout. Is there anyway I can modify the text of a textview that is part of an include? Mainly I would like to insert 4 of these rows with custom text specified in the xml if possible.
You can do it only at runtime. Even if you do it at runtime, make sure you call findViewById from the parent view as Android doesn't allow you to use the same ID at more than one place in an xml file.
You can either add these 4 includes with a different id and get them from code or do what #Anis said and inflate them at runtime and put there your text.

Set Link Clickable in Java-Android

I am designing an App, where there is TextView, and based on some check condition, I want to make links / phone numbers in the TextView clickable in Java side. Can I do this, I don't want to make it clickable by default which is done by setting in xml file
android:autoLink="all"
What I already tried:
Create regular TextView and don't set autoLink="all",
TextView myView =
(TextView)view.findViewById(R.id.thisIstheTextView);
myView .setLinksClickable(true);
also tried :
myView .setMovementMethod(LinkMovementMethod.getInstance());
None of the above Java code works, I don't want a predesigned xml TextView with autoLink enabled, I want to change the TextView behavior in Java code based on if () conditions. There is no error in my code, but I am not able to achieve what I want. Can you please share your knowledge. Thank you.
Use setAutoLinkMask (int mask).
The possible values that can be combined for mask are those defined for the Linkify class.
Linkify.addLinks(myView, Linkify.ALL);
Fixed the issue.

Categories

Resources