I recently just started working with Xamarin Forms and have the task of switching a working iOS project to Droid. It's a multiplatform project so I'm creating the switch in the shared project, but trying to manage its style in .Droid styles.xml or a custom renderer. I'm needing the control to be a bit larger for the tablets.
Using styles.xml, I was able to change the width of the switch control using this line:
<item name="switchMinWidth">120dp</item>
But to my knowledge there's no way to change the height of the control in this manner. I've tried using a custom renderer and used the SetHeight, SetMinHeight, and SetMinimumHeight methods of the Control (Android.Switch.Widget) but nothing is working.
Here's what the switches look like currently:
https://imgur.com/XIPp6vV
I've also tried doing a HeightRequest in the xaml itself but it doesn't change the actual height of the control. Is there anything I can do to make these switches a little taller?
How to increase size of switch control Xamarin forms?
Switch height is controlled by <switch android:track="#drawable/shape_mythumb".../>, and the track's height determines the Switch's overall height. So yo could add this property in your custom SwitchRenderer :
public class MySwitchRenderer : SwitchRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Switch> e)
{
base.OnElementChanged(e);
if(Control != null)
{
Control.SetTrackResource(Resource.Drawable.track);
}
}
}
You need custom a Track layout, you could draw a shape whatever you want.
Example :
track.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/switch_track_on"
android:state_checked="true"/>
<item
android:drawable="#drawable/switch_track_off"
android:state_checked="false"/>
</selector>
switch_track_on.xml
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners
android:radius="15dp" />
<size
android:width="75dp"
android:height="25dp" />
<solid
android:color="#6decacec" />
</shape>
switch_track_off.xml
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners
android:radius="15dp" />
<size
android:width="75dp"
android:height="25dp" />
<solid
android:color="#6db3b1b3" />
</shape>
Related
android:scrollIndicators="top|bottom"
does provide scroll indicators, but they are barely visible at all.
Screenshot of barely visible Scrollindicators
What theme/style color are they using, or how can I assign a custom color?
You can customize the scroll bar by changing its width and setting a custom shape.
Add following tags in your scroll view
android:fadeScrollbars="false"
android:scrollbarStyle="insideInset" // set as required..
android:scrollbarTrackVertical="#drawable/vertical_scrollview_track"
android:scrollbarSize="20dp"// set as required width..
Create following drawable in your drawable folder
vertical_scrollview_track.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#E2E0EB" />
<stroke
android:width="1dp"
android:color="#b3a9a9" />
<size android:width="15dp" />
<corners android:radius="15dp" />
</shape>
change colour as required.
<style>
<item name="android:colorForeground">#FFF</item>
</style>
As with SwitchCompat's track, a low opacity / high transparency version of colorForeground is used. Setting it to full white (or black on light backgrounds) makes the scrollindicators at least a bit more visible before someone finds the real solution.
I have a question. I need to add all borders to an entry in Android using Xamarin.Forms. I created the renderer class in the PCL and referenced it in the xaml file. Then I created the specific class for the renderer in the Android Project.
I have this:
[assembly: ExportRenderer (typeof(EntryCustom), ypeof(EntryRendererCustom))]
namespace InstagramApp.Droid.Renderers
{
public class EntryRendererCustom : EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry>, e)
{
base.OnElementChanged(e);
if(Control != null)
{
}
}
}
}
Now i have to add the code in the IF statement but I'm new with xamarin and the renderers. Can someone help me? If someone can also explain me some basics on how to approach to the custom renderers it could be gold for me. Thanks all!
This is how you can do it :
[assembly: ExportRenderer(typeof(Entry), typeof(EntryRendererImplementation))]
namespace MyProject.Droid.Renderers
{
public class EntryRendererImplementation : EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.Background = this.Resources.GetDrawable(Resource.Drawable.RoundedCornerEntry);
Control.SetPadding(10,10,10,3);
}
}
}
}
You will have to create this file in Resources/drawable/RoundedCornerEntry.xml in your android project
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" >
<shape android:shape="rectangle">
<gradient
android:startColor="#color/entry_background"
android:endColor="#color/entry_background"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#color/entry_border" />
<corners
android:radius="6dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<gradient
android:startColor="#color/entry_background"
android:endColor="#color/entry_background"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#c6c6c6" />
<corners
android:radius="6dp" />
</shape>
</item>
</selector>
Of course, you need to update your Resources/values/colors.xml file
Something like :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="entry_background">#ffffff</color>
<color name="entry_border">#BDBDBD</color>
</resources>
There you go!
you have a lot of example for custom render in the page of xamarin.
Here is a long explanation :
https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/custom-renderer/
Here is an example for a hybridwebview:
https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/custom-renderer/hybridwebview/
Well to understand how to work with Custome Renderers there are plenty of tutorial both written and videos about that so it is better if you check that yourself ,
there are other ways to add the borders without the need of using custom renderer, and this depends on what is the image you have in mind when you say borders
Put the entry inside a frame
Put the entry above a boxView, with this you would have more control over the "Border" for example, thickness, opacity and even animation (maybe you want the borders to blink)
To do the second approach I would use a grid for that like this
<Grid>
<BoxView Color="Blue" />
<Entry/>
</Grid>
The order here is important in a grid for this case the elements which are defined first are placed below . Another obviouse point is the the height and width of the BoxView should be slightly larger than those for the Enrty, and the bigger the box the thicker the border (and I am still talking about code here ;) )
I am trying to substitute and override the default touch highlight color of the theme for the corporate ones.
I have successfully done it for the actionbar buttons by using actionBarItemBackground on my theme properties, but I am looking at an application-wide change where every pressed element in buttons, actionbar, drawer or menus defaults to my color instead of the Holo blue. I have tried properties like colorPressedHighlight. colorFocusedHighlight but none worked.
I would also like to change the color of the scroll end hinting, the little gradients on the sides of a scrollable element when it has reached one end and the uses is still attempting to scroll.
Given the high volume of incorrect answers, let me restate again. I know what a selector is, I know how to use it, I have explicitly stated that I have overriden the theme with several different subproperties but none does what I asked for. I am looking for the property to change both the default touch highlight for all elements, and the color for the scroll cache hinting, again for all elements.
To change the end-of-scroll hint you can try this library for a fancy solution, or just the code below for a quick solution(code source and explanation):
static void brandGlowEffect(Context context, int brandColor) {
int glowDrawableId = context.getResources().getIdentifier("overscroll_glow", "drawable", "android");
Drawable androidGlow = context.getResources().getDrawable(glowDrawableId);
androidGlow.setColorFilter(brandColor, PorterDuff.Mode.MULTIPLY);
}
Note that both of these are a bit hacky fundamentally as they works on platform drawable resources that are not guaranteed to stay the same between Android releases, though the plus note is that these resources have not been renamed from 1.0 to KitKat.
I found the answer here: https://stackoverflow.com/a/9062723/1266326
The name of the component is not scroll hinting but overscroll.
What you want to do is add a custom_color.xml file to your apps drawables folder. You can name it what ever you want and it might look similar to
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="#004F81"
android:endColor="#004F81"
android:angle="270" />
<stroke
android:width="3dp"
android:color="#004F81" />
<corners
android:radius="3dp" />
</shape>
</item>
<item android:state_focused="true" >
<shape>
<gradient
android:endColor="#004F81"
android:startColor="#004F81"
android:angle="270" />
<stroke
android:width="3dp"
android:color="#004F81" />
<corners
android:radius="3dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:endColor="#43AFE8"
android:startColor="#43AFE8"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#43AFE8" />
<corners
android:radius="6dp" />
<padding
android:left="2dp"
android:top="2dp"
android:right="2dp"
android:bottom="2dp" />
</shape>
</item>
</selector>
Then in all of your layout xml files just set the back round of each element to your custom back round
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/custom_color"
android:textColor="#FFFFFF"
android:text="Button" />
I am currently implementing this and it works quite well.
You can read here about the State List.
Basically what you'd want to do is create a selector that overrides the color with your color and then create a theme that is a "child" of the default holo theme and change the elements you want to change the color for with your selector. This will give you a more application wide solution.
Read about the Style and Themes here.
I guess the right answer would be "No, there isn't a theme attribute you can override in your own themes that will magically change the highlight color across your app's Activities and Fragments". Or, in my case "No, there isn't a theme attribute you can reference to get your current theme's highlight color".
As to "Why?", I guess it's because Google itself doesn't use a theme attribute across all of the standard UI widgets (TextView, EditText, Button, ListView, etc.) because they rely on PNG drawables (9 patches) and not only on state lists.
Great question, though.
You can set up your styles here: http://android-holo-colors.com/
Then download, integrate and set it in your app.
i am trying to create a tab as following which is a Holo Light theme but as i am using android 2.3.6 i got to create it on my own.
the problem i am facing is i am bot be able to create the blue bottom like
here is the code so far in a drawable , can any one tell me how to create the bottom border for the same ?
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="63dp">
<shape android:shape="rectangle">
<solid android:color="#898989" />
</shape>
</item>
<item android:top="63dp" android:bottom="3px">
<shape android:shape="rectangle">
<solid android:color="#color/app_background" />
</shape>
</item>
<item android:left="#dimen/tab_space" android:right="#dimen/tab_space">
<shape android:shape="rectangle">
<gradient android:angle="90" android:startColor="#d9d9d9"
android:endColor="#dfdfdf"/>
<!-- this is create a border but on every side -->
<stroke android:width="3px" android:color="#38c0f4" />
</shape>
</item>
</layer-list>
so wht i dont know is to create
1) bottom stroke
2) shadow under each tab
You could just set a 9 patch image as the background image. And this image could contain a stroke.
Even if this is not totally on topic of your question, I suggest to migrate your project to ActionBarSherlock. ActionBarSherlock lets you use all the ICS Actionbar Apis with earlier API-Levels and brings the ICS Actionbar Style to all Devices with at least Android 2.2.
Using this project will make it very easy to move your app to Ice Cream Sandwich compatibility, and solve your current design problem.
I have a button as in the following:
<Button
android:text="Submit"
android:id="#+id/Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
In my onCreate() event, I am calling Button01 like this:
setContentView(R.layout.main);
View Button01 = this.findViewById(R.id.Button01);
Button01.setOnClickListener(this);
There is a background in the application, and I want to set an opacity on this submit button. How can I set an opacity for this view? Is it something that I can set on the java side, or can I set in the main.xml file?
On the java side I tried Button01.mutate().SetAlpha(100), but it gave me an error.
I'm amazed by everyone else's MUCH more complicated answers.
XML
You can very simply define the alpha in the color definition of the button (or any other view) in your xml:
android:color="#66FF0000" // Partially transparent red
In the above example, the color would be a partially transparent red.
When defining the color of a view, the format can be either #RRGGBB or #AARRGGBB, where AA is the hex alpha value. FF would be fully opaque and 00 would be full transparent.
Dynamically
If you need to dynamically alter the opacity in your code, use
myButton.getBackground().setAlpha(128); // 50% transparent
Where the INT ranges from 0 (fully transparent) to 255 (fully opaque).
I guess you may have already found the answer, but if not (and for other developers), you can do it like this:
btnMybutton.getBackground().setAlpha(45);
Here I have set the opacity to 45. You can basically set it from anything between 0(fully transparent) to 255 (completely opaque)
Much more easier from the above.
Default alpha attribute is there for button
android:alpha="0.5"
The range is between 0 for complete transparent and 1 for complete opacity.
What I would suggest you do is create a custom ARGB color in your colors.xml file such as :
<resources>
<color name="translucent_black">#80000000</color>
</resources>
then set your button background to that color :
android:background="#android:color/translucent_black"
Another thing you can do if you want to play around with the shape of the button is to create a Shape drawable resource where you set up the properties what the button should look like :
file: res/drawable/rounded_corner_box.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#80000000"
android:endColor="#80FFFFFF"
android:angle="45"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<corners android:radius="8dp" />
</shape>
Then use that as the button background :
android:background="#drawable/rounded_corner_box"
I just found your question while having the similar problem with a TextView. I was able to solve it, by extending TextView and overriding onSetAlpha. Maybe you could try something similar with your button:
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
public class AlphaTextView extends TextView {
public AlphaTextView(Context context) {
super(context);
}
public AlphaTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AlphaTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public boolean onSetAlpha(int alpha) {
setTextColor(getTextColors().withAlpha(alpha));
setHintTextColor(getHintTextColors().withAlpha(alpha));
setLinkTextColor(getLinkTextColors().withAlpha(alpha));
return true;
}
}
According to the android docs view alpha is a value between 0 and 1. So to set it use something like this:
View v;
v.setAlpha(.5f);
android:background="#android:color/transparent"
The above is something that I know...
I think creating a custom button class is the best idea
API Level 11
Recently I came across this android:alpha xml attribute which takes a value between 0 and 1. The corresponding method is setAlpha(float).
Although btnMybutton.getBackground().setAlpha(45); is nice idea, it just apply alpha to background and not the whole view.
If you want apply alpha to view use btnMybutton.setAlpha(0.30f); instead. This apply opacity to View. It accepts a value between 0 and 1.
Doc says:
Sets the opacity of the view. This is a value from 0 to 1, where 0
means the view is completely transparent and 1 means the view is
completely opaque. If this view overrides onSetAlpha(int) to return
true, then this view is responsible for applying the opacity itself.
Otherwise, calling this method is equivalent to calling
setLayerType(int, android.graphics.Paint) and setting a hardware
layer. Note that setting alpha to a translucent value (0 < alpha < 1)
may have performance implications. It is generally best to use the
alpha property sparingly and transiently, as in the case of fading
animations.
For a view you can set opacity by the following.
view_name.setAlpha(float_value);
The property view.setAlpha(int) is deprecated for the API version greater than 11. Henceforth, property like .setAlpha(0.5f) is used.
I've run into this problem with ICS/JB because the default buttons for the Holo theme consist of images that are slightly transparent. For a background this is especially noticeable.
Gingerbread vs. ICS+:
Copying over all of the drawable states and images for each resolution and making the transparent images solid is a pain, so I've opted for a dirtier solution: wrap the button in a holder that has a white background. Here's a crude XML drawable (ButtonHolder) which does exactly that:
Your XML file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/Content">
<RelativeLayout style="#style/ButtonHolder">
<Button android:id="#+id/myButton"
style="#style/Button"
android:text="#string/proceed"/>
</RelativeLayout>
</LinearLayout>
ButtonHolder.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#color/white"/>
</shape>
</item>
</layer-list>
styles.xml
.
.
.
<style name="ButtonHolder">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:background">#drawable/buttonholder</item>
</style>
<style name="Button" parent="#android:style/Widget.Button">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:textStyle">bold</item>
</style>
.
.
.
However, this results in a white border because the Holo button images include margins to account for the pressed space:
So the solution is to give the white background a margin (4dp worked for me) and rounded corners (2dp) to completely hide the white yet make the button solid:
ButtonHolder.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#android:color/transparent"/>
</shape>
</item>
<item android:top="4dp" android:bottom="4dp" android:left="4dp" android:right="4dp">
<shape android:shape="rectangle">
<solid android:color="#color/white"/>
<corners android:radius="2dp" />
</shape>
</item>
</layer-list>
The final result looks like this:
You should target this style for v14+, and tweak or exclude it for Gingerbread/Honeycomb because their native button image sizes are different from ICS and JB's (e.g. this exact style behind a Gingerbread button results in a small bit of white below the button).
For API < 11 for textView color I did the following:
int textViewColor = textView.getTextColors().getDefaultColor();
textView.setTextColor(Color.argb(128, Color.red(textViewColor), Color.green(textViewColor), Color.blue(textViewColor))); //50% transparent
A little cumbersome, but hey, it works :-)
If you use Kotlin , it's very easy to set alpha like this
imageView.alpha= 0.5F
where the value must be a float number.
I know this already has a bunch of answers but I found that for buttons it is just easiest to create your own .xml selectors and set that to the background of said button. That way you can also change it state when pressed or enabled and so on. Here is a quick snippet of one that I use. If you want to add a transparency to any of the colors, add a leading hex value (#XXcccccc). (XX == "alpha of color")
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid
android:color="#70c656" />
<stroke
android:width="1dp"
android:color="#53933f" />
<corners
android:radius="4dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#70c656"
android:endColor="#53933f"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#53933f" />
<corners
android:radius="4dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>