How to call a function using a String in Android and also pass parameters along with it.

By | December 5, 2012

Hi all …

Today’s post is all about calling functions using Simple Strings.

Here we will do two things.

1. Call a function without parameters.
2. Call a function with parameters.

String to functions

String to functions

This is the class which contains the functions that we are going to call using “simple Strings”.


package com.coderzheaven.Stringtofunction;

public class Test {

	public String functionWithParams(String p1, String p2) {
		return p1 + ", " + p2;
	}

	public String noParamsFunction() {
		return "Return from noParamsFunction";
	}

}

Now this is the main activity that calls these functions.

package com.coderzheaven.Stringtofunction;

import java.lang.reflect.Method;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

	String TAG = getClass().getSimpleName();
	TextView tv;
	
	@SuppressWarnings("rawtypes")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		final Object paramsObj[] = { "Hello", "CoderzHeaven" };
		final Class[] class_params = { String.class, String.class };
		final String class_name = getPackageName() + ".Test";

		Button b1 = (Button) findViewById(R.id.button1);
		b1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				/** This is the function without parameters in Test.java **/
				callFunctionWithoutParameter(class_name, "noParamsFunction");
			}
		});

		Button b2 = (Button) findViewById(R.id.button2);
		b2.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				/** This is the function with two parameters in Test.java **/
				callFunctionWithParams(class_name, "functionWithParams",
						class_params, paramsObj);
			}
		});
		
		tv = (TextView)findViewById(R.id.tv);

	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	void callFunctionWithoutParameter(String class_name, String function_name) {
		try {
			Class c = Class.forName(class_name);
			Method m = c.getMethod(function_name);
			Object iClass = c.newInstance();
			String p = (String) m.invoke(iClass);
			Log.i(TAG, p);
			tv.setText("Result (Without Prameters) : " + p);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	void callFunctionWithParams(String aClass, String aMethod, Class[] params,
			Object[] args) {
		try {
			Class c = Class.forName(aClass);
			Method m = c.getDeclaredMethod(aMethod, params);
			Object i = c.newInstance();
			String p = (String) m.invoke(i, args);
			Log.i(TAG, p);
			tv.setText("Result (With Prameters) : " + p);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

Now this is the layout that the mainactivity uses.

<LinearLayout 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:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Call function without parameters" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Call function with parameters" />

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="@dimen/padding_medium"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />

</LinearLayout>

Leave a Reply

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