Monday, 19 June 2017

React Native Application run command

Make sure in your system node install than after do below process:

npm install -g react-native-cli


react-native init {project name}

cd {project name}

react-native run-android

Saturday, 17 June 2017

Install ATOM and run project in ionic through command line

npm install -g gulp
npm install -g cordova
npm install -g  ionic

Atom install

    sudo dpkg -i atom-amd64.deb
    sudo apt-get -f install


cd path of project

For create project run below command

ionic start {Project name} {template type}

for template type refer this link https://ionicframework.com/docs//intro/tutorial/

path of project : ionic serve OR npm run ionic:serve
path of project : ionic cordova run android

IF error occurs for gradle than fire below command in project path
path of project : sudo apt-get install gradle


If any config file change than run below command

cordova platform rm android

after it run below command

path of project : ionic cordova run android

 if fire this command ionic serve and get below error             
Error: Node Sass does not yet support your current environment: Linux 64-bit with Unsupported runtime (57)
For more information on which environments are supported please see:
https://github.com/sass/node-sass/releases/tag/v4.5.0

than remove node_modules folder from project and fire command like npm install

Monday, 22 May 2017

git command for branch merge and checkout ubuntu android

When child branch i m working
➜  demobranch git:(promotion) ✗ git add --all
➜  demobranch git:(promotion) ✗ git commit -m 'exxpan dable view with recyckview'
➜  demobranch git:(promotion) git pull origin promotion
➜  demobranch git:(promotion) git push origin promotion

if idea file error occur than
➜  demobranch git:(promotion) ✗ git add --all                                  
➜  demobranch git:(promotion) ✗ git commit -m 'idea file push'                
➜  demobranch git:(promotion) git pull origin promotion  
➜  demobranch git:(promotion) git push origin promotion

Checkout promotion to master
➜  demobranch git:(promotion) git checkout master
➜  demobranch git:(master) git pull origin master
➜  demobranch git:(master) git merge promotion
➜  demobranch git:(master) git push origin master

Thursday, 1 December 2016

Check programitically device wheather it is phone or tablet in android

public  boolean isTabletDevice(FragmentActivity activityContext) {
        // Verifies if the Generalized Size of the device is XLARGE to be
        // considered a Tablet
        boolean large = ((activityContext.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
        boolean xlarge= ((activityContext.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE);
        // If XLarge, checks if the Generalized Density is at least MDPI
        // (160dpi)
        if (large) {
            DisplayMetrics metrics = new DisplayMetrics();
            Activity activity = (Activity) activityContext;
            activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

            // MDPI=160, DEFAULT=160, DENSITY_HIGH=240, DENSITY_MEDIUM=160,
            // DENSITY_TV=213, DENSITY_XHIGH=320
            if (metrics.densityDpi == DisplayMetrics.DENSITY_DEFAULT
                    || metrics.densityDpi == DisplayMetrics.DENSITY_HIGH
                    || metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM
                    || metrics.densityDpi == DisplayMetrics.DENSITY_TV
                    || metrics.densityDpi == DisplayMetrics.DENSITY_XHIGH) {
                System.out.println("7 Inch Tab");
                // Yes, this is a tablet!
                return true;
            }
        }
        else if (xlarge) {
               DisplayMetrics metrics = new DisplayMetrics();
                Activity activity = (Activity) activityContext;
                activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
            if (metrics.densityDpi == DisplayMetrics.DENSITY_DEFAULT
                    || metrics.densityDpi == DisplayMetrics.DENSITY_HIGH
                    || metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM
                    || metrics.densityDpi == DisplayMetrics.DENSITY_TV
                    || metrics.densityDpi == DisplayMetrics.DENSITY_XHIGH) {
                System.out.println("10 Inch Tab");
                // Yes, this is a tablet!
                return true;
            }
        }

        // No, this is not a tablet!
        return false;
    }

Tuesday, 8 November 2016

Recycleview demo with List and Grid



activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     tools:context="com.srtpl.recycleandgriddemo.MainActivity">

    <Button
        android:id="@+id/bt_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="LIST"/>
    <Button
        android:id="@+id/bt_grid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/bt_list"
        android:text="GRID"/>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycle"
        android:layout_below="@+id/bt_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycle_grid"
        android:layout_below="@+id/bt_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"
        />
</RelativeLayout>

Employee.java

package com.srtpl.recycleandgriddemo;

 public class Employee {
    String name;
    String position;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }
}



import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

EmployeeRowHolder.java
 
public class EmployeeRowHolder extends RecyclerView.ViewHolder {


    public TextView mTextViewOne;
    public TextView mTextViewTwo;


    public EmployeeRowHolder(View convertView) {
        super(convertView);

        this.mTextViewOne = (TextView) convertView.findViewById(R.id.txt_1);
        this.mTextViewTwo = (TextView) convertView.findViewById(R.id.txt_2);

    }

}


MainActivity.java
 

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    RecyclerView mRecyclerView;
    RecyclerView mRecyclerViewGrid;

    ArrayList<Employee> mArrayListEmployees;
    EmployeeRecyclerAdapter mEmployeeRecyclerAdapter;
    Button mButtonList;
    Button mButtonGrid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRecyclerView = (RecyclerView) findViewById(R.id.recycle);
        mRecyclerViewGrid = (RecyclerView) findViewById(R.id.recycle_grid);
        mButtonList = (Button) findViewById(R.id.bt_list);
        mButtonGrid = (Button) findViewById(R.id.bt_grid);

        mArrayListEmployees = new ArrayList<>();

        Employee mEmployee = new Employee();
        mEmployee.setName("EMp 1");
        mEmployee.setPosition("Team Leader");
        mArrayListEmployees.add(mEmployee);

        Employee mEmployee1 = new Employee();
        mEmployee1.setName("EMp 2");
        mEmployee1.setPosition("Developer");
        mArrayListEmployees.add(mEmployee1);

        Employee mEmployee2 = new Employee();
        mEmployee2.setName("EMp 3");
        mEmployee2.setPosition("Tester");
        mArrayListEmployees.add(mEmployee2);

        Employee mEmployee3 = new Employee();
        mEmployee3.setName("EMp 4");
        mEmployee3.setPosition("Support");
        mArrayListEmployees.add(mEmployee3);

        Employee mEmployee4 = new Employee();
        mEmployee4.setName("EMp 5");
        mEmployee4.setPosition("Designer");
        mArrayListEmployees.add(mEmployee4);

        Employee mEmployee5 = new Employee();
        mEmployee5.setName("EMp 6");
        mEmployee5.setPosition("BDM");
        mArrayListEmployees.add(mEmployee5);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerViewGrid.setLayoutManager(new GridLayoutManager(this, 2));

        mEmployeeRecyclerAdapter = new EmployeeRecyclerAdapter(MainActivity.this, mArrayListEmployees);
        mRecyclerView.setAdapter(mEmployeeRecyclerAdapter);
        mRecyclerViewGrid.setAdapter(mEmployeeRecyclerAdapter);
        mButtonList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRecyclerView.setVisibility(View.VISIBLE);
                mRecyclerViewGrid.setVisibility(View.GONE);
            }
        });
        mButtonGrid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRecyclerView.setVisibility(View.GONE);
                mRecyclerViewGrid.setVisibility(View.VISIBLE);
            }
        });
    }

    public class EmployeeRecyclerAdapter extends RecyclerView.Adapter<EmployeeRowHolder> {


        private List<Employee> list;

        private Context mContext;

        public EmployeeRecyclerAdapter(Context context, List<Employee> feedItemList) {
            this.list = feedItemList;
            this.mContext = context;

        }


        @Override
        public EmployeeRowHolder onCreateViewHolder(ViewGroup viewGroup, final int i) {
            View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_item, null);
            EmployeeRowHolder mh = new EmployeeRowHolder(v);

            return mh;
        }

        @Override
        public void onBindViewHolder(final EmployeeRowHolder queryListRowHolder, final int i) {
            queryListRowHolder.mTextViewOne.setText(list.get(i).getName());
            queryListRowHolder.mTextViewTwo.setText(list.get(i).getPosition());

        }


        @Override
        public int getItemCount() {
            return (null != list ? list.size() : 0);
        }
    }

}

Saturday, 2 July 2016

Progress dialog visibility in single asynctask android

ProgressDialog mProgressDialog;
public class getData extends AsyncTask<Void,Void,Void>
    {

        private boolean showLoading;

        public getDocumnet(boolean showLoading) {
            this.showLoading = showLoading;
        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            if (showLoading==true)
            {
                   mProgressDialog=ProgressDialog.show(MainActivity.this,"",getString(R.string.loading));
            }

        }

        @Override
        protected Void doInBackground(Void... voids) {

            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);

            if (showLoading==true)
            {
                if (mProgressDialog != null) {
                    mProgressDialog.dismiss();
                }
            }

            
        }
    }

Call this asynctask like below

new getData(true).execute(); // if you want to show progress dialog

new getData(false).execute(); // if you want to hide progress dialog

Saturday, 25 June 2016

Realm Integration in Android

RLMApplication.java

public class RLMApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this)
                .name(Realm.DEFAULT_REALM_NAME)
                .schemaVersion(0)
                .deleteRealmIfMigrationNeeded()
                .build();
        Realm.setDefaultConfiguration(realmConfiguration);
    }
}

Contacts.java

public class Contacts extends RealmObject {

    private String name;
    private String email;
    private String address;
    private int age;
    private long id;

    private RealmList<Friends> friends;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public RealmList<Friends> getFriends() {
        return friends;
    }

    public void setFriends(RealmList<Friends> friends) {
        this.friends = friends;
    }
}


Friends.java

public class Friends extends RealmObject {

    private long fid;
    private String name;
    private String id;

    public long getFid() {
        return fid;
    }

    public void setFid(long fid) {
        this.fid = fid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {


    TextView mTextViewAdd;
    ListView mListView;
    Realm mRealm;
    CustomAdapter mCustomAdapter;
    LayoutInflater mLayoutInflater;
    RealmResults<Contacts> mContactses;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRealm = Realm.getDefaultInstance();
        // mContactses = new RealmResults<Contacts>();
        mTextViewAdd = (TextView) findViewById(R.id.txt_add);
        mListView = (ListView) findViewById(R.id.list);
        mLayoutInflater = getLayoutInflater();
        setRealmData();
        mTextViewAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View content = mLayoutInflater.inflate(R.layout.edit_item, null);
                final EditText editTitle = (EditText) content.findViewById(R.id.title);

                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setView(content)
                        .setTitle("Add Contact")
                        .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                mRealm.beginTransaction();
                                Contacts contact = mRealm.createObject(Contacts.class);
                                contact.setId(System.currentTimeMillis());
                                contact.setName(editTitle.getText().toString());
                                contact.setEmail("Contact@hostname.com");
                                contact.setAddress("Contact's Address");
                                contact.setAge(20);
                                mRealm.commitTransaction();

                                mCustomAdapter.notifyDataSetChanged();

                            }
                        })
                        .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });

    }

    private void setRealmData() {

        RealmQuery<Contacts> query = mRealm.where(Contacts.class);
        mContactses = query.findAll();
        mCustomAdapter = new CustomAdapter(MainActivity.this, mContactses, true);
        mListView.setAdapter(mCustomAdapter);

    }

    public class CustomAdapter extends RealmBaseAdapter<Contacts> {

        Context mActivity;
        RealmResults<Contacts> list;

        public CustomAdapter(Context context, RealmResults<Contacts> realmResults, boolean automaticUpdate) {
            super(context, realmResults, automaticUpdate);
            this.list = realmResults;
            this.mActivity = context;

        }

        @Override
        public int getCount() {
            return list.size();
        }


        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(final int i, View convertView, ViewGroup viewGroup) {
            LayoutInflater mInflater = (LayoutInflater)
                    mActivity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.row_item, null);
            TextView mTextViewName = (TextView) convertView.findViewById(R.id.txt_name);
            Button mButtonEdit = (Button) convertView.findViewById(R.id.bt_edt);
            Button mButtonDelete = (Button) convertView.findViewById(R.id.bt_del);
            Button mButtonView= (Button) convertView.findViewById(R.id.bt_view);

            mTextViewName.setText(list.get(i).getName());
            mButtonView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    long cid=mContactses.get(i).getId();
                    Intent mIntent=new Intent(MainActivity.this,FriendActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    Bundle mBundle=new Bundle();
                    mBundle.putString("id",String.valueOf(cid));
                    mIntent.putExtras(mBundle);
                    startActivity(mIntent);
                }
            });
            mButtonEdit.setOnClickListener(new View.OnClickListener() {
                @Override

                public void onClick(View v) {
                    View content = mLayoutInflater.inflate(R.layout.edit_item, null);
                    final EditText editTitle = (EditText) content.findViewById(R.id.title);
                    editTitle.setText(list.get(i).getName());
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setView(content)
                            .setTitle("Add Contact")
                            .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                    RealmResults<Contacts> results = mRealm.where(Contacts.class).findAll();

                                    mRealm.beginTransaction();
                                    results.get(i).setName(editTitle.getText().toString());

                                    mRealm.commitTransaction();

                                    notifyDataSetChanged();

                                }
                            })
                            .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }
            });
            mButtonDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mRealm.beginTransaction();
                    list.remove(i);
                    mRealm.commitTransaction();
                    mCustomAdapter.notifyDataSetChanged();
                }
            });
            return convertView;
        }
    }
}

FriendActivity.java

public class FriendActivity extends AppCompatActivity {

    TextView mTextViewAdd;
    ListView mListView;
    Realm mRealm;
    CustomAdapter mCustomAdapter;
    LayoutInflater mLayoutInflater;
    RealmResults<Friends> mContactses;
    Bundle mBundle;
    long cid;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRealm = Realm.getDefaultInstance();
        mBundle = getIntent().getExtras();
        cid = Long.parseLong(mBundle.getString("id"));
        mTextViewAdd = (TextView) findViewById(R.id.txt_add);
        mListView = (ListView) findViewById(R.id.list);
        mLayoutInflater = getLayoutInflater();
        setRealmData();
        mTextViewAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View content = mLayoutInflater.inflate(R.layout.edit_item, null);
                final EditText editTitle = (EditText) content.findViewById(R.id.title);

                AlertDialog.Builder builder = new AlertDialog.Builder(FriendActivity.this);
                builder.setView(content)
                        .setTitle("Add Friend")
                        .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                mRealm.beginTransaction();
                                Friends contact = mRealm.createObject(Friends.class);
                                contact.setFid(System.currentTimeMillis());
                                contact.setId(String.valueOf(cid));
                                contact.setName(editTitle.getText().toString());
                                mRealm.commitTransaction();

                                mCustomAdapter.notifyDataSetChanged();

                            }
                        })
                        .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });

    }

    private void setRealmData() {

        RealmQuery<Friends> query = mRealm.where(Friends.class);
        query.contains("id", String.valueOf(cid));
        mContactses = query.findAll();
        mCustomAdapter = new CustomAdapter(FriendActivity.this, mContactses, true);
        mListView.setAdapter(mCustomAdapter);

    }

    public class CustomAdapter extends RealmBaseAdapter<Friends> {

        Context mActivity;
        RealmResults<Friends> list;
    

        public CustomAdapter(Context context, RealmResults<Friends> realmResults, boolean automaticUpdate) {
            super(context, realmResults, automaticUpdate);
            this.list = realmResults;
            this.mActivity = context;

        }

        @Override
        public int getCount() {
            return list.size();
        }


        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(final int i, View convertView, ViewGroup viewGroup) {
            LayoutInflater mInflater = (LayoutInflater)
                    mActivity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.row_item, null);
            TextView mTextViewName = (TextView) convertView.findViewById(R.id.txt_name);
            Button mButtonEdit = (Button) convertView.findViewById(R.id.bt_edt);
            Button mButtonDelete = (Button) convertView.findViewById(R.id.bt_del);
            mTextViewName.setText(list.get(i).getName());
            mButtonEdit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    View content = mLayoutInflater.inflate(R.layout.edit_item, null);
                    final EditText editTitle = (EditText) content.findViewById(R.id.title);
                    editTitle.setText(list.get(i).getName());
                    AlertDialog.Builder builder = new AlertDialog.Builder(FriendActivity.this);
                    builder.setView(content)
                            .setTitle("Add Friend")
                            .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                    RealmResults<Friends> results = mRealm.where(Friends.class).findAll();

                                    mRealm.beginTransaction();
                                    results.get(i).setName(editTitle.getText().toString());

                                    mRealm.commitTransaction();

                                    notifyDataSetChanged();

                                }
                            })
                            .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }
            });
            mButtonDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mRealm.beginTransaction();
                    list.remove(i);
                    mRealm.commitTransaction();
                    mCustomAdapter.notifyDataSetChanged();
                }
            });
            return convertView;
        }
    }

}