I am new to Xamrin. I am attempting to build a Xamarin Forms app (as little native as possible) for running on both Android and IOS.
I am running in the Android Emulator and by buttons all have uppercase text even though I'm specifying text in mixed case.
I have found a number of pages that say to add the following to styles.xml:
<item name="android:textAllCaps">false</item>
and another that referenced:
<item name="textAllCaps">false</item>
It looks like, though, that there's been some changes according to the docs in styles.xml, so I added it and it now looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="MainTheme" parent="MainTheme.Base">
<!-- As of Xamarin.Forms 4.6 the theme has moved into the Forms binary -->
<!-- If you want to override anything you can do that here. -->
<!-- Underneath are a couple of entries to get you started. -->
<!-- Set theme colors from https://aka.ms/material-colors -->
<!-- colorPrimary is used for the default action bar background -->
<!--<item name="colorPrimary">#2196F3</item>-->
<!-- colorPrimaryDark is used for the status bar -->
<!--<item name="colorPrimaryDark">#1976D2</item>-->
<!-- colorAccent is used as the default value for colorControlActivated
which is used to tint widgets -->
<!--<item name="colorAccent">#FF4081</item>-->
<item name="textAllCaps">false</item>
<item name="android:textAllCaps">false</item>
</style>
</resources>
But when I run the emulator, it shows all button text in all uppercase.
I don't know what else to try.
You can create the Custom Renderer for button and set the SetAllCaps to false and try.
Here is the Code:
public class CustomButtonRenderer : ButtonRenderer
{
public CustomButtonRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
if (e.OldElement != null)
{
// Cleanup
}
if (e.NewElement != null)
{
var button = (Button)this.Control;
button.SetAllCaps(false);
}
}
}
I made a test and found the issue is related to the version of Xamarin.Forms.
If you are using Xamarin.Forms 4.6 , the solution you mentioned before does the trick , we just need to add <item name="android:textAllCaps">false</item> into style.xml .
If you're using the latest version(4.8) , the solution above does not work , we need to create custom renderer for the Button , pls copy the code into your android project.
[assembly: ExportRenderer(typeof(Xamarin.Forms.Button), typeof(MyRenderer))]
namespace YourNameSpace.Droid
{
class MyRenderer : ButtonRenderer
{
public MyRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
{
base.OnElementChanged(e);
if (e.OldElement != null)
{
// Cleanup
}
if (e.NewElement != null)
{
Control.SetAllCaps(false);
}
}
}
}
I am using custom ActionBar library in my Android App. there i want to show a searchView but can not find any solution, i m working in library that is
https://github.com/johannilsson/android-actionbar
This is a class
private class ExampleAction extends AbstractAction {
public ExampleAction() {
super(R.drawable.ic_title_export_default);
}
#Override
public void performAction(View view) {
Toast.makeText(OtherActivity.this, "Example action",
Toast.LENGTH_SHORT).show();
}
}
//use for signle action how to use for multiactions
ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);
actionBar.addAction(new ExampleAction());
actually i found code for searchbar but it is in .cs format how to use this searchview
https://github.com/zleao/MonoDroid.ActionBar
Thanks bro & sis
Take a look at this answer.
According to this answer SearchView is available support library.
add dependency to build.gradle
compile 'com.android.support:appcompat-v7:22.0.0'
and make imports
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.SearchView;
According to this answer it worked on API 8 on emulator. For more details, check the link with the question and answer itself.
Hope it helps.
UPDATE
By adding this code
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:hmkcode="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:orderInCategory="100"
hmkcode:showAsAction="always"
android:icon="#drawable/ic_action_search"
android:title="Search"/>
<item
android:id="#+id/action_copy"
android:orderInCategory="100"
hmkcode:showAsAction="always"
android:icon="#drawable/ic_content_copy"
android:title="Copy"/>
<item
android:id="#+id/action_share"
android:orderInCategory="100"
hmkcode:showAsAction="always"
android:icon="#drawable/ic_social_share"
android:title="Share"/>
you can achieve this:
You can customize items in the way you want. For more details see this example.
i go a button:
<Button
android:id="#+id/dialog_menu_topic"
style="#style/dialog_menu_topic"/>
with a style reference:
<style name="dialog_menu_topic">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_alignParentRight">true</item>
<item name="android:layout_alignParentTop">true</item>
<item name="android:text">#string/menu_button</item>
<item name="android:onClick">menuButtonClicked</item>
<item name="android:visibility">visible</item>
This activity is declared as a Dialog:
<style name="dialog_style" parent="#android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
And if i simply do:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myActivity);
Button menuButton = (Button) findViewById(R.id.dialog_menu_topic);
menuButton.setVisibility(Button.GONE);
}
Nothing happens. I tried with getting the layoutparams, add a rule where i set visibility to gone and set the params to the button. But the button is showing up again and again. Any help is appreciated.
The app isn't crashing and the button isn't hiding as he should do. Attention: Style and ID of the Button got the same Name, but they are at a different Location, so they can have the same Name that's not the Problem! Any help is appreciated.
Update Button.setVisibility(int value) Needs an integer. Both Button and View.GONE returning the same integer.. that's not the Problem and yes i tried it with View.GONE, even that it was not necessary...:
11-27 14:56:24.538: E/Button id Button.Gone(27826): 8
11-27 14:56:24.538: E/View id View.Gone(27826): 8
Logcat Output of View.GONE und Button.GONE...
Update When i use my onClick method of this Button to setVisibility it works!:
public void myOnclickFromMenuButton(View v){
v.setVisibility(View.GONE);
}
But if i use it at my normal onCreate-Method like:
View myButtonView = (View) findViewById(R.id.dialog_menu_topic);
myButtonView.setVisibility(View.GONE);
again nothing happens.... it's really a bad joke... ;//
Update my menuButton where i setVisibility to gone and my View v from the onClick method have exactly the same id:
11-27 18:48:56.867: E/menuButton.getId(12417): 2131361795
11-27 18:49:00.850: E/v getId(12417): 2131361795
From the v the Button disapears but in the normal onCreate it won't!
Please Need help..
Try
menuButton.setVisibility(View.GONE);
I think you should try menuButton.setVisibility(View.GONE);
I've been trying to change the textcolor of my timepicker. But I can't find where the parent style is located. I've tried both
<style name="MyTimePicker" parent="#android:style/Widget.Holo.TimePicker">
<item name="android:textColor">#color/text</item>
</style>
and
<style name="MyTimePicker" parent="#android:style/Widget.TimePicker">
<item name="android:textColor">#color/text</item>
</style>
My minSdkVersion is 15. My targetSdkVersion is 20. I have rebuilded and cleaned my project.
I think I've been through every similar question on SO and none of them really have provided a solution for me. The only answer that might work is using some sort of library, but I'm not a big fan of that solution. Is the path to the parent something different from what I'm using, because I'm pretty sure I should be able to access it somehow?
Edit
This is how the theme is applied;
<TimePicker
style="#style/MyTimePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/timePicker"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true" />
On a note this is the error I receive (forgot to place it before):
Error:Error retrieving parent for item: No resource found that matches the given name '#android:style/Widget.Holo.TimePicker'.
Edit 2
A couple of the questions I've viewed to try to solve this:
How can I override TimePicker to change text color - I think this question gets as close to an answer, but I'm not entirely sure what I need to do? Do I need to import the android TimePicker style into my project?
https://stackoverflow.com/questions/24973586/set-textcolor-for-timepicker-in-customized-app-theme - No answer is given.
How can i change the textcolor of my timepicker and datepicker? - Tried the 0 votes answer, but it didn't work.
How to change the default color of DatePicker and TimePicker dialog in Android? - Again can't find the TimePicker in a similar way.
Android - How do I change the textColor in a TimePicker? - Again, can't find the actual TimePicker parent.
These are probably the best questions/answers to my problem, but none of them help me. It would be nice to get a definitive answer on this.
I have combined Paul Burke's Answer and Simon's Answer to succesfully edit the text colour of the TimePicker.
Here's how it is accomplished:
TimePicker time_picker; //Instantiated in onCreate()
Resources system;
private void set_timepicker_text_colour(){
system = Resources.getSystem();
int hour_numberpicker_id = system.getIdentifier("hour", "id", "android");
int minute_numberpicker_id = system.getIdentifier("minute", "id", "android");
int ampm_numberpicker_id = system.getIdentifier("amPm", "id", "android");
NumberPicker hour_numberpicker = (NumberPicker) time_picker.findViewById(hour_numberpicker_id);
NumberPicker minute_numberpicker = (NumberPicker) time_picker.findViewById(minute_numberpicker_id);
NumberPicker ampm_numberpicker = (NumberPicker) time_picker.findViewById(ampm_numberpicker_id);
set_numberpicker_text_colour(hour_numberpicker);
set_numberpicker_text_colour(minute_numberpicker);
set_numberpicker_text_colour(ampm_numberpicker);
}
private void set_numberpicker_text_colour(NumberPicker number_picker){
final int count = number_picker.getChildCount();
final int color = getResources().getColor(R.color.text);
for(int i = 0; i < count; i++){
View child = number_picker.getChildAt(i);
try{
Field wheelpaint_field = number_picker.getClass().getDeclaredField("mSelectorWheelPaint");
wheelpaint_field.setAccessible(true);
((Paint)wheelpaint_field.get(number_picker)).setColor(color);
((EditText)child).setTextColor(color);
number_picker.invalidate();
}
catch(NoSuchFieldException e){
Log.w("setNumberPickerTextColor", e);
}
catch(IllegalAccessException e){
Log.w("setNumberPickerTextColor", e);
}
catch(IllegalArgumentException e){
Log.w("setNumberPickerTextColor", e);
}
}
}
Please note that this answer might be outdated by now. I ran into this a while ago with something that might have been buggy (see my question for more details). Otherwise you should probably follow Vikram's answer.
Not sure why you would need to dive into Java Reflection API for this. Its a simple styling matter. The attribute that you need to override is: textColorPrimary.
<style name="AppTheme" parent="#android:style/Theme.Holo.Light">
....
<item name="android:textColorPrimary">#ff0000</item>
</style>
If you're using the TimePicker inside a Dialog, override android:textColorPrimary in the dialog's theme.
That's about it.
A TimePicker is really just two NumberPickers. Looking into the Widget.NumberPicker style and layout, you'll find the it uses
#style/TextAppearance.Large.Inverse.NumberPickerInputText
Unfortunately, TextAppearance.Large.Inverse.NumberPickerInputText doesn't use one of the attributes that you can set in your theme. So you have two options:
Copy the necessary classes to make your own version of NumberPicker and TimePicker. (You might be able to extract something from libraries like HoloEverywhere)
Use hacks.
If you want to go the second route, you can do this:
private int mNumberPickerInputId = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Resources system = Resources.getSystem();
// This is the internal id of the EditText used in NumberPicker (hack)
mNumberPickerInputId =
system.getIdentifier("numberpicker_input", "id", "android");
// just used for full example, use your TimePicker
TimePicker timePicker = new TimePicker(this);
setContentView(timePicker);
final int hourSpinnerId =
system.getIdentifier("hour", "id", "android");
View hourSpinner = timePicker.findViewById(hourSpinnerId);
if (hourSpinner != null) {
setNumberPickerTextColor(hourSpinner, Color.BLUE);
}
final int minSpinnerId =
system.getIdentifier("minute", "id", "android");
View minSpinner = timePicker.findViewById(minSpinnerId);
if (minSpinner != null) {
setNumberPickerTextColor(minSpinner, Color.BLUE);
}
final int amPmSpinnerId =
system.getIdentifier("amPm", "id", "android");
View amPmSpinner = timePicker.findViewById(amPmSpinnerId);
if (amPmSpinner != null) {
setNumberPickerTextColor(amPmSpinner, Color.BLUE);
}
}
private void setNumberPickerTextColor(View spinner, int color) {
TextView input = (TextView) spinner.findViewById(mNumberPickerInputId);
input.setTextColor(color);
}
EDIT
Upon further investigation, this hack doesn't really work well. It won't allow you to change the color of the NumberPicker above/below values. The color also resets after the use interacts with it. It seems that your only option will be to create your own copies of the necessary classes (option #1 above).
Adding to Vikram answer , Set the theme to timePicker do not set it as style
in styles.xml
<style name="timePickerOrange">
<item name="android:textColorPrimary">#color/orange</item> <!-- color for digits -->
<item name="android:textColor">#color/orange</item> <!-- color for colon -->
<item name="android:colorControlNormal">#color/orange</item> <!-- color for (horizontal) delimeters -->
</style>
and for timePicker
<TimePicker
android:theme="#style/timePickerOrange"
android:id="#+id/timePicker_defaultTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/hero_regular"
android:timePickerMode="spinner"
app:fontFamily="#font/hero_regular" />
#Vikram is right
This is so simple.You can try this way
<style name="abirStyle" parent="#android:style/Theme.Holo.Light.Dialog.NoActionBar">
<item name="android:textColor">#color/colorPrimary</item>
<item name="android:background">#null</item>
<item name="android:textColorPrimary">#color/colorPrimary</item>
</style>
You can try to set android:textColorPrimaryInverse and android:textColorSecondaryInverse. That should do the job without using Reflection.
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:textColorPrimaryInverse">#android:color/black</item>
<item name="android:textColorSecondaryInverse">#color/colorLightGrey</item>
</style>
For those using the spinner, this is an easy way to change the colors and even provides the appropriate shadow effect of the previous and next numbers. Spinner requires a theme because most of the default xml attributes for colors only effect the clock mode.
styles.xml
<style name="timepicker">
<item name="android:textColorSecondary">#color/white </item>
<item name="android:textColorPrimary">#color/white </item>
</style>
xml layout file
<TimePicker
android:id="#+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="#style/timepicker"
android:layout_below="#id/line1"
android:layout_centerHorizontal="true"
android:padding="15dp"
android:timePickerMode="spinner" />
I have some difficulties with setting up a "theme switcher" programmatically.
I would like to switch themes from app (between White (Theme.Light.NoTitleBar) and Dark (Theme.Black.NoTitleBar)) and what I do is:
I set a SharedPreference:
final String PREFS_NAME = "MyPrefsFile";
final SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
final SharedPreferences.Editor editor = settings.edit();
and than I have a two buttons to switch themes (second one is almost identical)
Button ThemeWhite = (Button) findViewById(R.id.ThemeWhite);
ThemeWhite.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
editor.putBoolean("Theme", false);
editor.commit();
System.exit(2);
}
});
and in begging of each activity I check SharedPreference
boolean theme = settings.getBoolean("Theme", false);
if(theme){
this.setTheme(R.style.Theme_NoBarBlack);
} else{
this.setTheme(R.style.Theme_NoBar);
}
setContentView(R.layout.aplikacja);
I define themes in file styles.xml in folder values:
<resources>
<style name="Theme.NoBar" parent="#android:style/Theme.Light.NoTitleBar" />
<style name="Theme.NoBarBlack" parent="#android:style/Theme.NoTitleBar" />
in values-v11:
<resources>
<style name="Theme.NoBar" parent="#android:style/Theme.Holo.Light.NoActionBar" />
<style name="Theme.NoBarBlack" parent="#android:style/Theme.Holo.NoActionBar" />
in values-v14:
<resources>
<style name="Theme.NoBar" parent="#android:style/Theme.DeviceDefault.Light.NoActionBar" />
<style name="Theme.NoBarBlack" parent="#android:style/Theme.DeviceDefault.NoActionBar" />
manifest file:
<application
...
android:theme="#style/Theme.NoBar" >
Everything is working excellent on android >4.0 but when I use 2.2 it doesn't change theme - just font is getting white as it should be but there is no dark background.
I tried checking if it at least works and changed Theme.NoBarBlack in values (for android <3.0) and its value the same as Theme.NoBar and then when I pressed button font wasn't changed -as it should do.
EDIT (PROBLEM FOUND):
So after trying it turn out that on Android 2.2 Manifest file set Background and rest, and programmatically changing theme affect only text color. any idea why that happens?
ANSWER (as I don't have 10 reputation):
On android <3.0 it matters if
super.onCreate(savedInstanceState);
Is before setting up theme or not.
So it should be:
public void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Theme_NoBar);
super.onCreate(savedInstanceState);
setContentView(R.layout.lista);
}
Sorry guys for making problem out of nothing but as it was working on >3.0 I was confused. Spend half of day on it but working. :)
There's a mistake in your styles.xml:
for Theme.NoBarBlack, you set the parent to #android:style/Theme.NoActionBar, but it doesn't exist.
I think you mean #android:style:Theme.NoTitleBar.