Sunday, 16 September 2018

Android Activity Life Cycle Example

Create New project ActivityLifeCycle with empty activity.

// activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main" 
   android:layout_width="match_parent"
    android:layout_height="match_parent" 
   android:orientation="vertical" 
   tools:context="com.krishanandroid.activitylifecycle.MainActivity">

    <TextView       
 android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <Button      
  android:layout_width="match_parent"   
     android:layout_height="wrap_content"  
      android:text="Open Second Screen"  
      android:id="@+id/button"  
      android:onClick="openSecondScreen"/>
</LinearLayout>

// MainActivty.Java
package com.krishanandroid.activitylifecycle;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toast.makeText(MainActivity.this,"1. onCreate",Toast.LENGTH_LONG).show();
    }

    @Override    protected void onStart() {
        super.onStart();
        Toast.makeText(MainActivity.this,"2. onStart",Toast.LENGTH_LONG).show();
    }

    @Override    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        Toast.makeText(MainActivity.this,"3. onRestore",Toast.LENGTH_LONG).show();
    }

    @Override    protected void onResume() {
        super.onResume();
        Toast.makeText(MainActivity.this,"4. onResume",Toast.LENGTH_LONG).show();
    }

    @Override    protected void onPause() {
        super.onPause();
        Toast.makeText(MainActivity.this,"5. onPause",Toast.LENGTH_LONG).show();
    }

    @Override    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Toast.makeText(MainActivity.this,"6. onSave",Toast.LENGTH_LONG).show();
    }

    @Override    protected void onStop() {
        super.onStop();
        Toast.makeText(MainActivity.this,"7. onStop",Toast.LENGTH_LONG).show();
    }

    @Override    protected void onRestart() {
        super.onRestart();
        Toast.makeText(MainActivity.this,"8. onRestart",Toast.LENGTH_LONG).show();
    }

    @Override    protected void onDestroy() {
        super.onDestroy();
        Toast.makeText(MainActivity.this,"9. onDestroy",Toast.LENGTH_LONG).show();
    }

    // On button click implementation    public void openSecondScreen(View view) {

        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
        startActivity(intent);
    }
}

// activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
   xmlns:tools="http://schemas.android.com/tools" 
   android:id="@+id/activity_second" 
   android:layout_width="match_parent" 
   android:layout_height="match_parent" 
   android:orientation="vertical"  
  tools:context="com.krishanandroid.activitylifecycle.SecondActivity">

    <TextView     
   android:layout_width="match_parent"   
     android:layout_height="wrap_content" 
       android:text="Welcome to Second Screen"  
      android:textColor="@color/colorAccent"   
     android:textSize="24dp"/>

</LinearLayout>

// SecondActivity.java
package com.krishanandroid.activitylifecycle;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class SecondActivity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
    }
}
Live Demo: https://www.youtube.com/watch?v=PYnBSDMsO2c&t=121s


Saturday, 15 September 2018

RecyclerView Custom Adapter Example

Create New project RecyclerView Custom Adapter with empty activity

Add dependencies in build.gradle as below
dependencies {
    implementation 'com.android.support:recyclerview-v7:26.0.0'
}

// activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"    
xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"   
 android:layout_height="match_parent"   
 android:orientation="vertical"  
  tools:context="com.krishanandroid.recyclerviewcustomadapter.MainActivity">

    <EditText  android:layout_width="match_parent"  
      android:layout_height="wrap_content" 
       android:id="@+id/et_eno" 
       android:hint="enter eno" 
       android:inputType="number"
        android:textSize="16sp"   
     android:gravity="center_horizontal"/>

    <EditText    
    android:layout_width="match_parent"  
      android:layout_height="wrap_content" 
       android:id="@+id/et_name"  
      android:hint="enter name"   
     android:inputType="text"  
      android:textSize="16sp"  
      android:gravity="center_horizontal"/>

    <EditText   
     android:layout_width="match_parent"  
      android:layout_height="wrap_content" 
       android:id="@+id/et_salary"  
      android:hint="enter sal"
        android:inputType="number" 
       android:textSize="16sp"   
     android:gravity="center_horizontal"/>

    <Button     
   android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button_add"  
      android:text="Add"  
      android:textSize="16sp"/>

    <android.support.v7.widget.RecyclerView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"  
      android:id="@+id/recyclerView1"    
    android:padding="4dp">

    </android.support.v7.widget.RecyclerView>

</LinearLayout>

// MainActivity.java
package com.krishanandroid.recyclerviewcustomadapter;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;

import static android.support.v7.widget.LinearLayoutManager.VERTICAL;

public class MainActivity extends AppCompatActivity {

    EditText                mEno, mESalary, mEName;
    Button                  mButtonAdd;

    RecyclerView            mRecyclerView;

    ArrayList<RowItems>     arrayList;
    LinearLayoutManager     linearLayoutManager;
    RecyclerViewAdapter     myAdapter;

    public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{

        @Override        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            View view = getLayoutInflater().inflate(R.layout.row_items, parent,false);

            ViewHolder viewHolder = new ViewHolder(view);
            return viewHolder;
        }

        @Override        public void onBindViewHolder(ViewHolder holder, int position) {

            RowItems items = arrayList.get(position);
            holder.tv1.setText(""+items.getEno());
            holder.tv2.setText(items.getEname());
            holder.tv3.setText(""+items.getEsalary());
        }

        @Override        public int getItemCount() {
            return arrayList.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            TextView tv1, tv2, tv3;
            public ViewHolder(View itemView) {
                super(itemView);
                tv1 = itemView.findViewById(R.id.tv1);
                tv2 = itemView.findViewById(R.id.tv2);
                tv3 = itemView.findViewById(R.id.tv3);
            }
        }
    }

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mEno = findViewById(R.id.et_eno);
        mESalary = findViewById(R.id.et_name);
        mEName = findViewById(R.id.et_salary);
        mButtonAdd = findViewById(R.id.button_add);

        mRecyclerView = findViewById(R.id.recyclerView1);

        arrayList = new ArrayList<RowItems>();
        myAdapter = new RecyclerViewAdapter();

        linearLayoutManager = new LinearLayoutManager(MainActivity.this, VERTICAL, false);

        mRecyclerView.setAdapter(myAdapter);

        mRecyclerView.setLayoutManager(linearLayoutManager);

        mButtonAdd.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                String no = mEno.getText().toString();
                String salary = mESalary.getText().toString();
                String name = mEName.getText().toString();

                RowItems rowItems = new RowItems(no, salary, name);

                arrayList.add(rowItems);
                myAdapter.notifyDataSetChanged();
                mEno.setText(" ");
                mESalary.setText(" ");
                mEName.setText(" ");
                mEno.requestFocus();
            }
        });
    }
}

// row_items.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView    
    android:layout_width="wrap_content"  
      android:layout_height="wrap_content" 
       android:text="eno"    
    android:id="@+id/tv1"   
     android:textSize="16sp" 
       android:padding="4dp"/>

    <TextView   
     android:layout_width="wrap_content"  
      android:layout_height="wrap_content" 
       android:text="ename"   
     android:id="@+id/tv2"   
     android:layout_marginLeft="12dp" 
       android:textSize="16sp" 
       android:padding="4dp"/>

    <TextView    
    android:layout_width="wrap_content"   
     android:layout_height="wrap_content"
        android:text="esalary"    
    android:layout_marginLeft="12dp"
        android:id="@+id/tv3" 
       android:textSize="16sp" 
       android:padding="4dp"/>
</LinearLayout>

// RowItems.java
package com.krishanandroid.recyclerviewcustomadapter;

/** * Created by user on 9/13/2018. */// Bean classpublic class RowItems {
    String eno, ename, esalary;

    // constructors    public RowItems(String eno, String ename, String esalary) {
        this.eno = eno;
        this.ename = ename;
        this.esalary = esalary;
    }

    public String getEno() {
        return eno;
    }

    public void setEno(String eno) {
        this.eno = eno;
    }

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public String getEsalary() {
        return esalary;
    }

    public void setEsalary(String esalary) {
        this.esalary = esalary;
    }
}
Live Demo: https://www.youtube.com/watch?v=K9jVvwQ1MPs




Thursday, 13 September 2018

ListView Custom Adapter Example

// activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.krishanandroid.listviewcustomadapter.MainActivity">

    <EditText  android:id="@+id/et_actor"
        android:layout_width="match_parent" 
       android:layout_height="wrap_content"
        android:hint="@string/enter_actor_name"
        android:textSize="16sp"
        android:gravity="center"
        android:textColor="@color/colorAccent"
        android:inputType="text"/>

    <EditText android:id="@+id/et_actress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/enter_actress_name"
        android:gravity="center"
        android:textSize="16sp"
        android:textColor="@color/colorAccent"
        android:inputType="text"/>

    <EditText  android:id="@+id/et_movie"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/enter_movie_name"
        android:gravity="center"
        android:textSize="16sp"
        android:textColor="@color/colorAccent"
        android:inputType="text"/>

    <Button android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Add"
        android:id="@+id/button_add"
        android:textSize="16sp"
        android:layout_marginBottom="12dp"/>

    <ListView  android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="4dp"
        android:id="@+id/listview"></ListView>

</LinearLayout>




// Data.java
package com.krishanandroid.listviewcustomadapter;

/** * Created by user on 9/13/2018. */
public class Data {
    private String actor;
    private String actress;
    private String movie;

    // Beans class    public Data(String actor, String actress, String movie) {
        this.actor = actor;
        this.actress = actress;
        this.movie = movie;
    }

    public String getActor() {
        return actor;
    }

    public void setActor(String actor) {
        this.actor = actor;
    }

    public String getActress() {
        return actress;
    }

    public void setActress(String actress) {
        this.actress = actress;
    }

    public String getMovie() {
        return movie;
    }

    public void setMovie(String movie) {
        this.movie = movie;
    }
}
// MainActivity.java
package com.krishanandroid.listviewcustomadapter;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    EditText            mActor, mActress, mMovies;
    Button              mButton;
    ListView            mList;

    ArrayList<Data>     arrayList;
    MyAdapter           myAdapter;

    // inner class    class MyAdapter extends BaseAdapter{

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

        @Override        public Object getItem(int position) {
            return null;
        }

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

        @Override        public View getView(int position, View convertView, ViewGroup parent) {
            Data data = arrayList.get(position);

            View view = getLayoutInflater().inflate(R.layout.row_items,null);
            TextView tv1 = view.findViewById(R.id.t_actor);
            TextView tv2 = view.findViewById(R.id.t_actress);
            TextView tv3 = view.findViewById(R.id.t_movies);

            tv1.setText(data.getActor());
            tv2.setText(data.getActress());
            tv3.setText(data.getMovie());

            return view;
        }
    }

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // initialization        mActor = findViewById(R.id. et_actor);
        mActress = findViewById(R.id. et_actress);
        mMovies = findViewById(R.id. et_movie);
        mButton = findViewById(R.id. button_add);
        mList = findViewById(R.id. listview);

        arrayList = new ArrayList<Data>();
        myAdapter = new MyAdapter();
        mList.setAdapter(myAdapter);

        mButton.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                String actor_name = mActor.getText().toString();
                String actress_name = mActress.getText().toString();
                String movie_name = mMovies.getText().toString();

                Data data = new Data(actor_name, actress_name, movie_name);

                arrayList.add(data);
                myAdapter.notifyDataSetChanged(); // adapter changes
                mActor.setText(" "); // set empty after data added to list view                mActress.setText(" ");
                mMovies.setText(" ");
                mActor.requestFocus(); // cursor focus            }
        });
    }
}

Live Demo: https://www.youtube.com/watch?v=Ya5-eWg9SHI