I have a ListView with a TextView in each row. I have a default color.xml with is set in the row.xml
I have different colors for different states
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- pressed -->
<item
android:color="#ffffff"
android:state_pressed="true"/>
<!-- focused -->
<item android:state_selected="true"
android:color="#8b8989"/>
<!-- default -->
<item android:color="#ffffff"/>
</selector>
This works like a charm. But when Im trying to change the color for some rows in code, this doesn't seem to work. The second_color.xml looks just the same, but with different colors. The color is changed, but for the other states (not default) nothing changes.
I change the color like this:
TextView tl = (TextView) v.findViewById(R.id.textlabel);
tl.setTextColor(getContext().getResources().getColor(R.color.second_color));
Solved it!
In order to set this in code it's required to create a ColorStateList.
ColorStateList cl = null;
try {
XmlResourceParser xrp = getResources().getXml(R.color.live_color);
cl = ColorStateList.createFromXml(getResources(), xrp);
} catch (Exception ex) {}
if(cl != null){
tl.setTextColor(cl);
}
if your xml file is saved at /res/row.xml then you reference it with R.color.row
TextView tl = (TextView) v.findViewById(R.id.textlabel);
tl.setTextColor(R.color.row);
Related
I try to create set of TextView programmatically but a want to customize each TextView by xml style. I create TextView instances and place it into LinearLayout.
In my style.xml script I add custom values for text size and color, and all work fine.
But I cant't set text alignment to center by my styles.xml file.
It's my java code snippet:
import android.view.View;
public class GramophoneForm extends AppCompatActivity
{
private LinearLayout m_button_container = null;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
....
....
m_button_container = this.findViewById(R.id.gr_view_container);
List<? extends Object> ct_list = getClassificationTypes();
setObjectsList(ct_list);
}
public void setObjectsList(List<? extends Object> objects)
{
m_button_container.removeAllViews();
for(Object o : objects)
{
TextView item = new TextView(this);
item.setTextAppearance(this, R.style.selectionTextStyle);
////////////////////Is't works fine, but I want set up text alignment by xml style
item.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
////////////////////
item.setText(o.toString());
item.setTag(o);
m_button_container.addView(item);
}
}
}
My styles.xml code snippet:
<style name="selectionTextStyle" parent="#android:style/Widget.TextView">
<item name="android:layout_width">match_parent</item>
<item name="android:textSize">25dp</item>
<item name="android:textColor">#FFFF00FF</item>
<item name="android:textStyle">bold</item>
<!-- don't work -->
<item name="android:layout_centerInParent">true</item>
<!-- don't work
<item name="android:textAlignment">4</item>
-->
<!--don't work
<item name="android:textAlignment">center</item>
-->
</style>
A possible solution for your problem is to create a XML layout with a TextView with the style applied to it, and instead of creating an instance of TextView every time, inflate the layout.
XML example, lets say its named myTextViewLayout:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/selectionTextStyle" />
And in your for loop inflate it and add it to the container:
for(Object o : objects){
TextView item = (TextView)getLayoutInflater().inflate(R.layout.myTextViewLayout, null);
item.setText(o.toString());
item.setTag(o);
m_button_container.addView(item);
}
You should insert a dimension value within the style xml:
<dimen name="buttonHeight">40px</dimen>
Then you simply should reference the dimension value. So in your layout file you'll write:
android:layout_height="#dimen/buttonHeight"
change your parent attribute from this parent="#android:style/Widget.TextView" to this android:Widget.Material.Light.TextView hope it will work!
I want to change my navigation drawer list item's background colour to be changed when i click on it.
Also it should change the text and icon colour of that item as well.
Thanks in advance..
This is quite simple and is similar to changing the background of any view that you use. You can simply create a method and write all the change code into it. Pass the view as a parameter in the method.
Whenever you click on any of the navigation drawer items, pass your view on the method.
For instance, check this method
private boolean viewSelected(View view) {
ViewHolder currentViewHolder = (ViewHolder) view.getTag();
KTextView yourtextView = currentViewHolder.yourtextView;
view.setBackgroundColor(ResourceUtil.getInstance().getColor(R.color.colorSideMenuSelectedBackground));
currentViewHolder.viewSelector.setVisibility(View.VISIBLE);
yourtextView.setTypeface(yourtextView.getContext(), ResourceUtil.getInstance().getString(R.string.yourFontName));
if (lastSelectedView == null) {
lastSelectedView = view;
return true;
}
if (lastSelectedView != view) {
ViewHolder oldViewHolder = (ViewHolder) lastSelectedView.getTag();
oldViewHolder.viewSelector.setVisibility(View.GONE);
lastSelectedView.setBackgroundColor(ResourceUtil.getInstance().getColor(android.R.color.white));
KTextView newTextView = oldViewHolder.yourtextView;
newTextView.setTypeface(yourtextView.getContext(), ResourceUtil.getInstance().getString(R.string.yourfontname));
lastSelectedView = view;
return true;
}
return false;
}
This method will simply change the background, font and color of the views.
Hope this helps!
This can be done using https://developer.android.com/guide/topics/resources/color-list-resource.
Create two drawable files :
1.drawer_background
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="checked_background_color" android:state_checked="true" />
<item android:color="default_background_color" />
</selector>
2.drawer_text_background
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="checked_text_color" android:state_checked="true" />
<item android:color="default_text_color" />
</selector>
And add these two properties to your NavigationView ie
app:itemIconTint="#drawable/drawer_background"
app:itemTextColor="#color/drawer_text_background"
one more if some color is overlapping with some other color
app:itemBackground="#android:color/transparent"
Now, All you have to do is to set an item as checked on Click listener of that item to change background and text color. You can do it programmatically.
I solved it setting this attribute to my NavigationView app:itemBackground="#drawable/drawer_selector"
and my drawer_selector is as below
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/white" />
<item android:state_focused="true" android:drawable="#color/white" />
<item android:state_activated="true" android:drawable="#color/white" />
<item android:drawable="#color/orange" />
How can I change text color of highlighted text when User selects text for copy paste.
In this image I want to change the color of text
world
from black to white. how can I do that?
I tried adding ColorStateList as drawable but it didn't help.
My TextView:
<TextView
android:id="#+id/tv_hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textColorHighlight="#color/light_blue"
android:textIsSelectable="true"/>
I am not sure it is possible without doing it yourself :
public class MyTextView extends TextView {
... Constructors, ...
private ForegroundColorSpan mSpan = new ForegroundColorSpan(0xffff0000);
#Override
public void setText(CharSequence text, BufferType type) {
// make sure the text is spannable
if (type == BufferType.NORMAL) {
type = BufferType.SPANNABLE;
}
super.setText(text, type);
}
#Override
protected void onSelectionChanged(int selStart, int selEnd) {
super.onSelectionChanged(selStart, selEnd);
Spannable txt = (Spannable) getText();
// ok even if not currently attached
txt.removeSpan(mSpan);
if (selStart != selEnd) {
txt.setSpan(mSpan, selStart, selEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
Inside your res/color folder create a file called text_color_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#d48383"/>
<item android:state_pressed="false" android:color="#121212"/>
<item android:state_selected="true" android:color="#d48383"/>
<item android:state_focused="true" android:color="#d48383"/>
</selector>
Then inside your TextView set this as:
android:textColor="#color/text_color_selector"
Create color folder in your res directory. Then add this xml file. Let's text_color_change.xml
res/color/text_color_change.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#444"/>
<item android:state_focused="true" android:color="#444"/>
<item android:state_pressed="true" android:color="#444"/>
<item android:color="#ccc"/>
</selector>
Then in the TextView, add the textColor as the above file.
<TextView
....
android:textColor="#color/text_color_change" />
Add this property selector property, the selected item will keep it's color state until something else is selected.
in your
selector.xml add this
<!-- Activated -->
<item
android:state_activated="true"
android:color="#ff0000" />
<!-- Active -->
<item
android:state_active="true"
android:color="#ff0000" />
check this for more detail.
For some reason the text color of the selected item in my spinner is white. Here's the code for the Activity:
spinner = (Spinner) findViewById(R.id.spinner_categories);
arr_list_categories = manager.get_all_categories();
arr_list_categories_names = new ArrayList<String>();
// Loop through the Categories array
for (int i = 0; i < arr_list_categories.size(); i++)
{
Category curr_category = arr_list_categories.get(i); // Get the Category object at current position
arr_list_categories_names.add(curr_category.getCategory_name()
.toString()); // Add the name of the Category to the Names Array
}
// Setting the Adapter, to display retrieved data in the Spinner
adapter=new ArrayAdapter<String> (this, R.layout.simple_spinner_item, arr_list_categories_names);
// Setting the display source for the Spinner View
adapter.setDropDownViewResource(R.layout.simple_spinner_item);
// Populating the Spinner
spinner.setAdapter(adapter);
// Registering for On Item Selected listener
spinner.setOnItemSelectedListener(spinnerListener);
And here's the XML of simple_spinner_item:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:padding="5dip"
android:textColor="#FF0000"
android:textSize="20sp" />
The dropdown items appear in red, but not the selected item. I tried to change the textColor in XML to selector: android:textColor="#drawable/spinner_text_color_selector" and created this selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#FF0000"/><!-- pressed -->
<item android:state_focused="true" android:color="#FF0000"/><!-- focused -->
<item android:state_selected="true" android:color="#FF0000"/>
<item android:state_activated="true" android:color="#FF0000"/>
<item android:color="#FF0000"/><!-- default -->
</selector>
But the selected item's text color was still white. So I tried to do this in OnItemSelectedListener:
TextView selected_tv = (TextView) parent.getChildAt(0);
selected_tv.setTextColor(Color.BLUE);
Still white...
Why is it happening and how do I fix it? Thank you!
TextView selected_tv = (TextView) parent.getChildAt(0);
selected_tv.setTextColor(Color.BLUE);
In this code, change in first line like this (Use TextView's id):
TextView selected_tv = (TextView) parent.findViewById(R.id.selected_tv);
selected_tv.setTextColor(Color.BLUE);
I am totaly new in Android development.
I try to change the FontColor and the Background of ListViewItem if it`s selected or pressed.
Changing the Background is absolutely no Problem by using following selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/background_pressed"></item>
<item android:state_focused="true"
android:drawable="#drawable/background_pressed"></item>
<item android:state_selected="true"
android:drawable="#drawable/background_pressed"></item>
<item android:drawable="#android:color/transparent"></item>
</selector>
But how can I change the Background and the FontColor?
Changing the TextView's font color is done in the same way as the listitem's backgroundcolor: create a StateListDrawable and set the TextView's android:textColor to your custom drawable.
For an example, see Change ListViews text color on click. Combine this with what you already have for the listitem's background.
If you want the colors to fade into eachother, set (e.g.) android:exitFadeDuration="#android:integer/config_mediumAnimTime" in the <selector>-tag.
Try this
#Override
public void onCreate(Bundle savedInstanceState) {
TextView txtName;
txtName.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TextView textView = (TextView) v;
textView.setBackgroundColor(Color.RED);
textView.setTextColor(Color.YELLOW);
}
});
}