Search This Blog

Tuesday, 12 July 2016

How To Show Circle Progress Bar

<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/dark_grey" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:text="Raw Material Availability" android:layout_marginLeft="10dp" android:textColor="@color/white" android:textSize="20sp" android:textStyle="bold" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#2196f3" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:padding="20dp" android:orientation="horizontal"> <Button android:layout_width="100sp" android:layout_height="100sp" android:background="@drawable/mybutton" android:gravity="center" android:layout_gravity="center" android:text="Full" android:textColor="@color/white"/> </LinearLayout> </LinearLayout>

Monday, 11 July 2016

JSON Example

MainActivity.CS:-

using Android.App;
using Android.Widget;
using Android.OS;
using System.Collections.Generic;

namespace JsonServies
{
[Activity (Label = "JsonServies", MainLauncher = true, Icon = "@mipmap/icon")]
public class MainActivity : Activity
{
protected override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);

// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);

ListView lstPersons = (ListView)FindViewById (Resource.Id.lstPersons);

Services obj = new Services ();
obj.GetMovieData (this,lstPersons);
}
}
}


Main.AXML:-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:id="@+id/lstPersons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>


Movies.CS:-

using System;
using System.Collections.Generic;

namespace JsonServies
{
public class Movie
{
public Movie ()
{
}

public string name { get; set; }
public string height { get; set; }
public string mass { get; set; }
public string hair_color { get; set; }
public string skin_color { get; set; }
public string eye_color { get; set; }
public string birth_year { get; set; }
public string gender { get; set; }
public string homeworld { get; set; }
public List<string> films { get; set; }
public List<string> species { get; set; }
public List<object> vehicles { get; set; }
public List<object> starships { get; set; }
public string created { get; set; }
public string edited { get; set; }
public string url { get; set; }
}
}



Services.CS:-

using System;
using System.Net;
using Newtonsoft.Json;
using System.Collections.Generic;
using Android.Widget;
using Android.App;

namespace JsonServies
{
public class Services
{
List<Movie> objList;
public Services ()
{
}
public void GetMovieData(Activity act, ListView lstMovies){

WebClient objWebClient = new WebClient ();
objWebClient.DownloadStringCompleted += async
delegate(object sender, DownloadStringCompletedEventArgs e) {
Activity.RunOnUiThread(() => {
List<Movie> objList=JsonConvert.DeserializeObject<List<Movie>>(e.Result.ToString());
});
};
objWebClient.DownloadStringAsync (new Uri ("http://swapi.co/api/people/2/"));
}
}
}





Navigation,Send Data,Toast Example

-> Toast Message:-
Toast.MakeText(this,"Message",ToastLength.Short).Show();

-> Page Navigation:-
Intent<Obj Name>=new Intent(this,typeof(<Target Activity>));
StartActivity (objintent);

-> Pass The Data From One Page To Another Page:-
<Obj Name>.PutExtra ("<Key>",<Data>.ToString());
 StartActivity (objintent);

-> Recive Data From One Page to This Page:-
String <Obj Name> =Intent.GetStringExtra ("Key");

Sunday, 10 July 2016

SQLite Example in Xamarin

LoginAdopter.CS:-

using System;
using Android.Widget;
using System.Collections.Generic;
using Android.App;
using Android.Views;
namespace SQLiteExample
{
public class LoginAdapter:BaseAdapter
{
List<Registration> ObjRegistration;
Activity act;
public LoginAdapter (Activity act,List<Registration> ObjRegistration)
{
this.act = act;
this.ObjRegistration = ObjRegistration;
}
public override Java.Lang.Object GetItem (int position)
{
return position;
}
public override long GetItemId (int position)
{
return position;
}
public override Android.Views.View GetView (int position, Android.Views.View convertView, Android.Views.ViewGroup parent)
{
View rootView = act.LayoutInflater.Inflate (Resource.Layout.LayoutItem, parent, false);
TextView tvUsername = rootView.FindViewById<TextView> (Resource.Id.tvUsername);
TextView tvPassword= rootView.FindViewById<TextView> (Resource.Id.tvPassword);
Registration item = ObjRegistration [position];
tvUsername.Text = item.UserName;
tvPassword.Text = item.Password;
return rootView;
}
public override int Count {
get {
return ObjRegistration.Count;
}
}
}
}


App.CS:-

using System;
using SQLite;

namespace SQLiteExample
{
public class App
{
public App ()
{
}

public static SQLiteConnection conn;
}
}
Main.AXML:-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText
        android:id="@+id/etUserName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter User Name" />
    <EditText
        android:id="@+id/etPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="Enter password" />
    <Button
        android:id="@+id/btnSubmit"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Submit" />
    <Button
        android:id="@+id/login"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="login Page" />
    <ListView
        android:id="@+id/lstLoginDetails"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>


MainActivity.CS:-

using Android.App;
using Android.Widget;
using SQLite;
using System;
using Android.OS;
using Android.Content;
using System.Collections.Generic;
using System.Linq;
namespace SQLiteExample
{
[Activity (Label = "SQLiteExample", MainLauncher = true, Icon = "@mipmap/icon")]
public class MainActivity : Activity
{
protected override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);
SetContentView (Resource.Layout.Main);
CreateDatabase ();
Button btnSubmit = (Button)FindViewById (Resource.Id.btnSubmit);
Button login = (Button)FindViewById (Resource.Id.login);
login.Click += async delegate(object sender, EventArgs e) {
Intent objIntent=new Intent(this,typeof(LoginPage));
StartActivity(objIntent);
};
EditText etUserName = (EditText)FindViewById (Resource.Id.etUserName);
EditText etPassword = (EditText)FindViewById (Resource.Id.etPassword);
btnSubmit.Click += async delegate(object sender, EventArgs e) {
Registration objRegister=new Registration();
objRegister.Id=1;
objRegister.UserName=etUserName.Text;
objRegister.Password=etPassword.Text;
int i= App.conn.Insert(objRegister);
if(i==1){
Toast.MakeText(this,"Successfully Inserted",ToastLength.Long).Show();
}else{
Toast.MakeText(this,"some thing went wrong. Please reinsert once.",ToastLength.Long).Show();
}
List<Registration> results=App.conn.Table<Registration>().ToList();
ListView logindetails = (ListView)FindViewById(Resource.Id.lstLoginDetails);
LoginAdapter ObjLoginAdapter = new LoginAdapter(this,results);
logindetails.Adapter=ObjLoginAdapter;
};
}
private void CreateDatabase(){
string folder = System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal);
App.conn = new SQLiteConnection (System.IO.Path.Combine (folder, "PersonData.db"));
App.conn.CreateTable<Registration>();
}
}
}

LoginLayout.AXML:-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText
        android:id="@+id/etUserName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter User Name" />
    <EditText
        android:id="@+id/etPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="Enter password" />
    <Button
        android:id="@+id/btnLogin"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Login" />
</LinearLayout>

LoginPage.CS:-


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using SQLite;
namespace SQLiteExample
{
[Activity (Label = "LoginPage")]
public class LoginPage : Activity
{
SQLiteConnection con;
protected override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);
SetContentView (Resource.Layout.LoginLayout);
Button btnLogin = (Button)FindViewById (Resource.Id.btnLogin);
EditText etUserName = (EditText)FindViewById (Resource.Id.etUserName);
EditText etPassword = (EditText)FindViewById (Resource.Id.etPassword);
btnLogin.Click += async delegate(object sender, EventArgs e) {
bool isSuccess=false;
List<Registration> results=App.conn.Table<Registration>().ToList();
// foreach(var eachItem in results){
// if(eachItem.UserName==etUserName.Text&&eachItem.Password==etPassword.Text){
// Intent objIntent=new Intent(this,typeof(LoginPage));
// StartActivity(objIntent);
// break;
// isSuccess=true;
// }else{
// isSuccess=false;
//
// }
};
}
}
}
Registration.CS:-

using System;
using SQLite;

namespace SQLiteExample
{
public class Registration
{
public Registration ()
{
}

[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
}
}

Wednesday, 29 June 2016

ListView inMVVM

XamlPage(View)

<ContentPage.Content>
    <StackLayout Orientation="Vertical">
      <ListView x:Name="lstPersons" ItemsSource="{Binding PersonsList}" >
        <ListView.ItemTemplate>
          <DataTemplate>
            <ViewCell>
              <StackLayout Orientation="Horizontal">
                <Label Text="{Binding PersonName}" VerticalOptions="Start" HorizontalOptions="StartAndExpand"/>
                <Label Text="{Binding PersonAddress}" VerticalOptions="Start" HorizontalOptions="StartAndExpand"/>
                <Label Text="{Binding PersonId}" VerticalOptions="Start" HorizontalOptions="StartAndExpand"/>
              </StackLayout>
            </ViewCell>
          </DataTemplate>
        </ListView.ItemTemplate>
      </ListView>
    </StackLayout>
  </ContentPage.Content>

Class(Model)

class Person
    {
        public string PersonName { set; get; }
        public string PersonAddress { set; get; }
        public int PersonId { set; get; }
    }


ViewModelClass


 class HomePageViewModel
    {

        public HomePageViewModel()
        {
            bindPersonsList();
        }


       private void bindPersonsList()
        {
            _PersonsList.Clear();
            _PersonsList.Add(new Person { PersonName = "Ramesh", PersonAddress = "Kandukur", PersonId = 1 });
            _PersonsList.Add(new Person { PersonName = "Venakt", PersonAddress = "Hyderabad", PersonId = 2 });
            _PersonsList.Add(new Person { PersonName = "Chiru", PersonAddress = "Kanigiri", PersonId = 3 });
            _PersonsList.Add(new Person { PersonName = "Subbu", PersonAddress = "Chirala", PersonId = 4 });
            _PersonsList.Add(new Person { PersonName = "Ramesh", PersonAddress = "Kandukur", PersonId = 1 });
            _PersonsList.Add(new Person { PersonName = "Ramesh", PersonAddress = "Kandukur", PersonId = 1 });
            _PersonsList.Add(new Person { PersonName = "Ramesh", PersonAddress = "Kandukur", PersonId = 1 });
        }



        private ObservableCollection<Person> _PersonsList = new ObservableCollection<Person>();
        public ObservableCollection<Person> PersonsList
        {
            set
            {
                _PersonsList = value;
            }
            get
            {
                return _PersonsList;
            }
        }
    }

ListView BackGround change

We Can Insert This Was In Xaml.cs File

lstPersons.BackgroundColor = Color.Red;



other wise xaml file

we can mention x


<ListView x:Name="lstPerson" ItemsSource="{Binding PersonsList}" >
        <ListView.BackgroundColor>
          <OnPlatform x:TypeArguments="Color">
            <OnPlatform.Android>#00FF00</OnPlatform.Android>
          </OnPlatform>
        </ListView.BackgroundColor>

Tuesday, 28 June 2016

Xamarin.Forms Circle Image for iOS

(If U Want Full http://www.codeproject.com/Tips/832752/Xamarin-Forms-Circle-Image-for-iOS

Using the Code

Just put the class in the shared project, and the renderer in the iOS project.

The Renderer

using System;
using Xamarin.Forms;
using MonoTouch.UIKit;
using Xamarin.Forms.Platform.iOS;
using System.Drawing;
using RBVRenderer;
using RBVRenderer.iOS;

[assembly: ExportRendererAttribute(typeof(CircleImage), typeof(CircleImageRenderer))]
namespace RBVRenderer.iOS
{

    public class CircleImageRenderer : ViewRenderer<CircleImage,UIView>
    {
        UIImageView imageView;
        protected override void OnElementChanged(ElementChangedEventArgs<CircleImage> e)
        {
            base.OnElementChanged(e);

            var rbv = e.NewElement;
            if (rbv != null) {

                var mainView = new UIView();

                imageView = new UIImageView (UIImage.FromBundle(rbv.FileSource));
                imageView.Frame = new RectangleF 
                (0, 0, (float)rbv.WidthRequest, (float)rbv.HeightRequest);
                imageView.Layer.CornerRadius = imageView.Frame.Size.Width / 2;
                if (rbv.HasBorder) {
                    imageView.Layer.BorderColor = rbv.BorderColor.ToCGColor();
                    imageView.Layer.BorderWidth = 2;
                }
                imageView.ClipsToBounds = true;

                mainView.Add (imageView);

                SetNativeControl(mainView);
            }
        }

        protected override void OnElementPropertyChanged
        (object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            if (e.PropertyName == CircleImage.HasBorderProperty.PropertyName) {
                if (Element.HasBorder) {
                    imageView.Layer.BorderWidth = 2;
                    imageView.Layer.BorderColor = this.Element.BorderColor.ToCGColor ();
                } else {
                    imageView.Layer.BorderWidth = 0;
                }
            }
        }
    }
}

The Class

using System;
using Xamarin.Forms;

namespace RBVRenderer
{
    public class CircleImage : Image
    {
        public static readonly BindableProperty FileSourceProperty =
            BindableProperty.Create<CircleImage, string>(p => p.FileSource, "");

        /// <summary>
        /// string filename
        /// </summary>
        /// <value>The source.</value>
        public string FileSource
        {
            get { return (string)base.GetValue(FileSourceProperty);}
            set { base.SetValue(FileSourceProperty, value);}
        }

        public static readonly BindableProperty BorderColorProperty =
            BindableProperty.Create<CircleImage, Color>(p => p.BorderColor, Color.White);

        public Color BorderColor
        {
            get { return (Color)base.GetValue(BorderColorProperty);}
            set { base.SetValue(BorderColorProperty, value);}
        }

        public static readonly BindableProperty HasBorderProperty =
            BindableProperty.Create<CircleImage, bool>(p => p.HasBorder, false);

        public bool HasBorder
        {
            get { return (bool)base.GetValue(HasBorderProperty);}
            set { base.SetValue(HasBorderProperty, value);}
        }
    }
}

Usage

<?xml version="1.0" encoding="UTF-8" ?>
<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:me="clr-namespace:RBVRenderer;assembly=RBVRenderer"
    x:Class="RBVRenderer.MainPage">
    <ContentPage.Content>
        <StackLayout Padding="20" 
        Spacing="20" BackgroundColor="Blue">

            <me:CircleImage  
                    x:Name="rbi" 
                    WidthRequest="50" 
                    HeightRequest="50"  
                    BorderColor="White"
                    FileSource="cyansquare.png">
            </me:CircleImage>

            <Label Text="Has Border" />
            <Switch BindingContext="{x:Reference rbi}"
                IsToggled="{Binding HasBorder, Mode=OneWayToSource}" />

        </StackLayout>
    </ContentPage.Content>
</ContentPage>