I want to have a textview which represents a rating, and I wanted the background to go from light to dark according to the value. meaning 1=white background and 5=red background. What do you think the best way of doing this would be?
Thanks
Amit
For binding BackgroundColor:
MvvmCross includes a BackgroundColor binding and there is an example of it being used with a TextView in ValueConversion sample in View_Colors.axml
<TextView
android:layout_width="fill_parent"
android:layout_height="100dp"
local:MvxBind="BackgroundColor Color, Converter=NativeColor; Text Color; TextColor Color, Converter=ContrastColor"
/>
For converting from an int to an colo(u)r:
I'd recommend you use a ValueConverter which inherits from MvxColorConverter.vs - e.g. something like:
public void RatingColorConverter : MvxColorConverter
{
protected override MvxColor Convert(object value, object parameter, System.Globalization.CultureInfo culture)
{
switch ((int)value)
{
case 1:
return new MvxColor(255,255,255);
case 2:
return new MvxColor(255,200,200);
// etc
}
}
}
Related
So I have this String "green" inside a constant (lernset.color). I would like to set a .foregroundColor() to a symbol (systemName: "folder") inside of a list, so that it has the same color as written inside the constant (lernset.color).
The problem I get: I dont know how to convert this constant (lernset.color) which has the datatype of a String into the datatype of Color.
I already tried:
... .foregroundColor(Color.lernset.color)
... .foregroundColor(Color.String(lernset.color))
I also tried assigning the constant to a variable... but
nothing worked.
[ So normally you would type: .foregroundColor(Color.green)
But in this case I want the List to automatically adapt, so that the Color could change the way the constant (lernset.color) changes...
Thank you for your help in advance
Use
Color(lernset.color)
It only works if the String value of lernset.color is defined as a Color Set in Assets
This
Color.lernset.color
or
Color.green
References a static variable. You would need something like
extension Color{
static let yourColor: Color = Color("nameHere")
}
Then you can call
Color.yourColor
With "nameHere" being the name of a Color Set
If you want to use a string you can add an extension to Color :
extension Color {
static subscript(name: String) -> Color {
switch name {
case "green":
return Color.green
case "white":
return Color.white
case "black":
return Color.black
default:
return Color.accentColor
}
}
}
Usage :
Color[lernset.color]
My parent activity contains a ViewPager that hosts 3 fragments.
MainView:
<CoordinatorLayout>
<AppBarLayout/>
<ViewPager/>
</CoordinatorLayout>
In one fragment, I have a RecyclerView that displays CardViews as items
Child fragment:
<CoordinatorLayout>
<MvxSwipeRefreshLayout>
<MvxRecyclerView
local:MvxItemTemplate="#layout/child_item">
</MvxSwipeRefreshLayout>
</CoordinatorLayout>
Inside the child fragment I want each child (a CardView) to host its own RecyclerView that 1) displays its child horizontally and 2) scrollable. But the problem is, even though I have set the layoutManager and the orientation values in XML, it does not work. It displays things vertically and does not scroll.
Child_item
<CardView>
<MvxRecyclerView
local:layoutManager="android.support.v7.widget.LinearLayoutManager"
android:orientation="horizontal"
local:MvxItemTemplate="#layout/child_child"
android:background="#color/Red"/>
</CardView>
Child item inside the Cardview child_child.xml:
<LinearLayout android:background="#color/Grey">
<Button>
</LinearLayout>
Currently, the main ViewPager works fine; I can swipe to change fragments. The child fragment also supports vertical scrolling; I tried adding multiple cardviews so that it has to scroll, and I can do that just fine. Inside the cardview I added a Seekbar to see if child elements were receiving touch inputs, which also worked. Only the RecyclerView inside the card is not receiving any touch inputs, or responding to the orientation = horizontal request.
How it is currently: The "XXXXX" buttons are stacked vertically. Note that there are more than 2 "XXXX" buttons. THe cardview clipped it but ot does not scroll.
What is expected/what I'm trying to achieve: the buttons should be stacked horizontally and should be scrollable.
There is a bug in MvxRecyclerView in MvvmCross 4.4.0 that ignores XML atrributes and simply hardcodes a default LinearLayoutManager. Further developments at GitHub issue.
For the time being, for this use case, I hard-coded an MvxRecyclerView subclass to SetLayoutManager(new LinearLayoutManager(context) { Orientation = Horizontal }); and it works.
[Register("bug.droid.components.BugFixRecyclerView")]
public sealed class BugFixRecyclerView : MvxRecyclerView
{
public BugFixRecyclerView(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public BugFixRecyclerView(Context context, IAttributeSet attrs) : this(context, attrs, 0, new MvxRecyclerAdapter())
{
}
public BugFixRecyclerView(Context context, IAttributeSet attrs, int defStyle) : this(context, attrs, defStyle, new MvxRecyclerAdapter())
{
}
public BugFixRecyclerView(Context context, IAttributeSet attrs, int defStyle, IMvxRecyclerAdapter adapter) : base(context, attrs, defStyle, adapter)
{
if (adapter == null)
return;
var layoutManager = new LinearLayoutManager(context) { Orientation = Horizontal };
SetLayoutManager(layoutManager);
var itemTemplateId = MvxAttributeHelpers.ReadListItemTemplateId(context, attrs);
var itemTemplateSelector = MvxRecyclerViewAttributeExtensions.BuildItemTemplateSelector(context, attrs);
adapter.ItemTemplateSelector = itemTemplateSelector;
Adapter = adapter;
if (itemTemplateSelector.GetType() == typeof(MvxDefaultTemplateSelector))
ItemTemplateId = itemTemplateId;
}
}
and in the CardView's RecyclerView, replace the default MvxRecyclerView to use this subclass.
I have 2 columns like TPF and TPF_FLAG which i get from my rest service. TPF_FLAG will be 0 or 1 which indicates that should i change the background color of TPF cell or not. If it 1 that TPF cell background should be red. How do i do this?
You can use the renderer on the column like this:
{
text: 'TD',
dataIndex: 'TPF',
renderer: function (value, metaData, record) {
if(record.get('TPF_FLAG') == 1){
metaData.tdStyle = 'background-color: red';
}
return value;
}
}
And note, that the value you return from the renderer will be shown in the column.
Also, you can look at the docs for more things you can do with renderer.
I know I can override paint to change the text color in a labelfield, but I was wondering if there is a command like setBackground(BackgroundFactory.createSolidBackground(Color.WHITE)); only for the foreground.
In override "paint" method also you have g.setColor(color); method;
You said "without overiding paint"; So I am not using the paint method here;
label=new LabelField("Change color", Field.FIELD_HCENTER)
{
protected void applyTheme(Graphics g, boolean arg1)
{
g.setColor(Color.GREEN);
super.applyTheme(g, arg1);
}
};
add(label);
Like this you can do; I am providing this because it is one way of set the Color to the LabelField;
I dont think without overriding paint method you can set the LabelField's text color. If you set the background of the labelField then it will effect on the labelField not on the labelfield's text. So for changing the color of the text you have to override the paint method of labelfield. Give the color you want to the text and labelField's text will get the color you defined to them.
Yes you can override protected void applyTheme(Graphics g, boolean arg1) to change the text color of the labelfield.
i think this may help you
How can I set text color of all Fields to white?
AFAIK, there is no such option in BB API. You can only override Field.paint(Grahpics grahpics), smth like this:
...
public void paint(Grahpics grahpics) {
int initialColor = grahpics.getColor(); // just in case
grahpics.setColor(Color.WHITE);
super.paint(grahpics);
grahpics.setColor(initialColor);
}
...
So you could create a set of custom white-colored Fields (WhiteLabelField, WhiteEditField, etc.) and use them istead of the originals.