Tuesday, October 30, 2018

Android - Setting onClickListener for Multiple Buttons

It is very basic practice to implement onClickListener() for more than one views. But making it easier to implement and handle is different task. For some reasons, many people try to implement mulitiple onClickListener() for individual views. But you can implement interface to handle it easily. You don't have to write new onClickListener for Every Button. Just Implement View.OnClickLister to your Activity/Fragment. It will implement new Method called onClick() for handling onClick Event s for Button,TextView etc. 

You Just Simply have to Follow these steps for making it easy.


Step 1 : Implement OnClickListener() interface in your Activity/Fragment

public class MainActivity extends AppCompatActivity implements 
View.OnClickListener {
}
After implementing interface to your Activity/Fragment it will ask you to implement its method onClick().

Step 2 : Implement onClick() method in your Activity/Fragment

public class MainActivity extends AppCompatActivity implements 
View.OnClickListener {
       
    @Override
    public void onClick(View v) {
     // handling onClick Events
    }
}
Here, you can see that it added onClick() method to your class. It is default method called when onClick is called from view. It means that when user will perform click on your view, your class will redirect call to this method.

Step 3 : Implement OnClickListener() For Buttons

public class MainActivity extends AppCompatActivity implements 
View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_layout);

        Button one = (Button) findViewById(R.id.buttonOne);
        one.setOnClickListener(this); // calling onClick() method
        Button two = (Button) findViewById(R.id.buttonTwo);
        two.setOnClickListener(this); 
        Button three = (Button) findViewById(R.id.buttonThree);
        three.setOnClickListener(this); 
    }

    @Override
    public void onClick(View v) {
    }
}
Here we are adding this keyword to the onClickListener() method. It redirects call to the onClick() method. If you don't use `this` keyword here, your call will not be redirected to onClick() method when user performs click. 

Note that : If you haven't implemented View.OnClickListener interface to your class it will show an error when you implement 'this' keyword to button's onClickListener() method.

Step 4 : Find Buttons by ID and Implement Your Code for button click.

public class MainActivity extends AppCompatActivity implements 
View.OnClickListener {
       
    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.buttonOne:
                // code for button when user clicks buttonOne.
                break;

            case R.id.buttonTwo:
                // do your code
                break;

            case R.id.buttonThree:
                // do your code
                break;

            default:
                break;
        }
    }
}
Here, we are saperating all the views by its ID. So we can implement different tasks for diferent views. It will allow you to handle all the onClick calls from one place and it will look better if you manage your code like this. 

Source: androidacademic.blogspot.com

Monday, October 29, 2018

Handle Date/Time in Java (Android)

import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class Main {
public static void main(String[] args) throws Exception {
Date today = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String dateToStr = format.format(today);
System.out.println(dateToStr);

System.out.println("Input time:");
Scanner scanner = new Scanner(System.in);
String inputTime = scanner.nextLine();

// String time = "13:10";

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); // or "hh:mm" for 12 hour format
Date date = sdf.parse(inputTime);

int hour = date.getHours(); // int
int min = date.getMinutes(); // int

System.out.println("Time input: " + hour + ":" + min);
}

}