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. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.view.Menu;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;
  9. public class MainActivity extends Activity {
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. Button button=(Button)findViewById(R.id.button1);
  15. button.setOnClickListener(new OnClickListener() {
  16. @Override
  17. public void onClick(View arg0) {
  18. // TODO Auto-generated method stub
  19. Intent intent=new Intent(MainActivity.this,First.class);
  20. startActivity(intent);
  21. }
  22. });
  23. }
  24. @Override
  25. public boolean onCreateOptionsMenu(Menu menu) {
  26. // Inflate the menu; this adds items to the action bar if it is present.
  27. getMenuInflater().inflate(R.menu.main, menu);
  28. return true;
  29. }
  30. }
Step 6
Create a Java class file named "First" with the following.
  1. package com.optimizecode;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import org.apache.http.HttpEntity;
  7. import org.apache.http.HttpResponse;
  8. import org.apache.http.StatusLine;
  9. import org.apache.http.client.ClientProtocolException;
  10. import org.apache.http.client.HttpClient;
  11. import org.apache.http.client.methods.HttpGet;
  12. import org.apache.http.impl.client.DefaultHttpClient;
  13. import org.json.JSONArray;
  14. import org.json.JSONException;
  15. import org.json.JSONObject;
  16. import android.app.Activity;
  17. import android.content.Context;
  18. import android.os.AsyncTask;
  19. import android.os.Bundle;
  20. import android.os.StrictMode;
  21. import android.util.AttributeSet;
  22. import android.util.Log;
  23. import android.view.Menu;
  24. import android.view.View;
  25. import android.widget.TextView;
  26. public class First extends Activity {
  27. TextView textView1,textView2,textView3;
  28. String str1,str2;
  29. Second progressBar;
  30. String name1,name2,name3;
  31. String experiencePoints1,experiencePoints2,experiencePoints3;
  32. String vehicleColor,vehicleType,fuel;
  33. private static String url = "http://docs.blackberry.com/sampledata.json";
  34. public void onCreate(Bundle savedInstanceState)
  35. {
  36. super.onCreate(savedInstanceState);
  37. setContentView(R.layout.first);
  38. textView1=(TextView)findViewById(R.id.textView1);
  39. textView2=(TextView)findViewById(R.id.textView2);
  40. textView3=(TextView)findViewById(R.id.textView3);
  41. progressBar=(Second)findViewById(R.id.progressBar);
  42. progressBar.setVisibility(View.VISIBLE);
  43. }
  44. }
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. import java.text.AttributedCharacterIterator.Attribute;
  3. import android.content.Context;
  4. import android.util.AttributeSet;
  5. import android.view.LayoutInflater;
  6. import android.view.MotionEvent;
  7. import android.widget.RelativeLayout;
  8. public class Second extends RelativeLayout {
  9. public Second(Context context,AttributeSet attr) {
  10. super(context,attr);
  11. // TODO Auto-generated constructor stub
  12. LayoutInflater inflater=(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
  13. if(inflater!=null)
  14. inflater.inflate(R.layout.second,this);
  15. }
  16. @Override
  17. public boolean onTouchEvent(MotionEvent event) {
  18. // TODO Auto-generated method stub
  19. return true;
  20. }
  21. }