There is compound drawable option in TextView to avoid using a parent ViewGroup and an ImageView next to TextView. Which is neat and faster in performance as said by lint. I'm trying to do something similar. A TextView with 2 texts inside, one aligned to left, one aligned to write. Sounds strange but it will only allowed for single line TextView.
And to do that, I can extend TextView and set its gravity to right side. And inside onDraw let the super class draw on right side, and then draw text on left side.
Problem is, I'm not really sure about all this. My question is, will there be a big performance difference ? I dont have any slower device to test. I will be using this TextView inside an item layout of GridView, Item layout already have many views, it would be nice if I could merge some views to one. But again, will there be a performance difference, like noticeable by user ? And if there will be, the approach I will be using by extending the TextView, is there any problem or I should try some other way ?
Thank you
From what I have read I would imagine that you are using a custom adapter for your GridView?
If so, cant you just use TableRow and insert 2 TextViews inside that with each layout weight set to 1. Then you will have 2 columns in one row?
Just an idea.
Related
I need to implement such a element - TextView of predefined size with white border line at the bottom. Inside of the TextView a button with predefined style and size should be. TextView shouldn'y be clickable and button should be. I see some possible solutions:
1) implement TableLayout and put all my buttons into the table's row. But there's not nice solution - my layout becomes too long. So I think about another solution.
2) make my element as a custom view. But the problem is that I need to use a lot of these elements through my app and I need use onClickListener to each of them. I know how to implement onClick() method for all items but how I can use singular onClick() method for each of the item? And how should I build this custom view - should it be extending from RelativeLayout, where TextView and Button with defined properties should be put?
I also thought about using for the button, but in this case my TextView will be also clickable.
Tell me please what approach is less complicated and more convenient.
I used another way if solving this issue. I put buttons and other control elements into the TableLayout with predefined TableRow style. In this case I do not need implement TextView and borders margins which I need I build with corresponding TableRow style.
I am making a calendar. Each day is a textview with a number denoting the day of the month.
I'd like to overlay another view which signals that there is an event on that date. See below image or think about how the google calendar app looks. How do I do this?
One way is to make the two textview inside a Relative Layout or a Frame Layout. You can look at this for reference.
You could use a TableLayout to store the TextViews in rows and colums.
A possible way of marking a special day would be changing the background resource of the specific cell with one that has a marking on it, that does not overlap with the number.
If you want more fancy stuff, you could write your custom View subclassing TextView, which handles its onDraw calls and everything else.
As far as I know you can use a RelativeLayout to overlap views.
You can try setting left drawable (android:drawableLeft="") to the TextView. OR, you can use some transparent background images with indicators drawn in top-left.
i'm trying to layout a fairly basic screen. it's just a details view screen after a list item click.
initially i'm looking to acheive a side by side stacked label value type of screen.
for example, where to the left of colon is static text and to the right of colon being dynamic values based on row clicked.
first name: john
last name: doe
last login date: yesterday
additional info: blah
i've started with a relative layout with a bunch of text views in it all positioned accordingly to acheive something like what i was looking for.
The problem arises when the dynamic data being displayed grows and stretches its enclosing text view. Of course the static labels don't grow and everything gets mis aligned...
I'm wondering if there is a different way of tackling this sort of layout...
i was shying away from stacking a bunch of horizontal linear layouts inside a vertical linear layout.
is table layout the way to go? i've read that "they usually aren’t the best tool for doing so, as they are derived from LinearLayout and not the most efficient of layout controls".
Yes there is. Use a listview with a layout predefined in xml. Use a simplelistadapter and pass it the dataset that you want to populate it with.
Edit
Here is a great tutorial:
http://www.vogella.com/articles/AndroidListView/article.html
For a simple form like you want to create I'd suggest using a TableLayout. It is simple to use. As you said the alternative to a TableLayout would be horizontal LinearLayouts in a parent vertical LinearLayout. Using a TableLayout will also automatically align the right side dynamic content for you. Everything on the right side will be treated as a column so if one resizes, they all resize to match.
In your particular scenario I would believe that the TableLayout would work well (although I have also heard similar issues of efficiency/performance). As long as the entire Viewgroup of this Activity isn't complex, I don't think the performance will be too noticeable.
If you are attempting to make the RelativeLayout version work, perhaps you can try this: Have all of your static labels are aligned to the left using android:layout_alignParentLeft and have each aligned to the top of the dynamic TextView they are corresponding to using android:layout_alignTop. This should keep the static TextViews aligned to the left while aligned to the dynamic view relative to it.
Now that those views are aligned, we can horizontally align the dynamic views to the longest static TextView using android:layout_toRightOf. From there, all the remaining dynamic views can also android:layout_alignLeft to this anchor dynamic TextView, or also align to the longest static TextView in the same manner that the anchor was. This solves the horizontal alignment of all the dynamic TextViews.
Finally, we can set that each dynamic TextView falls under the next, since the dynamic TextViews are our determining our vertical location within this RelativeLayout. Each view can use android:layout_alignBelow to chain the fields to align vertically.
I believe this should work for you and I can edit this post later in the day if you would like a sample of code.
Good evening,
I am trying to wrap my text around an image view like the question is asking here:
Textview wrap around View
in landscape.
but I wish to not do it in HTML / CSS. plus, that question is 2 years old. I am hoping someone has figured out a work around with this.
Also, I wish to keep my portrait view the way it is with the img view under the text view i.e. no wrapping.
Can anyone help me?
A work around I can think off the top of my head and from previous issues with TextView formatting, the best way is to have, say, a RelativeLayout, and just place 3 separate Views inside of it. It will represent the TextView that is right next to the image, the ImageView itself next to the text, and then the last TextView under the first text, but with the layout_width="match_parent". There is no other way to do this with the TextView or ImageView otherwise.
EDIT:
The only other workaround I believe you can do if you are so intent on using 1 TextView, I would suggest using SpannableStringBuilder with SpannableString. I didn't originally give you that as an option for a specific reason. If you go back to that link you posted on your question, one of the answers say they found an answer. I believe that person linked to a good resource, but they are wrong for what your needs are. When using SpannableStringBuilder, you are essentially creating a string that can have potentially many different version of a single string into one, like 3 different colors on a single string, or strings with 3 different sizes, all in one TextView. Like what you need.
Now the link talks about putting an image and having the text wrap around it nicely, but its not that simple, and that example isn't what you need. What you could do to provide it all in one TextView is to add line breaks after the first few lines where you know the image will be to the right, and then not do so after the image. But the thing is it still needs to go in a RelativeLayout. But instead of two TextViews you only need one. Just place the TextView at the top left of whatever the parent is and the ImageView to the top right of whatever the parent is. This simulates a floating element. But I think doing it that way is more trouble than its worth. It really is because you have to do calculations to figure out where to put the line breaks, etc. You are better off using two TextViews in which to make it look like you need. So in my opinion, DO NOT USE SpannableStringBuilder, but if you do not mind trying to figure out all that spacing, that does give you what you need with a single TextView.
I have a quite problematic UI layout to implement, and I'm not sure if it's even possible using standard UI widgets. It looks something like this:
Picture 1
The green, lined thing is supposed to be a ListView, and the red rectangle is another View. This red View should be scrolled with the ListView, as if it's part of it. Some list-elements should also be narrower, because that embedded View gets in their way. Could you please recommend any ideas? Can this be done somehow with the Android UI framework? I was thinking about some kind of a floating View above the ListView, which reacts to the List's scrolling events too, but it doesn't seem like an elegant solution.
Thanks
I don't think you can accomplish that easily with a ListView. You could do the overlay using a FrameLayout, but it would be very awkward to get it to stay probably aligned as the user scrolls.
How many elements are you talking about?
I would probably use a LinearLayout within a ScrollPane to simulate the ListView.
Or, a TableLayout where the overlayed view is contained within a single, complex row.
I would set the green rows that the red block overlap and the red block as one big view in the listview. So the items in your listview would be (for the example pic) two green rows, then the view of three green rows and the overlapping red block, and then the remainder of the green rows.
Trying to have the red block on an overlay that scrolls with the listview sounds like more trouble than it's worth.