Is it possible to do something like this in Android? (basically the equivalent of select and option in HTML):
<ListView android:layout_height="match_parent"
android:layout_width="match_parent">
<ListViewItem>TEST</ListViewItem>
<ListViewItem>TEST1</ListViewItem>
</ListView>
I have tried this, it obviously doesn't work. All I can find online is information about using adapters, but as I have some hard coded values I don't need to use an adapter - I'm happy to put it directly in the XML file.
Is this possible?
1 ] Create a String array in strings.xml
2 ] Use the following attribute inside Listview tag
android:entries="#array/your_array_name"
Done !
No It is not possible in android but used android:entries properties to load list item using String resource array.
Related
I have a string with placeholder e.g
<string name="str_1">Hello %s</string>
I want to use this in xml layout as android:text="#string/str_1". Is there any way to use this in xml layout to fill the placeholder?
Thanks in advance. I already know String.format(str,str...) in java/kotlin but i want to use this in xml layout without data binding.
You can use something like this.
Write your string in your (strings.xml) with declaring a string variable (%1$s) inside it. For decimal, we use (%2$d).
<string name="my_string">My string name is %1$s</string>
And inside the android code (yourFile.java), use this string where you want it.
String.format(getResources().getString(R.string.my_string), stringName);
This is not a good answer but it may help you get some idea to get going.
Thanks.
It's not possible to format your strings directly in your XML. Inside your code you can use both String.format and context.getString() to get and format your string.
If you want to show something just in the XML and not have it in the final build (only have it in compile time), you can also use tools: namespace like following:
<TextView
...
tools:text="Hello, this is a test"
/>
This will only appear in the layout editor and will not have any effect in the APK. It can also be used for other fields too, like tools:visiblity.
Currently there is very easy way to define MvxItemTemplate and handle it in Android:
<MvxListView
android:id="#+id/grdEvents"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="40dp"
local:MvxItemTemplate="#layout/eventslistcell"
local:MvxBind="ItemsSource Events; ItemClick GoEventDetails" />
And that's it. No additional adapters, no complications. Only separate axml file for the cell.
Is there a similar way to do that with iOS project? (MvvmCross documentation is still very very poor about that).
Yes! You can define your own custom class derived from MvxTableViewCell, which provides the layout used for an individual cell. For a complete sample see the WorkingWithCollections sample on GitHub.
In short, you first define your custom cell as MvxTableViewCell. You can do so with a xib file as well as without it.
Then you need to create an instance of MvxSimpleTableViewSource or MvxStandardTableViewSource and pass into it the name of your custom cell nib or the type of your custom cell class:
var tableViewSource =
new MvxSimpleTableViewSource( tableView, typeof( MyCustomCell ), "MyCustomCell" );
The third parameterd specifies an cell type identifier, which would come handy if you used multiple different types of cells in one table view. The system then uses it to recycle the correct type of cell that it needs.
Finally you set the instance as the Source of your table view:
tableView.Source = tableViewSource;
I'm trying to create an android drop down box but finding it difficult to find a suitable example that isn't overly complicated.
What I want is a simple drop down box with 4 or 5 options in it. Those options should be placed in an xml file that the spinner reads in.
All the examples have dealt with multiple spinners on a page of seem to have bloated code that doesn't make sense.
In res > Values > Strings
<string-array name="mySpinnerOptions">
<item>Cow</item>
<item>Dog</item>
<item>Cat</item>
<item>Horse</item>
</string-array>
In your content_myView file
<Spinner
android:id="#+id/mySpinner"
android:entries="#array/mySpinnerOptions"
android:layout_width="your_width"
android:layout_height="your_height"/>
Here's a simple example that you can get the gist of from steps 1 and 2.
https://www.mkyong.com/android/android-spinner-drop-down-list-example/
Summary:
Create string array in strings.xml
Add android:entries="#array/your_array" to your Spinner component in your layout xml
I saw a xml layout which has a textView as below
<TextView
android:id="#+id/tvHeaderTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Change password"
android:tag="#string/regular"
android:textAllCaps="true"
android:textColor="#color/header_color"
android:textSize="#dimen/font30" />
I want to know what is android:tag property is used for. A detailed answer for be greatly appreciated.
Note - I read that it is used for data binding, but could not follow the context.
Update 1 - (Thanks Blackbelt for the answer)
Usages
1. Could be used for compile time binding of xml elements with the activity. Although android recommends the use of id.
2. Could be used for identifying elements for list adapter. For eg, a special case of a list adapter with multiple sort support. Then we could use the tag element to identify the desired list item.
I would be glad to know if it could also be used for assigning any other property?
I want to know what is android:tag property is used for
it is a different way to add associate additional information with the view object itself. In you case your tag is a string, but it can store complex objects too. For instance the tag could be a model class set and retrieve at runtime using the setTag()/getTag() pair. The way this information is used is up to the users. Android allows to findViewWithTag too. In your case, for instance you could look for the same object using findViewById(R.id. tvHeaderTitle); or using findViewWithTag(getString(R.string. regular)); . An example of usage is with ListView when you have a button part of you item and, upon click you want to know the which item in your dataset is associated with that row. Using the pair setTag/getTag this thing is easily achievable
I am creating listview dyanamicaly in the code..Not through XML....!!
Two things i couldnt apply to the listview though code which i could apply through XML attributes.
1) android:divider="#android:color/transparent"
2) android:scrollbars="none"
What are the equivalent API's for these.
There are usually a pair of get and set methods with a similar name to the xml attribute. I believe the ones you want are View#setVerticalScrollBarEnabled() and ListView#setDivider():
http://developer.android.com/reference/android/view/View.html#setVerticalScrollBarEnabled%28boolean%29
http://developer.android.com/reference/android/widget/ListView.html#setDivider%28android.graphics.drawable.Drawable%29
You can use this for de divider.
setDivider(getResources().getDrawable(android.R.color.transparent));
About the scrollbars I can't found anything. But if you adjust the content to the list size the scroollbar should not appear.