Skip to content

Instantly share code, notes, and snippets.

@nosix
Last active July 24, 2016 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nosix/6edfa98579101fc8eebf8aa135828ce4 to your computer and use it in GitHub Desktop.
Save nosix/6edfa98579101fc8eebf8aa135828ce4 to your computer and use it in GitHub Desktop.
Dialog Activity for Android (SDK 22) in Kotlin 1.0.2
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="org.anyspirit.mycustom.NewItemActivity">
<EditText
android:id="@+id/new_item_label"
android:labelFor="@+id/new_item_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:singleLine="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="@+id/new_item_submit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:backgroundTint="@color/colorAccent"
android:contentDescription="@string/add"
android:src="@android:drawable/ic_menu_add" />
<ImageButton
android:id="@+id/new_item_cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:backgroundTint="@color/colorAccentDark"
android:contentDescription="@string/back"
android:src="@android:drawable/ic_menu_close_clear_cancel" />
</LinearLayout>
</LinearLayout>
...
<activity
android:name=".NewItemActivity"
android:label="@string/new_item"
android:theme="@style/DialogTheme" />
...
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.MotionEvent
import android.widget.EditText
class NewItemActivity : AppCompatActivity() {
companion object {
val ITEM_NAME = "new_item_activity.item_name"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_new_item)
findViewById(R.id.new_item_cancel).setOnTouchListener {
view, motionEvent ->
setResult(RESULT_CANCELED)
finish()
true
}
findViewById(R.id.new_item_submit).setOnTouchListener {
view, motionEvent ->
setResult(RESULT_OK, getResultData())
finish()
true
}
}
private fun getResultData(): Intent {
val text = findViewById(R.id.new_item_label).let {
it as EditText
it.text.toString()
}
return Intent().putExtra(ITEM_NAME, text)
}
// As an alternative to 'setCanceledOnTouchOutside(false)' method.
override fun onTouchEvent(event: MotionEvent): Boolean = true
}
...
<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">@android:style/Animation</item>
<item name="android:windowMinWidthMajor">90%</item>
<item name="android:windowMinWidthMinor">90%</item>
</style>
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment