Set text colour of android.widget.SearchView - java

I am adding a android.widget.SearchView to a view in code but I cannot find how to set the text colour for this.
SearchView = new SearchView(getContext());
I see that there are methods to set the text alignment and direction.
I also see that I can set the background colour and tint but nothing for the foreground.

Try This
int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null,
null);
TextView textView = (TextView) searchView.findViewById(id);
textView.setTextColor(Color.WHITE);
Is it possible to change the textcolor on an Android SearchView?

Related

Show long text in short TextView?

How to create a TextView with a long text so that the text in this TextView is automatically reviewed and displayed from end to end like horizontal slides?
In fact, I want textview to scroll automatically and scroll again from the beginning after the text is finished.
The image is the output of my code, but I do not want this.
enter image description here
make ellipsize=marquee and singleLine=true within textView
I found the answer!
You must add these features to TextView :
android:ellipsize = "marquee"
android:fadingEdge = "horizontal"
android:marqueeRepeatLimit = "marquee_forever"
android:scrollHorizontally = "true"
android:singleLine = "true"
then:
TextView txt = findViewById(R.id.text);
txt.setSelected(true);

How to set BackgroundTintList of a button to default?

I'm changing the BackgroundTintList property of my button with the following line.
myButton.setBackgroundTintList(getColorStateList(R.color.green));
As a result my Button changes it's color from grey to green, and this is what I'd like to achieve.
My problem is that later on I'd like to set back the original grey color of the button, but I have no idea how to do it. I have tried to get the BackgroundTintList property of the button at the very beginning of my code (before I change it) but the following line returns NULL
ColorStateList buttonBackgroundTint = myButton.getBackgroundTintList();
Once I have set the BackgroundTintList to green, setting it to NULL changes my button to white and not to its default grey.
What would be the way to set my button to grey again?
You can try this line:
myButton.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#d8d8d8")));
if you want to change the button color back to its default/original color.
I haven't found any way to do this easily. The only way I could accomplish your goal was to hold on to the original background Drawable, create a clone of it, manually tint the clone, and then swap back and forth between these new drawables.
private Drawable original;
private Drawable tinted;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
this.original = button.getBackground();
this.tinted = button.getBackground().getConstantState().newDrawable().mutate();
ColorStateList custom = getResources().getColorStateList(R.color.my_button, getTheme());
tinted.setTintList(custom);
...
}
Then later on I can either write button.setBackground(original) or button.setBackground(tinted) to swap between the two.
I just created a new Button and get backgroundTintList
actionSearch.backgroundTintList = MaterialButton(requireContext()).backgroundTintList
if you want add custom color
view.backgroundTintList = ColorStateList.valueOf(getColor(context, R.color.color)

How can I set a textview's style programmatically?

How can I add a style to a textview in java? I am trying to add one under my values/styles.xml, not to add each attribute individually.
LinearLayout messagesLayout = (LinearLayout) findViewById(R.id.messages_layout);
TextView sentOne = new TextView(this);
sentOne.setText(sentMessage);
messagesLayout.addView(sentOne);
You may find this answer by Benjamin Piette handy. To change this into working code for a TextView just change it up a bit:
TextView tv = new TextView (new ContextThemeWrapper(this, R.style.mystyle), null, 0);
EDIT: to set other things like margins, height & width and others, use LayoutParams. To set the params throrin19's answer can be helpful.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
params.setMargins(left, top, right, bottom);
tv.setLayoutParams(params);
There is an answer for this already: https://stackoverflow.com/a/7919203/5544859
textview.setTypeface(Typeface.DEFAULT_BOLD);
to preserve the previously set typeface attributes you can use:
textview.setTypeface(textview.getTypeface(), Typeface.DEFAULT_BOLD);
Credit to: #Raz

Change the color of an ImageView with on button click?

I am trying to change the color of an imageView when a button is clicked, I've tried this so far.
ImageView.setBackgroundColor(Color.rgb(102,31,31));
is there anything else i can use besides
ImageView.setBackgroundColor(Color.BLACK)
Please help.
Create a color variable, like so :
int yellow = Color.ParseColor("#XXXXXX");
...
imageView.setBackgroundColor(yellow);
Try this:-
ImageView backgroundImg = (ImageView)findViewById(R.id.imageView);
backgroundImg.setBackgroundColor(Color.rgb(100, 100, 50));
Or else change the background color of parent layout.
Try This :
ImageView.setBackgroundColor(getResources().getColor(R.color.yourColor))
In your color.xml add yourColor :
<color name="yourColor">#yourColorCode</color>

How to disable OnClickListerners

I am developing an app where the user has to match the image and corresponding name of it correctly.
My problem is when the user selects the image first and selects the wrong name it will display wrong answer and if he selects the answer it will be displayed correct answer.
The user should not have to re-select the image again
I have made the onClickListerner's null but it wont work some of my code is as follows,
txt_tag[0] = (TextView) findViewById(R.id.txt_tag1);
txt_tag[0].setOnClickListener(this);
txt_tag[0].setTypeface(tf);
txt_tag[1] = (TextView) findViewById(R.id.txt_tag2);
txt_tag[1].setOnClickListener(this);
txt_tag[1].setTypeface(tf);
txt_tag[2] = (TextView) findViewById(R.id.txt_tag3);
txt_tag[2].setOnClickListener(this);
txt_tag[2].setTypeface(tf);
txt_tag[3] = (TextView) findViewById(R.id.txt_tag4);
txt_tag[3].setOnClickListener(this);
txt_tag[3].setTypeface(tf);
img[0] = (ImageButton) findViewById(R.id.img1);
img[0].setOnClickListener(this);
img[1] = (ImageButton) findViewById(R.id.img2);
img[1].setOnClickListener(this);
img[2] = (ImageButton) findViewById(R.id.img3);
img[2].setOnClickListener(this);
img[3] = (ImageButton) findViewById(R.id.img4);
img[3].setOnClickListener(this);
btn_nxt = (Button) findViewById(R.id.btn_next);
btn_nxt.setOnClickListener(this);
and I have called an method inside that method where I have made all onClickListerner's null
txt_tag[0].setOnClickListener(null);
txt_tag[1].setOnClickListener(null);
txt_tag[2].setOnClickListener(null);
txt_tag[3].setOnClickListener(null);
img[0].setOnClickListener(null);
img[1].setOnClickListener(null);
img[2].setOnClickListener(null);
img[3].setOnClickListener(null);
Can anyone tell me where I am going wrong or any modifications I can do to it.
Thanks in advance
Try using
txt_tag[0].setClickable(false);
txt_tag[1].setClickable(false);
..
img[0].setClickable(false);
img[1].setClickable(false);
..
Your question is not that clear.. but if you want your image and text tag not clickable.. make them android:clickable="false" in xml or setClickable(false);
If I were you I would be checking that logic in a listener. So if the quiestion (if it's a quiz) is in the state "ANSWERED", don't react to event.
Your question is unclear, but I understand it as follows:
You have a bunch of ImageViews and a bunch of TextViews and a Mapping between them.
You want to be able to first select an ImageView, then a TextView. If they match, "correct answer" will be displayed somewhere, if not, "wrong answer" will be displayed
If you click on a TextView before an ImageView is selected, nothing happens
If you click on a Textview and another TextView is already selected, nothing happens
If that is correct, you can do this like this: You keep two variables
int selectedImage = -1;
int selectedText = -1;
In your OnClickListener you update their values like this:
if (source instanceof ImageViews) {
selectedImage = getArrayIndex(source); // I guess you already have a method to retrieve the index
selectedText = -1; // reset textSelection
} else {
if (selectedText < 0) {
selectedText = getArrayIndex(source);
}
}
updateAnswerTextView(); // here you check if the two selections (selectedText and selectedImage) match and display the corresponding string.
Instead, you could just iterate through the TextView array and call
setClickable(false);
on every element as soon as one is clicked. If a new image is selected, you will have to set them to clickable again.
EDIT: And I agree with Rob, you should not remove your Listeners to achieve this behaviour.
Here's my suggestion, if you want you code up specific behaviour you can use the onClickListener callback to achieve what you want.
In the listener, check the state of the image; if it is already selected and you want to ignore the event then you just exit from your callback.
I think setting the onClickListener to null is the wrong thing to do.

Categories