Change SearchView Text Color Xamarin.Android - c#

I want to change the search text view color in Xamarin.Android. I have tried below code so far
searchView = this.Activity.FindViewById<Android.Support.V7.Widget.SearchView (Resource.Id.searchView);
searchView.SetOnQueryTextListener(this);
var textViewId = searchView.Context.Resources.GetIdentifier("android:id/search_src_text", null, null);
var textView = (searchView.FindViewById(textViewId) as TextView);
if (textView != null)
textView.SetTextColor(global::Android.Graphics.Color.White);
I am getting NULL when I try to capture the textView
In textViewId I'm getting the id of the view something like 126312727
Can anyone help me on above?

This is right out of app using Android.Support.V7.Widget.SearchView
var id = searchView.Context.Resources.GetIdentifier("search_src_text", "id", PackageName);
var searchEditText = searchView.FindViewById<EditText>(id);
searchEditText.SetTextColor(Color.Red);

I needed to use the below with AppCompat and CrossCurrentActivity plugin.
var searchEditText = (CrossCurrentActivity.Current?.Activity as MainActivity)?.FindViewById<AutoCompleteTextView>(Resource.Id.search_src_text);
searchEditText.SetTextColor(Color.Red);

Related

Xamarin change searchbar underline color and Icon Spacing Fix [duplicate]

Pretty straight-forward, how do I change the color of the line along the bottom of a SearchView in Android? I've tried, queryBackground, background, and various other things but cant seem to get it in XML. Im happy doing it programmatically or in XML. Just want it to work!
Cheers
Use Below method to change colour of bottom line of SearchView.
Here I add Blue Colour as bottom Line
private void customizeSearchView() {
int searchTextId = getResources().getIdentifier("android:id/search_src_text", null, null);
EditText searchBox = ((EditText) searchView.findViewById(searchTextId));
searchBox.setBackgroundColor(Color.WHITE);
searchBox.getLayoutParams().height = LinearLayout.LayoutParams.MATCH_PARENT;
int search_plateId = getResources().getIdentifier("android:id/search_plate", null, null);
View mSearchPlate = ((View) searchView.findViewById(search_plateId));
mSearchPlate.setBackgroundColor(Color.BLUE);
int searchCloseImageId = getResources().getIdentifier("android:id/search_close_btn", null, null);
ImageView searchClose = ((ImageView) searchView.findViewById(searchCloseImageId));// change color
searchClose.setBackgroundColor(Color.WHITE);
}
hi try this it may help you
android.support.v7.widget.SearchView searchView=findViewById(R.id.search_view);
View v = searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);
v.setBackgroundColor(Color.parseColor("color code"));
androidx.appcompat.widget.SearchView searchView = findViewById(R.id.Search_Categorie);
View v = searchView.findViewById(androidx.appcompat.R.id.search_plate);
v.setBackgroundColor(Color.parseColor("#ffffff"));

Unity3D - How to change color for ItemLabel in DropDown

I am using a DropDown element in Unity,
in my code I disable some choices if the requirements are not met with:
var toogles = dropDownList.GetComponentsInChildren<Toggle>(true);
toogles[index].interactable = enable;
I also want to change the color of the ItemLabel text for the disabled toggles,
I tried:
var mylabel = dropDownList.GetComponentsInChildren<Text>(true);
mylabel[index].color = Color.red;
but doesn´t work, I somehow need to store that ItemLabel in a variable that is created under Dropdown/Template/Item/ItemLabel
I tried to create a public Text where I drag & dropped the ItemLabel that is created under template, but since its not the exact Text that gets created, its only a template, it didnt work.
Anybody knows how to access that ItemLabel of the DropDown?
EDIT:
I got this far:
if(toogles[i].interactable == false)
{
_dropdown.itemText.color = Color.red;
}
but dont know how to add the indexes, so only some of the items will be Red
I was able to find and iterate through the itemLabels with this code:
var myLabels = GameObject.FindGameObjectsWithTag("ItemLabel");
var objectCount = myLabels.Length;
Debug.Log("Object count:" + objectCount);
Text[] myItemLabels = new Text[6];
int k = 0;
foreach (var obj in myLabels)
{
myItemLabels[k] = obj.GetComponent<Text>();
k++;
}

Set Navigation Title color in Xamarin(Ios)

I want set navigation Bar Title color in Xamarin(Ios) using C#.
I set below way but it not work.
this.NavigationController.NavigationBar.BarTintColor = UIColor.Clear.FromHexString("#0072BA", 1.0f);
this.NavigationController.NavigationBar.TitleTextAttributes = UIColor.Magenta;
the BarTintColor work but TitleTextAttributes not work.. I also see documentation but in document the title text color not mention.
Any help be Appreciated..
After Spending 1 Hour I got the solution.
Type UIKit.UINavigationBar does not contain a definition for SetTitleTextAttributes and no extension method SetTitleTextAttributes of type UIKit.UINavigationBar could be found. and it give below error :
Are you missing an assembly reference ?
So Instead of calling SetTitleTextAttributes, do the following:
this.NavigationController.NavigationBar.TitleTextAttributes = new UIStringAttributes()
{
ForegroundColor = UIColor.White
};
Hope this will help other.
You can use a custom view as the navigation bar's title view, then you can modify it easily.
In your "ViewDidLoad" method, using this code:
public override void ViewDidLoad()
{
//Normal way
//this.Title = "Normal";//or this.NavigationItem.Title = "Normal;
//Using custom view
UILabel lbTitle = new UILabel(new CoreGraphics.CGRect(0, 0, 50, 30));
lbTitle.Text = "CustomView";
lbTitle.TextColor = UIColor.Red;
lbTitle.Font = UIFont.SystemFontOfSize(15);
this.NavigationItem.TitleView = lbTitle;
//Using a picture
//this.NavigationItem.TitleView = new UIImageView();
}
Hope it can help you.

Find control by tag on WinForm

I have a dynamic Label with Tag which is created 14 Label and added in a GroupBox, then, I want to search for Label that have a Tag = "ABC", I have tried this :
var items = grouptodayerror.ControlCollection;
var finditem = items.Cast<Control>().FirstOrDefault(control => String.Equals(control.Tag, "ABC");
grouptodayerror is my GroupBox, and getting an error message
Error 1 'ControlCollection': cannot reference a type through an expression; try 'System.Windows.Forms.Control.ControlCollection' instead
I tried this too :
Label findbox = grouptodayerror.Controls.Find("ABC", true).FirstOrDefault() as Label;
if (findbox != null)
findbox.Text = "CDE";
But I'm getting error null reference on first line.
My question is :
How can I get the ControlCollection?
Did the second code can find a Tag or just a Text of Control?
Is there any way to accomplish this?
Thanks in advance.
Can you try this one
foreach (Label lb in Controls.OfType<GroupBox>().SelectMany(groupBox => groupBox.Controls.OfType<Label>()).ToList())
{
if (lb.Tag.Equals("ABC"))
{
//Write your logic here
}
}

BindingInflate with different ViewModel

In my android view, I try to create a bunch of LinearLayouts using BindingInflate. It actually works fine, but I need to provide another ViewModel as DataContext. How do I achieve this?
In xaml I would just use
newLayout.DataContext = mySecondViewModel;
Following code is obviously not working, but something I would like to use:
var layout = this.BindingInflate(Resource.Layout.statistics_header, layoutContainer);
layout.BindingContext = mySecondViewModel
UPDATE:
Thanks for the comment - I did not know about the FrameControl. But unfortunatly I still could not figure out, how to use this.
What I want to achieve, is to fill up a viewpager with some fragments and a relative layout with corresponding headers. It would be easy to connect them to the same viewmodel.
Here is my current code:
var relativeLayout = FindViewById<RelativeLayout>(Resource.Id.statistics_header);
var layoutContainer = new MvxFrameControl(this, null)
{
LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent),
DataContext = statistic,
};
var layout = this.BindingInflate(Resource.Layout.statistics_header, layoutContainer);
relativeLayout.AddView(layoutContainer);
return new MvxPagerFragmentAdapter.FragmentInfo
{
FragmentType = typeof(StatisticsFragment),
Title = "StatisticsFrag",
ViewModel = statistic,
ConnectedView = layoutContainer
};
It fails that it's not possible to bind. But the DataContext is set to the right ViewModel where every property is set.
Am I missing a point?
Regarding to Stuarts comment I answer this for him:
I need to use a MvxFrameControl and can set the DataContext:
var layoutContainer = new MvxFrameControl(Resource.Layout.statistics_header, this, null)
{
LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent),
DataContext = statistic
};
layoutContainers.Add(layoutContainer);
return new MvxPagerFragmentAdapter.FragmentInfo
{
FragmentType = typeof(StatisticsFragment),
Title = "StatisticsFrag",
ViewModel = statistic,
ConnectedView = layoutContainer
};

Categories