Modify included layout in Android? - java

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.

Related

Dynamically change the android id of an element

Is it possible to change the android id of a view or any of their elements, such as an imageView based on some code? That is, verifying a variable such as a bool and having a different id depending on the value such as image_true and image_false.
I am working on an automation project with Appium and I need to be able to differentiate a success pop-up from an error pop-up. They are essentially the same component but the image is different depending on the state of it.
You can use View#setId with some positive integer as ids are basically integers
Your answer is not clear enough but it's possible to change it by #setId method of View. And consider this issue that changing the view id when runtime may cause errors inside the code if it is using that view by hardcoded id so it can't be found by those components anymore.
An alternative is to dynamically change tags using View#setTag which can accept strings. To access the view using tags, you can use findViewWithTag.
Sample code to access view using tag:
LinearLayout layout = findViewById(R.id.layout);
TextView textView = layout.findViewWithTag("someTag");

Insert multiple items in a layout

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.

Java & Android: Best way to create a dynamically expandable list of EditTexts in an activity?

as I'm very new to Java and Android programming, I'm slowly getting used to the Android studio and the resource system, xml management and so on..
I'm trying to create an activity with a start count of 10 EditTextes, vertically listed in a LinearLayout.
Till this point, I can manage everything with a XML file & hard-coding the 10 EditTextes.
The problem with it is, that I'd like to provide a button that can be clicked by the user to automatically expand the list, for example add 10 more EditTextes to the list while runtime.
AFAIK, I can't solve this problem with XML only.
I know, I can get the layout (XML layout) to an object in my MainActivity class using LayoutInflater().inflate...
Here's my question:
Is it the right way to define the layout in a XML file and modify it with the method described above or would it be better to create the whole layout with Java in my class:
LinearLayout layout = new LinearLayout(this);
// add EditTexts
// set attributes
//setContentView(layout);
Are there any disadvantages using this way or is it the same as doing it with XML ? I mean, to address the created EditTexts later again, I also do have to pass an ID to them, so I also have to create an ids.xml where all those IDs are listed. That sounds like much work for me..
Am I on the right way or are there better options to create/manipulate layouts?
If you want to fill a listview with multiple views, it'll be best to use an Adapter. An Adapter can be used to fill a list, and add rows to this list.
If you want a tutorial on how to create a list: http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
The best part about an Adapter is, that you are able to create a custom layouts for it. So if you want to have an List Item with only EditTexts or ImageViews, it's easy to create.
In the end, you'll only have 1 layout file for the row and 1 layout file which will contain the list.
Inflating a layout is always easier than to create them like " LinearLayout layout = new LinearLayout(this); // add EditTexts // set attributes //setContentView(layout);"
There are a lot of tutorials on how to create an Adapter, you'll get it!

How to use textview in Android's PreferenceScreen?

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

Replicate a view inside a xml

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.

Categories

Resources