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!
Related
Is it right to have same Id for a TextView and a ImageView ?
Since they belong to one entity I gave both of them same Id.
If yes.. then how can I find these views by id separately ?
Is it right to have same Id for a TextView and a ImageView ?
Short Answer: NO
Long Answer: It is not right to use same IDs because by doing it can cause runtime errors. Consider the below example
layout.xml
<TextView
android:id="#+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginTop="12dp" />
<ImageView
android:id="#+id/location"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginStart="33dp"
android:layout_marginTop="5dp"
app:srcCompat="#drawable/openLoc" />
LocationActivity.java
setContentView(R.layout.activity_profile); //inflated layout
txtLocation = (TextView) findViewById(R.id.location);
Here you will face the problem in Activites because it will be confused that which element should be picked from two.
Btw YES you can use same IDs in the different layouts because it won't make any runtime error as it will search IDs on inflated layout only.
EDIT: You can have same IDs in the same layout. It causes an issue when you call it by findViewById() and throws similar exception
java.lang.ClassCastException
android.support.v7.widget.AppCompatImageView cannot be cast to android.widget.TextView
Suggestion: I don't know why you want to assign same IDs to two elements but if you want readability then I would suggest you to set ids in a way that elements can be easily identifiable by ID like android:id="#+id/txtLocation" android:id="#+id/imgLocation" it makes it easy to identify element type just by reading ID. You can make it even easier by appending layout name in beginning like android:id="#+id/profileTxtLocation". Now this will help you while coding as autocomplete feature will assist you. Just type layout name you will get the list of all layout elements, then you will type the kind of element you get the list of all asked elements(es: textViews) in layout.
You theoretically can but it's highly advised not to. Duplicate IDs within same layout since it will prevent finding view by ID (You will need to iterate over children or otherwise reference them), and cause collision/outright crash when those views will attempt to store/restore their savedInstanceState.
I don't know what to say to this but I want mask or put patch on Text view.
See the picture below. I want effect like this. I've searched lot but didn't find any post with same requirement. How to achieve this?
You can pass two images in one TextView Like below Code:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/icon"
android:drawableRight="#drawable/pacth_icon"
/>
use
android:drawableLeft
and
android:drawableRight
to set your images in TextView.And you can setText and background Image as well.
I'm looking for how to do texttospeech from extern file, eg from resources.
Normally it works that, it read view's text/className/etc .
There would be great to click on specific view eg unnamed button with only image, and tts from resource with mapped strings.
EDIT:
Yes it goes for TalkBack, yes It reads "Button", and I would like to assign it a specific string.
Also assign text for specific button even if it has text assigned in xml layout.
EG:
<Button
android:id="#+id/this_is_button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/self_destruct"
android:onClick="selfDestruct" />
and it will read the content from android:text. Or just "Button" if we have not assigned text here.
The problem is how to assign for this button another text which would be read.
Please take a look at this site:
http://developer.android.com/guide/topics/ui/accessibility/apps.html
It describes what to do to make you arr better in terms of Accessibility.
The Labeling User Interface Elements section should be important to you:
http://developer.android.com/guide/topics/ui/accessibility/apps.html#label-ui
Here is an example how to label a ImageButton:
<ImageButton
android:id=”#+id/add_note_button”
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src=”#drawable/add_note”
android:contentDescription=”#string/add_note”/>
More information about android:contentDescription: http://developer.android.com/reference/android/view/View.html#attr_android:contentDescription
You can set there any string from your resources so the message can be localized the same way as any other text displayed on screen etc.
Please note that android:contentDescription works even on view that have text associated (then the android:contentDescription will be spoken instead)
i am developing an app, i would have linked an image but i don't have enough reputation to do so....my problem is The text doesn't fit in one line it goes to the second line,obviously thats because i have set the height to "wrap_content",but i want it to show the text only in one line and not 2 or more,what should i change the height to?i don't want any java code for this,my fragment is already heavy with lots of code,is there any way to do this on xml, so that it doesn't display different results for different screen size?also is there any way i can append "..." to the end of the name? like "backupRestoreConf..." or something like that?
use android:maxLines="1" (or android:singleLine="true", but it was deprecated and now it is not anymore. What to think.)
android:maxLines="1"
Use this in your XML
you need to set this in xml for your textview:
android:singleLine="true"
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.