Android Object

 
  1. Create Simple Object For a Android Class  
  1. Create More Objects For More Events in Android  
1. 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. If you have any doubt regarding create a new project Click Here. 
   
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"   
   android:paddingBottom="@dimen/activity_vertical_margin"   
   android:paddingLeft="@dimen/activity_horizontal_margin"   
   android:paddingRight="@dimen/activity_horizontal_margin"   
   android:paddingTop="@dimen/activity_vertical_margin"   
   tools:context=".MainActivity" >   
   <TextView   
    android:id="@+id/textView1"   
    android:layout_width="wrap_content"   
    android:layout_height="wrap_content"   
    android:layout_alignParentTop="true"   
    android:layout_centerHorizontal="true"   
    android:layout_marginTop="67dp"   
    android:textAppearance="?android:attr/textAppearanceLarge" />   
  </RelativeLayout>  

Step 3 : Open src -> package -> MainActivity.java and add following code : 

  package com.gudivaga.hemanthsomaraju;   
  import android.os.Bundle;   
  import android.app.Activity;   
  import android.view.Menu;   
  import android.widget.TextView;   
  public class MainActivity extends Activity {   
  @Override   
  protected void onCreate(Bundle savedInstanceState) {   
  super.onCreate(savedInstanceState);   
  setContentView(R.layout.activity_main);   
  TextView t1=(TextView) findViewById(R.id.textView1);   
  SubClass obj=new SubClass();   
  t1.setText(obj.display());   
  }   
  @Override   
  public boolean onCreateOptionsMenu(Menu menu) {   
  // Inflate the menu; this adds items to the action bar if it is present.   
  getMenuInflater().inflate(R.menu.main, menu);   
  return true;   
  }   
  }   
  class SubClass   
  {   
  String str="This is SubClass Event By Hemanth Somaraju";   
  String display()   
  {    
  return str;   
  }   
  }   

Step 4 : Open AndroidManifest.xml and add following code : 
  <?xml version="1.0" encoding="utf-8"?>   
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"   
   package="com.gudivaga.hemanthsomaraju"   
   android:versionCode="1"   
   android:versionName="1.0" >   
   <uses-sdk   
    android:minSdkVersion="8"   
    android:targetSdkVersion="19" />   
   <application   
    android:allowBackup="true"   
    android:icon="@drawable/ic_launcher"   
    android:label="@string/app_name"   
    android:theme="@style/AppTheme" >   
    <activity   
     android:name="com.gudivaga.hemanthsomaraju.MainActivity"   
     android:label="@string/app_name" >   
     <intent-filter>   
      <action android:name="android.intent.action.MAIN" />   
      <category android:name="android.intent.category.LAUNCHER" />   
     </intent-filter>   
    </activity>   
   </application>   
  </manifest>   
Step 5 : Our output will be like this : 
  
Image 
  
2. 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. If you have any doubt regarding create a new project Click Here. 
  
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 com.gudivada.hemanthsomaraju;   
  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 b1;   
  TextView t1, t2, t3, t4;   
  String str1, str2, str3, str4;   
   @Override   
   public void onCreate(Bundle savedInstanceState) {   
    super.onCreate(savedInstanceState);   
    setContentView(R.layout.activity_main);   
    b1 = (Button) findViewById(R.id.button1);   
    t1 = (TextView) findViewById(R.id.textView2);   
    t2 = (TextView) findViewById(R.id.textView3);   
    t3 = (TextView) findViewById(R.id.textView4);   
    t4 = (TextView) findViewById(R.id.textView5);   
    b1.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();   
    t1.setText(str1);   
    t2.setText(str2);   
    t3.setText(str3);   
    t4.setText(str4);   
  }   
  private String function1() {   
  return "Hemanth";   
  }   
  });     
   }    
   public String function2() {   
   return "Somaraju";   
   }   
   @Override   
   public boolean onCreateOptionsMenu(Menu menu) {   
   getMenuInflater().inflate(R.menu.main, menu);   
   return true;   
   }   
  }   
  class SubClass1 {   
  String function3() {   
  return "From";   
  }   
  }   
  class SubClass2 {   
  String function4() {   
  return "Kakinada,India";   
  }   
  }   
Step 4 : Open AndroidManifest.xml and add following code : 

  <?xml version="1.0" encoding="utf-8"?>   
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"   
   package="com.gudivada.hemanthsomaraju"   
   android:versionCode="1"   
   android:versionName="1.0" >   
   <uses-sdk   
    android:minSdkVersion="8"   
    android:targetSdkVersion="18" />   
   <application   
    android:allowBackup="true"   
    android:icon="@drawable/ic_launcher"   
    android:label="@string/app_name"   
    android:theme="@style/AppTheme" >   
    <activity   
     android:name="com.gudivada.hemanthsomaraju.MainActivity"   
     android:label="@string/app_name" >   
     <intent-filter>   
      <action android:name="android.intent.action.MAIN" />   
      <category android:name="android.intent.category.LAUNCHER" />   
     </intent-filter>   
    </activity>   
   </application>   
  </manifest> 
  
  
Step 5 : Our output will be like this : 

Image Image