0

I have setup my own android library and now want to use the methods within..

For some reason I am having issues understanding how this works, looking at other questions/ internet has not helped.

In my library class i have the following.

    public void testMethod(){

    Toast s = Toast.makeText(this, "test!", Toast.LENGTH_SHORT).show();
}

I want to be able to reference this in my main for example, how do i do this?

I have found several links which do look at this however it has not helped as it is different to what I want to do. Am I being very stupid and missing something?

I am guessing an interface needs to be setup on my Main Class? I am not sure.

By the way I have already setup the library and have referenced in manifest etc..

7
  • Does it not work if you make an object of the class in which the method exists and then you call object_name.testMethod(); Commented Feb 19, 2015 at 13:19
  • sorry im a noobie when it comes to this... So creating an instance of the class as a object then referencing that? Commented Feb 19, 2015 at 13:22
  • Your class should be public..Ensure you have imported the package. Commented Feb 19, 2015 at 13:23
  • @Ranjith yes I have done that.. just wasnt sure exactly how to reference as I have many many methods to implement (because it is a library) Commented Feb 19, 2015 at 13:26
  • Sidenote: You aren't calling .show() on the toast, so it isn't going to display anything even if you do succeed in calling the method Commented Feb 19, 2015 at 13:28

1 Answer 1

2

You should import to your project the package name you used to create your library, then, create an object of the class you want to use from your library, and finally, call the method.

Supose your library package name it's: "com.mylibrary" Your library class it's called: "MyClass" Your method it's called: "MyMethod"

In Your Project you should do:

//Import Your Library Package
import com.mylibrary;

//Instantiate The Library Class
MyClass mytest = new MyClass();

//Call The Library Class Method You Want To Use.
mytest.MyMethod();
Sign up to request clarification or add additional context in comments.

11 Comments

just tried this, //Call The Library Class Method You Want To Use. mytest.MyMethod(); doesnt work, says cannot resolve symbol?
Did you check that your project has reference to your library, if you're using eclipse, right click on your project and select properties, go to Android, at the botton of the android platforms you'll see a box where you can reference libraries, add your own there and apply.
I am using android studio and have made sure of this several times... I followed link
Ok, then, can you post an image showing the "Cannot Resolve Symbol" and the part where you call the method? and also, you can check the tips given by the IDE when there is an error or a warning, commonly they gives you options to fix them.
Ok, great, enjoy your development and have fun.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.