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.
Related
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" />
I want to remove the highlight color when user clicked on the textview. So I'm able to change all the color and make it successful but it does not open the link when I clicked on textview. If I remove the AutoLink its worked...
So my question is how can i remove the highlight without using autolink?
XML file:
<TextView
android:id="#+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Customer Service"
android:textAlignment="center"
android:textSize="15sp"
android:clickable="true"
android:linksClickable="true"
android:autoLink="web"
android:background="#drawable/about_us_selector"
android:textColor="#color/about_us_color" />
Background drawable:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#color/light_gray"
android:state_pressed="true"/>
</selector>
Color:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
Java:
String webTv1 = "<a href='http://www.google.com'> Customer Service </a>";
tv1 = (TextView) findViewById(R.id.tv1);
tv1.setClickable(true);
tv1.setText(Html.fromHtml(webTv1));
//set user name in blue color and remove underline from the textview
Spannable spannedTv1 = Spannable.Factory.getInstance().newSpannable(
Html.fromHtml(webTv1));
Spannable processedText = removeUnderlines(spannedTv1);
if (tv1 != null) {
tv1.setText(processedText);
tv1.setMovementMethod(LinkMovementMethod.getInstance());
}
}
public static Spannable removeUnderlines(Spannable p_Text) {
URLSpan[] spans = p_Text.getSpans(0, p_Text.length(), URLSpan.class);
for (URLSpan span : spans) {
int start = p_Text.getSpanStart(span);
int end = p_Text.getSpanEnd(span);
p_Text.removeSpan(span);
span = new URLSpanNoUnderline(span.getURL());
p_Text.setSpan(span, start, end, 0);
}
return p_Text;
}
Finally, I managed to get the highlight removed:
Add another color/highlight.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#00ffffff"/>
</selector>
Then, define the color at your xml file:
android:textColorHighlight="#color/hightlight.xml"
Before that, remove the autoLink from the XML file...
I have a selector where I set a circle as the background for the state_selected = true but I want to change the color when I click on the object. How can I do it?
This is how my drawables are set up:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#D0021B"/>
<stroke android:width="2dp" android:color="#910012" />
<size
android:width="50dp"
android:height="50dp" />
<corners android:radius="50dp" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#android:color/black" android:state_selected="false" />
<item android:drawable="#drawable/nav_circle" android:state_selected="true" />
</selector>
You can do it programmatically:
public static void colorDrawable(View view, int color) {
Drawable wrappedDrawable = DrawableCompat.wrap(view.getBackground());
if (wrappedDrawable != null) {
DrawableCompat.setTint(wrappedDrawable.mutate(), color);
setBackgroundDrawable(view, wrappedDrawable);
}
}
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static void setBackgroundDrawable(View view, Drawable drawable) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
view.setBackgroundDrawable(drawable);
} else {
view.setBackground(drawable);
}
}
if you want to change the color at the moment it is pressed, you can use state_pressed.
<item android:state_pressed="true"
android:drawable="#color/pressedColor"/> <!-- pressed state -->
<item android:state_focused="true"
android:drawable="#color/pressedColor"/> <!-- focused state -->
<item android:drawable="#color/colorPrimary"/>
you can also use another drawable under the android:drawable attribute
<item android:state_pressed="true"
android:drawable="#drawable/button_solid"/>
however, if you want to change the background drawable when it is pressed, you can change the button background to a different xml drawable
button.setBackground(getDrawable(R.drawable.selectorDrawable2));
Maybe this will help you :
i=(ImageView)findViewById(R.id.image);
i.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
i.getBackground().setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_ATOP);
}
});
ImageView xml code :
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/nav_circle"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="#+id/image" />
I have textview which I want to change the color when I focus or cliclked it like a link text in web I have try to follow this but it still doesn't work
please help, thanks
this is my java code
public class TextColorActivity extends Activity {
/** Called when the activity is first created. */
ColorStateList cl = null;
private TextView title;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
title = (TextView)findViewById(R.id.hello);
try {
Log.d("test","try");
XmlResourceParser xpp = getResources().getXml(R.drawable.selector_txt);
cl = ColorStateList.createFromXml(getResources(), xpp);
} catch (Exception e) {}
title.setTextColor(cl);
title.setFocusable(true);
title.setClickable(true);
title.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d("test","click");
}
});
}
this is my selector_txt.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="#color/Darkgoldenrod"/>
<item android:state_pressed="true" android:state_enabled="false"
android:color="#color/Darkgreen" />
<item android:state_enabled="false" android:color="#color/Red" />
<item android:color="#color/blue"/>
and this is my main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/hello"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
android:textSize="30dp"
android:textStyle="bold"
android:duplicateParentState="true"/>
You can also set your color in the xml if you want. Just create a color folder in your res folder and move the xml file there then you can set it via android:textColor="#color/selector_txt"
Regards the problem you're having. Android will always use the first match in a selector. If a TextView is pressed it is also focused. So add android:pressed="false" to your first item or move the line after the pressed status line.
That's the full xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="#color/Red" />
<item android:state_pressed="true" android:color="#color/Darkgreen" />
<item android:state_focused="true" android:color="#color/Darkgoldenrod"/>
<item android:color="#color/blue"/>
</selector>
I would like to achieve this specific type of radio buttons in my layout:
= different graphics for the first item, middle ones, and last one, which have different rounded corners. I can imagine doing this with different styles for the 3 types of buttons (using custom styles, stateful drawables).
I'm implementing this using custom toggle buttons. I would like to take advantage of drawable selector for using different drawables for the first and the last items, so I use:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_first="true"
android:drawable="#drawable/radio_left_act"/>
<item android:state_checked="true" android:state_last="true"
android:drawable="#drawable/radio_right_act"/>
<item android:state_checked="true"
android:drawable="#drawable/radio_middle_act"/>
<item android:state_checked="false" android:state_first="true"
android:drawable="#drawable/radio_left_inact"/>
<item android:state_checked="false" android:state_last="true"
android:drawable="#drawable/radio_right_inact"/>
<item android:state_checked="false"
android:drawable="#drawable/radio_middle_inact"/>
</selector>
But now I have a problem, that states state_first, state_last are not set automatically in my LinearLayout, so I have to set them manually every time the buttons are clicked. Is there some way, some layout, where these states are set automatically? Thank you for any help.
Found nothing special, so here is a "default" solution, with custom toggle buttons. Here are 3 different styles (put to styles.xml) for first, middle ones and last buttons:
<!-- Toggle button styles -->
<style name="CustomToggle">
<item name="android:paddingTop">9dp</item>
<item name="android:paddingBottom">9dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">0dp</item>
<item name="android:layout_weight">1</item>
</style>
<style name="FirstToggle" parent="#style/CustomToggle">
<item name="android:background">#drawable/radio_first</item>
</style>
<style name="MiddleToggle" parent="#style/CustomToggle">
<item name="android:background">#drawable/radio_middle</item>
</style>
<style name="LastToggle" parent="#style/CustomToggle">
<item name="android:background">#drawable/radio_last</item>
</style>
And a short code for activity handling the events of toggle buttons, so only 1 button is checked in the same time, and checked button is disabled:
public class AktivityActivity extends Activity
{
ArrayList<ToggleButton> toggle_buttons;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.aktivity);
initToggleButtons();
}
private void initToggleButtons()
{
toggle_buttons = new ArrayList<ToggleButton>();
toggle_buttons.add((ToggleButton) findViewById(R.id.toggle_1));
toggle_buttons.add((ToggleButton) findViewById(R.id.toggle_2));
toggle_buttons.add((ToggleButton) findViewById(R.id.toggle_3));
// Listen on all toggle buttons
for (ToggleButton toggle_button : toggle_buttons)
toggle_button.setOnCheckedChangeListener(check_listener);
// Check first toggle button
updateToggleButtons(toggle_buttons.get(0));
}
// Only one toggle can be checked, and checked button must be disabled
private void updateToggleButtons(ToggleButton checked_button)
{
for (ToggleButton toggle_button : toggle_buttons)
{
toggle_button.setChecked(toggle_button == checked_button);
toggle_button.setEnabled(toggle_button != checked_button);
}
}
// Toggle buttons change listener
OnCheckedChangeListener check_listener = new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (isChecked)
updateToggleButtons((ToggleButton) buttonView);
}
};
}
Maybe it can be usefull for somebody...
You should check out the Wordpress Android project. They use a "ToggleButton" that gives similar functionality. For the .xml look here. To download the complete source go here.
It wont be the same as what you want, as they just have toggle buttons, but that can most likely be adapted for your needed style of radio buttons (if it's not already built in).
The Wordpress Android project helped me learn a lot. Everything from theming, custom buttons, custom layouts, toggle buttons, xmlrpc, and more.
The simplest way I found for doing this is:
1) Extend RadioButton class as follows:
import android.content.Context;
import android.view.ViewGroup;
import android.widget.RadioButton;
public class RoundedButton extends RadioButton {
private static final int[] STATE_ONLY_ONE = new int[] {
android.R.attr.state_first,
android.R.attr.state_last,
};
private static final int[] STATE_FIRST = new int[] {
android.R.attr.state_first
};
private static final int[] STATE_LAST = new int[] {
android.R.attr.state_last
};
public RoundedButton(Context context) {
super(context);
}
#Override
protected int[] onCreateDrawableState(int extraSpace) {
ViewGroup parent = (ViewGroup) getParent();
if (parent == null) {
return super.onCreateDrawableState(extraSpace);
}
final int size = parent.getChildCount();
final boolean isFirst = (parent.getChildAt(0) == this);
final boolean isLast = (parent.getChildAt(size-1) == this);
int[] states = super.onCreateDrawableState(extraSpace + 2);
if (isFirst && isLast) {
mergeDrawableStates(states, STATE_ONLY_ONE);
} else if (isFirst) {
mergeDrawableStates(states, STATE_FIRST);
} else if (isLast) {
mergeDrawableStates(states, STATE_LAST);
}
return states;
}
}
2) Create One XML file in "res/drawable/rbtn_selector.xml" add below XML code for Radio Button background.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- First Checked -->
<item android:state_first="true" android:state_checked="true">
<shape>
<gradient
android:angle="90"
android:startColor="#color/radio_button_selected_start"
android:endColor="#color/radio_button_selected_end"
android:type="linear" />
<!--<solid android:color="#android:color/holo_blue_dark" />-->
<corners android:topLeftRadius="10dp" android:topRightRadius="10dp"/>
<stroke android:width="#dimen/radio_button_border" android:color="#color/radio_button_border_selected" />
</shape>
</item>
<!-- First Unchecked -->
<item android:state_first="true" android:state_checked="false">
<shape>
<gradient
android:angle="90"
android:startColor="#color/radio_button_unselected_start"
android:endColor="#color/radio_button_unselected_end"
android:type="linear" />
<!--<solid android:color="#android:color/holo_purple"/>-->
<corners android:topLeftRadius="10dp" android:topRightRadius="#dimen/radio_button_radius"/>
<stroke android:width="#dimen/radio_button_border" android:color="#color/radio_button_border_unselected" />
</shape>
</item>
<!-- Last Checked -->
<item android:state_last="true" android:state_checked="true">
<shape>
<gradient
android:angle="90"
android:startColor="#color/radio_button_selected_start"
android:endColor="#color/radio_button_selected_end"
android:type="linear" />
<!--<solid android:color="#android:color/holo_green_dark" />-->
<corners android:bottomLeftRadius="#dimen/radio_button_radius" android:bottomRightRadius="#dimen/radio_button_radius"/>
<stroke android:width="#dimen/radio_button_border" android:color="#color/radio_button_border_selected" />
</shape>
</item>
<!-- Last Unchecked -->
<item android:state_last="true" android:state_checked="false">
<shape>
<gradient
android:angle="90"
android:startColor="#color/radio_button_unselected_start"
android:endColor="#color/radio_button_unselected_end"
android:type="linear" />
<!--<solid android:color="#android:color/holo_red_dark"/>-->
<corners android:bottomLeftRadius="#dimen/radio_button_radius" android:bottomRightRadius="#dimen/radio_button_radius"/>
<stroke android:width="#dimen/radio_button_border" android:color="#color/radio_button_border_unselected" />
</shape>
</item>
<!-- Default Checked -->
<item android:state_checked="true">
<shape>
<gradient
android:angle="90"
android:startColor="#color/radio_button_selected_start"
android:endColor="#color/radio_button_selected_end"
android:type="linear" />
<stroke android:width="#dimen/radio_button_border" android:color="#color/radio_button_border_selected" />
<!--<solid android:color="#android:color/holo_orange_dark" />-->
</shape>
</item>
<!-- Default Unchecked -->
<item android:state_checked="false">
<shape>
<gradient
android:angle="90"
android:startColor="#color/radio_button_unselected_start"
android:endColor="#color/radio_button_unselected_end"
android:type="linear" />
<stroke android:width="#dimen/radio_button_border" android:color="#color/radio_button_border_unselected" />
<!--<solid android:color="#android:color/holo_green_light"/>-->
</shape>
</item>
</selector>
3) Create One XML file in "res/drawable/rbtn_textcolor_selector.xml" add below XML code for Radio Buttons Text selector color.(Text Color Selector xml file)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#color/radio_text_selected"/>
<item android:color="#color/radio_text_unselected"/>
</selector>
4) Set your style to the button:
4.1) Programmatically add some RoundedButton to an exixsting RadioGroup:
RoundedButton newRadioButton = new RoundedButton(this.getActivity());
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
newRadioButton.setBackgroundDrawable(this.getActivity().getResources().getDrawable(R.drawable.rbtn_selector));
} else {
newRadioButton.setBackground(this.getActivity().getResources().getDrawable(R.drawable.rbtn_selector));
}
newRadioButton.setTextColor(this.getActivity().getResources().getColorStateList(R.color.rbtn_textcolor_selector));
4.2) Xml:
<RoundedButton
android:id="#+id/bt_id_1"
android:background="#drawable/rbtn_selector"
android:textColor="#drawable/rbtn_textcolor_selector" />
5) Choose your own colors and dimensions the one I used in the example are:
<color name="radio_text_selected">#FFF</color>
<color name="radio_text_unselected">#222</color>
<color name="radio_button_selected_start">#5393c5</color>
<color name="radio_button_selected_end">#6facd5</color>
<color name="radio_button_unselected_start">#f9f9f9</color>
<color name="radio_button_unselected_end">#eee</color>
<color name="radio_button_border_selected">#2373a5</color>
<color name="radio_button_border_unselected">#aaa</color>
and:
<dimen name="radio_button_radius">10dp</dimen>
<dimen name="radio_button_border">0.7dp</dimen>