android

Thursday, 20 October 2016

Difference between ImageView.setBackgroundResource and ImageView.setImageResource in android


In android there are two methods of ImageView to set image.


 1. ImageView.setBackgroundResource and
 2. ImageView.setsetImageResource.


                          Let's try to understand the difference between these two methods.
  The first method ImageView.setBackgroundResource is use to set the background of an     ImageView.

  The second method ImageView.setImageResource is use  to set the src image of the           ImageView. 

   for example:
          ImageView iv=(ImageView)findViewById(R.id.imageview1);
          iv.setBackgroundResource(R.drawable.yourimagename);
  
   It will fit the image for the entire background,it means that it will stretch the image to fill the       background entirely even if the image size is  small.

   Then, iv.setImageResource(R.drawable.yourimagename);
   
  It will take the size of the image in ImageView.for this,we have to set:
  
       android:layout_width="wrap_content" and
       android:layout_height="wrap_content

  If the size of the image is smaller than ImageView,then the remaining border will be left           blank and  the background will be visible.


 Note:The image must be put in res/drawable/ folder.
                                                

Saturday, 8 October 2016

How to avoid duplicate listitems from being repeating?

 Hello friends ,in this post i will show you how to avod list items from being duplicate.
  Here is the code:
 
File name: MainActivity.java

import android.app.Activity;
.
.
.
.
.
public class MainActivity extends Activity{

 Listview listview; 
 Customlist cl;
 ArrayList<Student> title=new ArrayList<Student>();
 Student s=new Student();
 @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     listview=findViewById(R.id.ListView1);
     s.setName("Android");
     title.add(s);
     also use setList method
     cl.setList(title);
  cl=new Customlist(this,title);
     listview.setAdapter(title);
}

Create custom inner class


class Customlist extends BaseAdapter {
    Context mContext;
    String data;
    ArrayList<Student> mtitle=new ArrayList<Student>();

    public Customlist(Context mContext,ArrayList<Student> mtitle){
        this.mContext=mContext;
        this.mtitle=mtitle;
    }

    public void setList(ArrayList<Student> mtitle){
        this.mtitle=mtitle;
        notifyDataSetChanged();
    }
    @Override    public int getCount() {
        return mtitle.size();
    }

    @Override    public Object getItem(int position) {
        return mtitle.get(position);
    }

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

    @Override    public View getView(final int position, View convertView, ViewGroup parent) {
        View v=convertView;
        Holder h=null;

           if(v==null){
            LayoutInflater mLayoutInflater=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v=mLayoutInflater.inflate(R.layout.listitem,null);
            h.name=(TextView)v.findViewById(R.id.tv_name);
            v.setTag(h);        
          }else{
                 h=(Holder)v.getTag();
             }
         name.setText(mtitle.get(position).getName()));
        return v;
    }
    static class Holder{
       TextView name;
     }

}

}



  Last create Student class:
  Filename :Student.java
  public class Student{
  
    String name;
   public String getName(){
     return name;
   } 
    public void setName(String name){
       this.name=name;
    }
   
    }