Is it somehow possible that instead of:
Button btnNextWord = (Button) this.findViewById(R.id.btnNextWord);
Eclipse automatically generates for me something like:
Button btnNextWord = this.btnNextWord;
or
Button btnNextWord = R.id.getBtnNextWord(this);
??
No, because what you're doing in requesting a reference to a child element that has a certain name and Eclipse isn't actually loading in the layout xml so it can't know what is in it.
I know this is a very late response but it might help someone who read.
You can use the android annotations library for doing something similar to what you want.
As you can see on their page, you can write a field in the code like this:
#ViewById
ListView bookmarkList;
The library will findById an item with id R.id.bookmarkList and cast it to ListView.
Or you can use the annotation like this
#ViewById(R.id.myTextView)
TextView someName;
if you want to give a better name to the field.
DISCLAIMER:
I never used the library so I'm not sure whether you can use just that annotation or you're bound to use their whole set of annotations.
Related
Is that possible to do getActivity().findViewById() using #Bind. I want to bind view in another fragment using this way. But so far it can only be done in standard way :
Button mSubmit = (Button) getActivity().findViewById(R.id.btnSubmit)
I'm not sure I fully understand the question, but it seems like using the two argument form of ButterKnife.bind would work -- pass getActivity() as the 2nd argument.
(I suspect this may have already been answered -- if someone wants to find an existing answer, please do.)
I want to be able to set the text of buttons using the strings xml file. I have this code;
Button playVid = (Button)this.findViewById(R.id.vidbutton1);
playVid.SetText(this.getApplicationContext().getString(R.string.play_video));
And this xml
<string name="play_video">Play Video</string>
But I get the compile
error: cannot resolve method settext(java.lang.string)
I am using Android Studio. Everywhere I have read suggest that you can use strings to set text (makes sense, right?), so I am very confused.
This also will not work:
playVid.SetText("Test");
Bug in AS?
Mind your casing. Use setText() instead of SetText().
Also there's an overload setText(int) that takes in a resource id. You can use it to set a value from resources without using getString() to obtain it yourself first.
Methods in Java usually start with a lower-case letter. Maybe that's what your problem is here.
Try playVid.setText("Test"); instead of playVid.SetText("Test");
This works perfectly fine:
Button button = (Button) findViewById(R.id.some_button);
button.setText(R.string.hello_world);
Make sure you imports are correct ;)
I'm new to automatization, Android, Selenium, Appium and xpath, too. I know it's suck a great beggining.
I write tests for Android devices, but the application I have to test have a lot of costum views. I found out the best way to interact with these custom items is to put an "android:contentDescription" field in the Views. My only question is how to get access to the element with have a specified contentDescription? This is az android specific question, I'm not even sure that the content-desc is the field I'm looking for.
I have the hierarchy provided by Android UI Animator Viewer:
http://i.imgur.com/NUGc56o.png
The ways i've tried:
xpath: //*[contains(#android:contentDescription,'example text')]
I was able to get access by finding them as an ImageView, but as I mentioned I need to work with custom Views
My code looks like somtihng like this:
driver.findElementByXPath("//*[constains(#content-desc,'Login')]").click();
Thanks for the help!
You could also try using Accessibility labels or the UIAutomator locator strategy.
Here's Appium's documentation on those.
Your xpath is incorrect. It should be: "//android.widget.ImageView[#content-desc='Login']"
Here's some pseudocode of what you should do:
login_image = driver.findElementByXPath("//android.widget.ImageView[#content-desc='Login']"); // Gets you the WebElement
print login_image.getClass(); // Just for debugging, make sure it's not nil/null
login_image.click(); // Click on it!
I am trying to create an EditText with auto-capitalization and auto-correction implemented. I have manually figured out how to add InputFilters to allow auto-capitalization, though this only works after the first letter is typed, and I have had no luck with auto correction (I tried to create an InputFilter that used AutoText, but I'm not sure how all that works). Ideally, I could just use EditText.setInputType(...) to handle everything, but so far this has not worked. Is there a way to achieve this? My failed attempt is shown below (I just get normal input).
EditText mEditText = new EditText(this);
int inputType = InputType.TYPE_CLASS_TEXT;
if (auto_capitalize) {
inputType = mEditText.getInputType() | InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS;
}
if (auto_correct) {
inputType = mEditText.getInputType() | InputType.TYPE_TEXT_FLAG_AUTO_CORRECT;
}
mEditText.setInputType(inputType);
Please note, I am only interested in solutions for creating this EditText in code - not via XML.
Edit
I found sound new documentation describing TextKeyListener, however after trying to use this:
mEditText.setKeyListener(new TextKeyListener(TextKeyListener.Capitalize.CHARACTERS, true));
and using #farble1670's idea of using setRawInputType, so as not to affect the KeyListeners, there is still no change to the text.
Through XML it would be setup like so.
android:inputType="textMultiLine|textNoSuggestions"
You simply add a pipe (|) between variables. I see you were doing it through code but I was just throwing this out there for reference.
I hope you've found an answer to the question. The answer might help those those come to the thread later. So, you can set multiple tags in similar manner as you do in XML using a | (pipe).
Something like:
EditText mEditText = new EditText(this);
mEditText.setInputType(InputTpe.TYPE_TEXT_FLAG_CAP_CHARACTERS|InputType.TYPE_TEXT_FLAG_AUTO_CORRECT);
Also, depending on your situation you might want to use setInputTypeor setRawInputype.
Yes, it seems like that should work. However, looking at the docs,
The type of data being placed in a text field, used to help an input
method decide how to let the user enter text. The constants here
correspond to those defined by InputType. Generally you can select a
single value, though some can be combined together as indicated.
Setting this attribute to anything besides none also implies that the
text is editable.
http://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType
So it looks like in general, you can't expect to set two values. The above link shows which flags can be combined together.
Also, if you look at android:setInputType, it says this maps to the setRawInputType() method, not setInputType(). You might try calling setRawInputType() in stead of setInputType().
http://developer.android.com/reference/android/widget/TextView.html#setRawInputType(int)
I'm looking for a way to bind a visual component, lets say a TextView and some value.
I have a background service that changes the value and I want that change to be reflected on a TextView in an automatic "Flex binding" way.
There is any Android built in tool to do that?
I have not tried it myself, but take a look at this: http://download.oracle.com/javase/tutorial/uiswing/events/propertychangelistener.html
And this: http://download.oracle.com/javase/tutorial/javabeans/properties/bound.html
And this: http://developer.android.com/reference/java/beans/package-summary.html
It looks as though you can implement your 'value' as a bound property, and then register an onPropertyChangedListener, wherein you would then update your TextView.
Am not sure if I understood your problem correctly, but here is one way to get auto-binding kinda stuff.
Create a Model class and a static variable on that. Use your TextView.text to populate using this ModelClass.staticTextProperty. Now, whenever you update this ModelClass.staticTextProperty using any background service, it will be updated in the view.
Hope it helped.
I do not know how Flex does it exactly, but greenInject may offer something similar:
https://github.com/greenrobot/greenInject/wiki/Value