0

I'm new to both Xamarin and C#, and am looking for a way to make my object code reusable instead of having to copy/paste similar code blocks for a large number of items.

Presently I'm working with RatingBar objects on a control. I have about 20 of them with nearly identical behavior. Unfortunately, having neither knowledge of C# or Xamarin, I'm having trouble forming a Google search that's leading me to useful results.

Here's a sample of two similar items in the layout:

'''XML
     <TextView
        android:text="Death"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lbl_death" />
    <RatingBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/arcanadeath"
        android:numStars="5"
        android:rating="1"
        android:stepSize="1" />
    <TextView
        android:text="Fate"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lbl_fate" />
    <RatingBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/arcanafate"
        android:numStars="5"
        android:rating="1"
        android:stepSize="1" />

'''

And the associated code in the Activity:

'''C#

    RatingBar death = FindViewById<RatingBar>(Resource.Id.arcanadeath);
    death.Rating = thisMage.ArcanaDeath;
    death.RatingBarChange += (sender, e) =>
    {
        thisMage.ArcanaDeath = (int)death.Rating;
        conn.Update(thisMage);
    };

    RatingBar fate = FindViewById<RatingBar>(Resource.Id.arcanafate);
    fate.Rating = thisMage.ArcanaFate;
    fate.RatingBarChange += (sender, e) =>
    {
        thisMage.ArcanaFate = (int)fate.Rating;
        conn.Update(thisMage);
    };

'''

If anyone could tell me how to wrap this up into a class, or reuseable object of some kind - or direct me to what I should be searching for to understand how to do this, I would greatly appreciate it.

Thank you,

4
  • 1
    google "xamarin android custom control' Commented Sep 2, 2019 at 19:19
  • Thanks @Jason. I looked through the first page of hits for that, and I'm not finding items that help me at the level I need, given my inexperience with Xamarin. Commented Sep 2, 2019 at 20:18
  • @xodusprime then google for the terms that you don't understand till you get to the terms that you understand... Commented Sep 2, 2019 at 23:59
  • could it work ? Commented Sep 23, 2019 at 8:06

1 Answer 1

1

you could create a simple custom components like this:

create RateLayout.cs:

class RateLayout : LinearLayout
{
    private RateChangeListener listener;
    protected RateLayout(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
    {

    }

    public RateLayout(Context context) : base(context)
    {

        init(context);
    }

    public RateLayout(Context context, IAttributeSet attrs) : base(context, attrs)
    {
        init(context);
    }

    public RateLayout(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
    {
        init(context);
    }
    public void SetRateChangeListener(RateChangeListener listener)
    {
        this.listener = listener;
    }
    private void init(Context context)
    {
        LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.MatchParent);
        View view = LayoutInflater.From(context).Inflate(Resource.Layout.rate_layout,null);
        view.LayoutParameters = parms;
        RatingBar death = view.FindViewById<RatingBar>(Resource.Id.arcanadeath);

        death.RatingBarChange += (sender, e) =>
        {
            listener.RateDeathChange((int)death.Rating);
        };

        RatingBar fate = view.FindViewById<RatingBar>(Resource.Id.arcanafate);

        fate.RatingBarChange += (sender, e) =>
        {
            listener.RateDeathChange((int)fate.Rating);
        };
        AddView(view);
    }

}

create an intetface RateChangeListener.cs :

interface RateChangeListener
{
    void RateDeathChange(int rate);
    void RateFateChange(int rate);
}

then when you add to activity's layout axml:

 ...
 <App2.RateLayout 
    android:id="@+id/ratelayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

 />
 ...

in activity code:

public class YourActivity: Activity,RateChangeListener
{
   protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.activity_layout);
        ...
        RateLayout rateLayout = FindViewById<RateLayout>(Resource.Id.ratelayout);
        rateLayout.SetRateChangeListener(this);
    }

   public void RateDeathChange(int rate)
    {
        Toast.MakeText(this, rate + "  star", ToastLength.Short).Show();
    }

    public void RateFateChange(int rate)
    {
        Toast.MakeText(this, rate + "  star", ToastLength.Short).Show();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.