Android Class


  1. Create a Simple Class in Android  
  1. Perform an Action to Another Class  
1. Create a Simple Class 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"    
   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_alignParentLeft="true"    
   android:layout_alignParentTop="true"    
   android:layout_marginLeft="18dp"    
   android:layout_marginTop="40dp"    
   android:text="This Shows A Simple Android Class Example By Hemanth Somaraju"    
   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;   
  public class MainActivity extends Activity {   
  @Override   
  protected void onCreate(Bundle savedInstanceState) {   
  super.onCreate(savedInstanceState);   
  setContentView(R.layout.activity_main);   
  }   
  @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;   
  }   
  } 
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. Perform an Action to Another 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="60dp"   
    android:text="Class Example"   
    android:textAppearance="?android:attr/textAppearanceLarge" />   
   <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="30dp"   
    android:text="Get Next Class Value" />   
   <TextView   
    android:id="@+id/textView2"   
    android:layout_width="wrap_content"   
    android:layout_height="wrap_content"   
    android:layout_below="@+id/button1"   
    android:layout_centerHorizontal="true"   
    android:layout_marginTop="51dp"   
    android:text="Hemanth"   
    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.view.View;   
  import android.widget.Button;   
  import android.widget.TextView;   
  public class MainActivity extends Activity {   
  Button b1;   
  TextView t1;   
  @Override   
  protected void onCreate(Bundle savedInstanceState) {   
  super.onCreate(savedInstanceState);   
  setContentView(R.layout.activity_main);   
  b1=(Button) findViewById(R.id.button1);   
  t1=(TextView) findViewById(R.id.textView2);   
  b1.setOnClickListener(new View.OnClickListener() {   
  @Override   
  public void onClick(View v) {   
  // TODO Auto-generated method stub   
  SubClass obj=new SubClass();   
  String Value=obj.display().toString();   
  t1.setText(Value);   
  }   
  });   
  }   
  @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 {   
  public String str=null;   
  SubClass()   
  {   
  str="Somaraju";   
  }   
  String display() {   
  //return the SubClass Value to MainClass   
  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 
Image