Tuesday 6 October 2015

How to create custom renderer for ListView in android Xamarin.Forms?

Step 1- add follwoing code to your xaml page


<local:CustomListView  
x:Name="List"
    ItemsSource="{Binding yourDataList}"
    SelectedItem="{Binding ItemSelected, Mode=TwoWay}"
  BackgroundColor="Transparent">
  </local:CustomListView>


 Step 2- create class in your PCL

namespace YourNameSpace
{
public class CustomListView:ListView
{

}
}

 Step 3- create class platform specific like

 [assembly: ExportRenderer (typeof (CustomListView), typeof (CustomListViewRenderer))]
namespace YourNamespace.Droid
{
public class CustomListViewRenderer: ListViewRenderer
{
public CustomListViewRenderer ()
{
}
protected override void OnElementChanged (ElementChangedEventArgs<ListView> e)
{
base.OnElementChanged (e);
var listView= Control as Android.Widget.ListView;
listView.Divider = null;
listView.DividerHeight = 0;

}
}
}

Friday 26 June 2015

How to create custom renderer for Slider Xamarin.forms android

[assembly: ExportRenderer (typeof (CustomSlider), typeof (CustomSliderRenderer))]
namespace Sample.Droid
{
public class CustomSliderRenderer :SliderRenderer
{
public CustomSliderRenderer ()
{
}

protected override void OnElementChanged(ElementChangedEventArgs<Slider> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.ProgressDrawable.SetColorFilter(Android.Graphics.Color.Red, PorterDuff.Mode.SrcIn);
this.Control.SetOnSeekBarChangeListener(new SeekBarListener());
}
}



public class SeekBarListener : Java.Lang.Object, SeekBar.IOnSeekBarChangeListener
{
public void OnProgressChanged(SeekBar seekBar, int progress, bool fromUser)
{
seekBar.Progress = progress;
}

public void OnStartTrackingTouch(SeekBar seekBar)
{
}

public void OnStopTrackingTouch(SeekBar seekBar)
{
}
}
}
}

Monday 25 May 2015

How to read/write value in sharedpreferences xamarin.forms android?


// read values using key value 
public static string GetString(this Application application, string key)
{
var prefs = application.Context.GetSharedPreferences(application.PackageName, FileCreationMode.Private);
return prefs.GetString(key, string.Empty);
}


//write values
public static void SaveString(this Application application, string key, string value)
{
var prefs = application.Context.GetSharedPreferences(application.PackageName, FileCreationMode.Private);

var prefEditor = prefs.Edit();

prefEditor.PutString(key, value);

prefEditor.Commit();

How to create Bitmap image and store to sdcard xamarin.forms android?

Bitmap bitmap = Control.DrawingCache;
            Canvas canvas = new Canvas(bitmap);
            ImageView image = new ImageView(Control.Context);
            Java.IO.File file = Android.OS.Environment.ExternalStorageDirectory;
            //Captured chart image
            image.SetImageBitmap(bitmap);
var path = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;

path = System.IO.Path.Combine(path, e.Name);      
FileStream fos = new FileStream(path, FileMode.CreateNew);

            bitmap.Compress(Bitmap.CompressFormat.Png, 100, fos);

Wednesday 2 April 2014

How to create CountDown Timer in Xamarin Android?

private System.Timers.Timer _timer;
private int _countSeconds;

void Main()
{
    _timer = new System.Timers.Timer();
    //Trigger event every second
    _timer.Interval = 1000;
    _timer.Elapsed += OnTimedEvent;
    //count down 5 seconds
    _countSeconds = 5;

    _timer.Enabled = true;
}

private void OnTimedEvent(object sender, System.Timers.ElapsedEventArgs e)
{
    _countSeconds--;

    //Update visual representation here
    //Remember to do it on UI thread

    if (_countSeconds == 0)
    {
        _timer.Stop();
    }
}

Wednesday 30 October 2013

How to create AsyncTask in Xamarin Android?

//here is code for AsyncTask perform long running background operation in Android.  

public class BackGroundTask : AsyncTask


private ProgressDialog _progressDialog;


public BackGroundTask(Context context)
{
_context = context;  

}

protected override void OnPreExecute()
{
base.OnPreExecute();

_progressDialog = ProgressDialog.Show(_context, " Progressing", "Please wait...");
}

protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] @params)

{

}

protected override void OnPostExecute(Java.Lang.Object result)
{
base.OnPostExecute(result);  
_progressDialog.Hide();

}

How to launch Facebook app through Xamarin Android ?

here if facebook is not installed on your divice then it will redirect to web browser.

public void getFacebook(){
bool result=isAppInstalled("com.facebook.katana");
if(result)
{  
string v = FB_URL.Replace("https", "fb");  
var uri = Android.Net.Uri.Parse (v);
var intent = new Intent (Intent.ActionView, uri); 
StartActivity(intent);    
}
          else
{
StartActivity(new Intent(Intent.ActionView, Android.Net.Uri.Parse(FB_URL)));    
}
}


protected bool isAppInstalled(string packageName) 
{
Intent mIntent = PackageManager.GetLaunchIntentForPackage(packageName);
if (mIntent != null) 
{
return true;
}
else 
{
return false;
}
}