Create and call function in android activity from another activity programming file

Create class object of another class and access method in Activity programmatically.

Its very easy to create and call function through another java programming class because there android programming is supports OOP( OBJECT ORIENTED PROGRAMMING ) concepts so you can easily create anther class file and access its methods by using objects of that particular class file. So here is the complete step by step tutorial for Create and call function in android activity from another programming file.

android-project-download-code-button

How to Create and call function in android activity from another programming file .

Code for MainActivity.java file.

 package com.android_examples.com.functioncallviaanotherclass;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.android_examples.com.functioncallviaanotherclass.FunctionCalling;
public class MainActivity extends Activity {

 Button FNCall;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 FNCall = (Button)findViewById(R.id.button1);
 
 FNCall.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 //Creating function calling class object.
 FunctionCalling FN = new FunctionCalling(FunctionCalling.context);
 
 //Calling function from another class and storing string return value in string b.
 String b = FN.CallViaOtherClassFunction();
 
 // Printing b value on activity screen using toast message.
 Toast.makeText(MainActivity.this, b , Toast.LENGTH_LONG).show();
 
 }
 }); 
 }
}

Code for FunctionCalling.java file.

package com.android_examples.com.functioncallviaanotherclass;
import android.content.Context;

public class FunctionCalling {

 static Context context;
 public FunctionCalling(Context c) {
 context = c;

 }
 public String CallViaOtherClassFunction()
 {
 
 String a = "Value Called from another class";
 return a;
 
 }
 
}

 

Code for activity_main.xml layout file.

 <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="com.android_examples.com.functioncallviaanotherclass.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Click Here to call function via another class"
 />

</RelativeLayout>

Screenshot:

Create and call function in android activity from another programming file

Click Here To Download Create and call function in android activity from another programming file project.

Leave a Reply

Your email address will not be published. Required fields are marked *