Advertisement
Guest User

Untitled

a guest
Apr 13th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 60.44 KB | None | 0 0
  1. 1) Create calculater
  2. implementation 'net.objecthunter:exp4j:0.4.8'
  3. Button.xml
  4. <?xml version="1.0" encoding="utf-8"?>
  5. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  6. <item android:state_pressed="true">
  7. <shape>
  8. <gradient android:angle="90" android:endColor="#FFFFFF" android:startColor="#9EB8FF" android:type="linear"/>
  9. <padding android:bottom="0dp" android:left="0dp" android:right="0dp" android:top="0dp"/>
  10. <size android:width="60dp" android:height="60dp"/>
  11. <stroke android:width="1dp" android:color="#ff3da6ef"/>
  12. </shape>
  13. </item>
  14. <item>
  15. <shape>
  16. <gradient android:angle="90" android:endColor="#FFFFFF" android:startColor="#ffd9d9d9" android:type="linear"/>
  17. <padding android:bottom="0dp" android:left="0dp" android:right="0dp" android:top="0dp"/>
  18. <size android:width="60dp" android:height="60dp"/>
  19. <stroke android:width="0.5dp" android:color="#ffcecece"/>
  20. </shape>
  21. </item>
  22. </selector>
  23. MainActivity.kt
  24. package com.javahelps.kotlincalculator
  25. import android.os.Bundle
  26. import android.support.v7.app.AppCompatActivity
  27. import android.view.View
  28. import android.widget.Button
  29. import android.widget.TextView
  30. import net.objecthunter.exp4j.ExpressionBuilder
  31. class MainActivity : AppCompatActivity() {
  32. lateinit var txtInput: TextView
  33. var lastNumeric: Boolean = false
  34. var stateError: Boolean = false
  35. var lastDot: Boolean = false
  36. override fun onCreate(savedInstanceState: Bundle?) {
  37. super.onCreate(savedInstanceState)
  38. setContentView(R.layout.activity_main)
  39. txtInput = findViewById(R.id.txtInput)
  40. }
  41. fun onDigit(view: View) {
  42. if (stateError) { txtInput.text = (view as Button).text
  43. stateError = false
  44. } else {
  45. txtInput.append((view as Button).text)
  46. {
  47. lastNumeric = true
  48. }
  49. fun onDecimalPoint(view: View) {
  50. if (lastNumeric && !stateError && !lastDot) {
  51. txtInput.append(".")
  52. lastNumeric = false
  53. lastDot = true
  54. }
  55. }
  56. fun onOperator(view: View) {
  57. if (lastNumeric && !stateError) {
  58. txtInput.append((view as Button).text)
  59. lastNumeric = false
  60. lastDot = false }
  61. }
  62. fun onClear(view: View) {
  63. this.txtInput.text = ""
  64. lastNumeric = false
  65. stateError = false
  66. lastDot = false
  67. }
  68. fun onEqual(view: View) {
  69. if (lastNumeric && !stateError) {
  70. val txt = txtInput.text.toString()
  71. val expression = ExpressionBuilder(txt).build()
  72. try {
  73. val result = expression.evaluate()
  74. txtInput.text = result.toString()
  75. lastDot = true // Result contains a dot
  76. } catch (ex: ArithmeticException) {
  77. txtInput.text = "Error"
  78. stateError = true
  79. lastNumeric = false
  80. }
  81. }
  82. }
  83. }
  84. Activity_main.xml
  85. <?xml version="1.0" encoding="utf-8"?>
  86. <android.support.constraint.ConstraintLayout
  87. xmlns:android="http://schemas.android.com/apk/res/android"
  88. xmlns:tools="http://schemas.android.com/tools"
  89. xmlns:app="http://schemas.android.com/apk/res-auto"
  90. android:layout_width="match_parent"
  91. android:layout_height="match_parent"
  92. android:paddingTop="8dp"
  93. tools:context=".MainActivity">
  94.  
  95. <TextView
  96. android:layout_width="match_parent"
  97. android:layout_height="wrap_content"
  98. android:ems="10"
  99. android:textSize="48sp"
  100. android:background="#efefef"
  101. android:id="@+id/txtInput"
  102. android:gravity="right|center_vertical"
  103. android:maxLength="12"
  104. android:layout_marginLeft="8dp"
  105. android:layout_marginEnd="8dp"
  106. android:layout_marginStart="8dp"
  107. app:layout_constraintTop_toTopOf="parent"
  108. android:layout_marginRight="8dp"/>
  109. <TableLayout
  110. android:layout_width="match_parent"
  111. android:layout_height="0dp"
  112. android:layout_marginTop="8dp"
  113. app:layout_goneMarginTop="8dp"
  114. android:layout_marginLeft="8dp"
  115. android:layout_marginBottom="8dp"
  116. android:layout_marginRight="8dp"
  117. app:layout_constraintLeft_toLeftOf="parent"
  118. app:layout_constraintRight_toRightOf="parent"
  119. app:layout_constraintBottom_toBottomOf="parent"
  120. app:layout_constraintTop_toBottomOf="@+id/txtInput"
  121. android:gravity="fill">
  122. <TableRow
  123. android:layout_width="match_parent"
  124. android:layout_height="0dp"
  125. android:layout_weight="1"
  126. android:gravity="center">
  127. <Button
  128. android:text="7"
  129. android:background="@drawable/button"
  130. android:layout_width="0dp"
  131. android:layout_weight="1"
  132. android:layout_height="match_parent"
  133. android:onClick="onDigit"
  134. android:id="@+id/btnSeven"/>
  135. <Button
  136. android:text="8"
  137. android:background="@drawable/button"
  138. android:layout_width="0dp"
  139. android:layout_weight="1"
  140. android:layout_height="match_parent"
  141. android:onClick="onDigit"
  142. android:id="@+id/btnEight"/>
  143. <Button
  144. android:text="9"
  145. android:background="@drawable/button"
  146. android:layout_width="0dp"
  147. android:layout_weight="1"
  148. android:layout_height="match_parent"
  149. android:onClick="onDigit"
  150. android:id="@+id/btnNine"/>
  151. <Button
  152. android:text="/"
  153. android:background="@drawable/button"
  154. android:layout_width="0dp"
  155. android:layout_weight="1"
  156. android:layout_height="match_parent"
  157. android:onClick="onOperator"
  158. android:id="@+id/btnDivide"/>
  159. </TableRow>
  160.  
  161. <TableRow
  162. android:layout_width="match_parent"
  163. android:layout_height="0dp"
  164. android:layout_weight="1"
  165. android:gravity="center">
  166.  
  167. <Button
  168. android:text="4"
  169. android:background="@drawable/button"
  170. android:layout_width="0dp"
  171. android:layout_weight="1"
  172. android:layout_height="match_parent"
  173. android:onClick="onDigit"
  174. android:id="@+id/btnFour"/>
  175.  
  176. <Button
  177. android:text="5"
  178. android:background="@drawable/button"
  179. android:layout_width="0dp"
  180. android:layout_weight="1"
  181. android:layout_height="match_parent"
  182. android:onClick="onDigit"
  183. android:id="@+id/btnFive"/>
  184.  
  185. <Button
  186. android:text="6"
  187. android:background="@drawable/button"
  188. android:layout_width="0dp"
  189. android:layout_weight="1"
  190. android:layout_height="match_parent"
  191. android:onClick="onDigit"
  192. android:id="@+id/btnSix"/>
  193.  
  194. <Button
  195. android:text="*"
  196. android:background="@drawable/button"
  197. android:layout_width="0dp"
  198. android:layout_weight="1"
  199. android:layout_height="match_parent"
  200. android:onClick="onOperator"
  201. android:id="@+id/btnMultiply"/>
  202. </TableRow>
  203.  
  204. <TableRow
  205. android:layout_width="match_parent"
  206. android:layout_height="0dp"
  207. android:layout_weight="1"
  208. android:gravity="center">
  209.  
  210. <Button
  211. android:text="1"
  212. android:background="@drawable/button"
  213. android:layout_width="0dp"
  214. android:layout_weight="1"
  215. android:layout_height="match_parent"
  216. android:onClick="onDigit"
  217. android:id="@+id/btnOne"/>
  218.  
  219. <Button
  220. android:text="2"
  221. android:background="@drawable/button"
  222. android:layout_width="0dp"
  223. android:layout_weight="1"
  224. android:layout_height="match_parent"
  225. android:onClick="onDigit"
  226. android:id="@+id/btnTwo"/>
  227.  
  228. <Button
  229. android:text="3"
  230. android:background="@drawable/button"
  231. android:layout_width="0dp"
  232. android:layout_weight="1"
  233. android:layout_height="match_parent"
  234. android:onClick="onDigit"
  235. android:id="@+id/btnThree"/>
  236.  
  237. <Button
  238. android:text="-"
  239. android:background="@drawable/button"
  240. android:layout_width="0dp"
  241. android:layout_weight="1"
  242. android:layout_height="match_parent"
  243. android:onClick="onOperator"
  244. android:id="@+id/btnSubtract"/>
  245. </TableRow>
  246.  
  247. <TableRow
  248. android:layout_width="match_parent"
  249. android:layout_height="0dp"
  250. android:layout_weight="1"
  251. android:gravity="center">
  252.  
  253. <Button
  254. android:text="."
  255. android:background="@drawable/button"
  256. android:layout_width="0dp"
  257. android:layout_weight="1"
  258. android:layout_height="match_parent"
  259. android:onClick="onDecimalPoint"
  260. android:id="@+id/btnDecimal"/>
  261.  
  262. <Button
  263. android:text="0"
  264. android:background="@drawable/button"
  265. android:layout_width="0dp"
  266. android:layout_weight="1"
  267. android:layout_height="match_parent"
  268. android:onClick="onDigit"
  269. android:id="@+id/btnZero"/>
  270.  
  271. <Button
  272. android:text="CLR"
  273. android:background="@drawable/button"
  274. android:layout_width="0dp"
  275. android:layout_weight="1"
  276. android:layout_height="match_parent"
  277. android:onClick="onClear"
  278. android:id="@+id/btnClear"/>
  279.  
  280. <Button
  281. android:text="+"
  282. android:background="@drawable/button"
  283. android:layout_width="0dp"
  284. android:layout_weight="1"
  285. android:layout_height="match_parent"
  286. android:onClick="onOperator"
  287. android:id="@+id/btnAdd"/>
  288. </TableRow>
  289.  
  290. <TableRow
  291. android:layout_width="match_parent"
  292. android:layout_height="0dp"
  293. android:layout_weight="1">
  294.  
  295. <Button
  296. android:text="="
  297. android:background="@drawable/button"
  298. android:layout_width="0dp"
  299. android:layout_weight="1"
  300. android:layout_height="match_parent"
  301. android:onClick="onEqual"
  302. android:id="@+id/btnEqual"/>
  303. </TableRow>
  304. </TableLayout>
  305. </android.support.constraint.ConstraintLayout>
  306.  
  307. 2) create menu item and also add toast
  308. menubar.xml
  309. res-new-android resource-manu.
  310. mamu-new-resource file.
  311. <?xml version="1.0" encoding="utf-8"?>
  312. <menu xmlns:android="http://schemas.android.com/apk/res/android"
  313. xmlns:app="http://schemas.android.com/apk/res-auto">
  314. <item android:id="@+id/home"
  315. android:icon="@drawable/img"
  316. app:showAsAction="always"
  317. android:title="Home">
  318.  
  319. </item>
  320. <item android:id="@+id/refresh"
  321. android:icon="@drawable/img"
  322. app:showAsAction="always"
  323. android:title="Refresh"/>
  324. <item android:id="@+id/delete"
  325. android:icon="@drawable/img"
  326. app:showAsAction="always"
  327. android:title="Delete"/>
  328. </menu>
  329.  
  330. MainActivity.kt
  331. package com.example.dialog
  332. import android.support.v7.app.AppCompatActivity
  333. import android.os.Bundle
  334. import android.support.v7.app.AlertDialog
  335. import android.view.Menu
  336. import android.view.MenuItem
  337. import android.widget.Toast
  338. class MainActivity : AppCompatActivity() {
  339. override fun onCreate(savedInstanceState: Bundle?) {
  340. super.onCreate(savedInstanceState)
  341. setContentView(R.layout.activity_main)
  342. }
  343. override fun onCreateOptionsMenu(menu: Menu?): Boolean {
  344. val inflater= menuInflater
  345. inflater.inflate(R.menu.menubar,menu)
  346. return super.onCreateOptionsMenu(menu)
  347. }
  348. override fun onOptionsItemSelected(item: MenuItem?): Boolean {
  349. if (item!=null){
  350. when(item.itemId){
  351. R.id.home->{
  352. Toast.makeText(applicationContext,"Home",Toast.LENGTH_SHORT).show()
  353. }
  354. R.id.refresh->{
  355. Toast.makeText(applicationContext,"Refresh",Toast.LENGTH_SHORT).show()
  356. }
  357. R.id.delete->{
  358. Toast.makeText(applicationContext,"Delete",Toast.LENGTH_SHORT).show()
  359. alert()
  360. }
  361. }
  362. }
  363. return super.onOptionsItemSelected(item)
  364. }
  365. private fun alert()
  366. {
  367. val builder=AlertDialog.Builder(this)
  368. builder.setTitle("Simple Alert")
  369. builder.setMessage("Alerrt Msg")
  370. builder.setPositiveButton("yes")
  371. {
  372. dialog, which -> Toast.makeText(applicationContext,"ButtonClick",Toast.LENGTH_SHORT).show()
  373. }
  374. val dialog:AlertDialog=builder.create()
  375. dialog.show()
  376. }
  377. }
  378. Activitymain.kt
  379. <?xml version="1.0" encoding="utf-8"?>
  380. <android.support.constraint.ConstraintLayout
  381. xmlns:android="http://schemas.android.com/apk/res/android"
  382. xmlns:tools="http://schemas.android.com/tools"
  383. xmlns:app="http://schemas.android.com/apk/res-auto"
  384. android:layout_width="match_parent"
  385. android:layout_height="match_parent"
  386. tools:context=".MainActivity">
  387. <TextView
  388. android:layout_width="wrap_content"
  389. android:layout_height="wrap_content"
  390. android:text="Hello World!"
  391. app:layout_constraintBottom_toBottomOf="parent"
  392. app:layout_constraintLeft_toLeftOf="parent"
  393. app:layout_constraintRight_toRightOf="parent"
  394. app:layout_constraintTop_toTopOf="parent"/>
  395. </android.support.constraint.ConstraintLayout>
  396.  
  397. second slip
  398. 1-AlertDialog
  399.  
  400. activity_main.xml
  401. <?xml version="1.0" encoding="utf-8"?>
  402. <RelativeLayout
  403. xmlns:android="http://schemas.android.com/apk/res/android"
  404. xmlns:tools="http://schemas.android.com/tools"
  405. xmlns:app="http://schemas.android.com/apk/res-auto"
  406. android:layout_width="match_parent"
  407. android:layout_height="match_parent"
  408. tools:context=".MainActivity">
  409.  
  410. <Button android:layout_width="wrap_content"
  411. android:layout_height="wrap_content"
  412. android:text="show alert"
  413. android:textAllCaps="false"
  414. android:id="@+id/dialog"/>
  415.  
  416. </RelativeLayout>
  417.  
  418. Mainactivity.kt
  419.  
  420. package com.example.loginpage
  421.  
  422. import android.support.v7.app.AppCompatActivity
  423. import android.os.Bundle
  424. import android.support.v7.app.AlertDialog
  425. import android.widget.Toast
  426. import kotlinx.android.synthetic.main.activity_main.*
  427.  
  428. class MainActivity : AppCompatActivity() {
  429.  
  430. override fun onCreate(savedInstanceState: Bundle?) {
  431. super.onCreate(savedInstanceState)
  432. setContentView(R.layout.activity_main)
  433. }
  434. override fun onBackPressed() {
  435. val builder =AlertDialog.Builder(this@MainActivity)
  436. builder.setTitle("are you sure")
  437. builder.setMessage("are you want to close app")
  438. builder.setPositiveButton("yes",{dialogInterfac,i->finish()})
  439. builder.setNegativeButton("no",{dialogInterfac,i->finish()})
  440. builder.show();
  441. }
  442. }
  443. 2-pass data from one page to another using intent
  444. MainActivity.kt
  445.  
  446. package com.example.intent
  447.  
  448. import android.content.Intent
  449. import android.support.v7.app.AppCompatActivity
  450. import android.os.Bundle
  451. import android.widget.Button
  452. import android.widget.EditText
  453. import kotlinx.android.synthetic.main.activity_main.*
  454.  
  455. class MainActivity : AppCompatActivity() {
  456. val name = null
  457. override fun onCreate(savedInstanceState: Bundle?) {
  458. super.onCreate(savedInstanceState)
  459. setContentView(R.layout.activity_main)
  460. val btn=findViewById<Button>(R.id.btn1)
  461. var message=findViewById<EditText>(R.id.edttxt)
  462. btn.setOnClickListener{
  463. val intent=Intent(this,Main2Activity::class.java)
  464. var name= message.text.toString()
  465. intent.putExtra("message",message.text.toString())
  466. startActivity(intent)
  467. }
  468. }
  469. }
  470.  
  471.  
  472. Activitymain.xml
  473.  
  474. <?xml version="1.0" encoding="utf-8"?>
  475. <RelativeLayout
  476. xmlns:android="http://schemas.android.com/apk/res/android"
  477. xmlns:tools="http://schemas.android.com/tools"
  478. xmlns:app="http://schemas.android.com/apk/res-auto"
  479. android:layout_width="match_parent"
  480. android:layout_height="match_parent"
  481. tools:context=".MainActivity">
  482.  
  483. <TextView
  484. android:layout_width="wrap_content"
  485. android:layout_height="wrap_content"
  486. android:text="Enter Name!"
  487. app:layout_constraintBottom_toBottomOf="parent"
  488. app:layout_constraintLeft_toLeftOf="parent"
  489. app:layout_constraintRight_toRightOf="parent"
  490. android:id="@+id/txt"
  491. android:textSize="25dp"
  492. app:layout_constraintTop_toTopOf="parent"/>
  493. <EditText
  494. android:layout_width="match_parent"
  495. android:layout_height="wrap_content"
  496. android:id="@+id/edttxt"
  497. android:layout_toRightOf="@+id/txt"
  498. />
  499.  
  500. <Button
  501. android:layout_width="wrap_content"
  502. android:layout_height="wrap_content"
  503. android:id="@+id/btn1"
  504. android:text="Submit"
  505. android:textSize="20dp"
  506. android:layout_below="@+id/txt"
  507. android:layout_marginTop="20dp"
  508. />
  509. </RelativeLayout>
  510. ActivitySecond.xml
  511. <?xml version="1.0" encoding="utf-8"?>
  512. <RelativeLayout
  513. xmlns:android="http://schemas.android.com/apk/res/android"
  514. xmlns:tools="http://schemas.android.com/tools"
  515. xmlns:app="http://schemas.android.com/apk/res-auto"
  516. android:layout_width="match_parent"
  517. android:layout_height="match_parent"
  518. tools:context=".Main2Activity">
  519. <TextView
  520. android:layout_width="wrap_content"
  521. android:layout_height="wrap_content"
  522. android:textSize="25dp"
  523. android:textStyle="bold"
  524. android:layout_centerInParent="true"
  525. android:id="@+id/text1"
  526. />
  527. </RelativeLayout>
  528. main2activity.kt
  529. package com.example.intent
  530. import android.support.v7.app.AppCompatActivity
  531. import android.os.Bundle
  532. import android.widget.TextView
  533. class Main2Activity : AppCompatActivity() {
  534. override fun onCreate(savedInstanceState: Bundle?) {
  535. super.onCreate(savedInstanceState)
  536. setContentView(R.layout.activity_second)
  537. var strUser: String = intent.getStringExtra("message")
  538. val n = findViewById<TextView>(R.id.text1)
  539. n.text="Welcome " + strUser
  540. }
  541. }
  542.  
  543.  
  544. ..3 Android resource program:
  545. colors.xml
  546. <?xml version="1.0" encoding="utf-8"?>
  547. <resources>
  548. <color name="colorPrimary">#008577</color>
  549. <color name="colorPrimaryDark">#00574B</color>
  550. <color name="colorAccent">#D81B60</color>
  551. </resources>
  552. styles.xml
  553. <resources>
  554. <!-- Base application theme. -->
  555. <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
  556. <!-- Customize your theme here. -->
  557. <item name="colorPrimary">@color/colorPrimary</item>
  558. <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
  559. <item name="colorAccent">@color/colorAccent</item>
  560. </style>
  561.  
  562. </resources>
  563.  
  564. string.xml
  565.  
  566. <resources>
  567. <string name="Hello_kotlin"></string>
  568. <string name="numbers">
  569. <item>1</item>
  570. <item>2</item>
  571. <item>3</item>
  572. </string>
  573. </resources>
  574.  
  575.  
  576. AndroidMAnifest.xml
  577.  
  578. <?xml version="1.0" encoding="utf-8"?>
  579. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  580. package="com.example.hello_kotlin">
  581.  
  582. <application
  583. android:allowBackup="true"
  584. android:icon="@mipmap/ic_launcher"
  585. android:label="@string/numbers"
  586.  
  587. android:roundIcon="@mipmap/ic_launcher_round"
  588. android:supportsRtl="true"
  589. android:theme="@style/AppTheme">
  590. <activity android:name=".MainActivity">
  591. <intent-filter>
  592. <action android:name="android.intent.action.MAIN"/>
  593.  
  594. <category android:name="android.intent.category.LAUNCHER"/>
  595. </intent-filter>
  596. </activity>
  597. </application>
  598.  
  599. </manifest>
  600.  
  601.  
  602.  
  603. Acitvitymain.xml
  604. <?xml version="1.0" encoding="utf-8"?>
  605. <RelativeLayout
  606. xmlns:android="http://schemas.android.com/apk/res/android"
  607. xmlns:tools="http://schemas.android.com/tools"
  608. xmlns:app="http://schemas.android.com/apk/res-auto"
  609. android:layout_width="match_parent"
  610. android:layout_height="match_parent"
  611. android:background="@drawable/download"
  612. tools:context=".MainActivity">
  613. <Button
  614. android:layout_width="wrap_content"
  615. android:layout_height="wrap_content"
  616. android:text="Click"
  617. android:layout_centerInParent="true"
  618. android:id="@+id/button" />
  619. <TextView
  620. android:layout_width="wrap_content"
  621. android:layout_height="wrap_content"
  622. android:text="@string/numbers"
  623. android:textSize="20dp"
  624. android:layout_below="@id/button"
  625. android:textColor="#ffff"
  626. />
  627. </RelativeLayout>
  628. activity_main.xml
  629.  
  630.  
  631. 4. create calculator using grid
  632. <?xml version="1.0" encoding="utf-8"?>
  633. <GridLayout
  634. xmlns:android="http://schemas.android.com/apk/res/android"
  635. xmlns:tools="http://schemas.android.com/tools"
  636. xmlns:app="http://schemas.android.com/apk/res-auto"
  637. android:layout_width="match_parent"
  638. android:layout_height="match_parent"
  639. tools:context=".MainActivity"
  640. android:layout_gravity="center"
  641. android:padding="16dp"
  642. android:rowCount="6"
  643. android:columnCount="4">
  644. <EditText
  645. android:layout_width="353dp"
  646. android:layout_height="wrap_content"
  647. android:inputType="textPersonName"
  648. android:ems="10"
  649. android:id="@+id/et_number"
  650. android:layout_columnSpan="4"/>
  651. <Button
  652. android:text="DEL"
  653. android:layout_width="wrap_content"
  654. android:layout_height="wrap_content" android:id="@+id/btn_del"
  655. android:layout_row="1"
  656. android:layout_column="0"
  657. />
  658. <Button
  659. android:text="C"
  660. android:layout_width="wrap_content"
  661. android:layout_height="wrap_content" android:id="@+id/btn_clear"
  662. android:layout_row="1"
  663. android:layout_column="1"/>
  664. <Button
  665. android:text="%"
  666. android:layout_width="wrap_content"
  667. android:layout_height="wrap_content" android:id="@+id/btn_mod"
  668. android:layout_row="1"
  669. android:layout_column="2"/>
  670. <Button
  671. android:text="+"
  672. android:layout_width="wrap_content"
  673. android:layout_height="wrap_content" android:id="@+id/btn_add"
  674. android:layout_row="1"
  675. android:layout_column="3"/>
  676. <Button
  677. android:text="1"
  678. android:layout_width="wrap_content"
  679. android:layout_height="wrap_content"
  680. android:id="@+id/btn_no1"
  681. android:layout_row="2"
  682. android:layout_column="0"/>
  683. <Button
  684. android:text="2"
  685. android:layout_width="wrap_content"
  686. android:layout_height="wrap_content" android:id="@+id/btn_no2"
  687. android:layout_row="2"
  688. android:layout_column="1"/>
  689. <Button
  690. android:text="3"
  691. android:layout_width="wrap_content"
  692. android:layout_height="wrap_content" android:id="@+id/btn_no3"
  693. android:layout_row="2"
  694. android:layout_column="2"/>
  695. <Button
  696. android:text="/"
  697. android:layout_width="wrap_content"
  698. android:layout_height="wrap_content" android:id="@+id/btn_divide"
  699. android:layout_row="2"
  700. android:layout_column="3"/>
  701. <Button
  702. android:text="4"
  703. android:layout_width="wrap_content"
  704. android:layout_height="wrap_content" android:id="@+id/btn_no4"
  705. android:layout_row="3"
  706. android:layout_column="0"/>
  707. <Button
  708. android:text="5"
  709. android:layout_width="wrap_content"
  710. android:layout_height="wrap_content" android:id="@+id/btn_no5"
  711. android:layout_row="3"
  712. android:layout_column="1"/>
  713. <Button
  714. android:text="6"
  715. android:layout_width="wrap_content"
  716. android:layout_height="wrap_content" android:id="@+id/btn_no6"
  717. android:layout_row="3"
  718. android:layout_column="2"/>
  719. <Button
  720. android:text="-"
  721. android:layout_width="wrap_content"
  722. android:layout_height="wrap_content" android:id="@+id/btn_subtract"
  723. android:layout_row="3"
  724. android:layout_column="3"/>
  725. <Button
  726. android:text="7"
  727. android:layout_width="wrap_content"
  728. android:layout_height="wrap_content" android:id="@+id/btn_no7"
  729. android:layout_row="4"
  730. android:layout_column="0"/>
  731. <Button
  732. android:text="8"
  733. android:layout_width="wrap_content"
  734. android:layout_height="wrap_content" android:id="@+id/btn_no8"
  735. android:layout_row="4"
  736. android:layout_column="1"/>
  737. <Button
  738. android:text="9"
  739. android:layout_width="wrap_content"
  740. android:layout_height="wrap_content" android:id="@+id/btn_no9"
  741. android:layout_row="4"
  742. android:layout_column="2"/>
  743. <Button
  744. android:text="x"
  745. android:layout_width="wrap_content"
  746. android:layout_height="wrap_content" android:id="@+id/btn_multiply"
  747. android:layout_row="4"
  748. android:layout_column="3"/>
  749. <Button
  750. android:text="Button"
  751. android:layout_width="wrap_content"
  752. android:layout_height="wrap_content" android:id="@+id/button18"
  753. android:layout_row="5"
  754. android:layout_column="0"/>
  755. <Button
  756. android:text="0"
  757. android:layout_width="wrap_content"
  758. android:layout_height="wrap_content"
  759. android:layout_row="5"
  760. android:layout_column="1"
  761. android:id="@+id/btn_no0"/>
  762. <Button
  763. android:text="="
  764. android:layout_width="171dp"
  765. android:layout_height="wrap_content" android:id="@+id/btn_answer"
  766. android:layout_row="5"
  767. android:layout_column="2"
  768. android:layout_columnSpan="2"/>
  769. </GridLayout>
  770.  
  771. MainActivity.kt
  772. import android.support.v7.app.AppCompatActivity
  773. import android.os.Bundle
  774. import android.widget.Toast
  775. import kotlinx.android.synthetic.main.activity_main.*
  776. class MainActivity : AppCompatActivity() {
  777. override fun onCreate(savedInstanceState: Bundle?) {
  778. super.onCreate(savedInstanceState)
  779. setContentView(R.layout.activity_main)
  780. var num_1 = 0
  781. var num_2 = 0
  782. var operator =""
  783. var answer = 0
  784. var answer_2 = 0.0
  785. //Button 0-9
  786. btn_no1.setOnClickListener {
  787. et_number.append("1").toString()
  788. }
  789. btn_no2.setOnClickListener {
  790. et_number.append("2").toString()
  791. }
  792. btn_no3.setOnClickListener {
  793. et_number.append("3").toString()
  794. }
  795. btn_no4.setOnClickListener {
  796. et_number.append("4").toString()
  797. }
  798. btn_no5.setOnClickListener {
  799. et_number.append("5").toString()
  800. }
  801. btn_no6.setOnClickListener {
  802. et_number.append("6").toString()
  803. }
  804. btn_no7.setOnClickListener {
  805. et_number.append("7").toString()
  806. }
  807. btn_no8.setOnClickListener {
  808. et_number.append("8").toString()
  809. }
  810. btn_no9.setOnClickListener {
  811. et_number.append("9").toString()
  812. }
  813. btn_no0.setOnClickListener {
  814. et_number.append("0").toString()
  815. }
  816. btn_add.setOnClickListener {
  817. num_1 = et_number.text.toString().toInt()
  818. et_number.setText("")
  819. et_number.setHint("+")
  820. operator = "+"
  821. }
  822. btn_divide.setOnClickListener {
  823. num_1 = et_number.text.toString().toInt()
  824. et_number.setText("")
  825. et_number.setHint("/")
  826. operator = "/"
  827. }
  828. btn_multiply.setOnClickListener {
  829. num_1 = et_number.text.toString().toInt()
  830. et_number.setText("")
  831. et_number.setHint("*")
  832. operator = "*"
  833. }
  834. btn_subtract.setOnClickListener {
  835. num_1 = et_number.text.toString().toInt()
  836. et_number.setText("")
  837. et_number.setHint("-")
  838. operator = "-"
  839. }
  840. btn_mod.setOnClickListener {
  841. num_1 = et_number.text.toString().toInt()
  842. et_number.setText("")
  843. et_number.setHint("%")
  844. operator = "%"
  845. }
  846. btn_clear.setOnClickListener {
  847. num_1 = 0
  848. num_2 = 0
  849. answer = 0
  850. operator = ""
  851. et_number.setHint("")
  852. et_number.setText("")
  853. }
  854. btn_del.setOnClickListener {
  855. var temp = et_number.text.toString().toInt()
  856. var ans = temp /10
  857. et_number.setText(ans.toString())
  858. }
  859. btn_answer.setOnClickListener {
  860. num_2 = et_number.text.toString().toInt()
  861. et_number.setText("")
  862. et_number.setHint("")
  863. if (operator.equals("+")){
  864. answer = num_1 + num_2
  865. Toast.makeText(this, answer.toString(), Toast.LENGTH_LONG).show()
  866. }
  867. else if (operator.equals("-")){
  868. answer = num_1 - num_2
  869. Toast.makeText(this, answer.toString(), Toast.LENGTH_LONG).show()
  870. }
  871. else if (operator.equals("/")){
  872. answer = num_1 / num_2
  873. Toast.makeText(this, answer.toString(), Toast.LENGTH_LONG).show()
  874. }
  875. else if (operator.equals("*")){
  876. answer = num_1 * num_2
  877. Toast.makeText(this, answer.toString(), Toast.LENGTH_LONG).show()
  878. }else if (operator.equals("%")){
  879. answer_2 = num_1.toDouble() % num_2.toDouble()
  880. Toast.makeText(this, answer_2.toString(), Toast.LENGTH_LONG).show()
  881. }else{
  882. Toast.makeText(this, "Wrong Input", Toast.LENGTH_LONG).show()
  883. }
  884. }
  885. }
  886. }
  887. 5: create areoplane mode
  888. activity_main.XML
  889.  
  890. <?xml version="1.0" encoding="utf-8"?>
  891. <RelativeLayout
  892. xmlns:android="http://schemas.android.com/apk/res/android"
  893. xmlns:tools="http://schemas.android.com/tools"
  894. xmlns:app="http://schemas.android.com/apk/res-auto"
  895. android:layout_width="match_parent"
  896. android:layout_height="match_parent"
  897. tools:context=".MainActivity">
  898.  
  899. <ImageView
  900. android:id="@+id/imageview"
  901. android:layout_width="40dp"
  902. android:layout_height="40dp"
  903. android:layout_marginTop="8dp"
  904. app:layout_constraintStart_toStartOf="parent"
  905. app:layout_constraintTop_toTopOf="parent"
  906. app:srcCompat="@drawable/ic_flight_black_24dp"
  907. tools:ignore="VectorDrawableCompat"/>
  908. <TextView
  909. android:id="@+id/textview1"
  910. android:layout_width="300dp"
  911. android:layout_height="36dp"
  912.  
  913. android:text="Flight Mode"
  914. android:textSize="24dp"
  915. android:textColor="@android:color/black"
  916.  
  917. android:layout_toRightOf="@+id/imageview"
  918. android:layout_marginLeft="45dp"/>
  919. </RelativeLayout>
  920.  
  921.  
  922. androidManifest.xml
  923.  
  924. <?xml version="1.0" encoding="utf-8"?>
  925. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  926. package="com.example.flight_mode">
  927.  
  928. <application
  929. android:allowBackup="true"
  930. android:icon="@mipmap/ic_launcher"
  931. android:label="@string/app_name"
  932. android:roundIcon="@mipmap/ic_launcher_round"
  933. android:supportsRtl="true"
  934. android:theme="@style/AppTheme">
  935. <activity android:name=".MainActivity">
  936. <intent-filter>
  937. <action android:name="android.intent.action.MAIN"/>
  938. <category android:name="android.intent.category.LAUNCHER"/>
  939. </intent-filter>
  940. </activity>
  941. <receiver android:name=".myreciver"
  942. android:enabled="true"
  943. android:exported="true">
  944. <intent-filter>
  945. <action android:name="android.intent.action.AIRPLANE_MODE"></action>
  946. </intent-filter>
  947. </receiver>
  948. </application>
  949. </manifest>
  950.  
  951.  
  952. myreciver.kt
  953.  
  954.  
  955. package com.example.flight_mode
  956. import android.content.BroadcastReceiver
  957. import android.content.Context
  958. import android.content.Intent
  959. import android.widget.Toast
  960. class myreciver:BroadcastReceiver() {
  961. override fun onReceive(context: Context, intent: Intent) {
  962. Toast.makeText(
  963. context, "Broadcast : Flight mode changed.",
  964. Toast.LENGTH_LONG
  965. ).show()
  966. }
  967. }
  968.  
  969. MainActivity.kt
  970.  
  971. package com.example.flight_mode
  972. import android.content.IntentFilter
  973. import android.os.Bundle
  974. import android.support.v7.app.AppCompatActivity
  975. import java.util.logging.Filter
  976.  
  977. class MainActivity : AppCompatActivity() {
  978. override fun onCreate(savedInstanceState: Bundle?) {
  979. super.onCreate(savedInstanceState)
  980. setContentView(R.layout.activity_main)
  981. val filter=IntentFilter()
  982. filter.addAction("com.example.flight_mode")
  983. filter.addAction("android.intent.action.AIRPLANE_MODE")
  984. val reciver=myreciver()
  985. registerReceiver(reciver,filter)
  986. }
  987. }
  988. 6: create application using image view
  989. activity_main.xml
  990.  
  991. <?xml version="1.0" encoding="utf-8"?>
  992. <GridLayout
  993. xmlns:android="http://schemas.android.com/apk/res/android"
  994. xmlns:tools="http://schemas.android.com/tools"
  995. xmlns:app="http://schemas.android.com/apk/res-auto"
  996. android:layout_width="match_parent"
  997. android:layout_height="match_parent"
  998. tools:context=".MainActivity" android:columnCount="3" android:rowCount="3">
  999. <ImageView
  1000. android:layout_width="wrap_content"
  1001. android:layout_height="wrap_content" app:srcCompat="@mipmap/ic_launcher_round" android:id="@+id/imageView"
  1002. android:layout_row="0" android:layout_column="0"/>
  1003. <ImageView
  1004. android:layout_width="40dp"
  1005. android:layout_height="40dp" app:srcCompat="@drawable/ic_assignment_ind_black_24dp"
  1006. android:id="@+id/imageView2" android:layout_row="0" android:layout_column="1"/>
  1007. <ImageView
  1008. android:layout_width="wrap_content"
  1009. android:layout_height="wrap_content" app:srcCompat="@mipmap/ic_launcher_round"
  1010. android:id="@+id/imageView3"
  1011. android:layout_row="0" android:layout_column="2"/>
  1012. <ImageView
  1013. android:layout_width="wrap_content"
  1014. android:layout_height="wrap_content" app:srcCompat="@mipmap/ic_launcher_round"
  1015. android:id="@+id/imageView4"
  1016. android:layout_row="1" android:layout_column="0"/>
  1017. <ImageView
  1018. android:layout_width="wrap_content"
  1019. android:layout_height="wrap_content" app:srcCompat="@mipmap/ic_launcher_round"
  1020. android:id="@+id/imageView5"
  1021. android:layout_row="1" android:layout_column="1"/>
  1022. <ImageView
  1023. android:layout_width="wrap_content"
  1024. android:layout_height="wrap_content" app:srcCompat="@mipmap/ic_launcher_round"
  1025. android:id="@+id/imageView6"
  1026. android:layout_row="1" android:layout_column="2"/>
  1027. </GridLayout>
  1028.  
  1029. MainActivity.kt
  1030.  
  1031. package com.trackrider.android.gridviewimage
  1032. import android.support.v7.app.AppCompatActivity
  1033. import android.os.Bundle
  1034. import android.widget.Toast
  1035. import kotlinx.android.synthetic.main.activity_main.*
  1036. class MainActivity : AppCompatActivity() {
  1037. override fun onCreate(savedInstanceState: Bundle?) {
  1038. super.onCreate(savedInstanceState)
  1039. setContentView(R.layout.activity_main)
  1040. //change text in toast as required
  1041. imageView.setOnClickListener {
  1042. Toast.makeText(this, "Image 1", Toast.LENGTH_LONG).show()
  1043. }
  1044. imageView2.setOnClickListener {
  1045. Toast.makeText(this, "Image 2", Toast.LENGTH_LONG).show()
  1046. }
  1047. imageView3.setOnClickListener {
  1048. Toast.makeText(this, "Image 3", Toast.LENGTH_LONG).show()
  1049. }
  1050. imageView4.setOnClickListener {
  1051. Toast.makeText(this, "Image 4", Toast.LENGTH_LONG).show()
  1052. }
  1053. imageView5.setOnClickListener {
  1054. Toast.makeText(this, "Image 5", Toast.LENGTH_LONG).show()
  1055. }
  1056. imageView6.setOnClickListener {
  1057. Toast.makeText(this, "Image 6", Toast.LENGTH_LONG).show()
  1058. }
  1059. }
  1060. }
  1061.  
  1062. 7: life cycle program
  1063. ActivityMain.xml
  1064. <?xml version="1.0" encoding="utf-8"?>
  1065. <RelativeLayout
  1066. xmlns:android="http://schemas.android.com/apk/res/android"
  1067. xmlns:tools="http://schemas.android.com/tools"
  1068. xmlns:app="http://schemas.android.com/apk/res-auto"
  1069. android:layout_width="match_parent"
  1070. android:layout_height="match_parent"
  1071. tools:context=".MainActivity">
  1072.  
  1073. <TextView
  1074. android:layout_width="wrap_content"
  1075. android:layout_height="wrap_content"
  1076. android:text="Welcome!"
  1077. android:textSize="30dp"
  1078. android:layout_centerInParent="true"
  1079. />
  1080. </RelativeLayout>
  1081.  
  1082. MainActivity.kt
  1083.  
  1084. package com.example.life_cycle
  1085.  
  1086. import android.content.Intent
  1087. import android.support.v7.app.AppCompatActivity
  1088. import android.os.Bundle
  1089. import android.widget.Button
  1090. import android.widget.Toast
  1091.  
  1092. class MainActivity : AppCompatActivity() {
  1093.  
  1094. override fun onCreate(savedInstanceState: Bundle?) {
  1095. super.onCreate(savedInstanceState)
  1096. setContentView(R.layout.activity_main)
  1097.  
  1098.  
  1099. }
  1100.  
  1101. override fun onStart() {
  1102. super.onStart()
  1103. Toast.makeText(applicationContext, "app Started..", Toast.LENGTH_LONG).show()
  1104. }
  1105.  
  1106. override fun onResume() {
  1107. super.onResume()
  1108. Toast.makeText(applicationContext,"Resumed",Toast.LENGTH_LONG).show()
  1109. }
  1110.  
  1111. override fun onRestart() {
  1112. super.onRestart()
  1113. Toast.makeText(applicationContext,"Activity restarted",Toast.LENGTH_LONG).show()
  1114. }
  1115.  
  1116. override fun onPause() {
  1117. super.onPause()
  1118. Toast.makeText(applicationContext,"Activity Paused",Toast.LENGTH_LONG).show()
  1119. }
  1120.  
  1121. override fun onDestroy() {
  1122. super.onDestroy()
  1123. Toast.makeText(applicationContext,"Activity destroyed",Toast.LENGTH_LONG).show()
  1124. }
  1125.  
  1126. override fun onStop() {
  1127. super.onStop()
  1128. Toast.makeText(applicationContext,"Activity stoped",Toast.LENGTH_LONG).show()
  1129. }
  1130.  
  1131. }
  1132.  
  1133.  
  1134. ActivityOne.xml
  1135.  
  1136. <?xml version="1.0" encoding="utf-8"?>
  1137. <android.support.constraint.ConstraintLayout
  1138. xmlns:android="http://schemas.android.com/apk/res/android"
  1139. xmlns:tools="http://schemas.android.com/tools"
  1140. xmlns:app="http://schemas.android.com/apk/res-auto"
  1141. android:layout_width="match_parent"
  1142. android:layout_height="match_parent"
  1143. tools:context=".Activity_one">
  1144.  
  1145. </android.support.constraint.ConstraintLayout>
  1146.  
  1147.  
  1148. Activityone.kt
  1149.  
  1150. package com.example.life_cycle
  1151.  
  1152. import android.support.v7.app.AppCompatActivity
  1153. import android.os.Bundle
  1154.  
  1155. class Activity_one : AppCompatActivity() {
  1156.  
  1157. override fun onCreate(savedInstanceState: Bundle?) {
  1158. super.onCreate(savedInstanceState)
  1159. setContentView(R.layout.activity_one)
  1160. }
  1161. }
  1162. 8:list view program
  1163. list view
  1164.  
  1165. <?xml version="1.0" encoding="utf-8"?>
  1166. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  1167. android:layout_width="match_parent"
  1168. android:layout_height="match_parent">
  1169.  
  1170.  
  1171. <TextView android:layout_width="wrap_content"
  1172. android:layout_height="wrap_content"
  1173. android:text="ListView"
  1174. android:textSize="40dp"/>
  1175.  
  1176. <ListView android:layout_width="wrap_content"
  1177. android:layout_height="wrap_content"
  1178. android:entries="@array/insert_list">
  1179. </ListView>
  1180.  
  1181. </RelativeLayout>
  1182.  
  1183. string.xml
  1184.  
  1185.  
  1186. <resources>
  1187. <string name="app_name">Linear_Layout</string>
  1188.  
  1189. <resources>
  1190. <array name="insert_list">
  1191. <item>one</item>
  1192. <item>two</item>
  1193. <item>three</item>
  1194. <item>four</item>
  1195. <item>five</item>
  1196. <item>six</item>
  1197. <item>seven</item>
  1198. <item>eight</item>
  1199. <item>nine</item>
  1200. <item>ten</item>
  1201. </array>
  1202. </resources>
  1203.  
  1204. </resources>
  1205.  
  1206. 9:login page program
  1207. activity_main.xml
  1208.  
  1209. <?xml version="1.0" encoding="utf-8"?>
  1210. <android.support.constraint.ConstraintLayout
  1211. xmlns:android="http://schemas.android.com/apk/res/android"
  1212. xmlns:tools="http://schemas.android.com/tools"
  1213. xmlns:app="http://schemas.android.com/apk/res-auto"
  1214. android:layout_width="match_parent"
  1215. android:layout_height="match_parent"
  1216. tools:context=".MainActivity">
  1217.  
  1218.  
  1219. <EditText
  1220. android:layout_width="wrap_content"
  1221. android:layout_height="wrap_content"
  1222. android:inputType="textPersonName"
  1223. android:ems="10"
  1224. android:id="@+id/et_username"
  1225. android:hint="Username" app:layout_constraintTop_toTopOf="parent"
  1226. app:layout_constraintStart_toStartOf="parent" android:layout_marginTop="260dp"
  1227. app:layout_constraintEnd_toEndOf="parent"/>
  1228. <EditText
  1229. android:layout_width="wrap_content"
  1230. android:layout_height="wrap_content"
  1231. android:inputType="textPassword"
  1232. android:ems="10"
  1233. android:id="@+id/et_password"
  1234. android:hint="Password" app:layout_constraintStart_toStartOf="@+id/et_username"
  1235. android:layout_marginTop="22dp" app:layout_constraintTop_toBottomOf="@+id/et_username"/>
  1236. <TextView
  1237. android:text="Login"
  1238. android:layout_width="86dp"
  1239. android:layout_height="61dp"
  1240. android:id="@+id/textView" android:textSize="30sp"
  1241. android:textColor="@color/colorPrimaryDark" android:textStyle="bold"
  1242. app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
  1243. android:layout_marginBottom="14dp" app:layout_constraintBottom_toTopOf="@+id/et_username"/>
  1244. <Button
  1245. android:text="Submit"
  1246. android:layout_width="wrap_content"
  1247. android:layout_height="wrap_content"
  1248. android:id="@+id/btn_submit"
  1249. app:layout_constraintStart_toStartOf="@+id/et_password"
  1250. app:layout_constraintBaseline_toBaselineOf="@+id/btn_reset"/>
  1251. <Button
  1252. android:text="Reset"
  1253. android:layout_width="wrap_content"
  1254. android:layout_height="wrap_content"
  1255. android:id="@+id/btn_reset" android:layout_marginTop="39dp"
  1256. app:layout_constraintTop_toBottomOf="@+id/et_password" app:layout_constraintEnd_toEndOf="@+id/et_password"/>
  1257. <android.support.constraint.Constraints android:layout_width="wrap_content" android:layout_height="wrap_content"
  1258. android:id="@+id/constraints" app:layout_constraintTop_toTopOf="parent"
  1259. app:layout_constraintStart_toStartOf="parent"
  1260. app:layout_constraintEnd_toStartOf="parent"
  1261. app:layout_constraintBottom_toTopOf="parent"/>
  1262. </android.support.constraint.ConstraintLayout>
  1263.  
  1264. MainActivity.kt
  1265.  
  1266. import android.support.v7.app.AppCompatActivity
  1267. import android.os.Bundle
  1268. import android.widget.Button
  1269. import android.widget.EditText
  1270. import android.widget.Toast
  1271. class MainActivity : AppCompatActivity() {
  1272. override fun onCreate(savedInstanceState: Bundle?) {
  1273. super.onCreate(savedInstanceState)
  1274. setContentView(R.layout.activity_main)
  1275. var mUsername = findViewById<EditText>(R.id.et_username)
  1276. var mPassword = findViewById<EditText>(R.id.et_password)
  1277. var mBtnSubmit = findViewById<Button>(R.id.btn_submit)
  1278. var mBtnReset = findViewById<Button>(R.id.btn_reset)
  1279. mBtnReset.setOnClickListener {
  1280. mUsername.setText("")
  1281. mPassword.setText("")
  1282. }
  1283. mBtnSubmit.setOnClickListener {
  1284. var uname = mUsername.text.toString()
  1285. var pwd = mPassword.text.toString()
  1286. if(uname.equals("abcd") && pwd.equals("abcd")){
  1287. Toast.makeText(this, “Login Successful”, Toast.LENGTH_LONG).show()
  1288. }else{
  1289. Toast.makeText(this, "Login Denied", Toast.LENGTH_LONG).show()
  1290. }
  1291. }
  1292. }
  1293. }
  1294. 10: permission program
  1295. ActivityMain.kt
  1296.  
  1297.  
  1298. <?xml version="1.0" encoding="utf-8"?>
  1299. <android.support.constraint.ConstraintLayout
  1300. xmlns:android="http://schemas.android.com/apk/res/android"
  1301. xmlns:tools="http://schemas.android.com/tools"
  1302. xmlns:app="http://schemas.android.com/apk/res-auto"
  1303. android:layout_width="match_parent"
  1304. android:layout_height="match_parent"
  1305. tools:context=".MainActivity">
  1306.  
  1307. <Button
  1308. android:layout_width="wrap_content"
  1309. android:layout_height="wrap_content"
  1310. android:id="@+id/button1"
  1311.  
  1312. tools:ignore="MissingConstraints" tools:layout_editor_absoluteY="134dp"
  1313. tools:layout_editor_absoluteX="16dp"
  1314. android:text="Request permission At Runtime"/>
  1315. </android.support.constraint.ConstraintLayout>
  1316.  
  1317.  
  1318. MainActivity.kt
  1319.  
  1320. package com.example.permission
  1321.  
  1322. import android.Manifest
  1323. import android.content.Context
  1324. import android.os.Build
  1325. import kotlinx.android.synthetic.main.activity_main.*
  1326. import android.support.v7.app.AppCompatActivity
  1327. import android.os.Bundle
  1328. import android.widget.Toast
  1329.  
  1330.  
  1331.  
  1332.  
  1333. class MainActivity : AppCompatActivity() {
  1334. private val PermissionsRequestCode = 123
  1335. private lateinit var managePermissions: ManagePermissions
  1336.  
  1337.  
  1338. override fun onCreate(savedInstanceState: Bundle?) {
  1339. super.onCreate(savedInstanceState)
  1340. setContentView(R.layout.activity_main)
  1341. val list = listOf<String>(
  1342. Manifest.permission.CAMERA,
  1343. Manifest.permission.READ_CONTACTS,
  1344. Manifest.permission.READ_EXTERNAL_STORAGE,
  1345. Manifest.permission.SEND_SMS,
  1346. Manifest.permission.READ_CALENDAR
  1347. )
  1348.  
  1349. managePermissions = ManagePermissions(this, list, PermissionsRequestCode)
  1350.  
  1351.  
  1352. button1.setOnClickListener{
  1353. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
  1354. managePermissions.checkPermissions()
  1355. }
  1356.  
  1357. }
  1358. override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>,
  1359. grantResults: IntArray) {
  1360. when (requestCode) {
  1361. PermissionsRequestCode ->{
  1362. val isPermissionsGranted = managePermissions
  1363. .processPermissionsResult(requestCode,permissions,grantResults)
  1364. if(isPermissionsGranted){
  1365. // Do the task now
  1366. toast("Permissions granted.")
  1367. }else{
  1368. toast("Permissions denied.")
  1369. }
  1370. return
  1371. }
  1372. }
  1373. }
  1374. }
  1375. // Extension function to show toast message
  1376. fun Context.toast(message: String) {
  1377. Toast.makeText(this, message, Toast.LENGTH_LONG).show()
  1378. }
  1379.  
  1380.  
  1381.  
  1382. MAnagepermissions.kt
  1383.  
  1384.  
  1385. package com.example.permission
  1386.  
  1387. import android.app.Activity
  1388. import android.app.AlertDialog
  1389. import android.content.pm.PackageManager
  1390. import android.support.v4.app.ActivityCompat
  1391. import android.support.v4.content.ContextCompat
  1392.  
  1393.  
  1394. class ManagePermissions(val activity: Activity,val list: List<String>,val code:Int) {
  1395.  
  1396. // Check permissions at runtime
  1397. fun checkPermissions() {
  1398. if (isPermissionsGranted() != PackageManager.PERMISSION_GRANTED) {
  1399. showAlert()
  1400. } else {
  1401. activity.toast("Permissions already granted.")
  1402. }
  1403. }
  1404. // Check permissions status
  1405. private fun isPermissionsGranted(): Int {
  1406. // PERMISSION_GRANTED : Constant Value: 0
  1407. // PERMISSION_DENIED : Constant Value: -1
  1408. var counter = 0;
  1409. for (permission in list) {
  1410. counter += ContextCompat.checkSelfPermission(activity, permission)
  1411. }
  1412. return counter
  1413. }
  1414. // Find the first denied permission
  1415. private fun deniedPermission(): String {
  1416. for (permission in list) {
  1417. if (ContextCompat.checkSelfPermission(activity, permission)
  1418. == PackageManager.PERMISSION_DENIED) return permission
  1419. }
  1420. return ""
  1421. }
  1422. // Show alert dialog to request permissions
  1423. private fun showAlert() {
  1424. val builder = AlertDialog.Builder(activity)
  1425. builder.setTitle("Need permission(s)")
  1426. builder.setMessage("Some permissions are required to do the task.")
  1427. builder.setPositiveButton("OK", { dialog, which -> requestPermissions() })
  1428. builder.setNeutralButton("Cancel", null)
  1429. val dialog = builder.create()
  1430. dialog.show()
  1431. }
  1432. // Request the permissions at run time
  1433. private fun requestPermissions() {
  1434. val permission = deniedPermission()
  1435. if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) {
  1436. // Show an explanation asynchronously
  1437. activity.toast("Should show an explanation.")
  1438. } else {
  1439. ActivityCompat.requestPermissions(activity, list.toTypedArray(), code)
  1440. }
  1441. }
  1442. // Process permissions result
  1443. fun processPermissionsResult(requestCode: Int, permissions: Array<String>,
  1444. grantResults: IntArray): Boolean {
  1445. var result = 0
  1446. if (grantResults.isNotEmpty()) {
  1447. for (item in grantResults) {
  1448. result += item
  1449. }
  1450. }
  1451. if (result == PackageManager.PERMISSION_GRANTED) return true
  1452. return false
  1453. }
  1454. }
  1455.  
  1456.  
  1457. AndoridManifest.xml
  1458.  
  1459. <?xml version="1.0" encoding="utf-8"?>
  1460. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  1461. package="com.example.permission">
  1462.  
  1463. <uses-permission android:name="android.permission.INTERNET"></uses-permission>
  1464. <uses-permission android:name="android.permission.CAMERA"></uses-permission>
  1465. <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
  1466. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
  1467. <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
  1468. <uses-permission android:name="android.permission.READ_CALENDAR"></uses-permission>
  1469.  
  1470. <application
  1471. android:allowBackup="true"
  1472. android:icon="@mipmap/ic_launcher"
  1473. android:label="@string/app_name"
  1474. android:roundIcon="@mipmap/ic_launcher_round"
  1475. android:supportsRtl="true"
  1476. android:theme="@style/AppTheme">
  1477. <activity android:name=".MainActivity">
  1478. <intent-filter>
  1479. <action android:name="android.intent.action.MAIN"/>
  1480.  
  1481. <category android:name="android.intent.category.LAUNCHER"/>
  1482. </intent-filter>
  1483. </activity>
  1484. </application>
  1485.  
  1486. </manifest>
  1487.  
  1488. string.xml
  1489. <resources>
  1490. <string name="app_name">Permission</string>
  1491. </resources>
  1492.  
  1493. 12: ringtone program
  1494. activity.xml
  1495.  
  1496. <?xml version="1.0" encoding="utf-8"?>
  1497. <LinearLayout
  1498. xmlns:android="http://schemas.android.com/apk/res/android"
  1499. xmlns:tools="http://schemas.android.com/tools"
  1500. xmlns:app="http://schemas.android.com/apk/res-auto"
  1501. android:layout_width="match_parent"
  1502. android:layout_height="match_parent"
  1503. android:orientation="vertical"
  1504. android:background="@color/colorAccent"
  1505. tools:context=".MainActivity">
  1506.  
  1507. <Button
  1508. android:id="@+id/button_play"
  1509. android:layout_width="wrap_content"
  1510. android:layout_height="wrap_content"
  1511. android:text="Play Default Ringtone"
  1512. android:layout_gravity="center_horizontal"
  1513. android:layout_margin="10dp"
  1514. />
  1515. <Button
  1516. android:id="@+id/button_stop"
  1517. android:layout_width="wrap_content"
  1518. android:layout_height="wrap_content"
  1519. android:text="Stop"
  1520. android:layout_gravity="center_horizontal"
  1521. android:layout_margin="10dp"
  1522. />
  1523. <Button
  1524. android:id="@+id/button_media_play"
  1525. android:layout_width="wrap_content"
  1526. android:layout_height="wrap_content"
  1527. android:text="Play Using Media Player"
  1528. android:layout_gravity="center_horizontal"
  1529. android:layout_margin="10dp"
  1530. />
  1531. <Button
  1532. android:id="@+id/button_media_stop"
  1533. android:layout_width="wrap_content"
  1534. android:layout_height="wrap_content"
  1535. android:text="Stop Media Player"
  1536. android:layout_gravity="center_horizontal"
  1537. android:layout_margin="10dp"
  1538. />
  1539.  
  1540. </LinearLayout>
  1541.  
  1542. activity.kt
  1543.  
  1544. package com.example.back_sound_service
  1545.  
  1546. import android.content.Context
  1547. import android.media.MediaPlayer
  1548. import android.media.Ringtone
  1549. import android.media.RingtoneManager
  1550. import android.media.audiofx.BassBoost
  1551. import android.support.v7.app.AppCompatActivity
  1552. import android.os.Bundle
  1553. import android.provider.Settings
  1554. import kotlinx.android.synthetic.main.activity_main.*
  1555.  
  1556. class MainActivity : AppCompatActivity() {
  1557.  
  1558. override fun onCreate(savedInstanceState: Bundle?) {
  1559. super.onCreate(savedInstanceState)
  1560. setContentView(R.layout.activity_main)
  1561.  
  1562. val ringtone: Ringtone = defaultRingtone
  1563. button_play.setOnClickListener{
  1564. if (!ringtone.isPlaying){
  1565. ringtone.play()
  1566. }
  1567. }
  1568.  
  1569. button_stop.setOnClickListener{
  1570. if (ringtone.isPlaying){
  1571. ringtone.stop()
  1572. }
  1573. }
  1574. val mediaPlayer:MediaPlayer = defaultRingtonePlayer()
  1575. button_media_play.setOnClickListener{
  1576. if (!mediaPlayer.isPlaying)mediaPlayer.start()
  1577. }
  1578. button_media_stop.setOnClickListener{
  1579. if (mediaPlayer.isPlaying){
  1580. mediaPlayer.stop()
  1581. }
  1582. }
  1583. }
  1584. }
  1585. val Context.defaultRingtone:Ringtone
  1586. get() {
  1587. val uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE)
  1588. return RingtoneManager.getRingtone(this, uri)
  1589. }
  1590. fun Context.defaultRingtonePlayer():MediaPlayer{
  1591. return MediaPlayer.create(this, Settings.System.DEFAULT_RINGTONE_URI)
  1592. }
  1593. 13:table layout program
  1594. table layout
  1595.  
  1596. <RelativeLayout
  1597. xmlns:android="http://schemas.android.com/apk/res/android"
  1598. xmlns:tools="http://schemas.android.com/tools"
  1599. xmlns:app="http://schemas.android.com/apk/res-auto"
  1600. android:layout_width="match_parent"
  1601. android:layout_height="match_parent"
  1602. android:background="#e6f0ff"
  1603. tools:context=".MainActivity">
  1604. <TableLayout android:layout_width="wrap_content"
  1605. android:layout_height="wrap_content"
  1606. android:layout_marginLeft="50dp"
  1607. android:layout_marginTop="150dp"
  1608. android:background="#cce0ff">
  1609. <TableRow>
  1610. <Button
  1611. android:id="@+id/btn1"
  1612. android:text="1"
  1613. android:layout_gravity="center"
  1614. />
  1615. <Button
  1616. android:id="@+id/btn2"
  1617. android:text="2"
  1618. android:layout_gravity="center"
  1619. />
  1620. <Button
  1621. android:id="@+id/btn3"
  1622. android:text="3"
  1623. android:layout_gravity="center"
  1624. />
  1625. </TableRow>
  1626. <TableRow>
  1627. <Button
  1628. android:id="@+id/btn4"
  1629. android:text="4"
  1630. android:layout_gravity="center"
  1631. />
  1632. <Button
  1633. android:id="@+id/btn5"
  1634. android:text="5"
  1635. android:layout_gravity="center"
  1636. /><Button
  1637. android:id="@+id/btn6"
  1638. android:text="6"
  1639. android:layout_gravity="center"
  1640. />
  1641. </TableRow>
  1642. <TableRow>
  1643. <Button
  1644. android:id="@+id/btn7"
  1645. android:text="7"
  1646. android:layout_gravity="center"
  1647.  
  1648.  
  1649. />
  1650. <Button
  1651. android:id="@+id/btn8"
  1652. android:text="8"
  1653. android:layout_gravity="center"
  1654. /><Button
  1655. android:id="@+id/btn9"
  1656. android:text="9"
  1657. android:layout_gravity="center"
  1658. />
  1659. </TableRow>
  1660. </TableLayout>
  1661. <Button
  1662. android:layout_width="wrap_content"
  1663. android:layout_height="wrap_content"
  1664.  
  1665. android:id="@+id/linearl"
  1666. android:text="LinearLayout"
  1667. />
  1668. <Button
  1669. android:layout_width="wrap_content"
  1670. android:layout_height="wrap_content"
  1671.  
  1672. android:id="@+id/relativel"
  1673. android:layout_toRightOf="@+id/linearl"
  1674. android:text="RelativeLayou"
  1675. />
  1676.  
  1677. <Button
  1678. android:layout_width="wrap_content"
  1679. android:layout_height="wrap_content"
  1680.  
  1681. android:id="@+id/fragmentl"
  1682. android:layout_toRightOf="@+id/relativel"
  1683. android:text="fragmentLayout" />
  1684. <Button
  1685. android:layout_width="wrap_content"
  1686. android:layout_height="wrap_content"
  1687.  
  1688. android:id="@+id/listview"
  1689. android:layout_below="@+id/linearl"
  1690. android:text="ListView" />
  1691. <Button
  1692. android:layout_width="wrap_content"
  1693. android:layout_height="wrap_content"
  1694.  
  1695. android:id="@+id/gridview"
  1696. android:layout_toRightOf="@+id/listview"
  1697. android:layout_below="@+id/linearl"
  1698. android:text="ListView" />
  1699. </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement