Set ID as a number in layout - java

How can I do view.setId(123) in the xml layout?
android:id="123" doesn't work, and android:id="#+id/123" would add it into the IDs file
Edit: I need to set the ID to a number because I'm setting it to a lot of views and I want to initiate them with a loop

It seems like you're doing something not intended. It's not possible to do it like you want, but there's a little workaround for that.
The most easy would be to define an array of these ids
static final int[] IDS = {R.id.id1, R.id.id2, ...};
or by using xml TypeArray
<resources>
<array name="ids">
<item>#id/id1</item>
<item>#id/id2</item>
...
</array>
</resources>
TypedArray ta = getResources().obtainTypedArray(R.array.ids);
for (int i = 0; i < ta.length(); i++)
findViewById(ta.getResourceId(i, 0);
ta.recycle();
But better consider refactoring your code to not have the need of using this.

Related

Best Vaadin component for a list of String

My issue is I have a simple List of Strings say
List<String> names = List.of("Frank","Joe","Eva");
All I want is display it on the UI. With some simple code like
ListComponent lc = new ListComponent.setItems(names);
I have tried it with Table which seems to work but code behind it is a bit boilerplate for this simple task(7-8 line of code).
I have tried also the Grid component and it works well when I want to bind a POJO to it , but with String.class type its a nightmare.
Grid<String> listGrid= new Grid<>(String.class) ;
listGrid.setItems(names);
it doesnt work because I have to provide getters for the column, which String.class doesnt have for the value. So I did this:
Grid<String> listGrid= new Grid<>(String.class) ;
listGrid.setItems(names);
listGrid.addColumn(String::toString).setCaption("name");
It works! However unspecified columns also appear in the grid, so now I have 3 columns Byte,Empty,name. And I dont know why. Where are these comes from?
What are the requirements for displaying them? Just to get them on the screen? Is Label enough?
for(String name: names) {
mylayout.addComponent(new Label(name))
}
If you need selection, then maybe ListSelect or ComboBox are the go-to’s.
If you want to avoid the additional columns, one way is to do as was pointed out in a comment, i.e. do removeAllColumns() before you go on creating your own columns.
Another approach would be to do new Grid<>() instead of new Grid<>(String.class). The main difference is that the second constructor uses reflection on the provided class and automatically configures columns for anything that looks like regular Java bean properties.
I would highly prefer to use grid.removeColumnByKey rather than removeAllColumns()
You can also use grid.setColumns to specify order of columns.
I will add link to vaadin documentantion for grid with java examples which is realy helpfull. enter link description here
I did something similar to adding the Strings in TextAreas. Because I needed some formatting, I added the text using StringBuilder.
List<String> details = getDetails();
StringBuilder builder = new StringBuilder();
for (String detail : details) {
TextArea ta = new TextArea();
ta.setSizeFull();
ta.setMaxHeight("100px");
ta.setValue(builder.append(detail).toString());
((Span) content).add(ta);
((Span) content).add(new Hr());
}
The result is like this:
Try out this
final Grid<String> grid = new Grid<>();
grid.setItems(new ArrayList<String>());
grid.addColumn(item -> item).setHeader("Value");

create "ArrayList" in xml resource file in android

I know how to create a simple Array in xml like this
<resources>
<string-array name="countries_array">
<item>Afghanistan</item>
<item>Albania</item>
<item>Algeria</item>
<item>American Samoa</item>
<item>Andorra</item>
<item>Angola</item>
<item>Anguilla</item>
<item>Antarctica</item>
</string-array>
</resources>
And then I initialize it like this in code
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries_array);
textView.setAdapter(adapter);
But I want to store more items than just a string, so I need a way to store an arraylist rather than a simple array.
Like below
<resources>
<Share>
<name>Apple</name>
<currentRate>5.69</currentRate>
<changeToday>-0.11</changeToday>
<changeTodayPercent>-0.02</changeTodayPercent>
<timeUpdated>2012,12,06,18,00,00</timeUpdated>
<Share>
<Share>
<name>Microsoft</name>
<currentRate>5.88</currentRate>
<changeToday>0.19</changeToday>
<changeTodayPercent>+0.09</changeTodayPercent>
<timeUpdated>2012,12,06,18,00,00</timeUpdated>
<share>
...
</resources>
I already have a class called Share.java with following variables that need to be set via setMethods
public class ShareHolding {
private String name;
private double currentRate;
private double changeToday;
private double changeTodayPercent;
private GregorianCalendar timeUpdated;
}
and add them to an arraylist
ArrayList<Share> allShares = new ArrayList<Share>();
allShares.getShare(0).setName("I want to access my xml file here and add the first shares name here");
As far as I know, you won't be able to mismatch data types using the resource file. The resource only supports one-way datatypes. e.g Array of string, array of integers, etc.
The best solution that would work is to make the Share xml into it's own file and parse is into your shared object. The android developer has some example of the built in xml parser

Creating a Custom Expanded View in Code

I want to customize the notification area, adding an icon to the right and few buttons.
I've read the tutorial here: http://developer.android.com/guide/topics/ui/notifiers/notifications.html
The problem is that I need to include this code in a library, an SDK that I want to distribute to improve notifications. (See http://hub.buzzbox.com/)
Is it possible to write all the UI in code, without the need of the xml to describe the remote view? This is because resources cannot be included in an SDK, so I would need to ask the users of my SDK to add an xml to their resources and to reference all the resources by name... which I would like to avoid.
I've already written other parts of the SDK user interface completely in Java code but I'm having issues to do the same for the Remove View.
A RemoteView is usually created like this:
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
Can I create a RemoteView from I layout that I create with Java code?
Any other solutions?
I thought that you might be able to do this with your own Parcel, but looking at the code, the Parcel simply stores the package name and layout (resource) id, as used by the main constructor.
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mPackage);
dest.writeInt(mLayoutId);
int count;
if (mActions != null) {
count = mActions.size();
} else {
count = 0;
}
dest.writeInt(count);
for (int i=0; i<count; i++) {
Action a = mActions.get(i);
a.writeToParcel(dest, 0);
}
}
I can't see a way of doing this and think it may not be possible.

Creating a list of integers in XML for android

I would like to create a list of Integers in the /res folder of an android project. However, I want those integers to point resources in /res/raw. So for example, I would like something like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer-array name="built_in_sounds">
<item>#raw/sound</item>
</integer-array>
</resources>
But id doesn't look like I can do that, is there any way to do this? Or should I just create the list in a java class?
Thank you
And the correct answer is actually, TypedArray
The documentation shows examples of lists of resources:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="icons">
<item>#drawable/home</item>
<item>#drawable/settings</item>
<item>#drawable/logout</item>
</array>
<array name="colors">
<item>#FFFF0000</item>
<item>#FF00FF00</item>
<item>#FF0000FF</item>
</array>
</resources>
And the code to retrieve the values:
Resources res = getResources();
TypedArray icons = res.obtainTypedArray(R.array.icons);
Drawable drawable = icons.getDrawable(0);
TypedArray colors = res.obtainTypedArray(R.array.icons);
int color = colors.getColor(0,0);
Okay, I finally found out how to do this. What I did, was just create a database. The database stored all of the primitives I needed to store, and than pointers to the objects that I needed to reference. Apparently the android SDK comes with support for SQLite.
To do this in XML without a database, see:
http://developer.android.com/guide/topics/resources/more-resources.html#Integer

Get an integer array from an xml resource in Android program

Just a quickie,
i have an xml resource in res/values/integers.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer-array name="UserBases">
<item>2</item>
<item>8</item>
<item>10</item>
<item>16</item>
</integer-array>
</resources>
and ive tried several things to access it:
int[] bases = R.array.UserBases;
this just returns and int reference to UserBases not the array itself
int[] bases = Resources.getSystem().getIntArray(R.array.UserBases);
and this throws an exception back at me telling me the int reference R.array.UserBases points to nothing
what is the best way to access this array, push it into a nice base-type int[] and then possibly push any modifications back into the xml resource.
I've checked the android documentation but I haven't found anything terribly fruitful.
You need to use Resources to get the int array; however you're using the system resources, which only includes the standard Android resources (e.g., those accessible via android.R.array.*). To get your own resources, you need to access the Resources via one of your Contexts.
For example, all Activities are Contexts, so in an Activity you can do this:
Resources r = getResources();
int[] bases = r.getIntArray(R.array.UserBases);
This is why it's often useful to pass around Context; you'll need it to get a hold of your application's Resources.
get an array from xml resources of android project can be accessed.
from array.xml
<string-array name="weather_values">
<item>sunny</item>
<item>cloudy</item>
<item>rainy</item>
</string-array>
in Activity
String[] stringsArray = getApplicationContext().getResources().getStringArray(R.array.weather_values);
in Fragment
String[] stringsArray = getContext().getResources().getStringArray(R.array.weather_values);
for output in log
System.out.println("Array Values " + Arrays.toString(stringsArray));
output is
I/System.out: Array Values [sunny, cloudy, rainy]

Categories