Is it possible to change ImageView ID dynamically? - android

I am facing a problem with my 2 listeners that listen to the same ImageView ID. Thinking to only enable 1 of them during menu click, and hide the other 1.

Unless your View is part of a custom View hierarchy then you shouldn't do it because of possible conflict. I think what you really want to do is not to have two listeners but change the content of a same listener based on a click. Why not simply use if ?

Of course you can. You can call View.setId(int) and View.getId() to set and get a View's id. But change ImageView ID dynamically is not recommended at all. Because conflicts of different views are easy to happen if you do so.

Related

best method to frequently change view inside layout

I want to add and remove some views frequently like
Recycler view,textview,seekbar,customview,imagebutton etc.!!!!
I know following methode to perform it
Add all view to layout and just play with Visibility.GONE, Visible and other..
using layout params add and remove view...
Use View.inflater to add and remove predefined XML views(I'm using)
So question is
1.Is there any other method to do it?
2. Which one you prefer and why?
When you make Visibility.GONE all the views and associated resources such as image, sound files will be kept in the memory and if you have a lot if resources that may slow down your application. I think better way would be to use Fragments.
Check out this link. Hope this helped

define a recurrent set of views to use multiple time in the same layout

I'm working on an android project where I have a set of views (2 TextViews and some checkboxes in a checkbox group) to replicate many time in the same activity.
Is it possible to define the layout only for one set and instantiate it many time?
Also, the views are grouped in a Relative layout, is it possible to position the without the id attributes (to avoid id duplication)?
I would use a ListView for this. Even if you got like 5 items it workd fine. If you got many more items it still works perfect. Take a look at this example.
You can do this by defining the fields you want to reuse in their own xml. you can then use the 'include' tag for where you want them to display.
http://developer.android.com/training/improving-layouts/reusing-layouts.html
You do need to define the id's to position them in the relative layout. What is your concern about replicating the id's.
The other thing worth mentioning is how to use findById() when using 'include'. You can put a id on the include tag (which is effectively the relative layout viewgroup). Find that group first (Cast to viewgroup) and then do a findbyId on that group for what ever view you are after.

Listen all buttons events Android

Its possible to have a listener to all buttons without setting the onClickListener in each button across all activities? and without making a extends button with the listener already set.
No. Each view has to be told what to listen to. You can specify it in xml if you prefer with the onClick attribute, but you'll still need to specify it on each object.
If all the buttons are doing the exact same thing, you can include that in the List kind of a layout and have one button with just one onCLick() event

In espresso How to getthe first ImageView?

In my view, there are two ImageViews. All of the properties are same. It does not have id, content-description, etc.
I want to click the first one, but I could not find the method to do it.
My Code:
Espresso.onView(ViewMatchers.withClassName(Matchers.endsWith(ImageView.class.getSimpleName())))
You need to call both perform and click once you have the proper view that you want. From your question it's not clear whether you are getting that view from your calls to matcher or not. If you're not getting the right view, the easiest way is to set an id property on the two ImageViews so that you can uniquely identify them.
Once you've done that, you'll make a call something like this:
Espresso.onView(ViewMatchers.withId(R.id.imageView1)).perform(ViewActions.click());
This will perform a click event on the ImageView that you've matched.

How to set up a real custom Listview?

In my app, I should set up a "real" custom listview.
For example :
First line(Some Text) Checkbox
Second line(Some Text) Spinner
What I mean is that each line has different widget. In the example, for the first line, we have a checkbox, for the second, it is a Spinner.
Does someone have any idea of how I can set it up?
Thanks a lot!
Do you really think you need a ListView? Or could a ScrollView fix your problem already?
If you definitely need a ListView, read my answer here: How to add multiple headers throughout a single ListView with addHeaderView()?
You can define different types for your items and return a different layout for each type, while still having the possibility to get higher performance by using convertView.
Check out an example given on this page for defining custom-listview: http://www.androidpeople.com/android-custom-listview-tutorial-example/
Same way, you can define your custom-layout for listview, you just need to implement getView method.

Categories

Resources