android

Wednesday, 19 April 2017

How to add a footer view ,Header view and main content area in Android Layout ?

In this article we are going to see how you can create a simple Android Layout that includes a header part, a footer part and the content area. 


1. Create a new Android Project



We have to specify the Application Name, the Company Name and the Package name in the appropriate text fields and then click Next.





In this step,specify different factors our app will run,in our case we will use Phone and Tablets.




In this step select Empty activity and press Next.











In this step specify name of the activity class and layout file and click on finish.



2.Change the layout of the Main Activity

 Open app/res/layout/activity_main.xml file : and paste the following code
   filename :activity_main.xml





The idea is very simple first,we have put relative layout as a root layout ,
then as a header view we have put linear layout with attribute android:layout_alignParentTop=true 
and with id android:id=@+id/header.

Then for main content area we have put another linear layout with sttribute 
  android:layout_below=@+id/header and with attribute android:layout_above=@+id/footer.
You can also use Scrollview instead of linear layout for more flexible scrolling content.

For footer view we have put another linear layout with attribute 
android:layout_alignParentBottom="true" with id as 
android:id=@+id/footer

3. Code the Main Activity

         For this example we don’t have to change anything to the auto generated code so we can leave         it as it is.

4. Run the application

        This is how our Layout looks on the emulator:


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;
    }
   
    }