Android Dynamic Binding


  1.  Create a simple example for Dynamic Binding 
  1.  Access Static Binding in Android 
  
1.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. 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" >  
    <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 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 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.main, menu);  
     return true;  
   }  
 }  
 class A  
 {  
 protected String str_A = null;  
 String display()  
 {  
 str_A = "Base Class Function Called By Hemanth Somaraju";  
 return str_A;  
 }  
 }  
 class B extends A  
 {  
 String display()  
 {  
 if (str_A == null)  
 {  
 str_A = "Derived Class Function Called By Hemanth Somaraju";  
 }  
 return str_A;  
 }  
 }  
 
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 : 


 



Step 6 : 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 By Hemanth Somaraju";  
 }  
 return str_A;  
 }  
 }  



Step 7 : Now our output will be like this :





2.Access Static Binding 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: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="Hemanth" />  
 </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 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(23);   
 str1 = String.valueOf(val1);  
 tv1.setText(str1);  
 float val2 = obj.bind((float) 10.23);  
 str2 = String.valueOf(val2);  
 tv2.setText(str2);  
 str3 = obj.bind("Somaraju !!!");  
 tv3.setText(str3);  
 }  
 });  
   }  
   @Override  
   public boolean onCreateOptionsMenu(Menu menu) {  
     getMenuInflater().inflate(R.menu.main, menu);  
     return true;  
   }  
 }  
 class SubClass {  
 int bind(int a) {  
 return a;  
 }  
 float bind(float a) {  
 return a;  
 }  
 String bind(String a) {  
 return a;  
 }  
 } 


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 :