accessing the children views of a View in Android - java

I am quite new to Java, let alone Android. I am stuck with a problem
that I encountered while instrumenting (using the instrumentation
framework of android) the code of a Android project. I have a ListView
and using the getTouchables() API I have store the view in a
ArrayList. When in debug session, I saw that each view has children
(mChildren) when expanded I could see that there is a "ImageView" and
"TextView" present. when the "TextView" is expanded, I could access
the mText variable which contains the text value I am looking for. In
brief, the value I am looking for, when looking at the "variables"
window of the debug perspective of Eclipse, is present at
'myview->mChildren->[1] (TextView)->mText->value'
Here "myview" is the name of the object of View class.
how do I access/ read the content of the "value" variable?
Please let me know if you need any info in this regard?
Thank you
-BA

You could access via the findViewByID method of the mView object;
For example
TextEdit mText = (TextView)myView.findViewById(R.id.mText);
mText.setText("Hello!");
Hope that's what you're after or helps.
Damon

Related

Dynamically change the android id of an element

Is it possible to change the android id of a view or any of their elements, such as an imageView based on some code? That is, verifying a variable such as a bool and having a different id depending on the value such as image_true and image_false.
I am working on an automation project with Appium and I need to be able to differentiate a success pop-up from an error pop-up. They are essentially the same component but the image is different depending on the state of it.
You can use View#setId with some positive integer as ids are basically integers
Your answer is not clear enough but it's possible to change it by #setId method of View. And consider this issue that changing the view id when runtime may cause errors inside the code if it is using that view by hardcoded id so it can't be found by those components anymore.
An alternative is to dynamically change tags using View#setTag which can accept strings. To access the view using tags, you can use findViewWithTag.
Sample code to access view using tag:
LinearLayout layout = findViewById(R.id.layout);
TextView textView = layout.findViewWithTag("someTag");

Understanding WebView Code

What exactly does this block of code (below) do?
WebView browser = (WebView) findViewById(R.id.activity_main_webview)
I have used it in my WebView app I started, and it seems to be important, but I don't understand what it does.
It just references to your webview view on the basis of ID you have given to view.
To say simple and short:
In android you define UI (for example layout of your activities) in some xml file. Each component in layout of activities could have an Id.
Android automatically create some java files to relate defined id to real components (IDs will save in a file/class which android named it R.java)
So when you want to access real component defined in xml file in the java code (for example in activity source code) you could use method findById and send id of requested component to it. This method returns real component (java object) related to given id.

How does the search view even get rendered?

I have been digging around in the search view source code for quite sometime now trying to understand how exactly does the search view even have a user interface to be rendered. First of all here is the source code:
SearchView source code
So what I don't understand is how does the user interface element of the search view even get rendered when the search view doesn't even have any onDraw() method. All that I can see is responsible for the display element is a bunch of view at the start which are in the constructor, the SearchView gets a reference to and change the background and set the image of these view. If all that I can see was done is getting references to some views and changing the background as well as the image without having it within the proper view hierarchy then how exactly does it even get rendered?
I understand what you are probably wondering why do I even need to understand this. Well I want to understand this so I can create my own custom search view. Since I only need like 2 function on my search view I figure it would be a lot better to make one that suits my need instead of the thousand of lines of code in the source one. Plus, I want to create one that I know I will understand how to use not the complex default one.

Android Testing: What do to when nothing has an id?

I'm trying to write some automation scripts for an app I have. I've done the tutorial on Robotium's site and have a basic understanding on how I can automation. However from what I can tell regarding the app I'm testing is by using the android hierarchy viewer I see that all of the views have no ids that were explicitly defined.
As you can see from the screen capture there are views upon nested views. The IDs for them read like 0x17e0 or 0x17de. How can I reference these, specifically, in a robotium script? The end result is I'm trying to get it to fire a click even on one of the Text Switcher views. So far I've only been able to make it work if I give it a pixel point to go to, or if I give it the text that appears in the button (but the text is dynamic and would make for a poor test).
Do I have to use the getCurrentViews() to filter down to the text switchers? Or do I have to figure out a way to traverse the entire tree going from FrameLayout>RelativeLayout>FrameLayout>LinearLayout>TextSwitcher ?
If I have to traverse the entire tree how do I get view upon view upon view?
While I couldn't get the ViewGroup() and getChildAt() methods to work for me I ended up doing something different:
// Grabbing all the LinearLayout views found under view with with id of 'content'
ArrayList<LinearLayout> linearLayouts = solo.getCurrentViews(LinearLayout.class, solo.getView(R.id.content));
// The 4th item in the linearLayouts list is the one I need
View pickerList = linearLayouts.get(3);
// Grabbing the buttons in the pickerList
ArrayList<TextSwitcher> buttons = solo.getCurrentViews(TextSwitcher.class,pickerList);
// Now I can click on the buttons
solo.clickOnView(buttons.get(0));
I will say this is slow. It takes about 10 seconds for the first button click to fire. But once it goes it flys.
I would vote to say you really probably want to get some IDs added somehwere in the hierarchy, it is a one line change that will make your life a hell of a lot easier.
But i am not without help if for some reason you cannot get this done, to do this you are going to have to walk the entire to get to the view you want.
Getting the top level view, you will be able to cast it into a ViewGroup. ViewGroups have a method called getChildAt() which you can then use to get a child at a given index, the index is 0 based and will match what you see in the hierarchy viewer so you can chain together theses commands to get to the view you want to interact with.
I have not used Robodtium very much, but i know that AndroidViewClient does exactly what you want. Here is a code snippet that dumps Id of the home view:
ViewClient(*ViewClient.connectToDeviceOrExit()).traverse(transform=ViewClient.TRAVERSE_CIT)
Here is the result dump:
com.android.launcher2.CellLayout id/cell3 None
com.android.launcher2.ShortcutAndWidgetContainer NO_ID None
com.android.launcher2.BubbleTextView NO_ID Email
com.android.launcher2.LauncherAppWidgetHostView NO_ID None
android.widget.AnalogClock id/0x7f0f0014 None
com.android.launcher2.BubbleTextView NO_ID Camera
com.android.launcher2.BubbleTextView NO_ID Settings
com.android.launcher2.BubbleTextView NO_ID Gallery
com.android.launcher2.LauncherAppWidgetHostView NO_ID None
android.widget.LinearLayout id/0x7f080167 None

Set Link Clickable in Java-Android

I am designing an App, where there is TextView, and based on some check condition, I want to make links / phone numbers in the TextView clickable in Java side. Can I do this, I don't want to make it clickable by default which is done by setting in xml file
android:autoLink="all"
What I already tried:
Create regular TextView and don't set autoLink="all",
TextView myView =
(TextView)view.findViewById(R.id.thisIstheTextView);
myView .setLinksClickable(true);
also tried :
myView .setMovementMethod(LinkMovementMethod.getInstance());
None of the above Java code works, I don't want a predesigned xml TextView with autoLink enabled, I want to change the TextView behavior in Java code based on if () conditions. There is no error in my code, but I am not able to achieve what I want. Can you please share your knowledge. Thank you.
Use setAutoLinkMask (int mask).
The possible values that can be combined for mask are those defined for the Linkify class.
Linkify.addLinks(myView, Linkify.ALL);
Fixed the issue.

Categories

Resources