I would like to connect many TextViews with resources, for example, I have got 21 TextViews in xml file which all create a table. I would like to simple attach them to my code, but I want to avoid 21 lines of code.. I thought about looping it, but I am probably wrong, because it doesn't work.
for (int id = 1; id < 21; id++)
textView[id] = (TextView) findViewById(R.id.textView+"id");
}
You can link ViewGroup which is your table here and iterate their childs.
Lets assume we have a linear layout named as mLinearLayout.
for(i=0; mLinearLayout.getChilds.size; i++){
View child = mLinearLayout.getChildAt(i);
if(child instanceof TextView)
mList.add((TextView) child)
}
For more complex layouts you might need recursive functions to iterate child view groups.
Good luck
Instead of findViewById you can try findViewWithTag because now you can use a string argument like what you want.
Of course, you'll probably have to adjust a few things to be able to use this.
Your TextViews will need to be a direct child of a parent layout in your xml and tag them: android:tag="mytext1"
Then in Java, for example:
LinearLayout parentLayout = (LinearLayout) findViewById(R.id.parent);
...
textview[i] = (TextView) parentLayout.findViewWithTag("mytext" + i);
And you can loop that findViewWithTag
Related
I am creating an app where I will be loading some images and data from a database, it should look like this:
Image______Name of user
Image______Name of user
Image______Name of user
etc..
I tried to create it just with a dummy image and some text to figure out how it works.
I create a LinearLayout, ImageView and a TextView, I add those two to the LinearLayout, and than I add that LinearLayout to a RelativeLayout.
The problem is, that all the images and text are placed in the same place, on top of each other. How can I change it so it is in the format I need?
relativeLayout = (RelativeLayout) findViewById(R.id.rel);
for(int i = 0; i< 30; i++)
{
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
TextView hello = new TextView(this);
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.mipmap.ic_launcher);
String hi = "Hey";
if(i == 0){hi = "Hello0";}
if(i == 2){hi = "Hello2";}
if(i == 3){hi = "Hello3";}
if(i == 4){hi = "Hello4";}
hello.setText(hi);
layout.addView(imageView);
layout.addView(hello);
relativeLayout.addView(layout);
}
I am using a for to loop it a few times just for test.
Instead of RecyclerView, add the items in a LinearLayout. You can also set position where to add the new item in the LinearLayout.
I would suggest you do instead is:
create a model object for the user details(name and picture)
Use ListView or RecyclerView with a simple adapter add items to an
ArrayList of model object and notify the adapter when data is
changed.
This way you'll be reusing the views, and that'll improve the performance much better.
for examples, you can take a look at these sample projects.
https://github.com/lokeshsaini94/SimpleAndroidExamples/tree/master/ListView
https://github.com/lokeshsaini94/SimpleAndroidExamples/tree/master/RecyclerView
>
Im displaying 13 cards dynamically by getting Resource as string and setting its margin in a layout but they are not setting up without overlapping. I tried a lot by changing margin but not working.
int counter=0;
forloop 1 to 13
int resID = getResources().getIdentifier(resourceName, "id", getPackageName());
im = (ImageView) findViewById(resID);
Context context = im.getContext();
cardID = context.getResources().getIdentifier(resourceName, "drawable", context.getPackageName());
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(im.getLayoutParams());
lp.setMargins(counter*30,0,0,0);//left,right,top,bottom
im.setLayoutParams(lp);
im.setImageResource(cardID);
counter++;
![Screen shot of 13 Cards ][2]
The issue is once you run out of horizontal space, you need to create a new row and add cards to that until that row runs out of space, etc.
A GridLayout would be better suited for what you're trying to accomplish. You could also use a GridView or TableLayout. It would be rather complicated to do this using a RelativeLayout since the child views need to be laid out in relation to each other, so you'd need to know exactly which view each child to the right of and which view it's below.
See this tutorial for an example of how to add views to a GridLayout dynamically:
http://android-er.blogspot.com/2014/09/insert-view-to-gridlayout-dynamically.html
I've got a layout which is called activity_main.xml which is my parent layout, and I am then inserting a child layout (activity_main_card.xml) within a for loop.
I also have an array, and what I am doing is using the length of the array to determine how many child elements it should create. All of my data to be used in the child element is stored in the array, so the idea is to loop through the array, create a child element for each loop and populate the data.
Instead, what is currently happening is that it is generating the 3 (length of array) child elements, but it is only populating the first one with the latest content in the array. This is because it keeps the variables the same.
What I need to do is somehow set dynamic variables that change as the loop iterates.
The code for my method is as follows:
for (int i = 0; i < cardArray.length; i++) {
LinearLayout item = (LinearLayout)findViewById(R.id.card_holder);
View child = getLayoutInflater().inflate(R.layout.activity_main_card, item, false);
item.addView(child);
//Set objects from array to variables
String cardTitle = cardArray[i][0];
String cardContent = cardArray[i][1];
String cardImage = cardArray[i][2];
//Set XML elements to variables
TextView title = (TextView) findViewById(R.id.card_title);
TextView content = (TextView) findViewById(R.id.card_content);
ImageView image = (ImageView) findViewById(R.id.card_image);
//Load variable content into card layout
title.setText(cardTitle);
content.setText(cardContent);
image.setImageDrawable(getResources().getDrawable(getResources().getIdentifier("drawable/" + cardImage, "drawable", getPackageName())));
}
What I thought I could do was to set the views like:
TextView title[i] = (TextView) findViewById(R.id.card_title);
however this doesn't work. Does anyone know how I can acheive this?
You need to get a reference to the current View you just created and then populate the TextView elements on that View. This is achievable by doing this:
TextView title = (TextView) child.findViewById(R.id.card_title);
TextView content = (TextView) child.findViewById(R.id.card_content);
ImageView image = (ImageView) child.findViewById(R.id.card_image);
You might be better off approaching this problem differently however, as Jonathan suggests a ListView and adapter might suit better for this situation.
You aren't using the View created, you are using the root layout of the activity in every loop because you apply the findViewById in the parent view:
Change:
TextView title = (TextView) findViewById(R.id.card_title);
By;
TextView title = (TextView) child.findViewById(R.id.card_title);
I'm working on an android project which allow the user to fill a form.
The picture below descripts my differents layouts for the form.
The black layout (LinearMain) is my principal linear layout.
The green one (LinearForm) descripts the rows of the form.
LinearMain.addView(LinearForm);
The blue one (LinearDescription) contains the description the row the form to fill in.
LinearForm.addView(LinearDescription);
And the gray linear layout descripts my controls. The control can be an EditText, a button or a CheckBox.
For example LinearForm.addView(LinearButton)
After filling in the form I want to retrieve the text in the EditText, Know if the CheckBox is Checked or not.
I already try to use an ArrayList (example ArrayList) but it's not very helpful.
Is there another way to figure this out?
Because you don't know exactly with view you are to deal with it is important here to use instance of to avoid exceptions
ViewGroup is an abstract class that extends all ViewGroups, LinearLayout for instance is a ViewGroup.
if(ViewGroup.getChildAt(int) instanceof Checkbox){
//do sommethng here
}else if(ViewGroup.getChildAt(int) instanceof Button){
//do sommethng here
}else if(ViewGroup.getChildAt(int) instanceof TextView){
//do sommethng here
}else if(ViewGroup.getChildAt(int) instanceof EditText){
//do sommethng here
}
ViewGroup.getChildAt(int) is documented here
you can create a for loop to go through LinearLayout's children, check if corresponding children is instanceof CheckBox and get the value ((CheckBox) getChildAt(i)).isChecked()
Sample code:
LinearLayout ll;
for ( int i = 0; i < ll.getChildCount(); i++ ) {
View child = ll.getChildAt(i);
if ( child instanceof CheckBox ) {
boolean checked = ((CheckBox) child).isChecked();
}
}
You could set id to your View, never mind what view it is by: LinearButton.setId();
and then get the text by using findViewById(R.id.chosenId).getText().toString();
for EditText or findViewById(R.id.chosenId).isChecked(); for CheckBox.
another way is to save your view in a arrayList of views and to access them using this array.
Have u created the layout in xml and set the id of each of the view ?
If yes then all u need to just create the object of the EditText using findViewById(R.id.edittext) and then use getText() method of the EditText to get the text from the EditText. And for CheckBox you can use findViewById(R.id.chosenId).isChecked().
I have a ViewGroup defined in XML with a view inside, at onCreate time I'd like to have a variable of those.
I don't want to go through the hassle of using a listview+adapter cause its clearly overkill as I know the list won't change since onCreate()
This is more or less the code I'd like to have.
TextView mytextview = myViewGroup.findViewById(R.id.mytext);
for(String test : strings){
mytextview = mytextview.clone();
mytextview.setText(test);
myViewGroup.addView(mytextview);
}
But it is not working.
Maybe use an inflater, and put the textview in an external layout file:
View v = LayoutInflater.from(this).inflate(R.layout.textview_include, null);
viewGroup.addView(v);
Using code of Mathias Lin and using the hint from javahead76:
LinearLayout x = (LinearLayout) findViewById(R.id.container);
for (int i = 0; i < 5; i++) {
View c = LayoutInflater.from(this).inflate(R.layout.singlerow, x);
TextView t = ((TextView)findViewById(R.id.textView1));
t.setId(i+10000);
t.setText("text"+i);
}
TextView b = (TextView) findViewById(10003);
b.setText("10003");
If you do this, you will most likely get the exact same id for every view created this way. This means doing things like ((TextView)v).setText("some text"); will be called on every TextView previously inflated from the same layout. You can still do it this way, but you should call setId() and have some reasonable method for ensuring you do not get the same id twice in a row - incrementation or universal time, etc.
Also, I think Android reserves a certain range of id's for dynamically created id's. You might avoid ID's in this range; but honestly, I don't know the id system works so I could be wrong on this point.