How to Change Android SDK Path in Android Studio


From Android Studio 1.0.1 and above

Go to
  1. File -> project Structure into Project Structure
  2. Left -> SDK Location
  3. SDK location select Android SDK location (old version use Press +, add another sdk)
Or

1. Close current Project (File->Close project)
       You'll get a Welcome to Android Studio Dialog. In that:
2. Click on Configure -> Project Defaults -> Project Structure
3. Click on SDK Location in the left column
4. Put the path to the Android SDK in "Android SDK location" field.
     (Example SDK location: C:\android-sdk; I have sub-folders like add-ons, platforms etc under C:\android-sdk)
5. Click OK to save changes





Java Functional Programming: Understanding Predicates

As java 8 introduces the predicate functions, but we may wonder where to use those true/false returning function in our daily programming life. The answer is we can use this in the following scenario:
1. when we have to evaluate the objects from the same class which will result in the true or false.
2. to filter the objects by using some of its variables etc.

lets hack Predicate with some examples:

A. Examining if the object is the intance of the particular class or not: 

/** * Created by yubraj on 1/26/16. */

public class MyClass {
    Predicate<MyClass> predicate1 = (MyClass myclass) -> this.equals(myclass);
    Predicate<MyClass> predicate2 =  this::equals;
    String text1 = "yubraj", text2 = "pokharel";
    public MyClass(String text1, String text2) {
        this.text1 = text1;        this.text2 = text2;    }

    @Override    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        MyClass myClass = (MyClass) o;
        if (!text1.equals(myClass.text1)) return false;
        return text2.equals(myClass.text2);
    }

    @Override    public int hashCode() {
        int result = text1.hashCode();    
        result = 31 * result + text2.hashCode();
        return result;    }

    public void myMethod(MyClass cl){
        System.out.println("Intance equals : " + predicate2.test(cl));    }

    public static void main(String[] args) {
        MyClass mc = new MyClass("sudhan", "pokharel");
        MyClass mc2 = new MyClass("yubraj", "pokharel");
        mc.myMethod(mc);
        System.out.println("-----------------------------------");
        mc2.myMethod(mc2);
    }

}

Output:
true
true
//because both are the intance of the similar object.



B. Filtering the objects

for male employee who are older then 21 years

lets add this in the Employee Class

public static Predicate<Employee> isAdultMale() {
    return p -> p.getAge() > 21 && p.getGender().equalsIgnoreCase("M");
}

then we can access all the employee who are Male and also more then 21 years by passing method as the variable eg:
public static void main(String[] args){
        Employee e1 = new Employee(1,23,"M","Rick","Beethovan");
        Employee e2 = new Employee(2,13,"F","Martina","Hengis");
        Employee e3 = new Employee(3,43,"M","Ricky","Martin");
        Employee e4 = new Employee(4,26,"M","Jon","Lowman");
        Employee e5 = new Employee(5,19,"F","Cristine","Maria");
        Employee e6 = new Employee(6,15,"M","David","Feezor");
        Employee e7 = new Employee(7,68,"F","Melissa","Roy");
        Employee e8 = new Employee(8,79,"M","Alex","Gussin");
        Employee e9 = new Employee(9,15,"F","Neetu","Singh");
        Employee e10 = new Employee(10,45,"M","Naveen","Jain");
         
        List<Employee> employees = new ArrayList<Employee>();
        employees.addAll(Arrays.asList(new Employee[]{e1,e2,e3,e4,e5,e6,e7,e8,e9,e10}));
                
        System.out.println(filterEmployees(employees, isAdultMale()));
}

Happy Coding :)

References: howtodoinjava, oracle

1. For this example I am using php server side code to gather the data from the data base and return it as a Json format. For this we can use the buid in code for php i.e.
json_encode($data);

for eg: (using Codeigniter model)

                $check_sql = "Select * from user where about_status = 1"; //status is flag for the active user
$check = $ci->db->query($check_sql);
$row = $check->result();

echo json_encode($row);

which gives output as:

{
"records": [
  {
    "Name" : "Yubraj Pokharel",
    "City" : "Kathmandu",
    "Country" : "NP"
  },
  {
    "Name" : "Chitra Shrestha",
    "City" : "Pokhara",
    "Country" : "NP"
  },
  {
    "Name" : "Prayag Upd",
    "City" : "California",
    "Country" : "US"
  },
  {
    "Name" : "Sudhan Pokharel",
    "City" : "Nepalgunj",
    "Country" : "NP"
  },
  {
    "Name" : "Mr Tom Cruise",
    "City" : "California",
    "Country" : "US"
  }
]
}

2. calling it from the angular page:

<html>
<head>
<title>Json Data</title>
        call angular js here
</head>
<body>
<div class="container">
<div class="row">
<div class="col-lg-12" ng-app="myApp" ng-controller="customersCtrl"> 
<table class="table">
<tr>
<th>Name</th>
<th>country</th>
</tr>
<tr  ng-repeat="x in names">
     <td>{{ x.Name }} </td>
     <td>{{ x.Country }}</td>
   </tr>
</table>
</div>
</div>
</div>

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
    $http.get("http://localhost/angular/news.php")
    .success(function(response) {$scope.names = response.records;});
});
</script>
</body>
</html>

3. Here it is done enjoy happy coding :)