set the textsize of a listview - android

I have a custom listview and have defined a style for it. I am not able to change the textsize of the items on listview. I am trying to change it through layout so that it can dynamically change depending on device.
styles.xml
<style parent="#android:attr/listViewStyle" name="ListStyle">
<item name="android:background">#android:color/white</item>
<item name="android:divider">#drawable/divider</item>
<item name="android:textSize">26dp</item>
<item name="android:textStyle">bold</item>
</style>
xml layout
<TvList style="ListStyle"
android:id="#+id/mainview"
android:layout_height="300dp"
android:layout_width="wrap_content" />
I know that i can use setTextSize in my code for changing the font size . But i want to change it applying styles.
Any suggestions on how to go about changing it?
Thanks

The size of the text within the List View is defined by the ListView's adapter's item resource. If you create a custom view for the list view item and define a TextView, you can change the size of the font within that text view, which will define the size of the text in each item within the List.

I would say the best way is to define resource files that you want and then override them in different configurations. For example, create a values/dimen.xml file and define:
<dimen name="text_size_medium">18sp</dimen>
and then create another file values-large/dimen.xml which defines
<dimen name="text_size_medium">22sp</dimen>
Then you just say in your styles.xml:
<item name="android:textSize">#dimen/text_size_medium</item>

Related

How do you set a default textColor inside a LinearLayout that contains multiple TextViews?

I have a LinearLayout with that has multiple TextViews and want to set up a default global color for that layout only without having to add a textColor field inside each TextView. Also, if it's possible, would it also be possible to override the color value by adding it inside the TextView? i.e. If I set blue as a default color and black for a single TextView, would the blue change to black?
To set the default global TextView colors, first you can create your own theme in AndroidManifest.xml file for the following items:
textColorPrimary - for Large texts
textColorSecondary - for Medium texts
textColorTertiary - for Small texts
textColorHint - for Hint texts
For example, in AndroidManifest.xml:
<style name="TextViewTheme" parent="android:Widget.TextView">
<!-- Set the default global color for TextViews to Holo Blue Dark -->
<item name="android:textColorPrimary">#android:color/holo_blue_dark</item>
<item name="android:textColorSecondary">#android:color/holo_blue_dark</item>
<item name="android:textColorTertiary">#android:color/holo_blue_dark</item>
<item name="android:textColorHint">#android:color/holo_blue_dark</item>
</style>
Next, set the theme style on your LinearLayout. You can also override the default for a single TextView to black color, like the following which set the first TextView Hint text color to black in activity_main.xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:theme="#style/TextViewTheme">
<TextView
android:id="#+id/phone_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/phone_tv"
android:textColor="#android:color/black"
android:textColorHint="#android:color/black" />
<TextView
android:id="#+id/email_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/email_tv" />
</LinearLayout>
Hope this helps!
You can override default text colors for the entire application by setting textColorPrimary and textColorSecondary in your parent in styles.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="textColorPrimary">#color/black</item>
<item name="textColorSecondary">#color/grey</item>
</style>
If your TextViews are indeed very many to the extent that calling setTextColor() on each of them would be a herculean task, why not use a view that supports an adapter (i.e ListView, RecyclerView etc). Your TextViews would show up the exact same way as you intend them to appear with the LinearLayout.
While using an adapter, you can set up a model TextView layout and set a global textColor for all the TextViews. You can override this global textcolor in your adapter by using simple if and else statements.
I hope this helps.. Merry coding!
This code will work even if you add or remove TextViews from your layout. Just put it in your activity's onCreate();
LinearLayout layout = (LinearLayout)findViewById(R.id.layout);
for (int i = 0; i < layout.getChildCount(); i++) {
View v = layout.getChildAt(i);
if (v instanceof TextView) {
((TextView) v).setTextColor(Color.BLACK);
}
}
change the color to what you like.
If you want after this code you can change the color for any specific TextView.
Pretty late answer but this does the trick.
Inside style.xml lately changed to themes.xml
<style name="ErrorStyle">
<item name="android:textColor">#FF0000</item>
</style>
Then inside your xml layout
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="#style/ErrorStyle">
.. All of your TextView
</LinearLayout>
Using this approach we can use other xml variation such as night mode, etc... and we still have te possibility to override the internal TextViews.

Changing the font for the List Preference

i have a list preference in my application.
Have extended with a custom list preference.
Now i would like to keep the font of the preference entries according to my styling . (Don't want to have it changed according to the Device's fond settings).
I don't want to change any UI but only the styling like font size , font type etc.
Is it possible to do so. ?
cheers,
Saurav
You can create a custom style in XML, in a <resources> tag (not the listView layout), and then in the single item XML layout you can add Style = "#Style/mystyle" etc.
For example, there is a style.xml file already in values, and you could add:
<style name="ListViewStyle" >
<item name="android:textColor">#000000</item>
<item name="android:textSize">20dp</item>
</style>
and then in the listView xml:
<TextView
style="#style/ListViewStyle"
android:id="#+id/mainListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

How do i change from the default look of a list activity list?

I have created a list activity class and made an array adapter. However, how would i change the appearance of the list it generates when run. As in how do i change the colour, style etc.
Thank you in advance.
You can create a styles.xml in your values folder then do something like
<!-- ListView -->
<style name="List_Item" parent="#android:style/Widget.ListView">
<item name="android:background">#drawable/list_item_bg</item> // if you have a drawable you want to use
//add whatever other attributes you want
</style>
then in the xml with your ListView delcare your style
<ListView
android:id="#+id/listID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/search_plate"
style="#style/List_Item"/> //here you implement your style
You can use Theme & Styles and xml attributes for ListView or any View inside your ListView

Different formats for different Textview defined by Custom style

I have a problem figuring out how to do this:
I am currently coding an app that comes with different themes (User can select the complete appereance of the app out of a list of different styles).
Then the list item is selected I want to call setTheme(R.style.Own_App_Style0); to change the complete appearance.
The problem is best explained by an example:
Lets say we have 2 TextView.
Theme1
1. TextView: TextColor should be green and TextSize 15sp.
2. TextView: TextColor should be red and TextSize 10sp.
Theme2
1. TextView: TextColor should be blue and TextSize 10sp.
2. TextView: TextColor should be yellow and TextSize 10sp.
Of course I know that by setting <item name="textViewStyle">#android:style/Widget.TextView</item> I can change the default appearance of TextViews.
But how can it be done to have lets say two (ore more) different types of TextView with different applied styles (and by xml)?
Found a solution (basically in this answer setTextAppearance through code referencing custom attribute). In case anyone else has this problem I shortly explain:
Declare in style.xml a attribute and in the actual style definition asign a value (reference) to this attribute:
<declare-styleable name="CustomTextView">
<attr name="mainTextView" format="reference"/>
</declare-styleable>
<style name="appstyle0" parent="android:style/Theme.Holo.Light">
<item name="#attr/mainTextView">#style/CustomTextViewAppearance1</item>
<item name="android:textViewStyle">#style/CustomTextViewAppearance2</item>
</style>
<style name="appstyle1" parent="android:style/Theme.Holo.Light">
<item name="#attr/mainTextView">#style/CustomTextViewAppearance2</item>
<item name="android:textViewStyle">#style/CustomTextViewAppearance1</item>
</style>
<style name="CustomTextViewAppearance1">
<item name="android:textSize">10dip</item>
</style>
<style name="CustomTextViewAppearance2">
<item name="android:textSize">30dip</item>
</style>
Now in the layout all textViews are like CustomTextViewAppearance2 (because this is set as standard in this style. And the textViews that should use the other style write into the definition:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="blablabla"
style="?mainButtonTextView"/>
When you now call .setTheme (after restart the activity) the appearance of the textviews switch. Like this method you can define as many different types of View styles and switch between them only by calling .setTheme.
Unfortunately, styles are static once they are defined. To have an entire cascade of styles modified programmatically, you would have to change the definition of the style itself. Instead, all you can do is change the style that is assigned to a TextView (or whatever style-able object), as outlined in the question I linked to in my comment above.

How do I change the text style of a spinner?

I'm creating a spinner in my layout xml files and setting an string array to this spinner.
If I change the textstyle of the spinner the text is not affected by the changes.
I read in the googlegroups that a spinner has no text and therefore the textstyle can not be changed and I have to change the style of the textview that is shown in the spinner. But how can I do that. Preferably in my xml file.
As my predecessor specified, you can't do it on the main XML layout file where the Spinner component is.
And the answer above is nice, but if we want to use Google's best practices, like you know... to use styles for everything... you could do it in 3 'easy' steps as follows:
Step 1: You need an extra file under your layout folder with the look for the Spinner's items:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/textViewSpinnerItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/SpinnerTextViewItem"
xmlns:android="http://schemas.android.com/apk/res/android" />
Name this file: spinner_item_text.xml
Step 2: Then, on your Activity Class when you are filling the Spinner with an array of items:
adapter = new ArrayAdapter<CharSequence>(this, R.layout.spinner_item_text, items);
spinner.setAdapter(adapter);
Note that the R.layout.spinner_item_text resource is in your own R's file.
Step 3: Under your values folder, create or use (you might have one already) the file styles.xml. The style entry needed should look like this one:
<style name="SpinnerTextViewItem" parent="#android:style/Widget.TextView" >
<item name="android:textSize" >8dp</item>
<item name="android:textStyle" >bold</item>
</style>
And that's it!
So far it has been really, really handy to put all about text sizes, styles, colors, etc... on a styles.xml file so it's easy to maintain.
Via XML Only
As a follow-up to #Cyril REAL's excellent answer, here is a thorough implementation of how to style your Spinners just through XML if you're populating your Spinner via android:entries.
The above answers work if you're creating your Spinner via code but if you're setting your Spinner entries via XML, i.e. using android:entries, then you can adjust the text size and other attributes with the following two theme settings:
In your res/values/styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppBaseTheme" parent="android:Theme.Holo">
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- For the resting Spinner style -->
<item name="android:spinnerItemStyle">
#style/spinnerItemStyle
</item>
<!-- For each individual Spinner list item once clicked on -->
<item name="android:spinnerDropDownItemStyle">
#style/spinnerDropDownItemStyle
</item>
</style>
<style name="spinnerItemStyle">
<item name="android:padding">10dp</item>
<item name="android:textSize">20sp</item>
<item name="android:textColor">#FFFFFF</item>
</style>
<style name="spinnerDropDownItemStyle">
<item name="android:padding">20dp</item>
<item name="android:textSize">30sp</item>
<item name="android:textColor">#FFFFFF</item>
</style>
</resources>
When you create the Adapter that backs the Spinner you can set a layout for the spinner item.
spinner.setAdapter(new ArrayAdapter(this, R.id.some_text_view));
You can style some_text_view the way you want.
<TextView android:id="#+id/some_text_view" android:textStyle="bold" />
Actually you can customize spinner's text by xml.
In your own style, define a :
<item name="android:spinnerItemStyle">#style/yourStyleForSpinnerItem</item>
and define this style also :
<style name="yourStyleForSpinnerItem">
// Stuff you want for the item style.
</style>
When you instantiate the spinner's adapter in the java code, you can use the default android.R.layout.simple_spinner_item
If you don't want such workarounds, there's a simple way. Get the textview from the spinner and change its parameters:
TextView tv = (TextView) spin.getSelectedView();
tv.setTypeface(Typeface.DEFAULT_BOLD); //to make text bold
tv.setTypeface(Typeface.DEFAULT); //to make text normal
if you want just change background of popup View in spinner, call this method:
spinner.setPopupBackgroundResource(R.color.pa_md_white);

Categories

Resources