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.)
Related
Recently after getting back into Android development, as I was reviewing some code, I noticed that code would reference a control directly by its id without the need to use findViewById. So if I had a textview with an id of tvUsername, I could just call:
tvUsername.setText("john");
In the past I was always using findViewById but am now wondering whether the ability to use a control directly without calling findViewById always existed, or did Google start supporting directly referencing it after some version.
Is findViewById still needed
Is findViewById still needed if you are using JAVA than it is needed,
But if you use Data Binding Library then no need to do findViewById
Also in KOTLIN the findViewById is not needed
You can directly use it like this
tvUsername.text ="john"
for more information read
Kotlin Android Extensions
Goodbye findViewById, say hello to Synthetic Binding
Before Kotlin, Android developers supposed to save reference to the Activity's Views in a variable like this:
Button fooBtn = (Button) findViewById(R.id.btn_foo)
to reduce the amount of the boiler-plate code and the number of findViewById calls.
With the introduction of the Kotlin's Android Extensions we can reference the same Button by simply using:
btn_foo
Questions:
Does the btn_foo have a reference to the Button saved, or does it call findViewById every time?
Do developers still suppose to use variables to store btn_foo to improve app's performance, or just use it directly in the code?
Edit: there is an explanation how Extensions work, however it is still a bit unclear.
It's cached, so findViewById isn't called every time you need it. Storing the variable won't definitely improve the app's performance
One of the Kotlin Android Extension (KAE) developers Ihor Kucherenko confirmed that:
KAE will keep a reference to the view after the first call, instead of using findViewById all the time. That works only for Activities and Fragments.
KAE will not cache data and will use findViewById every time for any other element (except for an Activity/Fragment).
So in case you are going to init a ViewHolder:
class FooViewHolder(view: View): RecyclerView.ViewHolder(view) {
fun bind(day: FooItem.Day) {
btn_foo.text = day.title
}
}
Decompile into Java call will look like:
((Button)this.itemView.findViewById(R.id.btn_foo)).setText((CharSequence)day.getTitle());
which is exactly what you want to avoid.
The developers might be aware of this.
Conclusion: fill free to use KAE without any additional variables, but only for your Activitiies/Fragments.
I've been working on android for few months, during my spare time, so probably i'm going to ask stupid questions.
In a class, I create an object, for example a button, and I'd like to 'reach' it from others classes.
Of course i could do it this way:
public class CreateButton{
public void createButton(){
Button myButton = new Button(context);
//I can "pass" my object to another class this way
ManageButton manageButton = new ManageButton(myButton);
// or this way
manageButton.writeButtonTextMethod(myButton);
}
}
or this way
public class CreateButton{
public Button createButton(){
Button myButton = new Button(context);
return myButton;
}
}
public class ManageButton{
public void writeButtonTextMethod(){
CreateButton createButton = new CreateButton()
Button myButton = createButton.createButton();
myButton.setText("W");
}
}
I'm wondering if there's a way to 'reach' myButton, created in createButton.class directly from ManageButton.class (and other classes).
With the code above, I have to 'call' createButton.class from ManageButton.class or vice versa to be allowed to manage myButton.
I can do it making myButton static, but it's not correct make view static.
For example, i can easily reach variables created in a class that extends Application. Is there something similar for views?
Much people/guides/articles claim is possible and correct sharing objects (and fields) between activities by using Application, this way:
Such people are idiots, as myButton will introduce a memory leak unless this is done very carefully.
So, I'm still not sure how i should share objects (TextView, Button, ImgaeView etc.) among different activities.
You don't share widgets between activities. At most, you share model data between activities (or, better yet, pointers to centrally-stored model data).
In a Web app, you do not share DOM nodes between Web pages, as that is not possible. At most, you share data passed as GET parameters and the like between Web pages, or you store data in local central storage (cookies, local storage, IndexedDB, etc.). Android activities are like Web pages -- they are loosely coupled.
If you have pieces of your UI that are so tightly coupled that they absolutely need to share widgets, they should not be separate activities.
Views are not meant to be shared across Activities. Even if you do disregard this and share them, you're more than likely to run into a WindowManager$BadTokenException, which essentially means you can't alter Views outside of its parent Activity.
However, to answer your question in a more general way about sharing non-view objects, the Application class is the right way to go. However, making the objects static is not the right way to do. Instead, make them instance variables, and access them in in your various activities by using:
Application application:
//Below line is in onCreate()
application = this.getApplication();
After that, simple access it using
application.myObject; //Or you could use getters and setters. Your choice.
Not quite sure why would u like to share one button between two activities?!
It is either button in ActivityA or ActivityB? No need, or use to do it..You shouldn't do it.
EDIT: This is more Java Basics topic then Android.. And from what I understood, u call "class" something that IMHO is method (because u can't CALL the class and because what did you described).
Best advices I can give u here:
Study Java basics (methods, classes, objects, constructors, inheritance..),
Study Android basics (simple examples with 1 activity til u understand it good),
Make MyButton Class which will extend standard button,
Create button(s) and change their properties
This few tasks will make u busy for some time. Take it easy because it will be easier later with complicated apps. Hope I helped you. Cheers
EDIT2: In last example u wrote (in comments to this answer), nothing make sense because as we wrote u before u don't share button between two activities. Pause! Read it again: You should't share button between two Activies. Questions u asked are not clear because for A and Z u can't say just class and "share" button between them. Which class? no line of code written so it is really difficult to figure out what u want.. :s
LAST LAST EDIT: Man.. Please understand that u r missing basics of Java. No bad intentions here, just try to guide u and tell u exactly what u should study. Why? Because it doesn't surprise me that you are confused to explain what u mean when classes are with one method named exactly the same.. They are just objects "doing something", u want to "manage" them by adding variable in constructor which doesn't exist etc.. Learn, next time write immediately code to save time for all of us. Cheers and enjoy learning
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
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.