Learn How to Use a Single Class in Multiple Activities in Android

Introduction

 
This article explains how to use a single class in multiple Activities in Android.
 
Suppose in your Android application there is a class that you need to use in every Android Activity. For example, you use a progress bar in your application and the progress bar will be the same for all the classes to show functionality. So this article explains how to use a single class in multiple Activities. I have also used a ProgressBar to solve this problem.  
 
In this, I have used a single class in one Activity. You can use it in multiple Activities depending on your needs by creating an element and give the package name.
 
Step 1
 
Create a  project like this:
 
Go to "File" -> "Android application Project".
 
app1
 
Click "Next".
 
app2
 
Click "Next".
app3
 
Click "Finish".
 
app4
 
Step 2
 
Create an XML file "activitymain.xml" with the following.
 
In this file, you will use a button on which you click to go to the next Activity.
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity"  
  10.     android:background="#12a4fe">  
  11.     <Button  
  12.         android:layout_width="fill_parent"  
  13.         android:layout_height="wrap_content"  
  14.         android:id="@+id/button1"  
  15.         android:text="Click on Button"/>  
  16. </RelativeLayout> 
Step 3
 
Create an XML file first with the following.
 
In this XML file I have written a package as an XML element, "<com.optimizecode.Second >". Here "Second" is class that will be inflated during runtime.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.      <TextView  
  7.         android:layout_width="wrap_content"  
  8.         android:layout_height="wrap_content"  
  9.         android:id="@+id/textView1"  
  10.         android:textStyle="bold"/>  
  11.     <TextView  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:id="@+id/textView2"  
  15.         android:textStyle="bold"  
  16.         android:layout_marginTop="100dp"/>  
  17.     <TextView  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content"  
  20.         android:id="@+id/textView3"  
  21.         android:textStyle="bold"  
  22.         android:layout_marginTop="200dp"/>  
  23.     <com.optimizecode.Second  
  24.         android:layout_width="wrap_content"  
  25.         android:layout_height="wrap_content"  
  26.         android:id="@+id/progressBar"  
  27.         android:visibility="gone"></com.optimizecode.Second>  
  28. </RelativeLayout> 
Step 4
 
Create another XML file, "second.xml" with the following.
 
This XML file will be inflated during runtime when you click on the button. In this file, I have taken the progress bar that will show processing inside the Activity. You can use anything depending on your needs.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.     <ProgressBar  
  7.         android:id="@+id/progressBar1"  
  8.         style="?android:attr/progressBarStyleLarge"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_centerInParent="true"  
  12.       />  
  13. </RelativeLayout> 
Step 5
 
Create another Java file named MainActivity with the following.
 
In this class, I have created the id of the button and set the button on its clicklistener and when the user clicks on the button the next Activity will be called. In the next Activity, a progress bar will inflate by calling another class.
  1. package com.optimizecode;  
  2.    
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.content.Intent;  
  6. import android.view.Menu;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10.    
  11. public class MainActivity extends Activity {  
  12.    
  13.        @Override  
  14.        protected void onCreate(Bundle savedInstanceState) {  
  15.               super.onCreate(savedInstanceState);  
  16.               setContentView(R.layout.activity_main);  
  17.        Button button=(Button)findViewById(R.id.button1);  
  18.        button.setOnClickListener(new OnClickListener() {  
  19.    
  20.               @Override  
  21.               public void onClick(View arg0) {  
  22.                      // TODO Auto-generated method stub  
  23.               Intent intent=new Intent(MainActivity.this,First.class);       
  24.               startActivity(intent);  
  25.               }  
  26.        });  
  27.        }  
  28.    
  29.        @Override  
  30.        public boolean onCreateOptionsMenu(Menu menu) {  
  31.               // Inflate the menu; this adds items to the action bar if it is present.  
  32.               getMenuInflater().inflate(R.menu.main, menu);  
  33.               return true;  
  34.        }   
Step 6
 
Create a Java class file named "First" with the following.
  1. package com.optimizecode;  
  2.    
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.io.InputStreamReader;  
  7.    
  8. import org.apache.http.HttpEntity;  
  9. import org.apache.http.HttpResponse;  
  10. import org.apache.http.StatusLine;  
  11. import org.apache.http.client.ClientProtocolException;  
  12. import org.apache.http.client.HttpClient;  
  13. import org.apache.http.client.methods.HttpGet;  
  14. import org.apache.http.impl.client.DefaultHttpClient;  
  15. import org.json.JSONArray;  
  16. import org.json.JSONException;  
  17. import org.json.JSONObject;  
  18.    
  19. import android.app.Activity;  
  20. import android.content.Context;  
  21. import android.os.AsyncTask;  
  22. import android.os.Bundle;  
  23. import android.os.StrictMode;  
  24. import android.util.AttributeSet;  
  25. import android.util.Log;  
  26. import android.view.Menu;  
  27. import android.view.View;  
  28. import android.widget.TextView;  
  29.    
  30. public class First extends Activity {  
  31.         
  32.           TextView textView1,textView2,textView3;  
  33.            String str1,str2;  
  34.            Second progressBar;  
  35.            String name1,name2,name3;  
  36.            String experiencePoints1,experiencePoints2,experiencePoints3;  
  37.            String vehicleColor,vehicleType,fuel;  
  38.            private static String url = "http://docs.blackberry.com/sampledata.json";  
  39.         
  40. public void onCreate(Bundle savedInstanceState)  
  41. {  
  42.        super.onCreate(savedInstanceState);  
  43.        setContentView(R.layout.first);  
  44.        textView1=(TextView)findViewById(R.id.textView1);  
  45.     textView2=(TextView)findViewById(R.id.textView2);  
  46.     textView3=(TextView)findViewById(R.id.textView3);  
  47.    
  48.      
  49.     progressBar=(Second)findViewById(R.id.progressBar);  
  50.        progressBar.setVisibility(View.VISIBLE);  
  51.    }   
Step 7
 
In this class file, we create the layout without extending the Activity class. We extend the RelativeLayout class and create the object of LayoutInflator by calling getSystemServices(). Now we call inflate() that inflates the layout.
 
Create a Java class file "Second" with the following.
  1. package com.optimizecode;  
  2.    
  3. import java.text.AttributedCharacterIterator.Attribute;  
  4.    
  5. import android.content.Context;  
  6. import android.util.AttributeSet;  
  7. import android.view.LayoutInflater;  
  8. import android.view.MotionEvent;  
  9. import android.widget.RelativeLayout;  
  10.    
  11. public class Second extends RelativeLayout {  
  12.    
  13.        public Second(Context context,AttributeSet attr) {  
  14.               super(context,attr);  
  15.               // TODO Auto-generated constructor stub  
  16.         
  17.         
  18.        LayoutInflater inflater=(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);  
  19.    if(inflater!=null)  
  20.        inflater.inflate(R.layout.second,this);  
  21.        }  
  22.        @Override  
  23.        public boolean onTouchEvent(MotionEvent event) {  
  24.               // TODO Auto-generated method stub  
  25.               return true;  
  26.        }  
  27. }  


Similar Articles