How do I set strokeColor to MaterialButton programmatically? - material-components-android

How to set the stroke color to MaterialButton programmatically
<com.google.android.material.button.MaterialButton
android:layout_weight="1"
android:id="#+id/viewQrLayout"
style="?attr/materialButtonOutlinedStyle"
android:textColor="#color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" View Receipt"
app:strokeColor="#color/red"
app:strokeWidth="2dp"
android:textAllCaps="false"
app:icon="#drawable/ic_baseline_qr_code_scanner_24"
app:iconTint="#color/color2"
android:layout_gravity="start"
app:iconPadding="0dp"
app:iconSize="25dp"
android:letterSpacing=".1"
app:cornerRadius="5dp"
android:textStyle="bold"
android:textAppearance="#style/TextAppearance.AppCompat.Small"
/>

To set the stroke color to MaterialButton programmatically
val colorList = ColorStateList(
arrayOf(
intArrayOf(-android.R.attr.state_enabled), // Disabled
intArrayOf(android.R.attr.state_enabled) // Enabled
),
intArrayOf(
Color.BLACK, // The color for the Disabled state
Color.parseColor("#FFBB86FC") // The color for the Enabled state
)
)
binding!!.viewQrLayout.strokeColor = colorList

Related

How to do you change the color of the "underline" marking the selected tab on a TabbedPage in Xamarin.Forms?

I'm currently adding several different themes (10-ish) to a Xamarin.Forms app. I've figured out how to dynamically change the color of everything except the "underline" on a tab on TabbedPage (see the attached image).
Screenshot identifying the TabbedPage "underline" with a red rectangle
I've tried to set the five different color properties (that I know of) on the TabbedPage as well as the BackgroundColor on the ContentPages in the tabs:
TabbedPage tabbedPage = new TabbedPage { Title = "Tabbed Page" };
tabbedPage.BarBackgroundColor = Color.Black;
tabbedPage.BarTextColor = Color.Gray;
tabbedPage.SelectedTabColor = Color.Black;
tabbedPage.UnselectedTabColor = Color.Black;
tabbedPage.BackgroundColor = Color.Black;
tabbedPage.Children.Add(new ContentPage { Title = "Page 1", BackgroundColor = Color.Gray });
tabbedPage.Children.Add(new ContentPage { Title = "Page 2", BackgroundColor = Color.Gray });
await Application.Current.MainPage.Navigation.PushAsync(tabbedPage);
Is it possible to set the color of the "underline" in Xamarin.Forms? Or do I need to make platform-specific changes to achieve this?
Use app:tabIndicatorColor.
All you have to do is to create a Tabar.xml file with below code in the layout folder(create one if needed) in the Android project's Resources folder and change the color via app:tabIndicatorColor;
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.tabs.TabLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="#ff0000"/>
And then add below code in onCreate method of MainActivity.cs
TabLayoutResource = Resource.Layout.Tabar;

Xamarin.Forms Sizing elements in a WrapLayout

Current:
Goal:
Attempting to implement a WrapLayout to allow for dynamic button addition in a clean horizontally adding format, as shown in the 'Goal' picture. However as seen in 'Current', the sizing of the buttons in the WrapLayout are far from ideal.
Have found through simple trial and error that using height and width requests in any of the elements (scrollView, wrapLayout, buttons) result in no change to the button formats.
HeightRequest = xx;
WidthRequest = xx;
The only way, I've found thus far, to change the sizing of the wrapLayout elements is to add a large amount of children, example:
As displayed, my understanding of how to format WrapLayout children is rather lacking. So, how to format the number of children allowed on each row and how to properly format the children of a WrapLayout?
Current implementation developed following the WrapLayout class shown in the Xamarin Developer Sample for WrapLayout
ScrollView scrollView = new ScrollView {
Margin = new Thickness(20, 20, 20, 20),
};
WrapLayout wrapLayout;
wrapLayout = new WrapLayout {
ColumnSpacing = 12,
};
scrollView.Content = wrapLayout;
wrapLayout.Children.Add(
new Button
{
Text = "9 ° (?)",
BackgroundColor = Color.Yellow,
BorderColor = Color.Black,
}
);
wrapLayout.Children.Add(
new Button
{
Text = "10.5 ° (?)",
BackgroundColor = Color.Gray,
BorderColor = Color.Black,
}
);
You should look at CollectionView Nuget Package
You can use the GridCollectionView, which is based on the WrapLayout.
There, you can either use:
UniformGrid
The number of columns arranged in each row is specified. Each column width becomes the width obtained by dividing the row width by that value. This number of columns can be set by PortraitColumns and LandscapeColumns properties.
"
AutoSpacingGrid
Once a column width is specified, each column is arranged until fitting in each row and adjusted automatically each spacing. A column width can be set by ColumnWidth property and Setting SpacingType property can change how to adjust the spacing.
An Example:
<ai:GridCollectionView
ItemsSource="{Binding ItemsSource}" TouchFeedbackColor="Yellow"
ColumnWidth="100" ColumnHeight="1.0" >
<ListView.ItemTemplate>
<DataTemplate>
<ai:ContentCell>
<Label Text="{Binding Name}" />
</ai:ContentCell>
</DataTemplate>
</ListView.ItemTemplate>
</ai:GridCollectionView>
I recommend using the AiForms.Layout package:
https://dev.to/muak_x/wraplayout-and-repeatablestacklayout-for-xamarinforms-1dck
here is the sample, and it works as we expected:
<aiforms:WrapLayout Spacing="4" UniformColumns="3" IsSquare="true" HorizontalOptions="FillAndExpand">
<BoxView Color="Red" />
<BoxView Color="Blue" />
<BoxView Color="Green" />
<BoxView Color="Black" />
<BoxView Color="Yellow" />
</aiforms:WrapLayout>
And there is a RepeatableWrapLayout with Itemtemplate and ItemsSource Binding.

How to change ListView item text color in Xamarin.Android?

I have a simple ListView like below : -
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/asimplelistView1"
/>
I am binding this ListView by array ArrayAdapter using:
ArrayAdapter<string> arrayAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleExpandableListItem1,mItems);
My app secondary text color is white(#FFFFFF) and theme is android:Theme.Material.Light..
My problem is: I need to change item text color to gray because items text color is not visible on white background.
In Java we can do like below:
// Create a List from String Array elements
List<String> fruits_list = new ArrayList<String>(Arrays.asList(fruits));
// Create an ArrayAdapter from List
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, fruits_list){
#Override
public View getView(int position, View convertView, ViewGroup parent){
// Get the Item from ListView
View view = super.getView(position, convertView, parent);
// Initialize a TextView for ListView each Item
TextView tv = (TextView) view.findViewById(android.R.id.text1);
// Set the text color of TextView (ListView Item)
tv.setTextColor(Color.RED);
// Generate ListView Item using TextView
return view;
}
};
How to achieve this in C# using xamarin.android
I had that same situation and was able to fix it setting a new "item" entry on the styles.xml file:
<item name="android:textColorPrimary">#color/colorAccent</item>
The color value can be any entry reference to your colors.xml or an hexadecimal value

Android Scale ImageView Within FrameLayout

My app requires the imageview scaling ,moving and rotating within the framelayout.All are done successfully but the actions are working within the imageview bounds,not on the framelayout.here is my xml and java :
<FrameLayout
android:id="#+id/square_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<ImageView
android:id="#+id/new_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="matrix"
/>
</FrameLayout>
ImageView imageView = (ImageView) findViewById(R.id.new_img);
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
//mScaleFactor gets from scaleGesture
float scaleImageCenterX = (imageView.getWidth()*mScaleFactor)/2;
float scaleImageCenterY = (imageView.getHeight()*mScaleFactor)/2;
mMatrix.reset();
mMatrix.postScale(mScaleFactor, mScaleFactor);
mMatrix.postRotate(mRotationDegrees,scaleImageCenterX,scaleImageCenterY);
mMatrix.postTranslate(mFocusX - scaleImageCenterX, mFocusY - scaleImageCenterY);
// ImageView view = (ImageView)imageView;
imageView.setImageMatrix(mMatrix);
return true;
}
Only the image is scaling (ImageView not Scaling). Please give the right direction or any suggestion friends.

Binding a TextBox's Width to its parent container's ActualWidth

I'm loading a Textbox and a Button into a horizontal StackPanel programmatically. The size of the button (which only contains an Image) is fixed, but I can't get the textbox to fill the available width of its parent. This is what the code looks like:
StackPanel parent = new StackPanel() {
Orientation = Orientation.Horizontal,
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Top,
};
TextBox textbox = new TextBox() {
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Top,
//MinWidth = 375,
};
Button btn = new Button() {
Content = new Image() {
MaxHeight = 40,
MaxWidth = 40,
MinHeight = 40,
MinWidth = 40,
Margin = new Thickness( 0 ),
Source = new BitmapImage(
new Uri( "btnimage.png", UriKind.Relative ) ),
},
HorizontalAlignment = HorizontalAlignment.Right,
BorderBrush = new SolidColorBrush( Colors.Transparent ),
Margin = new Thickness( 0 ),
};
btn.Click += ( ( s, e ) => OnBtnClicked( s, e, textbox ) );
parent.Children.Add( textbox );
parent.Children.Add( btn );
If I uncomment the MinWidth setting for the textbox it is displayed as I want it to, but I'd like to not have to specify a width explicitly. I tried adding a binding as follows but that doesn't work at all (the textbox just disappears!)
Binding widthBinding = new Binding() {
Source = parent.ActualWidth,
};
passwdBox.SetBinding( TextBox.WidthProperty, widthBinding );
Thanks for your help in advance!
Instead of using StackPanel (which always tries to keep elements as small as it can such that they all fit), use a Grid. Then you could do something like this:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/> <!-- Note "*" means to use the rest of the space -->
<ColumnDefinition Width="40"/>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0"/>
<Button Grid.Column="1">
<Button.Content>
<Image .../>
</Button.Content>
</Button>
</Grid>
You can convert this to code instead of XAML if you prefer, XAML's just easier to type here on-the-fly.
The right answer is don't do it. See my answer at this question and the same idea applies to Silverlight on Windows Phone.
In your example, you should be using a DockPanel.
<toolkit:DockPanel>
<Button toolkit:DockPanel.Dock="Right" />
<TextBox /> <!-- fill is implied -->
</toolkit:DockPanel>

Resources