I have a pretty simple question:
int i=0;
n = (TextView) findViewById(R.id. "value of i" );
How can I get this working? I want in the place of id to use my int value, is it possible? if so how do I go about doing this?
I'll put the code:
private void sxhmatismos7(String[][] pinakas)
{
TextView n;
int i=0;
for (i=0;i<12;i++)
{
n = (TextView) findViewById(R.id."HERE VALUE OF i");
if (n.getText().equals(pinakas[0][0]))
{
n.setVisibility(View.VISIBLE);
}
}
}
Something like this should work:
int id = resources.getIdentifier(String.valueOf(i), "id", "com.my.package");
n = (TextView) findViewById(id);
Android docs
There is only one way, you will have to create the TextView Dynamically and add it to your layout, there is a method called:
view.setId(int i);
here you can set the id of your view and can access it.
You don't use it that way. R.id.value is a Resource Id, typically from your layouts.
R.id.value is a public final int from the R.java file. The Id number is generated by your editor and is something like -11222388. There is almost never a time to not use code that looks like (View) findViewById(R.id.itemId);.
Related
Do you guys know if there's a proper manner to get all specific view types from the parent view as the title says?
I would like to get all the EditText views from my activity, so I wonder if there's a better way than using this solution get all views from an activity and then testing every view to check if it's an EditText or not?
Thank you for the help,
Florian
Approach 1 :
Let's say you named your edittext edittext_0, edittext_1, .. edittext_n. You can do:
ViewGroup vg = (ViewGroup) view;
for (int i = 0; i < n; i++) {
int id = vg.getResources().getIdentifier("edittext_"+i, "id", getPackageName());
edittext[i] = (EditText) vg.findViewById(id);
}
Actually the answers specified in the link given, are feasible if you have a large number of views, but if you want a specific type of view from a view/viewgroup, simply use findViewById method.
Example :
(in Kotlin) //Similar syntax in java
val view : View = findViewById(your_layout_or_any_other_view)
val childView : EditText = view.findViewById(edittext_id)
OPTIONAL READING
Optimised Answer for the answers given in link :
It is easier with Kotlin using for-in loop:
for (childView in ll.children) {
if(childView.id == your_edittext_id) //If required
//childView is a child of ll
}
//Here ll is id of LinearLayout defined in layout XML.
I passed the data from the first actvity (like int VNIMANI and int OBRATNOST). This data set my TextView after I came on second activity. now I need a button. And when I click on the button I need increment +1. But the code doesn't work.
TextView tvPaklic = (TextView) findViewById(R.id.paklic);
String paklic = String.valueOf( + 10+ (VNIMANI+OBRATNOST));
tvPaklic.setText(paklic);
}
int newPaklic = 0;
public void plusPaklic (View v){
newPaklic = newPaklic + 1;
displayPaklicDve (newPaklic);
}
private void displayPaklicDve(int newPaklic) {
TextView tvpaklicdve = (TextView) findViewById(R.id.paklic);
String paklicdve = String.valueOf(newPaklic);
tvpaklicdve.setText(paklicdve);
}
I would really like to suggest you refer this one Read this, will solve your problem
You were used 2 Textview objects with referring one id check that.
TextView tvPaklic = (TextView) findViewById(R.id.paklic);
TextView tvpaklicdve = (TextView) findViewById(R.id.paklic);
I have number of check-boxes with their ids as per a pattern sequence.
Like checkboxes1, checkboxes2, checkboxes3 etc..
I want to run a loop keeping each box, retrieve value, perform some function and move on.
Something like:
for(int i=0;i<9;i++)
{
String elementId="checkboxes"+Integer.toString(i);
CheckBox elementcb = (CheckBox) findViewById(R.id.elementId);
}
But of course the above thing won’t work as I can’t just simple append a variable in front of R.id.. So how can I achieve the above?
for(int i=0;i<9;i++)
{
String elementId="checkboxes"+Integer.toString(i);
int resID = getResources().getIdentifier(elementId,"id", getPackageName());
CheckBox elementcb = (CheckBox) findViewById(resID);
}
hope this help :)
I have dynamically generated ImageButtons with different ImageResource for each ImageButton. Now I want to know which ImageButton was clicked, how can I determine this ?
Need your help.
Thanks.
you can set an id for each created ImageButton and getId() for check witch button clicked
ImageButton im=new ImageButton(Yourcontext);
im.setId(giveAnID);
//where you check
int theID=im.getId();
In order to do this you could do two things:
Firstly, when dynamically generated the ImageButton you could call setId() in order to set a specific id to this View and store it in List, etc.
Then when you have a click event (or anything else), you can call the getId() method of the View to get the id.
Then you can compare and do anything you want.
Hope this helps!
Any resource is uniquely identified by its id which is generated in R.java file.
So you can use something like :
if(image.getId() == R.id.image) {
// do awesome stuff
}
If your code generates the imageButtons then, in this code you can write something like,
imageButton.setId(1);
and when your imageButton is clicked then you can get it with,
int id = imageButton.getId();
i had to do same thing and this is what i have done
for(int i = 0 ;i<mediaList.size();i++){
view_media_gallery_item = LayoutInflater.from(view.getContext()).inflate(R.layout.e_media_gallery_item, null);
TextView title = (TextView) view_media_gallery_item.findViewById(R.id.media_gallery_item_title);
TextView subtitle = (TextView) view_media_gallery_item.findViewById(R.id.media_gallery_item_subtitle);
ImageView flux_Title_Image =(ImageView) view_media_gallery_item.findViewById(R.id.media_gallery_item_img);
title.setId(i+100);
subtitle.setId(i+1000);
flux_Title_Image.setId(2000+i);
title.setText("" +mediaList.get(i).getTitle());
subtitle.setText(""+mediaList.get(i).getArtist());
System.out.println("view added::::");
view_media_gallery_item.setTag(mediaList.get(i));
view_media_gallery_item.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("view media clicked");
Media m = (Media )v.getTag();
medialistner.setOnItemclick(m);
}
});
I have a question that is driving me crazy.
I have a large number of buttons (10, more or less) on my screen, inside a TableRow.
I need to access them, and I had planned to perform through a loop.
Access to one, is very easy, adding this:
boton7 = (Button) findViewById (R.id.Btn7)
My question is, if you can dynamically set the id string (R.id.Btn7) to put in a can get the buttons for, and for example, change the color .... something like this:
for (int i = 0; i <10; i + +) {
Button eachBoton= (Button) findViewById (R.id.Btn + i);
eachBoton. setBackgroundColor (Color.Red);
}
That, of course, does not work .... my question is if anyone knows how exactly the chain can be mounted
R.id.Btn + i
to work.
Thanks a lot.
You can use Resources#getIdentifier() to get a resource identifier for the given resource name:
int resourceId = getResources().getIdentifier(
"Btn"+i,
"id",
this.getContext().getPackageName());
Button button = (Button) findViewById(resourceId);
Alternately you can prepare an array with all the ids you need and access elements of that array. This is more efficient:
private final int[] btns = {R.id.btn1, R.id.btn2, R.id.btn3, R.id.btn4, ...}
...
Button button = (Button) findViewById(btns[i]);
Give an identifier to your layout ("layout" in the example below) and then iterate over all the touchable children by using getTouchables. If it's a button, change the color.
View layout = findViewById(R.id.layout)
ArrayList<View> touchables = layout.getTouchables();
for (View b : touchables) {
if (b instanceof Button) {
b.setBackgroundColor(Color.Red);
}
}