Skip to content

Instantly share code, notes, and snippets.

@sarahholderness
Created May 25, 2017 16:44
Show Gist options
  • Save sarahholderness/300f5ec1d7dd710343f9a7f1ce95dbb0 to your computer and use it in GitHub Desktop.
Save sarahholderness/300f5ec1d7dd710343f9a7f1ce95dbb0 to your computer and use it in GitHub Desktop.
MainActivity in Java compared to in Kotlin.
package com.codeschool.candycoded;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = (TextView)this.findViewById(
R.id.text_view_title);
textView.setText("Welcome to Candy Coded!");
ArrayList<String> candy_list = new ArrayList<String>(
Arrays.asList("La La Lollipop", "Shooting Stars", "Tart Pop", "Citrus Stick",
"Sour Drops", "Super Pop", "La La Lollipop", "Shooting Stars",
"Tart Pop", "Citrus Stick", "Sour Drops", "Super Pop",
"La La Lollipop", "Shooting Stars", "Tart Pop"));
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this,
R.layout.list_item_candy,
R.id.text_view_candy,
candy_list);
ListView listView = (ListView)this.findViewById(
R.id.list_view_candy);
listView.setAdapter(adapter);
Context context = this;
String text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast toast = Toast.makeText(MainActivity.this, ""+i, Toast.LENGTH_SHORT);
toast.show();
}
});
}
}
package com.codeschool.candykotlin
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.TextView
import android.widget.Toast
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val textView: TextView = this?.findViewById(R.id.text_view_title) as TextView
textView?.setText(R.string.products_title)
val candy_list = arrayOf("La La Lollipop", "Shooting Stars", "Tart Pop", "Citrus Stick",
"Sour Drops", "Super Pop", "La La Lollipop", "Shooting Stars",
"Tart Pop", "Citrus Stick", "Sour Drops", "Super Pop",
"La La Lollipop", "Shooting Stars", "Tart Pop")
val adapter: ArrayAdapter<String> = ArrayAdapter<String>(this, R.layout.list_item_candy,
R.id.text_view_candy, candy_list)
val listView:ListView = this?.findViewById(R.id.list_view_candy) as ListView
listView?.adapter = adapter
val context = this
val text = "Hello toast!"
val duration = Toast.LENGTH_SHORT
val toast = Toast.makeText(context, text, duration)
toast.show()
listView?.setOnItemClickListener { parent, view, position, id ->
Toast.makeText(this, position, Toast.LENGTH_SHORT).show()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment