Skip to content

Instantly share code, notes, and snippets.

View the-dagger's full-sized avatar
🚀
To Boldly Go Where No Man Has Gone Before

Harshit Dwivedi the-dagger

🚀
To Boldly Go Where No Man Has Gone Before
View GitHub Profile
@the-dagger
the-dagger / MainActivity.java
Created January 26, 2017 19:41
PopularMovies
package com.example.news.androidp1;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.AsyncTaskLoader;
import android.support.v4.content.Loader;
import android.support.v4.text.TextUtilsCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
@the-dagger
the-dagger / gist:8ecd685ee18095c89a19be0a1a12c382
Created January 31, 2017 14:41 — forked from yqritc/gist:ccca77dc42f2364777e1
Equal column spacing for Android RecyclerView GridLayoutManager by using custom ItemDecoration

ItemOffsetDecoration

public class ItemOffsetDecoration extends RecyclerView.ItemDecoration {

    private int mItemOffset;

    public ItemOffsetDecoration(int itemOffset) {
        mItemOffset = itemOffset;
    }
try {
URL url = new URL("https://jsonplaceholder.typicode.com/posts");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setReadTimeout(3000);
httpURLConnection.setConnectTimeout(5000);
httpURLConnection.connect();
if (httpURLConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new IOException("HTTP error code" + httpURLConnection.getResponseCode());
@the-dagger
the-dagger / MainActivity.java
Last active December 28, 2017 13:25
Kotlin synthetic binding blog
class MainActivity extends AppCompatActivity{
TextView studentName, studentSubject, studentCenter;
//Since I might need them later on outside of my onCreate method
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
studentName = findViewById(R.id.studentName),
import kotlinx.android.synthetic.main.activity_main.*
class KotlinActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
studentName.text = "Harshit" //That's it!
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/studentName"
android:padding="8dp"
android:textSize="24sp"
class CourseViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {
fun bindView (course: Course) {
itemView.studentName.text = s.name
itemView.studentSubject.text = s.subject
itemView.studentCenter.text = s.center
}
}
public void bind(final Student student) {
studentName.setText(student.getName());
studentSubject.setText(student.getBatch());
Picasso.with(itemView.getContext()).load(student.getImage()).into(studentImage);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Student's name : " + student.getName(), Toast.LENGTH_SHORT).show();
}
});
fun bind(student: Student) {
with(student) {
studentName.text = name //the property in Student called directly instead of calling student.name or student.getName()
studentSubject.text = subject
Picasso.with(itemView.context).load(image).into(studentImage)
}
itemView.setOnClickListener { Toast.makeText(itemView.context, "Student's name : " + student.name, Toast.LENGTH_SHORT).show() }
}
fun startIntent(s : Student){
val intent = Intent(this,HomeActivity::class.java)
with(intent){
putExtra(NAME,name) // No need to use intent object anymore
putExtra(SUBJECT,subject)
putExtra(IMAGE,image)
}
startActivity(intent)
}