View within View (Android) - android

So I can pass a custom View to the setContentView() and it fills the users screen by default(right?), but if I define a TextField within my custom views constructor it won't display. Even if I change my custom View to a custom ViewGroup and use its addView() method.
So what am I missing here?
Also, whats the equivalent of System.out.println() in android to get some feedback?

for print statements, used Log.d("Name of the filter", "Text to be printed here"). Also, why aren't you defining your view in an XML file and adding the textview inside there? it is usually not a good idea to use addview because it makes laying things out more difficult (not as many options for location and size, etc.)

Related

How to customize Leanback's ListRows?

Android's leanback library provides a few standard ways to customize a RowFragment, both the Rows and the Cards themselves, but I'm not sure how to add custom elements outside of what is provided on the framework.
What I am specifically trying to achieve is to add a FooterItem to a row, similar to how a each Row has a a HeaderItem.
I'd like to get the existing functionality in ListRowView / ListRowPresenter, but I'm not sure how to do it "properly".
As with almost all things with Leanback, there's more than one way to skin the cat.
We needed to customize the ListRow to add more padding to the header. We've achieved this level of customization by overriding onBindRowViewHolder of ListRowPresenter, grabbing the View for the header (via View header = holder.getHeaderViewHolder().view;). And then we changed the height of it with header.getLayoutParams().height = rowItem.getRowHeaderHeight();.
With this same technique, you could dynamically add your footer view to holder.view (with addView).
Another approach would be following the what the documentation for RowPresenter recommends. The relevant quote is:
When a subclass of RowPresenter adds UI widgets, it should subclass RowPresenter.ViewHolder and override createRowViewHolder(ViewGroup) and initializeRowViewHolder(ViewHolder). The subclass must use layout id "row_content" for the widget that will be aligned to the title of any HeadersFragment that may exist in the parent fragment.
So with this new recommended approach, you'll basically be supplying your own xml. I believe you can even subclass ListRowPresenter. To see how the code works, look at ListRowView to see that it inflates the xml for R.layout.lb_list_row. And then it just uses the view for R.id.row_content.
Then that'll be the actual view that's supplied by holder.view and then you can set text on it and do whatever you want.
Let me know if you need any further clarification.

Android Viewholder implementation

I understand the idea and usage of Viewholder pattern, but still I have one question:
Assume we have one TextView in the viewholder, and 10 items to display ("item0, item1....").
If I call findViewById once, as I understand I have one object of that TextView.
So at first call to getView I inflate the view, find the reference and set text "item0".
At second call I get same TextView and set text "item1" to the same TextView.
Why item 0 text doesn't change?
Is there any cloning in the background?
Is there any cloning in the background?
Android preallocate a number of views that are enough to fill the screen of the device where you are running the app ( a pool of views ), identical from the content perspective but differently from the reference perspective
Assuming that you implement your ViewHolder inside an adapter class and you use the holder in the getView() method, the only thing that is for sure, is that the TextView in your case , describe a slot of the parent structure (e.g. ListView). Once you have defined the slot in an xml, that is inflated from your adapter, there is no cloning or something like that.
According to Google Documentation the holder idea is described as :
Your code might call findViewById() frequently during the scrolling of
ListView, which can slow down performance. Even when the Adapter
returns an inflated view for recycling, you still need to look up the
elements and update them. A way around repeated use of findViewById()
is to use the "view holder" design pattern.
A ViewHolder object stores each of the component views inside the tag
field of the Layout, so you can immediately access them without the
need to look them up repeatedly. First, you need to create a class to
hold your exact set of views.
There is not cloning , only reusability of the view

findViewByTag within a dialog

I've got a custom dialog layout that has two EditText fields and I've initially set the visibility to GONE for both (in the layout XML). In the dialog onCreate I want to do a findViewByTag to locate one of the two EditText fields so I can switch visibility to VISIBLE. Everything works find in the dialog if I switch visibility in the XML but I don't know how to get a reference to the dialog's main View from within the dialog so I can call findViewByTag.
I am inflating the layout in the dialog class's onCreate because that's how the example I found did it. I'm willing to change that if necessary to get the reference in the caller and set visibility before showing the dialog if that's the best way to do it.
Still pretty new to Android so any tips on how best to handle custom dialogs is appreciated.
I'm going to assume this example from outside of a view class.
Dialog amazingDialog = new Dialog(context);
amazingDialog.setContentView(R.layout.amazingdialogcontentview)
MyAmazingView view = (MyAmazingView)amazingDialog.findViewById(R.id.amazingview);
TextView tv = (TextView)amazingDialog.findViewById(R.id.textview);
I'm not sure precisely what your use case is, so there may be a better way to do this if you have access to some member variables you could initialize in onCreate, but if you don't:
You could try
View parent = myDialog.findViewById(R.id.parentId)
to get a known parent view of those EditTexts, and then call
parent.findViewWithTag(myTag)
to find your EditText.
Looking at the way you've phrased your question, and the fact you said you're new at Android, are you familiar with the difference between IDs and Tags?
An ID is a resource number assigned to an item (e.g., a View) by Android when you tell it to give something a name. You'd declare, in your XML:
<TextView android:id="#+id/myTextView"/> <!--with other parameters as necessary-->
And then you'd use
TextView tv = (TextView)findViewById(R.id.myTextView);
to find that TextView.
A Tag is an object that you can attach to a View (which I am pretty sure you can't do by XML), either for finding it later or for persisting some interesting information about it to use whenever you might next look it up (like a data object associated with its contents). So, you might say:
tv.setTag(myInterestingData);
so that you could later look up myInterestingData just by having a reference to tv.
After much reading and trial and error, I've concluded that the only way to do this is to use multiple EditText in the XML, all with visibility="gone". Then, in the Java code, have an if or switch to lookup and show the control either by tag or by ID. I was just trying to force too much abstraction into the Dialog class. With the multiple EditText I can use the class for multiple dialogs instead of having one class for each dialog.

Adding a view on runtime to a ViewFlippers

I have defined two views ExampleView1, ExampleView2, ExampleView3 and ExampleView4 in resources.
In my Activity I have an empty ViewFlipper. Based on doing some logic I want to add either ExampleView1 and ExampleView2 to the ViewFlipper and show the view.
Later I want to add based on internal logic either ExampleView3 and ExampleView4.
How do I do this? Is there some tutorial or can someone help me with example code?
Just use the addView method, which ViewFlipper inherits from ViewGroup. If your views are custom ones, you will have something like this:
flipper.addView(new ExampleView1());
On the other hand, if the views are defined inside an XML layout, you will have to inflate them first:
View view = LayoutInflater.from(context).inflate(R.layout.your_view, null);
flipper.addView(view);

Android Dynamic Content Design

I want to create a pocket reference application. So, much of the content would be texts, linkbuttons and images.
I wonder where is a good place to put all of the contents. I could place it hard-coded on the source code, So, when a user click a linkbutton, a new view will be opened and in the source code I specify the TextView and setText, etc. But I think it's not a good idea. Can I put the content in an xml file or in a database? Which one is better for this case?
I see that we are encouraged to put layout in main.xml. But, from what I read, the xml layout is static, what if I want to put some TextView, but I don't know how many TextView would be displayed, because the content would be loaded dynamically/programmatically?
Thank you.
Not sure it this is what you meant:
You can initialize your application ui by an android xml file layout.
to inflate, you use this method.
in your activity's onCreate()-Method or even later, you can then get the TextViews or whatever you want by calling findViewById(R.id.textview). Note that this method will search all over the layout xml file for the specified id and though blocks the ui thread while searching. if your textview is very near at bottom and many other elements come before it, this can take some time.
if you want to build your own layout dynamically, you have to do this programmatically of course.
for general layout declaring, refer this tutorial on android dev guide.
You could write the textView in a xml layout and inflate it dynamically in the activity as many times you want
View view = getLayoutInflater().inflate(R.layout.scroll_project, null);
//then add the view in linear layout as
layout.add(view);

Categories

Resources