Tuesday, June 27, 2017

[Android] Retrofit library




compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'

public class ApiClient {
    public static final String BASE_URL = "your URL";
    private static Retrofit retrofit = null;

    public static Retrofit getClient() {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

public interface ApiInterface {
    @GET("Retrofit.php")
    Call<List<Message>> getInbox();
}

public class Message {
    public int id;
    public String msg;
    public String url;

    public Message(int id, String msg, String url) {
        this.id = id;
        this.msg = msg;
        this.url = url;
    }
}

public class MainActivity extends AppCompatActivity {
    String TAG = "MainActivity";
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
        Call<List<Message>> call = apiService.getInbox();
        call.enqueue(new Callback<List<Message>>() {
            @Override
            public void onResponse(Call<List<Message>> call, Response<List<Message>> response) {
                // TODO - avoid looping
                List<Message> messageList = response.body();
                for (Message message : messageList) {
                    Log.d(TAG, new Gson().toJson(message));
                }
            }
            @Override
            public void onFailure(Call<List<Message>> call, Throwable t) {
                Toast.makeText(getApplicationContext(), "Unable to fetch json: " + t.getMessage(), Toast.LENGTH_LONG).show();
            }
        });
    }
}
    }
}

Tuesday, June 20, 2017

Tuesday, June 13, 2017

[Android Game]Shoot Asteroids





- Easy game move player with sensor to shoot Asteroids
- Require: device support GRAVITY SENSOR, ROTATION VECTOR SENSOR
- Enjoy this game

Download app from Google Play

Friday, June 9, 2017

[Android application] Object with Kotlin android




class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main
 var user = User("Kotlin", "Kotlin@yahoo.com"
 var msg: String = "name:" + user.name + "\n" + user.email 
 var tv = findViewById(R.id.tv) as TextView 
 tv.text = msg

    }

    inner class User(var name: String, var email: String)
}

[Android application] Compare code between Kotlin and Java





class MainActivity : AppCompatActivity() {
    // kotlin code 
 var a: Int = 10 
 var b: Int = 5 
 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main
 var btn_hello = findViewById(R.id.btn_hello) as Button 
 var c = a + b 
 var msg: String = "Kotlin" 
 btn_hello.setOnClickListener { 
 view -> 
 Toast.makeText(applicationContext, msg, Toast.LENGTH_SHORT).show() 
        } 
    }
}

[Android] Install Kotlin language for Android Studio


class MainActivity : AppCompatActivity() {
 private var TAG: String = "MainActivity" 
 private var a: Int = 0 
 private var b: Int = 0
 
 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState) 
 a = 10 
 b = 25 
 Log.d(TAG, "result:" + (a + b)) 
 // result:35 
  }
}

Monday, June 5, 2017

[Android Application] Money Manager

- easy manager your money with calendar
- simple add event 
- statistic money pay for day
- statistic money pay for month
- share event
- use for offline
- all in one screen
- enjoy this application
MoneyManager
Download app from Google Play