I'm new to android development and Java programming. I am trying to create a separate class without a new user interface. Supposed I have a main activity wherein if I click a button it will initiate a class and call the method in it and display the text on the main activity.
Now I have create created a Main activity and a class which named as BluetoothOn: Please take a look at my code:
This is the main activity where the enablemyBluetooth method is the button click
public class MainActivity{
....
....
public void enablemyBluetooth(View view){
BluetoothOn ble = new BluetoothOn();
ble.initializeBlue();
}
}
Now I create a class wherein I planned to do all the processes here without intervening on the Main activity of the program, like it is running on the background.. here in my code I just want to change the Textview but when I run it on my android it states that unfortunately the app stopped.
public class BluetoothOn {
private void initializeBlue(){
textView1 =(TextView)findViewById(R.id.textView1);
textView1.setText(BleisOn);
}
}
Please take note that I have compiled this program with no error/ all variables are declared. I have tried to create the Bluetooth class as private on the Main and it works. I just want to create it as another class on the package for organizing my code.
Is it really necessary that I have to create another activity if I want to create a separate class? Can anyone give me hints what should I do or declare? Please help thank you.
Update: thank you for all your answers, unfortunately I can't make this simple process to work on another class... I'm quite new to Java and still learning my way.. anyways here is what I encountered.
I followed the post suggested by unohu however it provides me an error on the Main that "The method to initializeBlue() in the type BluetoothOn is not applicable for the arguments.
Here is a more complete code of the classes:
Main activity:
public class MainActivity extends Activity {
public void enableBluetooth (View view){
BluetoothOn ble = new BluetoothOn();
ble.initializeBlue(); // error on this part
}
}
BluetoothOn class:
public class BluetoothOn{
public TextView textView1;
public void initializeBlue(View myView){
String BleisOn = "connect me now";
textView1 = (TextView)myView.findViewById(R.id.textView1);
textView1.setText(BleisOn);
}
}
note: I got a typo on the initial post, I declared the initialize as private but it should be public. I'm attempting to try luisdurazoa suggestion however I'm not familiar on Interface..