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.
Related
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 have a standard straight forward EditText, I want to show the dictionary suggestions on top of this EditText so I did this in the XML:
<EditText
android:id="#+id/messaging_messageEdit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:layout_weight="85"
android:background="#drawable/clanz_edit_text_holo_dark"
android:ems="10"
android:hint="Type your message here..."
android:singleLine="true"
android:inputType="textAutoComplete"
android:textColor="#dedede" >
</EditText>
I thought that the inputType parameter would take care of the auto dictionary view. On my phone (Nexus Android 5.1) the dictionary view appears but is blank. On a Genymotion emulator (Android 4.1.1) it does not display at all.
What am I missing?
This can be one solutions if you are looking for AutoComplete TextView.
<AutoCompleteTextView
android:id="#+id/from_station"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="#string/enter_start"
android:imeOptions="actionNext"
android:inputType="textNoSuggestions"
android:textColorHint="#color/transparent_black" />
You can also set threshold value using paramters.
Need to set adapter values at runtime.
If I understand correctly you want the keyboard to have auto correction right?
According to the Android developers website:
Can be combined with text and its variations to specify that this
field will be doing its own auto-completion and talking with the input
method appropriately. Corresponds to TYPE_TEXT_FLAG_AUTO_COMPLETE.
This is not what you're looking for. You are probably looking for textAutoCorrect which is this according to the Android developers website:
Can be combined with text and its variations to request
auto-correction of text being input. Corresponds to
TYPE_TEXT_FLAG_AUTO_CORRECT.
I've made a lot of apps and never used one of those though. It just auto corrects if you have a normal EditText.
I had the same problem not getting keyboard suggestions on some of my EditText views. Please pay attention there is a difference between autoComplete text views and getting keyboard suggestions while typing! They happen in two different places, first one inside the AutoCompleteTextView the other one on top of your keyboard while typing.. (for example you type te => it suggests "tea teaspoon teapot" to make your life easier.)
To achieve that I had to turn off the input types that I was setting programmatically and when you do so, the default behavior is back and that means getting keyboard word suggestions:
// etFieldValue.setInputType(InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE);
I tried the following but I still did not get a keyboard suggestions:
etFieldValue.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE|InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE);
at the end I commented all the above and it worked just fine!
here I also found that someone else had the reverse problem meaning they wanted to disable this functionality and they suggested to use the code that I had commented! read here
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'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!
I am creating an Android game and have an issue with dynamic text with a TextView. Essentiall within my layout, I have a TextView, within a Relative Layout with enough space for several lines.
What I would like to do is add 5 lines within the TextView, with a functionality that once it is trying to write a 6th line it automatically overwrites line 1, therefore only ever showing a max of 5 lines of text.
Example of What I am after:
dynamic line 1
...
dynamic line 5
Please find below my xml code:
<RelativeLayout android:id="#+id/battleconsole"
android:layout_width="fill_parent"
android:layout_height="135dp"
android:gravity="center_horizontal"
android:layout_below="#+id/spacer1" >
<TextView
android:id="#+id/battle_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Console with information" />
</RelativeLayout>
This is the code that I am using to update the text within the TextView:
TextView update = (TextView)findViewById(R.id.battle_details);
update.setText("test console data going in here");
I am not sure if this is even possible using a TextView, if not is there any other way I can solve this issue?
Thanks in advance
Keep the 5 lines in a collection (array or list). When you set the text on the TextView, join the lines in the collection on "\n". Then just replace the item in the collection with the updated text.
Android has a join method in the TextUtils class, but I prefer the Guava library's Joiner class. Check it out: Google Guava