While I'm developing a custom widget by using TextView widget, this question comes up to my mind.
When mText(member variable of TextView) is displayed, actually?
I have thought that, just like other widgets, if I override the onDraw method in the custom widget, which is derived from TextView, I can draw mText as I want.
But, it's not true.
I'm reviewing Android Widget source, and then I realized that mText is not displayed while onDraw is called, definitely.
Is there someone who knows about it?
take a look at protected makeNewLayout()
Related
I have a widget whose xml layout is simple: an ImageView and a TextView.
I can hardcode the rotation of the TextView in the xml by using android:rotation.
However I want to be able to set the rotation of the TextView programmatically. It seems that a View has a setRotation() method, so the TextView will inherit this method, such that a "normal" TextView can be rotated programmatically used this method.
But when the TextView is buried within a RemoteViews, you have to call the methods indirectly, like setTextViewText() and setTextViewTextSize().
And it also seems that you can call the general setFloat(int viewId, String methodName, float value) to set the value of any methodName, so you can set text size also via passing "setTextSize" to setFloat().
OK to the question....
Since there isn't a setTextViewRotation() method in RemoteViews, I figure I need to call that indirectly using setFloat(viewId, "setRotation", value).
But when I try that, my widget just shows a "Problem Loading Widget" message box.
It works with e.g. setFloat(viewId, "setTextSize", value) to change the text size, so I'm on the right track, but it doesn't work for setRotation in the same place in my code.
I'm guessing that it's because setRotation is an inherited method (from View), rather than a method of TextView itself, but it leaves me slightly stuck as to how to rotation my TextView.
Any ideas?
Thanks!
The reason why you are crashing is because setRotation() does not have the #RemotableViewMethod annotation in the Android source code, and therefore it cannot be set using setFloat().
I am not aware of any way for you to change the rotation property of a View dynamically in an app widget. One workaround is to support N rotations via N versions of your layout file, each of which has a hardcoded android:rotation value.
I have a linearlayout, which contains my TextView I want access.
In this linearlayout there is a listview, where every item is a linearlayout and contains also custom views.
Deep in there is a button with an onclickListener. After performing onClick(), I want to call a method which sets the text of my textview.
At the moment I am doing it like this:
(View)this.getParent().getParent().getParent().getParent().getParent().getParent().getParent().findViewById(R......)
It works, but it looks bad. Is there any possibility to do it a better way?
Image
Yes, its ugly, don't do that.
In your onClick(View v) get hold of TextView by asking Activity holding the layout:
#Override
public void onClick(View v) {
TextView tv = (TextView) YourActivity.this
.findViewById(R.id.your_text_view_id);
tv.setText("blabla");
}
if you do it from fragment instead of activity use :
YourFragment.this.getView().findViewById(R.id.your_text_view_id)
Or simply create a member TextView mTv, initialize it in your #OnCreate and use it everywhere as suggested in comments to your question
It looks bad, because it is generally a bad idea for a view to know something about outside of it self. So it is probably okay to know about your children but not back.
You'd better keep your views as simple as views, without any other logic. Do your logic in Activities/Fragments
See Circular dependency
i do have a fragment and in the fragment a textview. I want that this textview reacts to some events. For example i do have a variable and i want that my textview shows the variabel.
This works pretty fine so far, but if my variable changes my textview does not!
So i guess there must be something like an eventlistener but i haven't found one yet.
Can't imagine that it is hard to solve that problem, but i couldn't find the right listener.
Or is there maybe another recept?
Actually i don't think that it makes a big differents, if the textview is in a fragment or an normal activity!?
THX
I'm not too clear about this and neither are the docs.
When I'm creating a custom view, I override like so:
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
//more code here...
}
My question is whether it's necessary to call super.onDraw(canvas);. The code seems to work fine without it, but I want to be sure that it's okay to leave it out.
So is it necessary?
If you want it to call the superclass onDraw method (think TextView or KeyboardView rather than a generic View), then call super.onDraw. If you don't want that, i.e. you are planning on drawing the entire View yourself (which it seems you are), there's no reason to call it.
Also, if you're extending View (and not some class that extends view), super.onDraw doesn't really do anything.
For me, I call super.onDraw when I want to draw lines over a KeyboardView. So, super.onDraw draws the keyboard, and my custom LatinKeyboardView (which extends KeyboardView) draws the swiping path on top of the keyboard.
A peek at the source code shows that View.onDraw() is an empty method. So, calling super.onDraw(), if the parent class is View itself, does nothing. It's unnecessary yet harmless.
Whether you should go ahead and do it anyway is a separate question of efficiency, safety, and style.
Yes, it is. If you custom a TextView, the super.onDraw will ensure to draw whatever belongs to TextView attributes (like the text, text color, text shadow, etc...) The same with other Views like Button, CheckBox...
In case your custom View extend View (not a specific subclass of View, just View), it is still better to leave super.onDraw(canvas) there for some View's draw methods (like setBackgroundDrawable, etc...)
It's not required unless you are actually overriding the onDraw() method. If the new class doesn't override it, the super's method will automatically be called.
lets assume i have a LinearLayout , horizontal that contains a TextView and afterward a Spinner or another clicable TextView or an EditText.
I want that a click on any part of the line (if the layout has padding then the layout area as well!) will deleage the onTouchEvent to the Right part of the layout (EditText, TextView or Spinner) as if they were clicked themselves.
Doing it myself will require me either create my own versions of those widgets (too much work for little effect :-( ) or putting listeners on many items for the touch events and delegate them. I'm pretty sure Android has some methods or properties to do that, just didn't see any so far.
Can anyone help ?
I had to do something similar to this a while back, and ended up writing my own delegate and assigning the onclicklisteners for all of the components in my layout to that delegate. It's cumbersome, but not too painful to implement, and it turned out well.
Point being, I didn't see anything in the API to handle that sort of thing. The only other thing I might offer is that it is certainly possible to assign an onclicklistener to a component and simply send the event to another component's onclicklistener like so:
thislinearlayout.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
getOtherComponent().performClick();
}
});
You can do the same thing with touch listeners.