Customize TokenAutoComplete - android

I needed a recipient EditText in my app, so, I used this TokenAutoComplete library to do the same. However, I am having a few problem with this.
When I type a name, a drop down list comes. I want to give a custom layout to this drop down list. How can I do it? Currently I am showing just text here using toString method.
Below is the code.
#Override
public String toString() { return mContactFirstName }
I want to show here name and photo together. How can I do it?
Changing the layout for selected token. When the token is not selected/default, the token's background is white and it's text color is black. On the other hand, if the token is selected, the token's background should be black and it's text color should be white colored. I used a selector xml to change the token's background, however, I don't know how to change the text's color.
The selector xml is working perfectly fine and the background is changing when the token is selected and unselected, but, the text's color remains same.
I used below setSelected() method to change the text's color. But it's not working.
#Override
public void setSelected(boolean selected) {
super.setSelected(selected);
TextView tv = (TextView) findViewById(R.id.token_name);
if(selected) {
tv.setTextColor(Color.WHITE);
} else {
tv.setTextColor(Color.BLACK);
}
}
Please help me out.

In the example that TokenAutoComplete library provides you can see that the ContactsCompletionView provides setAdapter method. You can set your custom adapter there, any adapter that extends BaseAdapter will do.
so you just do like this:
myAdapter = new MyCustomAdapter(); // extending BaseAdapter
completionView.setAdapter(myAdapter);
in case of a custom adapter there are plenty of resources on the web, but basically in getView method you create custom view by inflatind it from xml. in the xml you can define the layout you desire (image, text, whatever...).
for more info refer to this question: Custom Adapter for List View

Related

Unable to set TextView background color in a RecyclerView

I have a RecyclerView which contains TextView. The holder code is below:
private class PhotoHolder extends RecyclerView.ViewHolder {
private TextView mTitleTextView;
public PhotoHolder(View itemView) {
super(itemView);
mTitleTextView = (TextView) itemView;
}
public void bindGalleryItem(GalleryItem item) {
mTitleTextView.setText(item.toString());
//--------------TEST CODE----------------
mTitleTextView.setBackgroundColor(2);
mTitleTextView.invalidate();
mTitleTextView.requestLayout();
}
}
Now, the line mTitleTextView.setText() works as expected but I also wanted to change background of the TextView. So, I tried calling the setBackgroundColor() method but even the call to invalidate and requestLayout don't set any background color.
Does anyone know a way to ensure setting of the background color? Is this the right place to set the color? Why is it not setting?
Also, I want to do this programmatically.
It should be mTitleTextView.setBackgroundColor(getResources().getColor(R.color.colorOne));
or to set the Text Color you can use the below
mTitleTextView.setTextColor(getResources().getColor(R.color.colorTwo));
Define your specific color in colors.xml
<color name="colorOne">#E60000</color>
<color name="colorTwo">#D3D3D3</color>
Color code are standards across different development activities. Android framework should understand the parameter value that you are planning to set to a view.
Reference Link for Graphics color code
You can use softwares similar to this to find the color code
Can you remove this :
mTitleTextView.invalidate();
mTitleTextView.requestLayout();
and ensure you are calling this method from right place i.e. nobody else is resetting textView's properties after it.
use like that :
mTitleTextView.setTextColor(getResources().getColor(R.color.solid_red));
Set your background color like this using color resources
//Like this you change your as your desire
mTitleTextView.setBackgroundColor(getResources().getColor(android.R.color.black));

How to change existing TextView style in action

I have some intent with set of TextViews and a Button. If user clicks the button and there is some error I want to change look of one TextView. E.g. change the border to red and make font bold. I wrote a style for it but I have not found method setStyle on the TextView. After some self study I realized that Android does not support setting the style programmatically. There are some workarounds, when you create the intent source. But my intent already exists, it seems odd to recreate it.
Could you tell me the proper way?
use the workaround and create the TextView again
forget the styles and use java methods to decorate existing TextView
something else
Changing the style of the textview directly does not work as you know. But you can create a second textview with other styles in your layout, which you can show up if needed.
Just add this xml attribute android:visibility="gone" to the second textview, so this second textview is not displayed at first, but available.
When you now want to change the style of your textview, you simple need to swap the two textviews by hidding the first one and showing the second one
textView1.setVisibility(View.GONE);
textView2.setVisibility(View.VISIBLE);
I used these two answers to make it work:
https://stackoverflow.com/a/5488652/1639556
https://stackoverflow.com/a/14195090/1639556
and the code is:
ViewManager parent = (ViewManager) unknown.getParent();
parent.removeView(unknown);
TextView newUnknown = (TextView)getLayoutInflater().inflate(R.layout.tvtemplate, null);
newUnknown.setId(unknown.getId());
parent.addView(newUnknown, unknown.getLayoutParams());
unknown = newUnknown;
You can try using setTextAppearance() on the textview. The link is: setTextAppearance
Your style will need TextAppearance.SomeThing.SomeOtherThing as the parent.
Use R.style.YourStyleName as the integer argument.

Customize Button of Android DatePicker

My current method to customize my UI is using the usual android DatePicker then use DatePicketDialog.getDatePicker() to get the inside component out, and customize it.
Now the result is in the image at https://dl.dropboxusercontent.com/u/3286004/Screen%20Shot%202557-08-29%20at%202.52.21%20AM.png
The Question is ... I want to customize the black line above the DONE button to another color.
Could you suggest how I can get that line component out, so I can change it.
Thank you in advance :D
This is absolutely possible, actually you could do whatever you want with it. Really, one of options is to use style and theme which however would not work in 4.x. The more, lets say, proper or easy way is to use views itself like following:
// we need this listener since only here all views are really drawn and accessible
yourDialog.setOnShowListener(new DialogInterface.OnShowListener() {
private boolean areButtonsFixed;
#Override
public void onShow(DialogInterface dialog) {
if (areButtonsFixed)
return;
// both buttons - you could search for only positive button or whatever button your dialog has
final Button btnPositive = getButton(DatePickerDialog.BUTTON_POSITIVE);
final Button btnNegative = getButton(DatePickerDialog.BUTTON_NEGATIVE);
final Button btnNeutral = getButton(DatePickerDialog.BUTTON_NEUTRAL);
// buttons layout parameters, change it into material style (gravity right)
final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) btnPositive.getLayoutParams();
lp.weight = 0; // was 1 to fill 50% horizontally
// positive button, set your own label
btnPositive.setText(R.string.dialog_ok_label);
// set text color and size
btnPositive.setTextColor(ResHelper.getColor(R.color.blue_bright));
btnPositive.setTextSize(TypedValue.COMPLEX_UNIT_PX, ResHelper.getDimensPx(R.dimen.text_size_14));
btnPositive.setLayoutParams(lp);
// divider above buttons
((LinearLayout) btnPositive.getParent().getParent()).setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
areButtonsFixed = true;
}
This (prelast line) will remove divider above buttons at all. If you wish to customize it instead do it like following:
((LinearLayout) btnPositive.getParent().getParent()).setDividerDrawable(R.drawable.yer_drawable);
One way would be to use another theme. This theme is Holo i think, so you can't change colors.
I think you can create your dialog with a custom layout.
If you used a custom layout, you can change colors.
Or, you should use another theme, or create your own theme.
EDIT
Yep, at run-time too.
Many things using on your layout are locked, like colors, especially on widgets (searchView for example)
In default dialog it is impossible, this line has system color. You should convert this dialog to activity, then you can change color there.

Android - Change Background Color of a part of String in EditText(Highlighting Text)

I'm making an app in which I there is an EditText and a ToggleButton. I want to create a highlighter mechanism with the help of these. For example, when the state of the ToggleButton is ON, the text that'll be entered in the EditText will have Green background as if it is being highlighted. Again, when the ToggleButton state will be changed to OFF, the text in the EditText will also stop getting highlighted immediately.
Please Help Me.
Thanks in Advance!!
You can change the background color of part of the text using SpannableString as described here: Set color of TextView span in Android
Another way of doing the same thing is to load the text from HTML with the tag set to the background color you want.
To change the colour of text use
textElement.setTextColor(0xFF00FF00); //this is green colour
To change the colour of edit text use
textElement.setBackgroundColor(Color.MAGENTA);
For more information Check this link.
Simply you set onCheckedChangeListener on your ToggleButton to change the background of your EditText. Your code will be something like this :
toggleButtonHighLight.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton button, boolean isChecked) {
if (checked) {
//highlight the EditText by setting its background to green
editText.setBackgroundResource(R.color.green);
} else {
//delete the highlight when the toggleButton is OFF
editText.setBackgroundColor(android.R.color.white);
}
}
});

Getting ListView drawables and applying it manually

i am trying to apply the style that a list item has, when it is selected, to a View (In my case a TextView)
Example
here it would be the orange-painted style. I found that this look is defined by a Drawable Object. So i tried, so get the drawable and applied it to my view
TextView tv = new TextView(this);
tv.setText("Hello, Android");
tv.setBackgroundDrawable(new ListView(this).getSelector());
setContentView(tv);
but it doesn't work.
Does anybody have an idea how to do that?
Ok I figured out how to do that. I found out hat ListView uses the ColorStateList list_selector_background which is defined in android.R.drawable. Using ResourceBrowser I got to know that the fourth color is the selected drawable so i added the following to my code
StateListDrawable listDrawables= (StateListDrawable)getResources().getDrawable(android.R.drawable.list_selector_background);
listDrawables.selectDrawable(3);
Drawable highlightDrawable = listDrawables.getCurrent();
Now I can set my Background using higlightDrawable. I don't know if it is possible to access the drawable in xml I did not try it yet. Thanks for help!
It would be a pain to do it by code.
You need to implement a selector and let android know which resource needs to be used when the view is pressed, enabled or focused.
Here is sample example on how to achieve the same.

Categories

Resources