RAVITEJA SATYAVADA
RAVITEJA SATYAVADA

Reputation: 2571

android activity method call

I am new to android development. Today I tried something that I created a project called androidPickerviws.

In that project I created two activities and in Timepicker.java activity there is method called displayTime(hours,minute);. Now my doubt is can I use this method displayTime() in Datepicker.java activity. If so, how can I do it..

Here iam providing my Timepicker.java activity code

package picker.view;

// imports...


public class Timepicker extends Activity{
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    LinearLayout linearlayout=new LinearLayout(this);
    linearlayout.setOrientation(LinearLayout.VERTICAL);
    LinearLayout.LayoutParams layoutparams=new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    LayoutParams params=new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);

    final TimePicker timepicker=new TimePicker(this);
    linearlayout.addView(timepicker, params);   

    Button ok=new Button(this);
    ok.setText("OK");
    linearlayout.addView(ok, params);
    addContentView(linearlayout,layoutparams);

    ok.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            final int hours=timepicker.getCurrentHour();
            final int minute=timepicker.getCurrentMinute();
            displayTime(hours,minute);
        }

        private void displayTime(int hours, int minute) {
            if(hours==0)
                Toast.makeText(getBaseContext(), "Time saved is 12 : "+minute+" AM", Toast.LENGTH_SHORT).show();
            else if(hours<12)
                Toast.makeText(getBaseContext(), "Time saved is "+hours+" : "+minute+" AM", Toast.LENGTH_SHORT).show();
            else if(hours==12)
                Toast.makeText(getBaseContext(), "Time saved is "+hours+" : "+minute+" PM", Toast.LENGTH_SHORT).show();
            else 
                Toast.makeText(getBaseContext(), "Time saved is "+(hours-12)+" : "+minute+" PM", Toast.LENGTH_SHORT).show();

        }
    });
}
   }

Upvotes: 0

Views: 270

Answers (2)

RAVITEJA SATYAVADA
RAVITEJA SATYAVADA

Reputation: 2571

I got the answer we can call like that but we have to mark that method as public and should placed output side of all the methods but inside of the class. The modified code is follows

 package picker.view;

 import android.app.Activity;
 import android.os.Bundle;
 import android.view.View;
 import android.view.ViewGroup.LayoutParams;
 import android.widget.LinearLayout;
 import android.widget.TimePicker;
 import android.widget.Button;
 import android.widget.Toast;


 public class Timepicker extends Activity{
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
/*  setContentView(R.layout.timepicker);

    final TimePicker timepicker=(TimePicker) findViewById(R.id.timePicker1);    

    Button ok=(Button) findViewById(R.id.button1);*/

    LinearLayout linearlayout=new LinearLayout(this);
    linearlayout.setOrientation(LinearLayout.VERTICAL);
    LinearLayout.LayoutParams layoutparams=new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    LayoutParams params=new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);

    final TimePicker timepicker=new TimePicker(this);
    linearlayout.addView(timepicker, params);   

    Button ok=new Button(this);
    ok.setText("OK");
    linearlayout.addView(ok, params);
    addContentView(linearlayout,layoutparams);

    ok.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            final int hours=timepicker.getCurrentHour();
            final int minute=timepicker.getCurrentMinute();
            displayTime(hours,minute);
        }
    });
}
public void displayTime(int hours, int minute) {
    if(hours==0)
        Toast.makeText(getBaseContext(), "Time saved is 12 : "+minute+" AM", Toast.LENGTH_SHORT).show();
    else if(hours<12)
        Toast.makeText(getBaseContext(), "Time saved is "+hours+" : "+minute+" AM", Toast.LENGTH_SHORT).show();
    else if(hours==12)
        Toast.makeText(getBaseContext(), "Time saved is "+hours+" : "+minute+" PM", Toast.LENGTH_SHORT).show();
    else 
        Toast.makeText(getBaseContext(), "Time saved is "+(hours-12)+" : "+minute+" PM", Toast.LENGTH_SHORT).show();

}

}

Upvotes: 2

caz_fr
caz_fr

Reputation: 11

You could put your code code in a static method inside a "Utils" class:

public class Utils {

private static void displayTime(Context context, int hours, int minute) {
   if(hours==0) {
        Toast.makeText(context, "Time saved is 12 : "+minute+" AM", Toast.LENGTH_SHORT).show();
   }
   else if(hours<12) {
        Toast.makeText(context, "Time saved is "+hours+" : "+minute+" AM", Toast.LENGTH_SHORT).show();
   }
   else if(hours==12) {
        Toast.makeText(context, "Time saved is "+hours+" : "+minute+" PM", Toast.LENGTH_SHORT).show();
   }
   else {
        Toast.makeText(context, "Time saved is "+(hours-12)+" : "+minute+" PM", Toast.LENGTH_SHORT).show();
   }    
}
}

And then call it in your activities with:

Utils.displayTime(this, 12, 45);

Upvotes: 1

Related Questions