Open top menu


In this Blog explain how to insert multiple value zero Index in array.

Add a multiple values zero index in ArrayList . First you can create Item class and create a  private type variable inside class as per your need  and generate getter/setter for Example:-op[


public class MultipleValues  {

   
private  String Value_one;
   
private  String Value_two;
   
private  String Value_three;
   
private  String Value_four;
   
private  String Value_five;
   
// Create class constructor................................
   
public MultipleValues(){}
   
// Create class Parameter type constructor................................
   
public MultipleValues(String Value_one,String Value_two, String Value_three,
        StringValue_four,String Value_five){
       
this.Value_one=Value_one;
       
this.Value_two=Value_two;
       
this.Value_three=Value_three;
       
this.Value_four=Value_four;
       
this.Value_five=Value_five;
    }

   
public String getValue_one() {
       
return Value_one;
    }

   
public void setValue_one(String value_one) {
       
Value_one = value_one;
    }

   
public String getValue_two() {
       
return Value_two;
    }

   
public void setValue_two(String value_two) {
       
Value_two = value_two;
    }

   
public String getValue_three() {
       
return Value_three;
    }

   
public void setValue_three(String value_three) {
       
Value_three = value_three;
    }

   
public String getValue_four() {
       
return Value_four;
    }

   
public void setValue_four(String value_four) {
       
Value_four = value_four;
    }

   
public String getValue_five() {
       
return Value_five;
    }

   
public void setValue_five(String value_five) {
       
Value_five = value_five;
    }
}

...................................................................................................................................................................



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:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin">
    <TextView        android:id="@+id/textView2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Android Beginner point"        android:textStyle="bold"        android:textColor="#066001"        android:textSize="24dp"/>
    <Button
        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="54dp"        android:text="GetValue 1" />
    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginEnd="163dp"        android:text=""        android:textStyle="bold"        android:textColor="@color/colorPrimary"        android:textSize="24dp"/>

    <Button
        android:id="@+id/button5"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="54dp"        android:text="GetValue 5"       />
    <TextView        android:id="@+id/textView5"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginEnd="163dp"        android:text=""        android:textStyle="bold"        android:textColor="@color/colorAccent"        android:textSize="24dp"/>
</LinearLayout>
...................................................................................................................................................................



MainActivity.Java

In this class explain how to add multiple value in array and how to get particular string value from array.
...................................................................................................................................................................
public class MainActivity extends AppCompatActivity  implements View.OnClickListener{

    ArrayList<MultipleValues> arrayList;
    TextView text1,text5;
    Button button1,button5;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text1=(TextView)findViewById(R.id.textView1);
        text5=(TextView)findViewById(R.id.textView5);
        button1=(Button)findViewById(R.id.button1);
        button1.setOnClickListener(this);
        button5=(Button)findViewById(R.id.button5);
        button5.setOnClickListener(this);
        arrayList = new ArrayList<MultipleValues>();

        
//add multiple value one by one.......................       
       MultipleValues values=new MultipleValues();

        values.setValue_one("your data 1");
        values.setValue_two("your data 2");
        values.setValue_three("your data 3");
        values.setValue_four("your data 4");
        values.setValue_five("your data 5");

        arrayList.add(values);// add value 0 index        
       // or        
        arrayList.set(0,values); // replaced zero index

You can add multiple value at time.
arrayList.add(new MultipleValues("your data 1","your data 2","your data 3",
 "your data 4","your data 5")); 




  }
    @Override    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button1:
                MultipleValues valuefromarray=arrayList.get(0);
                String str1=valuefromarray.getValue_one();
                text1.setText(str1);

                break;

            case R.id.button5:
                MultipleValues valuefromarray5=arrayList.get(0);
                String str5=valuefromarray5.getValue_five();
                text5.setText(str5);
                break;
        }
    }
}

 ................................................................................................................................................................


Read more


The RecyclerView latest version of listview and improved performance and benefits. RecyclerView provide flexible view with large data set. Recycle view support features made available on newer version of the platform. For example Android lower than 5.0(API level 21)  .that relies on framework classes cannot display material design elements.


First open build.gradle and add recyclerView dependency.


dependencies {
    compile fileTree(
dir: 'libs', include: ['*.jar'])
    testCompile
'junit:junit:4.12'
   
compile 'com.android.support:appcompat-v7:23.4.0'
   
compile 'com.android.support:design:23.1.1'
   
compile 'com.android.support:recyclerview-v7:23.1.1'
}



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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical" />
</RelativeLayout>


 ......................................................................................................................................................................


Create Item Class and add the getter/setter method.

StudentClassification.java.
 ......................................................................................................................................................................
public class StudentClasification {
    private String RollNumber, StudentName, PassoutYear;

    public StudentClasification() {

    }

    public StudentClasification(String RollNumber, String StudentName, String PassoutYear) {

        this.RollNumber = RollNumber;
        this.StudentName = StudentName;
        this.PassoutYear = PassoutYear;

    }
    public String getRollNumber() {
        return RollNumber;

    }

    public void setRollNumber(String rollNumber) {
       RollNumber = rollNumber;

    }
    public String getStudentName() {
        return StudentName;

    }

    public void setStudentName(String studentName) {
        StudentName = studentName;
    }

   public String getPassoutYear() {
        return PassoutYear;
    }
    public void setPassoutYear(String passoutYear) {
        PassoutYear = passoutYear;

    }
}
 
 
 ......................................................................................................................................................................



Create new list_row.xml layout and used in recyclerview row and display Student Detail in single row.   
 ......................................................................................................................................................................

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:focusable="true"

    android:paddingLeft="16dp"

    android:paddingRight="16dp"

    android:paddingTop="10dp"

    android:paddingBottom="10dp"

    android:clickable="true"

    android:background="?android:attr/selectableItemBackground"

    android:orientation="vertical">

    <TextView

        android:id="@+id/RollNumber"

        android:textSize="16dp"

        android:textStyle="bold"

        android:layout_alignParentTop="true"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" />


    <TextView

        android:id="@+id/StudentName"

        android:layout_below="@id/RollNumber"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" />


    <TextView

        android:id="@+id/year"

        android:layout_width="wrap_content"

        android:layout_alignParentRight="true"

        android:layout_height="wrap_content" />



</RelativeLayout>


 ......................................................................................................................................................................
 
 
Create StudentAdapter.java RecyclerView Adapter.
 ......................................................................................................................................................................

public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.MyViewHolder> {

    private List<StudentClasification> StudentClasificationList;
    public class MyViewHolder extends RecyclerView.ViewHolder {

        public TextView RollNumber, year, StudentName;

        public MyViewHolder(View view) {

            super(view);

            RollNumber = (TextView) view.findViewById(R.id.RollNumber);

            StudentName = (TextView) view.findViewById(R.id.StudentName);

            year = (TextView) view.findViewById(R.id.year);

        }

    }

    public StudentAdapter(List<StudentClasification> StudentClasificationList) {

        this.StudentClasificationList = StudentClasificationList;

    }

    @Override

    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView = LayoutInflater.from(parent.getContext())

                .inflate(R.layout.list_row, parent, false);

        return new MyViewHolder(itemView);

    }

    @Override

    public void onBindViewHolder(StudentAdapter.MyViewHolder holder, int position) {

        StudentClasification values = StudentClasificationList.get(position);

        holder.RollNumber.setText(values.getRollNumber());

        holder.StudentName.setText(values.getStudentName());

        holder.year.setText(values.getPassoutYear());

    }

    @Override

    public int getItemCount() {

        return StudentClasificationList.size();

    }

}
 


 ......................................................................................................................................................................


 
create new DividerItemDecoration.java Decorate class via this class set RecyclerView Divider/ separator.
 .....................................................................................................................................................................

public class DividerItemDecoration extends RecyclerView.ItemDecoration {

    private static final int[] ATTRS = new int[]{android.R.attr.listDivider};
    private Drawable mDivider;

    public DividerItemDecoration(Context context) {

        final TypedArray styledAttributes = context.obtainStyledAttributes(ATTRS);

        mDivider = styledAttributes.getDrawable(0);

        styledAttributes.recycle();

    }

    public DividerItemDecoration(Context context, int resId) {

        mDivider = ContextCompat.getDrawable(context, resId);

    }

    @Override

    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {

        int left = parent.getPaddingLeft();

        int right = parent.getWidth() - parent.getPaddingRight();
        int childCount = parent.getChildCount();

        for (int i = 0; i < childCount; i++) {

            View child = parent.getChildAt(i);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

            int top = child.getBottom() + params.bottomMargin;

            int bottom = top + mDivider.getIntrinsicHeight();

            mDivider.setBounds(left, top, right, bottom);

            mDivider.draw(c);

        }

    }

}
 
 


 ......................................................................................................................................................................


 
MainActivity.Java.
 ......................................................................................................................................................................
 
public class MainActivity extends AppCompatActivity {

    private List<StudentClasification> StudentList = new ArrayList<>();

    private RecyclerView recyclerView;

    private StudentAdapter mAdapter;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

        StudentList.add(new StudentClasification("ROLL NO.: AB2345","STUDENT NAME : Biertny","PASS YEAR: 2000"));

        StudentList.add(new StudentClasification("ROLL NO.: AB2678","STUDENT NAME : Johan","PASS YEAR: 2005"));

        StudentList.add(new StudentClasification("ROLL NO.: AB2675","STUDENT NAME : Jaison","PASS YEAR: 2007"));

        StudentList.add(new StudentClasification("ROLL NO.: AB2976","STUDENT NAME : Watson","PASS YEAR: 2009"));

        StudentList.add(new StudentClasification("ROLL NO.: AB2080","STUDENT NAME : Parank","PASS YEAR: 2010"));

        StudentList.add(new StudentClasification("ROLL NO.: AB2670","STUDENT NAME : Jojab","PASS YEAR: 2013"));

        StudentList.add(new StudentClasification("ROLL NO.: AB2640","STUDENT NAME : Boss","PASS YEAR: 2003"));

        StudentList.add(new StudentClasification("ROLL NO.: AB0876","STUDENT NAME : Jon","PASS YEAR: 2014"));

        StudentList.add(new StudentClasification("ROLL NO.: AB2005","STUDENT NAME : Rock","PASS YEAR: 2015"));

        StudentList.add(new StudentClasification("ROLL NO.: AB2000","STUDENT NAME : Brock","PASS YEAR: 2016"));

        recyclerView.addItemDecoration(new DividerItemDecoration(this));

        mAdapter=new StudentAdapter(StudentList);

        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());

        recyclerView.setLayoutManager(mLayoutManager);

        recyclerView.setItemAnimator(new DefaultItemAnimator());

        recyclerView.setAdapter(mAdapter);

    }

}
 
 
 ......................................................................................................................................................................



  

 

Read more









RecylerView Advantages:


1.In a ListView, it was recommended to use the the viewHolder pattern but In Case RecyclerView, this is compulsory using the RecyclerView,ViewHolder class.

2. ListView, the only type of View available is the vertical ListView. There is no official way to even implement a horizontal ListView.


Now Using a RecyclerView :

(i) LinearLayoutManager- support both vertical and Horizontal lists.

(ii) StaggeredLayoutManager-support Pinterest like Staggered lists.

(iii) GridLayoutManager- Support display grids as gallery.

3. In LIstVIew ,programmatically decorating items like adding borders or dividers but in recyclerView, used RecyclerView.ItemDecorater class gives huge control to the developers.

4. the simple listview click interface for example-AdapterView.OnItemClickListener Interface. But the RecyclerView Gives Much More Power and control to its developer by RecyclerView.OnItemTouchListener .

 

Disadvantages:-

1. More complex then ListVIew.

2. Take  a lot of time for beginner to understand.

3. You will need to spend lot of time and not an easy implementation comparison to     listView.


















Read more


Why we are used Sticky Intent in android?


Sticky intents are associated with the android system for the future broadcast events. The android system used sticky broadcast for certain system information .for example the batter status is send as sticky intent and can received at any time. For example



// Register for the battery changed event

IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);

Intent batteryStatus = this.registerReceiver(null, filter);


Stick intent is also a type of intent which allows a communication between function and service sendStickyBroadcast() perform a sendBroadcast(intent) know as sticky.

A sticky broadcast may be removed at any time via a call to the  removeStickyBroadcast() method, passing through as an argument a reference to the broadcast intent to be removed.
Read more