Skip to main content

Posts

Use a content provider

Its like bridge/proxy between java db and our/other app.   if content provider is there for a db then its really easy to access data from the db without coding directly for the db. Content resolver is provides the info for Content provider for where to access the info. A URI is needed to connect between the two.   Initial steps are to create three classes: Contract, Database and Provider Database class handles the actual db i.e CRUD operations Contract class contains all the info that is required by the Content Provider. Each content provider shold have a separate contract class.   Contract class explanation so its contains the interface containg column names of the table which are supposed to be accessed. then the name of Content authority for the Content provider (e.g org.example.myapp.provider) then the URI is to built which starts as “content://+CONTENT_AUTHORITY” as the base URI then the path i.e the table name then we have array of strings contain...

Making asynctasks rotation proof

package com.mango.rotationproof; import android.app.Activity; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by Avinash on 21-05-2015. */ public class TaskFrag extends Fragment { MyTask myTask; Activity activity; public TaskFrag() { } public void begintask() { myTask=new MyTask(activity); myTask.execute(); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); setRetainInstance(true); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public void onAttach(Activity activity) { super.onAttach(activity); this.activity=activity; if(myTask!=null) { myTask.onA...

java sample code

package in.mangoishapps.myapplication; import android.content.Context; import android.support.v7.widget.CardView; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; /** * Created by Avinash on 02-05-2015. */ public class RVAdapter extends RecyclerView.Adapter { LayoutInflater inflater; Context context; ArrayList chizes=new ArrayList (); RVAdapter(Context context) { this.context=context; inflater=LayoutInflater.from(context); } public void setData(ArrayList chizes) { this.chizes=chizes; } @Override public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { View view=inflater.inflate(R.layout.singlerow_linear,viewGroup,false); ViewHolder holder=new ViewHolder(view); return hold...

Navigation drawer, material design theme

Aim: To make an android app with navigation drawer, material design theme Feat: Toolbar is used instead of the ActionBar, When the user runs the app for the time , he/she can see the drawer opened on the launch of the app, also rotation doesn't effect the drawer (whose layout is a fragment). Used: Fragments in both Code:   MainActivity.java ______________________________________________________________________________________________________ DrawerList.Java______________________________________________________________________________________________ package cts.fastester; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.widget.Toolbar; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGr...

Database create, read, update, delete in android

To create a db , first create a schema create a class that extends SQLiteOpenHelper create an object of SQLdatabse and initialise using getwritabledatabase create sepatrate methods for CRUD operation Schema class package com.example.avinash.sql2; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import java.sql.SQLException; /** * Created by Avinash on 01-01-2015. */ public class DBAdapter {     DBHelper helper;     public DBAdapter(Context c) {         helper = new DBHelper(c);     }     public long insertdata(String u,String p)     {   SQLiteDatabase sqldb=helper.getWritableDatabase();         ContentValues cv=new ContentValues();         c...