How to use textview in Android's PreferenceScreen? - java

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

Related

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.

Modify included layout in Android?

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.

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.

Disable Android ListView selector

I am trying to disable the ListView selector on my ListView. I set the List Selector color to a transparent color. This seems to do the trick, but when I select certain text, they change colors. How can I make things work ok?
In your XML where you declare the ListView use:
<ListView android:id="#+id/my_list" android:listSelector="#00000000" />
Are you looking for ListView 's setClickable(false) and setEnabled(false) method ?
It's not the best solution, but for me it worked:
You can set the text color manually (in xml or at runtime)
You can change the color of selected items to match your background. This question was asked here.
Apply below code to your list view.
android:listSelector="#android:color/transparent"

Setting TextView to new string?

I used the layout resource editor to modify the main.xml layout file to add a second
TextView control to my app. by default, its set to something like #+id/TextView01. How do I Set the text attribute of the TextView control to my newly created String resource? I tried going into main.xml and just editing the android:text to point to the name of my new string, but it didn't seem to work.
you define your strings inside resources/strings/values.xml
and you can reference them using "#string/name_of_string" syntax in your layouts.
android:text="#string/name_of_string"

Categories

Resources