// the immutable parameters passed to the constructor are not member variables!, they need to be explicitly created as below
// parent class takes in the passed values to the child class.
class MyListAdapter(ctx: Context, res: Int) : ArrayAdapter<String>(ctx, res)
val context: Context
val resource: Int
init {
context = ctx
What are companion objects in Kotlin?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this is a context | |
AlertDialog.Builder builder = new AlertDialog.Builder(this); | |
// Check other builder methods like setCancelable, setIcon.. | |
builder.setTitle("Set Title"); | |
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog, int id) { | |
// do something | |
} | |
}); | |
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// starting an activity from another activity | |
// this is a Activity context | |
Intent intent = new Intent(this, AnotherActivity.java); | |
intent.putExtra(Intent.EXTRA_TEXT, "some text"); | |
intent.putExtra("id", 4); | |
startActivity(intent); | |
// Receiving intent in AnotherActivity | |
Bundle extras = getIntent().getExtras(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// if you are in an activity | |
LayoutInflater inflater = getLayoutInflater(); | |
// If you have the context | |
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
// or in a cleaner way, from() uses system service interally so its the same inflater | |
LayoutInflater inflater = LayoutInflater.from(context); | |
// inflating a view | |
// if you need to attach the inflated to rootView, returned view will be rootView. | |
// 3rd parameter is attach_to_root |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://stackoverflow.com/questions/10696986/how-to-set-the-part-of-the-text-view-is-clickable | |
SpannableString ss = new SpannableString("Android is a Software stack"); | |
ClickableSpan clickableSpan = new ClickableSpan() { | |
@Override | |
public void onClick(View textView) { | |
startActivity(new Intent(MyActivity.this, NextActivity.class)); | |
} | |
}; | |
// 0 is the start index and 7 is the end index | |
ss.setSpan(clickableSpan, 0, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
A ViewPager
is a ViewGroup
that allows the user to flip left and right through pages of data. You supply an implementation of a PagerAdapter
to generate the pages that the view shows.
Create a custom Pager Adapter
// override 4 methods as shown below
class MyPagerAdapter: PagerAdapter() {
override fun instantiateItem(container: ViewGroup, position: Int): Any {
In the broadest sense,
- A future or promise can be thought of as a value that will eventually become available.
- Future is used for sychronizing program execution.
- Future acts as a proxy for an result which will be eventually available.
- The construct ( future X ) immediately returns a future for the value of the expression X and concurrently begins evaluating X. When the evaluation of X yields a value, that value replaces the future.
- A future is a placeholder object for a result that does not yet exist. A promise is a writable, single-assignment container, which completes a future. Promises can complete the future with a result to indicate success, or with an exception to indicate failure.