I am doing a very simple feature like adding ellipsis at the end of a TextView.
I could add the feature in onMeasure(), onLayout() and OnGlobalLayoutListener() call. But I really wanna know what's the difference by implementing the same functionality but in these three different method.
Are there any preferences or pros and cons for choosing these different implementation ways?
Thank you
Try to follow this it will help
The layout process consists of two passes, measuring and layouting. A
bit simplified we can say that the measure pass sets how big the view
should be, the dimensions of it, and that the layout pass sets where
to place the view, the position of it. The layout part is only
interesting for views with children, in other words views that
inherits from ViewGroup.
Related
I am going to start one app where my activity page will contain "n" grouped views. Grouped view means "collections of views (i.e. One group can have TextView+Button+ImageView)". So the page will have "n" number of such grouped views.
I need suggestions like what would be the best practice to implement this. I could think of below ones:
1) Should a ScrollView be used (Then I will have to create groups in runtime and place one under another)?
2) Or a ListView be used (Then how can I accommodate the height of each row as grouped views height may differ from each other?)
Or is there any other way I can go along with?
Appreciate the suggestions and any sample examples if have. Advance Thanks.
Both options would work, it really depends on your use case.
Place a vertical LinearLayout inside of a ScrollView and add your grouped-views to the LinearLayout. I would recommend this if you have a relatively small number of such views (not necessarily a fixed number, but small enough that you wouldn't have to scroll many "pages" to see them all). Make sure the ScrollView has android:layout_height="match_parent" and the LinearLayout has android:layout_height="wrap_content".
If the number of grouped-views is not small, you could use a ListView and make an Adapter for it. This lets you take advantage of ListView's automatic view recycling when items get scrolled off screen.
For either case, make an XML file for just the grouped-views. In code, you would get a LayoutInflater object (usually by calling Activity.getLayoutInflater()) and call inflate(R.layout.your_grouped_views, null). If using the LinearLayout, you would add it in code with one of the LinearLayout.addView(..) methods; if using the ListView, your adapter would return the whole thing from getView(...).
create one xml layout containing the constant elements of your group view.
in you main xml layout which will be the contentView of your application, put a ScrollView and a single LinearLayout.
then in the program inflate as many views of your group view as you want.
For your answer i want to give you referance of this website, on this website you can learn create dynamic view in android...
i have to draw a ball inside a view (or anything else that is good for this task).
I have followed some tutorial about the matter, but every tutorial that i have found
uses only one view (that is shown on the screen without the use of a layout).
But in my Activity i use a layout, that is composed by many views, and i want to draw
only on one of them.
here a little mockup!
Anybody knows a way to do it?
Is the view the wrong container to do it?
Thanks is advance!
Bye
...
You should extend a view that inherits from ViewGroup. this kind of view lets you handle a view that contains other views. with that, you can control the drawing, measures and layout of each of it's children separately
Your mockup can be made with a LinearLayout, which will contain some TextView's and you own View (the one which is containing the ball).
I'm surprised by this question, because there are many examples over the net explaining how to build a layout containing multiple views.
I have a LinearLayout that comprises some number of TextViews. I set
android:gravity="center_horizontal"
for the parent Layout and result looks as below (for N=2):
I want it look like
In other words, I want them to be aligned to the left. More precisely, my plan is to find the longest TextView and then align other TextViews to the left bound of that view. Can anyone explain me in what callback of my Activity can I do it? I tried
public void onCreate(Bundle savedInstanceState)
but
textView.getWidth()
(that I use to find the longest TextView) returns 0 for all textViews.
The second question is: what method should I use to move a TextView "n" pixels left?
Hmm, it looks like the easiest route to get to what you want is embedding the LinearLayout in another Layout instead of attempting to do it by hand.
Possible approach:
RelativeLayout with gravity:center
Linear Layout with gravity:left and wrap_content as size
TextView1, wrap_content
TextView2
...
This should do the trick!
BTW, if you try to do anything with layout, overriding onCreate will never work! The layout will only be defined afterwards, when the drawing framework completed one "onMeasure" and one "onLayout" cycle. A (hacky) way to ensure that, is simply postpone that code (e.g., use "View.postDelayed(..)". A better way would be to have a custom, invisible View that is added in the onCreate method. When its onDraw was called for the first time, you can be sure that at least the first layout cycle was completed. All Views should then return their actual size.
In terms of moving Views: Did you have a look at "View.setOffset"?
I began to study Android not so long ago and have a question relating to which approach I should use to solve the simple task. Let's suppose I have a view (maybe, a button) and I want user to be able to move it across the screen with a finger. Until AbsoluteLayout was deprecated, the right approach seamed obvious. I would've just changed position of my view based on corresponding events. But what is right now?
Create a custom view of your own and add a onTouch event listener. It is very simple. Explained very well here.
If you're trying to move your views in order to navigate through your application or page through images in a gallery, Android provides a collection of widgets to do things like that. If you're trying to, say, pan across a large image, maybe this will help: Image in Canvas with touch events
Im using FrameLayout and update marginLeft and marginTop
You can add multiple children to frame layout, though this is not recommended because of multiple resolutions issues.
from the android docs:
FrameLayout is designed to block out an area on the screen to display
a single item. Generally, FrameLayout should be used to hold a single
child view, because it can be difficult to organize child views in a
way that's scalable to different screen sizes without the children
overlapping each other. You can, however, add multiple children to a
FrameLayout and control their position within the FrameLayout by
assigning gravity to each child, using the android:layout_gravity
attribute.
This "warning" is not applicable in your case because you are explicitly setting the margins according to the user touch event.
In Android, Which is the light weight view ?
ex:- View, Textview, Edittext, etc....
In some case we need to use a view to fill the area without showing the view to the user.
At the same time the screen should load fast.
You can use Space.
android.widget.Space
Space is a lightweight View subclass that may be used to create gaps between components in general purpose layouts.
If by "lightweight" you meant memory footprint, then there is none exists on Android, because each view have to derive from View, which itself is a massive object (well, not massive, it is about 8kB), so it's not that big.
But in terms of measure, layout and draw time the basic View performs well. You just have to set its visibility to INVISIBLE. And so it will be measured and put into the layout (contrary to GONE with which the view would not take up any space).
Unfortunately ViewStub is not meant to be used for this purpose. Its default visibility is GONE.
If you are really picky, then you can extend View and override methods like draw() (to do nothing, do not even call super), dispatchDraw(), setVillNotDraw(true), etc. (Take ViewStub as a sample).
You should have a look at ViewStub.
Use ViewStub if it is sufficient or LinearLayout which may be somewhat light weight.