Is possible to generate/inflate a view declared within a layout?
For example: I have a complex layout with a button defined inside and I want to duplicate that button and insert it in a GroupView. Is there a better way of doing that rather than inflate the whole xml layout for only picking one of the elements? Thanks
No, it's not possible but you can extract that view or part of the layout which you think you are gonna re-use many times and including it where your need through the tag:
<include android:id="#+id/id_layout" layout="#layout/layout_xml" />
Take a look at the documentation.
Related
Im developing an Android app in Java using Android Studio. I have a layout called activity_way_bill, where it must show a list of trips. Also, I have a layout called item_waybill_trip, where I have labels for displaying the trips details.
I need to insert X item_waybill_trip layouts into the activity_way_bill layout (the X number I will know at runtime). Right now, I just have it included like this, in the XML file:
<include android:id="#+id/trip" layout="#layout/item_waybill_trip" />
But this is a static solution and only allows me to include 1 (or a predefined number) of layouts. I need to include X, and set different texts for each one. How can I do this?
If you want to display the list of element with the same layout but different data than you have to use recycle view.
You can also define the count at runtime and change the count if you needed.
You can visit the site below and check how to use recycle view. :-
https://developer.android.com/guide/topics/ui/layout/recyclerview
As mentioned in the answer by #Mahavir Jain you could use a recyclerview for that but if you want to go the other way, you will have to create the dynamic layouts at runtime and add them to the parent layout using the .addView() method of the parent layout
If your app needs to display a scrolling list of elements based on large data sets (or data that frequently changes), you should use RecyclerView as described on this page.
I'm doing an Android project and I'm trying to figure out how to be dynamic in layouts.
So I have layout files complex_card.xml and simple_card.xml, each containing a ConstraintLayout tag with some other layout elements inside. complex_card takes more space than simple_card so I will use complex_card whenever I have a lot of space and can switch back to using simple_card when I don't have space.
In activity_main.xml, I've use both of the layouts via <include layout="R.layout.complex_card"/> tags. However, in real time, I want to be able to change that to <include layout="R.layout.simple_card"/>. How can I do that?
I've found a work around to this. simple_card is a light weight version of complex_card which means I can use view.visibility = View.GONE and view.visibility = View.VISIBLE to hide certain layouts in complex_card which looks exactly like simple_card.
However, the original problem isn't solved if there are 2 completely different layout files.
I would like to use textview in PreferenceScreen, it's a longer text that explains some specific setting. If I use summary or title attribute on some versions it gets formatted weirdly, doesn't display correctly etc. (The text is rather long).
Therefor I find it would be the best to use textview, is it possible to create custom settings element?
Use this:
<Preference
android:selectable="false"
android:enabled="true"
android:key="example_key"
android:title="example_title"
android:summary="anything_you_want" />
The attributes selectable will decide the click action of the Preference.
You can assign layout resource for your preference in your xml file, using tag android:layout="#layout/your_pref_layout".
Don't forget to use proper ids in your layout (android:id="#+android:id/title", android:id="#+android:id/summary") to assign views to be used as title/summary views.
For more info see for example this: Creating a custom layout for preferences
or this: How to add a button to PreferenceScreen
Yes You can create a layout and use that layout file to inflate as your preference screen you can see a example here.
custom EditTextPreference: can't get TextView by id
I am using an include tag in an Android layout file to include a relative layout row that I would like to repeat a few times. The problem is that after I include the row I cannot modify the text of a textview inside the layout. Is there anyway I can modify the text of a textview that is part of an include? Mainly I would like to insert 4 of these rows with custom text specified in the xml if possible.
You can do it only at runtime. Even if you do it at runtime, make sure you call findViewById from the parent view as Android doesn't allow you to use the same ID at more than one place in an xml file.
You can either add these 4 includes with a different id and get them from code or do what #Anis said and inflate them at runtime and put there your text.
My experience and good books had taught me that repeating of the same thing in code is bad. But when I use Android views, I often repeat the same thing even 4 times:
twice in XML:
<LinearLayout -1
android:id="#+id/lila"
...
>
...
</LineraLayout -2
and twice in code:
3 4
LinearLayout ll=(LinearLayout) findViewById(R.id.lila);
Of course, when I have to change the type of the view, I have to do it in four places, what causes errors. In the XML I could use refactoring to change simultaneously the class of layout or even of a view or widget. But that doesn't help much, because the most dangerous disrepancy, not catched by the compiler, can appear between code and XML. And later I have to waste time and look for the source of a runtime error.
Are there other possibilities to address views from layouts? Not so dangerous?
If not, do you know some trick to change or at least check all these places simultaneously?
Thank you.
Something that could help you (depending on case, of what you want to do for example with your LinearLayout) is declaring your Views as generic as possible. Here, in your example you could keep your LinearLayout as, ViewGroup if you do not need any LinearLayout specific methods. So, if later you decided to change that layout in xml, in RelativeLayout or FrameLayout, etc, you will not get any cast problems.
You could take the same approach for Buttons, or other Views, depending on what methods you use on them. For example if you just set an onClickListener on your Button, you can keep a refrence to it just to an View, (ex. View button=findViewById(R.id.button);), and later you could change it in a ImageView maybe, and still don't have any cast problems, since they both are extended from View.
So the solution in big lines : use a class at the highest level as you can.
for xml layout reusing, you can use include and merge layouts.
http://developer.android.com/resources/articles/layout-tricks-merge.html
As for Activity, you must use super class references as far as possible, because it provides you a flexibility to change specific object class.
Like instead of using we can
ViewGroup ll=(ViewGroup ) findViewById(R.id.lila);
now this can address relative layout, linear layout, table layout, etc.