How to use Dynamic Image Switcher Creation in Android

Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button.
Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <Gallery
        android:id="@+id/gallery1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" />
 

</RelativeLayout>
Step 3 : Open src -> package -> MainActivity.java and add following code :
package balaji.imageswitcher_dynamic;
 
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.ViewSwitcher.ViewFactory;
 
public class MainActivity extends Activity implements ViewFactory{
 
 RelativeLayout rl;
 Gallery gal;
 ImageSwitcher img;
 RelativeLayout.LayoutParams params;
 
 int imgs[] = 
  { 
   R.drawable.android, 
   R.drawable.cup, 
   R.drawable.donut, 
   R.drawable.eclair,
   R.drawable.froyo,
   R.drawable.ginger,
   R.drawable.honey,
   R.drawable.ics,
   R.drawable.jellybean
  };
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        rl = (RelativeLayout) findViewById(R.id.rl);
        gal = (Gallery) findViewById(R.id.gallery1);
        
        img = new ImageSwitcher (MainActivity.this);
        
        gal.setAdapter(new ImageAdapter(this));
        
        img.setFactory(this);
        
        img.setAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
        
        img.setAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
        
        params = new RelativeLayout.LayoutParams 
          ((int) LayoutParams.WRAP_CONTENT, (int) LayoutParams.WRAP_CONTENT);
        
        params.addRule(RelativeLayout.CENTER_IN_PARENT);
        
        img.setLayoutParams(params);
        
        rl.addView(img);
        
        gal.setOnItemClickListener(new OnItemClickListener() {
 
  @Override
  public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
    long arg3) {
   // TODO Auto-generated method stub
   
   img.setImageResource(imgs[arg2]);
  }
 });        
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    public class ImageAdapter extends BaseAdapter 
    {
     private Context ctx;
 
 public ImageAdapter(Context c) {
  ctx = c; 
 }
 
 @Override
 public int getCount() {
  // TODO Auto-generated method stub
   return imgs.length;
 }
 
 @Override
 public Object getItem(int position) {
  // TODO Auto-generated method stub
   return position;
 }
 
 @Override
 public long getItemId(int position) {
  // TODO Auto-generated method stub
   return position;
 }
 
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  // TODO Auto-generated method stub
   
  ImageView iView = new ImageView(ctx);
  iView.setImageResource(imgs[position]);
  iView.setScaleType(ImageView.ScaleType.FIT_XY);
  iView.setLayoutParams(new Gallery.LayoutParams(200, 150));
  
  return iView;
 }
    }
 
 @Override
 public View makeView() {
  // TODO Auto-generated method stub
  
  ImageView iView = new ImageView(this);
  iView.setScaleType(ImageView.ScaleType.FIT_CENTER);
  iView.setLayoutParams(new ImageSwitcher.LayoutParams
   (LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
  iView.setBackgroundColor(0xFFffffff);
  
  return iView;
 }
}
Output look like this :

How to Use Static Image Switcher Creation in Android

Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button.
Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :
<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" >
 
    <Gallery
        android:id="@+id/gallery1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" />
 
    <ImageSwitcher
        android:id="@+id/imageSwitcher1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/gallery1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="5dp" >
    </ImageSwitcher>
 
</RelativeLayout>
Step 3 : Open src -> package -> MainActivity.java and add following code :
package balaji.imageswitcher_static;
 
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ViewSwitcher.ViewFactory;
 
public class MainActivity extends Activity implements ViewFactory {
 
 int imgs[] = 
  { 
   R.drawable.android, 
   R.drawable.cup, 
   R.drawable.donut, 
   R.drawable.eclair,
   R.drawable.froyo,
   R.drawable.ginger,
   R.drawable.honey,
   R.drawable.ics,
   R.drawable.jellybean
  };
 
 ImageSwitcher imgSwitcher;
 
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 
  imgSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher1);
  imgSwitcher.setFactory(this);
  imgSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
    android.R.anim.fade_in));
  imgSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
    android.R.anim.fade_out));
 
  Gallery gallery = (Gallery) findViewById(R.id.gallery1);
  gallery.setAdapter(new ImageAdapter(this));
  
  gallery.setOnItemClickListener(new OnItemClickListener() {
 
   public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
     long arg3) {
    imgSwitcher.setImageResource(imgs[arg2]);
   }
  });
 }
 
 public class ImageAdapter extends BaseAdapter {
 
  private Context ctx;
 
  public ImageAdapter(Context c) {
   ctx = c; 
  }
 
  public int getCount() {
 
   return imgs.length;
  }
 
  public Object getItem(int arg0) {
 
   return arg0;
  }
 
  public long getItemId(int arg0) {
 
   return arg0;
  }
 
  public View getView(int arg0, View arg1, ViewGroup arg2) {
 
   ImageView iView = new ImageView(ctx);
   iView.setImageResource(imgs[arg0]);
   iView.setScaleType(ImageView.ScaleType.FIT_XY);
   iView.setLayoutParams(new Gallery.LayoutParams(200, 150));
   return iView;
  }
 }
 
 public View makeView() {
  ImageView iView = new ImageView(this);
  iView.setScaleType(ImageView.ScaleType.FIT_CENTER);
  iView.setLayoutParams(new ImageSwitcher.LayoutParams
   (LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
  iView.setBackgroundColor(0xFF000000);
  return iView;
 }
}
Our output will be like this :

How to Access Static Binding in Android

Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button.
Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :
<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" >
 
    <TextView
        android:id="@+id/textView1"
        android:textSize="20dp"
        android:textColor="#FF0000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:text="Static Binding" />
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="45dp"
        android:text="Bind" />
 
    <TextView
        android:id="@+id/textView2"
        android:textSize="18dp"
        android:textColor="#8A2BE2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:text="int" />
    
    <TextView
        android:id="@+id/textView3"
        android:textSize="18dp"
        android:textColor="#FF1493"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:text="float" />
    
    <TextView
        android:id="@+id/textView4"
        android:textSize="18dp"
        android:textColor="#8B008B"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView3"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:text="string" />
 
</RelativeLayout>
Step 3 : Open src -> package -> MainActivity.java and add following code :
package balaji.static_binding_ex1;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
public class MainActivity extends Activity {
 
 Button b;
 TextView tv1, tv2, tv3;
 String str1, str2, str3;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        b = (Button) findViewById(R.id.button1);
        
        tv1 = (TextView) findViewById(R.id.textView2);
        tv2 = (TextView) findViewById(R.id.textView3);
        tv3 = (TextView) findViewById(R.id.textView4);
        
        b.setOnClickListener(new View.OnClickListener() {
   
  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   
   SubClass obj = new SubClass();
   
   int val1 = obj.bind(10); 
   str1 = String.valueOf(val1);
   
   tv1.setText(str1);

   
   float val2 = obj.bind((float) 15.26);
   str2 = String.valueOf(val2);
   
   tv2.setText(str2);
   
   
   str3 = obj.bind("Success !!!");
   
   tv3.setText(str3);
  }
 });
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

class SubClass {
 
 int bind(int a) {
  
  return a;
 }
 
 float bind(float a) {
  
  return a;
 }
 
 String bind(String a) {
  
  return a;
 }
}
Output like :

How to Create a simple example for Dynamic Binding

Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button.
Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :
<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" >
 
     <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="37dp"
        android:text="Dynamic Binding" />
     
    <TextView
        android:id="@+id/textView1"
        android:textSize="20dp"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="150dp" />
 
</RelativeLayout>
Step 3 : Open src -> package -> MainActivity.java and add following code :
package balaji.dynamicbinding_ex1;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
public class MainActivity extends Activity {
 
 Button bind;
 TextView tv;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        bind = (Button) findViewById(R.id.button1);
        tv = (TextView) findViewById(R.id.textView1);
        
        bind.setOnClickListener(new View.OnClickListener() {
   
  public void onClick(View v) {
   // TODO Auto-generated method stub
    
   B obj = new B();
   
   tv.setText(obj.display());
  }
 });
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
 
class A
{ 
 protected String str_A = null;
 
 String display()
 {
  str_A = "Base Class Function Called";
  return str_A;
 }
}
 
class B extends A
{
 String display()
 {
  
  if (str_A == null)
  {
   str_A = "Derived Class Function Called";
  }
  
  return str_A;
 }
}
Our output will be like this :

Now rewrite the Code in class B
class B extends A
{
 String display()
 {
  super.display();
  
  if (str_A == null)
  {
   str_A = "Derived Class Function Called";
  }
  
  return str_A;
 }
}
Now our output will be like this :

How to Pass Different Values in Android

Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button.
Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :
<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" >
 
    <TextView
        android:id="@+id/textView1"
        android:textSize="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:text="Message Passing" />
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:text="Float Value Pass" />
 
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:text="Int Value Pass" />
 
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:text="String Value Pass" />
 
 
</RelativeLayout>
Step 3 : Open src -> package -> MainActivity.java and add following code :
package balaji.message_passing_ex2;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
 
public class MainActivity extends Activity {
 
 Button b1, b2, b3;
 String value;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        b1 = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);
        b3 = (Button) findViewById(R.id.button3);
        
        final SubClass obj = new SubClass();
        
        b1.setOnClickListener(new View.OnClickListener() {
   
  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
  
   float value1 = obj.float_pass((float) 11.20);
   value = String.valueOf(value1);
  
   Toast.makeText(getBaseContext(), value, Toast.LENGTH_SHORT).show();
  }
 });
        
        b2.setOnClickListener(new View.OnClickListener() {
   
  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   
   int value1 = obj.int_pass(52);
   value = String.valueOf(value1);
   
   Toast.makeText(getBaseContext(), value, Toast.LENGTH_SHORT).show();
  }
 });
        
        b3.setOnClickListener(new View.OnClickListener() {
   
  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   
   value = obj.String_pass("Hello ");
   
   Toast.makeText(getBaseContext(), value, Toast.LENGTH_SHORT).show();
  }
 });
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

class SubClass {
 
 float float_pass(float a) {
  
  a = a*a;
  
  return a;
 }
 
 int int_pass(int a) {
  
  a = a*a;
  
  return a;
 }
 
 String String_pass(String str) {
  
  str = str.concat("Friends");
  
  return str;
 }
}
Output look like as:

How to Create a simple Message Passing from One Class to Another Class

Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button.
Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :
<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" >
 
    <EditText
        android:id="@+id/editText1"
        android:hint="Enter your Name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:ems="10" >
 
        <requestFocus />
    </EditText>
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:text="Bind" />
 
    <TextView
        android:id="@+id/textView1"
        android:textSize="25dp"
        android:lineSpacingMultiplier="1.5"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />
 
</RelativeLayout>
Step 3 : Open src -> package -> MainActivity.java and add following code :
package balaji.messagepassing_ex1;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
 
public class MainActivity extends Activity {
 
 EditText name;
 Button Bind;
 TextView content;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        name = (EditText) findViewById(R.id.editText1);
        Bind = (Button) findViewById(R.id.button1);
        content = (TextView) findViewById(R.id.textView1);
        
        Bind.setOnClickListener(new View.OnClickListener() {
   
  public void onClick(View v) {
   // TODO Auto-generated method stub
    
   String temp1 = name.getText().toString();
   
   SubClass obj = new SubClass();
   String temp2 = obj.MsgBindingFn(temp1);
   
   content.setText(temp2);
  }
 });
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
 
class SubClass
{
 String str = "Hello friends, This is your ";
 
 String MsgBindingFn(String str1)
 {
  
  str = str.concat(str1);
  
  return str;
 }
}
Our output will be like this :


How to Multiple Inheritance Creation in Android

Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button.
Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :
<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" >
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="78dp"
        android:text="Multiple Inheritance" />
 
    <TextView
        android:id="@+id/textView1"
        android:textSize="20dp"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"  />
 
</RelativeLayout>
Step 3 : Open src -> package -> MainActivity.java and add following code :
package balaji.inheritance_multiple;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
public class MainActivity extends Activity {
 
	Button multiple;
	TextView tv;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        multiple = (Button) findViewById(R.id.button1);
        tv = (TextView) findViewById(R.id.textView1);
        
        multiple.setOnClickListener(new View.OnClickListener() {
			
		public void onClick(View v) {
			// TODO Auto-generated method stub
				
			H obj = new H();
			
			tv.setText(obj.display());
		}
	});
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
 
interface A
{
	String str_A = "A";
}
 
interface B
{
	String str_B = "N";
}
 
interface C
{
	String str_C = "D";
}
 
interface D
{
	String str_D = "R";
}
 
interface E
{
	String str_E = "O";
}
 
interface F
{
	String str_F = "I";
}
 
interface G
{
	String str_G = "D";
}
 
class H implements A, B, C, D, E, F, G
{
	String str_H = str_A.concat(str_B).concat(str_C).concat(str_D).
			concat(str_E).concat(str_F).concat(str_G);
	String display()
	{
		return str_H;
	}
}
Our output will be like this :

How to Use Multi-level Inheritance Creation in Android

Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button.
Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :
<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" >
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:text="Multi-Level Inheritance" />
 
    <TextView
        android:id="@+id/textView1"
        android:textSize="25dp"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="170dp" />
 
</RelativeLayout>
Step 3 : Open src -> package -> MainActivity.java and add following code :
package balaji.inheritance_multi_level;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
public class MainActivity extends Activity {
 
	Button Multilevel;
	TextView tv;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Multilevel = (Button) findViewById(R.id.button1);
        tv = (TextView) findViewById(R.id.textView1);
        
        Multilevel.setOnClickListener(new View.OnClickListener() {
			
		public void onClick(View v) {
			// TODO Auto-generated method stub
				
			C obj = new C();
			
			tv.setText(obj.display());
		}
	});
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
class A
{
	
	protected String str_A = "I ";
}
 
class B extends A
{
	
	protected String str_B = str_A.concat("Love ");
}
 
class C extends B
{
	
	String str_C = str_B.concat("Android");
	
	String display()
	{
		return str_C;
	}
}
Our output will be like this :

How to Use Single Inheritance in Android

Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button.
Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :
<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" >
    
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="38dp"
        android:text="Single Inheritance" />
 
    <TextView
        android:id="@+id/textView1"
        android:textSize="20dp"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="125dp" />
 
</RelativeLayout>
Step 3 : Open src -> package -> MainActivity.java and add following code :
package sawan.inheritance_single;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
public class MainActivity extends Activity {
	
	Button single; 
	TextView tv;
		
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        single = (Button) findViewById(R.id.button1);
        tv = (TextView) findViewById(R.id.textView1);
        
        single.setOnClickListener(new View.OnClickListener() {
			
		public void onClick(View v) {
			// TODO Auto-generated method stub
				
			B obj = new B();
			
			tv.setText(obj.display());
		}
	});
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
 
class A
{
	protected String str_A = "Hello ";
}
 
class B extends A
{
	String str_B = str_A.concat("Android !!!");
	
	String display()
	{
		return str_B;
	}
}
Output look like:

How to Use Different Length Attributes Method Overloading

Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button.
Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :
<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" >
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="Polymorphism" />
 
    <TextView
        android:id="@+id/textView1"
        android:textSize="15dp"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100dp" />
    
    <TextView
        android:id="@+id/textView2"
        android:textSize="15dp"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="150dp" />
    
    <TextView
        android:id="@+id/textView3"
        android:textSize="15dp"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="200dp" />
    
    <TextView
        android:id="@+id/textView4"
        android:textSize="15dp"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="250dp" />
    
    <TextView
        android:id="@+id/textView5"
        android:textSize="15dp"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="300dp" />
 
</RelativeLayout>
Step 3 : Open src -> package -> MainActivity.java and add following code :
package balaji.polymorphism_ex2;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
public class MainActivity extends Activity {
	
	Button poly;
	TextView t1, t2, t3, t4, t5;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        poly = (Button) findViewById(R.id.button1);
        
        t1 = (TextView) findViewById(R.id.textView1);
        t2 = (TextView) findViewById(R.id.textView2);
        t3 = (TextView) findViewById(R.id.textView3);
        t4 = (TextView) findViewById(R.id.textView4);
        t5 = (TextView) findViewById(R.id.textView5);
        
        poly.setOnClickListener(new View.OnClickListener() {
			
		public void onClick(View v) {
			// TODO Auto-generated method stub
					
			SubClass obj = new SubClass ();
		
			t1.setText(String.valueOf(obj.display()));
		
			t2.setText(String.valueOf(obj.display(11)));
		
			t3.setText(String.valueOf(obj.display((float)15.26)));
		
			t4.setText(String.valueOf(obj.display(11, 20)));
		
			t5.setText(obj.display("Overloading"));
		}
	});
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
 
class SubClass
{
	int display()
	{
		int a = 10;
		
		return a;		
	}
	
	int display(int temp1)
	{
		int a = 10;
		
		a = a + temp1;
		
		return a;
	}
	
	int display(int temp1, int temp2)
	{
		int c;
		
		c = temp1 + temp2;
		
		return c;
	}
	
	float display(float temp1)
	{
		float c;
		
		c = temp1 * 123 ;
		
		return c;
	}
	
	String display(String temp1)
	{
		String str = "Method ";
		
		str = str.concat(temp1);
		
		return str;
	}
}
Output look like as given image:

How to Use Same Length Attributes Method Overloading in Android

Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button.
Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :
<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" >
 
    <TextView
        android:id="@+id/textView1"
        android:textSize="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:text="Polymorphism - Same Length" />
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:text="Get" />
 
    <TextView
        android:id="@+id/textView2"
        android:textSize="18dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="220dp"
        android:text="1" />
    
    <TextView
        android:id="@+id/textView3"
        android:textSize="18dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="270dp"
        android:text="2" />
    
    <TextView
        android:id="@+id/textView4"
        android:textSize="18dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="320dp"
        android:text="3" />
    
    <TextView
        android:id="@+id/textView5"
        android:textSize="18dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="370dp"
        android:text="4" />
 
</RelativeLayout>
Step 3 : Open src -> package -> MainActivity.java and add following code :
package balaji.polymorphism_ex1;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
public class MainActivity extends Activity {
 
	Button b;
	TextView tv1, tv2, tv3, tv4;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        b = (Button) findViewById(R.id.button1);
        
        tv1 = (TextView) findViewById(R.id.textView2);
        tv2 = (TextView) findViewById(R.id.textView3);
        tv3 = (TextView) findViewById(R.id.textView4);
        tv4 = (TextView) findViewById(R.id.textView5);
        
        b.setOnClickListener(new View.OnClickListener() {
			
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
					
			SubClass obj = new SubClass();
		
			String str1 = obj.method_overloading(10, 10);
		
			String str2 = obj.method_overloading(10, (float)10.123);
		
			String str3 = obj.method_overloading((float)10.123, 10);
		
			String str4 = obj.method_overloading((float)10.123, (float)10.123);
		
			tv1.setText(str1);
			tv2.setText(str2);
			tv3.setText(str3);
			tv4.setText(str4);
		}
	});
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

class SubClass {
	
	String method_overloading(int a, int b)
	{
		
		return "(int, int) Method Called";
	}
	
	String method_overloading(int a, float b)
	{
		
		return "(int, float) Method Called";
	}
	
	String method_overloading(float a, int b)
	{
		
		return "(float, int) Method Called";
	}
	
	String method_overloading(float a, float b)
	{
		
		return "(float, float) Method Called";
	}
}
Output :

How to Use Private Access Specifier

Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button.
Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :
<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" >
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:text="Get 1" />
    
    
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="130dp"
        android:text="Get 2" />
 
</RelativeLayout>
Step 3 : Open src -> package -> MainActivity.java and add following code :
package sawan.encapsulation_private;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
 
public class MainActivity extends Activity {
 
 Button b1, b2;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        b1 = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);
        
        b1.setOnClickListener(new View.OnClickListener() {
   
  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   
   display(100);
  }
 
  private void display(int val) {
   // TODO Auto-generated method stub
   
   Toast.makeText(getBaseContext(), ""+val, Toast.LENGTH_SHORT).show();
  }
 });
        
        b2.setOnClickListener(new View.OnClickListener() {
   
  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   
   display(200);
  }
 
  private void display(int val) {
   // TODO Auto-generated method stub
   
   Toast.makeText(getBaseContext(), ""+val, Toast.LENGTH_SHORT).show();
  }
 });       
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
Output would be look like given image:

How to Use Protected Access Specifier

Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button.
Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :
<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" >
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:text="Get 1st Child-Class Value" />
    
    
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="120dp"
        android:text="Get 2nd Child-Class Value" />   
    
 
</RelativeLayout>
Step 3 : Open src -> package -> MainActivity.java and add following code :
package sawan.encapsulation_protected;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
 
public class MainActivity extends Activity {
 
 Button b1, b2;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        b1 = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);
        
        final SubClass1 obj1 = new SubClass1();
        final SubClass2 obj2 = new SubClass2();        
        
        b1.setOnClickListener(new View.OnClickListener() {
   
  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   
   String value1 = obj1.display1();
   
   display(value1);
  }
 });
        
        b2.setOnClickListener(new View.OnClickListener() {
   
  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   
   String value2 = obj2.display2();
   
   display(value2);
  }
 });        
        
    }
    
    public void display(String val) {
     
     Toast.makeText(getBaseContext(), val, Toast.LENGTH_SHORT).show();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

class SubClass {
 
 protected String value = "Success !!!";
}

class SubClass1 extends SubClass{
 
 String display1() {
  
  value = "SubClass1 - Protected Value";
  return value;
 }
}

class SubClass2 extends SubClass1{
 
 String display2() {
  
  value = "SubClass2 - Protected Value";
  return value;
 }
}
Output :

How to Use Public Access Specifier

Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button.
Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :
<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" >
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:text="Add 1 with Public Value" />
    
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="150dp"
        android:text="Multiply 2 with Public Value" />
 
</RelativeLayout>
Step 3 : Open src -> package -> MainActivity.java and add following code :
package sawan.encapsulation_public;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
 
public class MainActivity extends Activity {
 
    public int value = 10;
    Button b1, b2;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        b1 = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);
        
        b1.setOnClickListener(new View.OnClickListener() {
   
  public void onClick(View v) {
   // TODO Auto-generated method stub
   
   value = value + 1;
   
   display(value);
  }
 });        
        
        b2.setOnClickListener(new View.OnClickListener() {
   
  public void onClick(View v) {
   // TODO Auto-generated method stub
   
   value = value * 2;
   
   display(value); 
  }
 });       
    }
    
    public void display(int val) {
       
     Toast.makeText(getBaseContext(), String.valueOf(val), 
  Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
Output of above code :

How to Use Access Specifier in Android

Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button.
Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :
<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" >
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="48dp"
        android:text="Encapsulation" />
 
    <TextView
        android:id="@+id/textView1"
        android:textSize="15dp"
        android:lineSpacingMultiplier="1.5"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="180dp" />
    
    <TextView
        android:id="@+id/textView2"
        android:textSize="15dp"
        android:lineSpacingMultiplier="1.5"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="250dp" />
    
    <TextView
        android:id="@+id/textView3"
        android:textSize="15dp"
        android:lineSpacingMultiplier="1.5"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="320dp" />
 
</RelativeLayout>
Step 3 : Open src -> package -> MainActivity.java and add following code :
package balaji.encapsulation_public;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
public class MainActivity extends Activity {
 
 Button Encapsulate;
 TextView tv1, tv2, tv3;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Encapsulate = (Button) findViewById(R.id.button1);
        tv1 = (TextView) findViewById(R.id.textView1);
        tv2 = (TextView) findViewById(R.id.textView2);
        tv3 = (TextView) findViewById(R.id.textView3);
        
        Encapsulate.setOnClickListener(new View.OnClickListener() {
   
  public void onClick(View v) {
   // TODO Auto-generated method stub
    
   B obj = new B();
   
   tv1.setText(obj.display_public());
   tv2.setText(obj.display_protected());
   tv3.setText(obj.display_private());
  }
 });
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
 
class A
{
 public String str1 = null;
 protected String str2 = null;
 private String str3 = null;
 
 A()
 {
  str1 = "Class A Public String";
  str2 = "Class A Protected String";
  str3 = "Class A Private String";
 }
}
 
class B extends A
{
 private String str3 = "Class B Private String";
 
 B()
 {
  str1 = str1.concat(" & Shared");
  str2 = str2.concat(" & Shared");
  str3 = str3.concat(" & Not Shared");
 }
 
 String display_public()
 {
  return str1;
 }
 
 String display_protected()
 {
  return str2;
 }
 
 String display_private()
 {
  return str3;
 }
}
Our output will be like this :

How to Create More Objects for More Events in Android

Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button.

Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :
<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" >
 
    <TextView
        android:id="@+id/textView1"
        android:textSize="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:text="Object Example" />
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:text="Button" />
 
    <TextView
        android:id="@+id/textView2"
        android:textSize="18dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:text="1" />
    
    <TextView
        android:id="@+id/textView3"
        android:textSize="18dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:text="2" />
    
    <TextView
        android:id="@+id/textView4"
        android:textSize="18dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView3"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:text="3" />
    
    <TextView
        android:id="@+id/textView5"
        android:textSize="18dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView4"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:text="4" />
 
</RelativeLayout>
Step 3 : Open src -> package -> MainActivity.java and add following code :
package balaji.object_ex2;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
public class MainActivity extends Activity {
 
 Button b;
 TextView tv1, tv2, tv3, tv4;
 String str1, str2, str3, str4;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        b = (Button) findViewById(R.id.button1);
        
        tv1 = (TextView) findViewById(R.id.textView2);
        tv2 = (TextView) findViewById(R.id.textView3);
        tv3 = (TextView) findViewById(R.id.textView4);
        tv4 = (TextView) findViewById(R.id.textView5);
        
        b.setOnClickListener(new View.OnClickListener() {
   
  public void onClick(View v) {
   // TODO Auto-generated method stub
    
   SubClass1 obj1 = new SubClass1();
   SubClass2 obj2 = new SubClass2();
   
   str1 = function1();
          str2 = function2();
          str3 = obj1.function3().toString();
          str4 = obj2.function4().toString();
          
          tv1.setText(str1);
          tv2.setText(str2);
          tv3.setText(str3);
          tv4.setText(str4);
  }
 
  private String function1() {
   
   return "Function 1 Called";
  }
 });        
    }    
    
    public String function2() {

     return "Function 2 Called";
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
     getMenuInflater().inflate(R.menu.activity_main, menu);
     return true;
    }
}

class SubClass1 {
 
 String function3() {
  
  return "Function 3 Called";
 }
}
 
class SubClass2 {
 
 String function4() {
  
  return "Function 4 Called";
 }
}

And finallly output look like :

How to Create Simple Object for a Android Class

Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button.
Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :
<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" >
 
    <TextView
        android:id="@+id/textView1"
        android:textSize="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100dp" />
 
</RelativeLayout>
Step 3 : Open src -> package -> MainActivity.java and add following code :
package sawan.object_ex1;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
 
public class MainActivity extends Activity {
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        TextView tv = (TextView) findViewById(R.id.textView1);
        
        SubClass obj = new SubClass ();
        
        tv.setText(obj.display());        
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
 
class SubClass
{
 String str =  "This is SubClass Event";
 
 String display()
 {  
  return str;
 }
}
And finally your output look like :


How to Open and Display File in Swing

I got a project to fetch the data from file and display them on Swing .But because i am novice and not much friendly with Swing. I searched on Google and use Jlabel and JTextArea but they won't work for me as i want. They show only last line of file. I am in trouble then my colleague Sarita gave me the code of my problem. And i am giving you that code by which you'll not be get problem as i faced.

import java.io.*;
import java.nio.charset.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
public class JEditorPaneFileChooser implements ActionListener {
     JFrame myFrame = null;
   JEditorPane myPane = null;
   JMenuItem cmdOpen = null;
   JMenuItem cmdSave = null;
   String dirName = "G:\\JAVABK";
   String fileName = "students.txt";

   public static void main(String[] a) {
      (new JEditorPaneFileChooser()).test();
   }
   
private void test() {
      myFrame = new JFrame("JEditorPane JFileChooser Test");
      myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      myFrame.setSize(300,200);
      
      myPane = new JEditorPane();
      myPane.setContentType(".pdf");
      myPane.setText(
         "Hello GeekOnJava! - \u7535\u8111\u4F60\u597D\uFF01\n"
         + "Welcome to Sawan's Website!\n"
         + "\u6B22\u8FCE\u4F60\u8BBF\u95EE\u548C\u8363\u7F51\u7AD9"
         + "\uFF01\nwww.geekonjava.blogspot.com");
      myFrame.setContentPane(myPane);

      JMenuBar myBar = new JMenuBar();
      JMenu myMenu = getFileMenu();
      myBar.add(myMenu); 
      myFrame.setJMenuBar(myBar);

      myFrame.setVisible(true);
   }
   private JMenu getFileMenu() {
      JMenu myMenu = new JMenu("File");
      cmdOpen = new JMenuItem("Open");
      cmdOpen.addActionListener(this);
      myMenu.add(cmdOpen);

      cmdSave = new JMenuItem("Save");
      cmdSave.addActionListener(this);
      myMenu.add(cmdSave);
      return myMenu;
   }
   public void actionPerformed(ActionEvent e) {
      JFileChooser chooser = new JFileChooser();
      chooser.setCurrentDirectory(new File(dirName));
      chooser.setSelectedFile(new File(fileName));
      chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
FileNameExtensionFilter filtertd = new FileNameExtensionFilter(
        "DOCS", "docx");
      chooser.setFileFilter(filtertd);
      FileNameExtensionFilter filtert = new FileNameExtensionFilter(
        "TEXT", "txt");
      chooser.setFileFilter(filtert);
       FileNameExtensionFilter filterp = new FileNameExtensionFilter(
        "PDF", "pdf");
      chooser.setFileFilter(filterp);
       

      Object cmd = e.getSource();
      try {
         if (cmd == cmdOpen) {
            int code = chooser.showOpenDialog(myPane);
            if (code == JFileChooser.APPROVE_OPTION) {
                
                  
//              myPane.setText(extract.getText());
//              PdfOptions options = PdfOptions.create().fontEncoding("windows-1250");
//              OutputStream out = new FileOutputStream(new File(chooser.getSelectedFile()));
//                PdfConverter.getInstance().convert(doc, out, options);
                
               File selectedFile = chooser.getSelectedFile();
               fileName = selectedFile.getName();
               FileInputStream fis = 
               new FileInputStream(selectedFile);
               InputStreamReader in = 
               new InputStreamReader(fis, Charset.forName("UTF-8")); 

               
               char[] buffer = new char[1024];
               int n = in.read(buffer);
               String text = new String(buffer, 0, n);
               myPane.setText(text);
               in.close();
            }
         } else if (cmd == cmdSave) {
            int code = chooser.showOpenDialog(myPane);
            if (code == JFileChooser.APPROVE_OPTION) {
               File selectedFile = chooser.getSelectedFile();
               fileName = selectedFile.getName();
               FileOutputStream fos = 
               new FileOutputStream(selectedFile);
               OutputStreamWriter out = 
               new OutputStreamWriter(fos, Charset.forName("UTF-8")); 
               out.write(myPane.getText());
               out.close();
            }
         }
      } catch (Exception f) {
        f.printStackTrace();
      }
   }
}
I hope it would work for you as for me. Happy Coding.

Insert Image in SQLite and Display in ListView

Hello friends now i am telling you how to insert image in sqlite and display in listview. Its my hard work to find the code because many of site have display image in listview but they fetch the images from drawable folder which not need of myself and you too.



DataBaseHandler.java

package com.example.sqliteimagedemo;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseHandler extends SQLiteOpenHelper {

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "imagedb";

// Contacts table name
private static final String TABLE_CONTACTS = "contacts";

// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_IMAGE = "image";

public DataBaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_IMAGE + " BLOB" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

// Create tables again
onCreate(db);
}

/**
* All CRUD(Create, Read, Update, Delete) Operations
*/

public// Adding new contact
void addContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(KEY_NAME, contact._name); // Contact Name
values.put(KEY_IMAGE, contact._image); // Contact Phone

// Inserting Row
db.insert(TABLE_CONTACTS, null, values);
db.close(); // Closing database connection
}

// Getting single contact
Contact getContact(int id) {
SQLiteDatabase db = this.getReadableDatabase();

Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
KEY_NAME, KEY_IMAGE }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();

Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getBlob(1));

// return contact
return contact;

}

// Getting All Contacts
public List<Contact> getAllContacts() {
List<Contact> contactList = new ArrayList<Contact>();
// Select All Query
String selectQuery = "SELECT * FROM contacts ORDER BY name";

SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
contact.setID(Integer.parseInt(cursor.getString(0)));
contact.setName(cursor.getString(1));
contact.setImage(cursor.getBlob(2));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
// close inserting data from database
db.close();
// return contact list
return contactList;

}

// Updating single contact
public int updateContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName());
values.put(KEY_IMAGE, contact.getImage());

// updating row
return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
new String[] { String.valueOf(contact.getID()) });

}

// Deleting single contact
public void deleteContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
new String[] { String.valueOf(contact.getID()) });
db.close();
}

// Getting contacts Count
public int getContactsCount() {
String countQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();

// return count
return cursor.getCount();
}
}


Download ImageSQLite Project 

SQLiteDemoActivity.java

package com.example.sqliteimagedemo;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

public class SQLiteDemoActivity extends Activity {
ArrayList<Contact> imageArry = new ArrayList<Contact>();
ContactImageAdapter adapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


DataBaseHandler db = new DataBaseHandler(this);
// get image from drawable
Bitmap image = BitmapFactory.decodeResource(getResources(),
R.drawable.facebook);

// convert bitmap to byte
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte imageInByte[] = stream.toByteArray();
/**
* CRUD Operations
* */
// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
db.addContact(new Contact("FaceBook", imageInByte));
// display main List view bcard and contact name

// Reading all contacts from database
List<Contact> contacts = db.getAllContacts();
for (Contact cn : contacts) {
String log = "ID:" + cn.getID() + " Name: " + cn.getName()
+ " ,Image: " + cn.getImage();

// Writing Contacts to log
Log.d("Result: ", log);
//add contacts data in arrayList
imageArry.add(cn);

}
adapter = new ContactImageAdapter(this, R.layout.screen_list,
imageArry);
ListView dataList = (ListView) findViewById(R.id.list);
dataList.setAdapter(adapter);
}
}


Contact.java
package com.example.sqliteimagedemo;

public class Contact {

 // private variables
 int _id;
 String _name;
 byte[] _image;

 // Empty constructor
 public Contact() {

 }

 // constructor
 public Contact(int keyId, String name, byte[] image) {
 this._id = keyId;
 this._name = name;
 this._image = image;

 }

 // constructor
 public Contact(String name, byte[] image) {
 this._name = name;
 this._image = image;
 }

 // getting ID
 public int getID() {
 return this._id;
 }

 // setting id
 public void setID(int keyId) {
 this._id = keyId;
 }

 // getting name
 public String getName() {
 return this._name;
 }

 // setting name
 public void setName(String name) {
 this._name = name;
 }

 // getting image
 public byte[] getImage() {
 return this._image;
 }

 // setting image
 public void setImage(byte[] image) {
 this._image = image;
 }
 }

ContactImageAdapter.java


package com.example.sqliteimagedemo;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ContactImageAdapter extends ArrayAdapter<Contact>{
Context context;
int layoutResourceId;
ArrayList<Contact> data=new ArrayList<Contact>();
public ContactImageAdapter(Context context, int layoutResourceId, ArrayList<Contact> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ImageHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ImageHolder();
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
row.setTag(holder);
}
else
{
holder = (ImageHolder)row.getTag();
}
Contact picture = data.get(position);
holder.txtTitle.setText(picture ._name);
//convert byte to bitmap take from contact class
byte[] outImage=picture._image;
ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
holder.imgIcon.setImageBitmap(theImage);
return row;
}
static class ImageHolder
{
ImageView imgIcon;
TextView txtTitle;
}
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.55" >
</ListView>
</LinearLayout>

screen_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="10dp" >
<ImageView
android:id="@+id/imgIcon"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="0.71"
android:gravity="center_vertical" />
<TextView
android:id="@+id/txtTitle"
android:layout_width="80dp"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:textSize="14dp"
android:layout_marginLeft="7dp" />
</LinearLayout>

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sqliteimagedemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".SQLiteDemoActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Output will be like as below images: