Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1) Create calculater
- implementation 'net.objecthunter:exp4j:0.4.8'
- Button.xml
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:state_pressed="true">
- <shape>
- <gradient android:angle="90" android:endColor="#FFFFFF" android:startColor="#9EB8FF" android:type="linear"/>
- <padding android:bottom="0dp" android:left="0dp" android:right="0dp" android:top="0dp"/>
- <size android:width="60dp" android:height="60dp"/>
- <stroke android:width="1dp" android:color="#ff3da6ef"/>
- </shape>
- </item>
- <item>
- <shape>
- <gradient android:angle="90" android:endColor="#FFFFFF" android:startColor="#ffd9d9d9" android:type="linear"/>
- <padding android:bottom="0dp" android:left="0dp" android:right="0dp" android:top="0dp"/>
- <size android:width="60dp" android:height="60dp"/>
- <stroke android:width="0.5dp" android:color="#ffcecece"/>
- </shape>
- </item>
- </selector>
- MainActivity.kt
- package com.javahelps.kotlincalculator
- import android.os.Bundle
- import android.support.v7.app.AppCompatActivity
- import android.view.View
- import android.widget.Button
- import android.widget.TextView
- import net.objecthunter.exp4j.ExpressionBuilder
- class MainActivity : AppCompatActivity() {
- lateinit var txtInput: TextView
- var lastNumeric: Boolean = false
- var stateError: Boolean = false
- var lastDot: Boolean = false
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- txtInput = findViewById(R.id.txtInput)
- }
- fun onDigit(view: View) {
- if (stateError) { txtInput.text = (view as Button).text
- stateError = false
- } else {
- txtInput.append((view as Button).text)
- {
- lastNumeric = true
- }
- fun onDecimalPoint(view: View) {
- if (lastNumeric && !stateError && !lastDot) {
- txtInput.append(".")
- lastNumeric = false
- lastDot = true
- }
- }
- fun onOperator(view: View) {
- if (lastNumeric && !stateError) {
- txtInput.append((view as Button).text)
- lastNumeric = false
- lastDot = false }
- }
- fun onClear(view: View) {
- this.txtInput.text = ""
- lastNumeric = false
- stateError = false
- lastDot = false
- }
- fun onEqual(view: View) {
- if (lastNumeric && !stateError) {
- val txt = txtInput.text.toString()
- val expression = ExpressionBuilder(txt).build()
- try {
- val result = expression.evaluate()
- txtInput.text = result.toString()
- lastDot = true // Result contains a dot
- } catch (ex: ArithmeticException) {
- txtInput.text = "Error"
- stateError = true
- lastNumeric = false
- }
- }
- }
- }
- Activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingTop="8dp"
- tools:context=".MainActivity">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:ems="10"
- android:textSize="48sp"
- android:background="#efefef"
- android:id="@+id/txtInput"
- android:gravity="right|center_vertical"
- android:maxLength="12"
- android:layout_marginLeft="8dp"
- android:layout_marginEnd="8dp"
- android:layout_marginStart="8dp"
- app:layout_constraintTop_toTopOf="parent"
- android:layout_marginRight="8dp"/>
- <TableLayout
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:layout_marginTop="8dp"
- app:layout_goneMarginTop="8dp"
- android:layout_marginLeft="8dp"
- android:layout_marginBottom="8dp"
- android:layout_marginRight="8dp"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/txtInput"
- android:gravity="fill">
- <TableRow
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:layout_weight="1"
- android:gravity="center">
- <Button
- android:text="7"
- android:background="@drawable/button"
- android:layout_width="0dp"
- android:layout_weight="1"
- android:layout_height="match_parent"
- android:onClick="onDigit"
- android:id="@+id/btnSeven"/>
- <Button
- android:text="8"
- android:background="@drawable/button"
- android:layout_width="0dp"
- android:layout_weight="1"
- android:layout_height="match_parent"
- android:onClick="onDigit"
- android:id="@+id/btnEight"/>
- <Button
- android:text="9"
- android:background="@drawable/button"
- android:layout_width="0dp"
- android:layout_weight="1"
- android:layout_height="match_parent"
- android:onClick="onDigit"
- android:id="@+id/btnNine"/>
- <Button
- android:text="/"
- android:background="@drawable/button"
- android:layout_width="0dp"
- android:layout_weight="1"
- android:layout_height="match_parent"
- android:onClick="onOperator"
- android:id="@+id/btnDivide"/>
- </TableRow>
- <TableRow
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:layout_weight="1"
- android:gravity="center">
- <Button
- android:text="4"
- android:background="@drawable/button"
- android:layout_width="0dp"
- android:layout_weight="1"
- android:layout_height="match_parent"
- android:onClick="onDigit"
- android:id="@+id/btnFour"/>
- <Button
- android:text="5"
- android:background="@drawable/button"
- android:layout_width="0dp"
- android:layout_weight="1"
- android:layout_height="match_parent"
- android:onClick="onDigit"
- android:id="@+id/btnFive"/>
- <Button
- android:text="6"
- android:background="@drawable/button"
- android:layout_width="0dp"
- android:layout_weight="1"
- android:layout_height="match_parent"
- android:onClick="onDigit"
- android:id="@+id/btnSix"/>
- <Button
- android:text="*"
- android:background="@drawable/button"
- android:layout_width="0dp"
- android:layout_weight="1"
- android:layout_height="match_parent"
- android:onClick="onOperator"
- android:id="@+id/btnMultiply"/>
- </TableRow>
- <TableRow
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:layout_weight="1"
- android:gravity="center">
- <Button
- android:text="1"
- android:background="@drawable/button"
- android:layout_width="0dp"
- android:layout_weight="1"
- android:layout_height="match_parent"
- android:onClick="onDigit"
- android:id="@+id/btnOne"/>
- <Button
- android:text="2"
- android:background="@drawable/button"
- android:layout_width="0dp"
- android:layout_weight="1"
- android:layout_height="match_parent"
- android:onClick="onDigit"
- android:id="@+id/btnTwo"/>
- <Button
- android:text="3"
- android:background="@drawable/button"
- android:layout_width="0dp"
- android:layout_weight="1"
- android:layout_height="match_parent"
- android:onClick="onDigit"
- android:id="@+id/btnThree"/>
- <Button
- android:text="-"
- android:background="@drawable/button"
- android:layout_width="0dp"
- android:layout_weight="1"
- android:layout_height="match_parent"
- android:onClick="onOperator"
- android:id="@+id/btnSubtract"/>
- </TableRow>
- <TableRow
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:layout_weight="1"
- android:gravity="center">
- <Button
- android:text="."
- android:background="@drawable/button"
- android:layout_width="0dp"
- android:layout_weight="1"
- android:layout_height="match_parent"
- android:onClick="onDecimalPoint"
- android:id="@+id/btnDecimal"/>
- <Button
- android:text="0"
- android:background="@drawable/button"
- android:layout_width="0dp"
- android:layout_weight="1"
- android:layout_height="match_parent"
- android:onClick="onDigit"
- android:id="@+id/btnZero"/>
- <Button
- android:text="CLR"
- android:background="@drawable/button"
- android:layout_width="0dp"
- android:layout_weight="1"
- android:layout_height="match_parent"
- android:onClick="onClear"
- android:id="@+id/btnClear"/>
- <Button
- android:text="+"
- android:background="@drawable/button"
- android:layout_width="0dp"
- android:layout_weight="1"
- android:layout_height="match_parent"
- android:onClick="onOperator"
- android:id="@+id/btnAdd"/>
- </TableRow>
- <TableRow
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:layout_weight="1">
- <Button
- android:text="="
- android:background="@drawable/button"
- android:layout_width="0dp"
- android:layout_weight="1"
- android:layout_height="match_parent"
- android:onClick="onEqual"
- android:id="@+id/btnEqual"/>
- </TableRow>
- </TableLayout>
- </android.support.constraint.ConstraintLayout>
- 2) create menu item and also add toast
- menubar.xml
- res-new-android resource-manu.
- mamu-new-resource file.
- <?xml version="1.0" encoding="utf-8"?>
- <menu xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto">
- <item android:id="@+id/home"
- android:icon="@drawable/img"
- app:showAsAction="always"
- android:title="Home">
- </item>
- <item android:id="@+id/refresh"
- android:icon="@drawable/img"
- app:showAsAction="always"
- android:title="Refresh"/>
- <item android:id="@+id/delete"
- android:icon="@drawable/img"
- app:showAsAction="always"
- android:title="Delete"/>
- </menu>
- MainActivity.kt
- package com.example.dialog
- import android.support.v7.app.AppCompatActivity
- import android.os.Bundle
- import android.support.v7.app.AlertDialog
- import android.view.Menu
- import android.view.MenuItem
- import android.widget.Toast
- class MainActivity : AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- }
- override fun onCreateOptionsMenu(menu: Menu?): Boolean {
- val inflater= menuInflater
- inflater.inflate(R.menu.menubar,menu)
- return super.onCreateOptionsMenu(menu)
- }
- override fun onOptionsItemSelected(item: MenuItem?): Boolean {
- if (item!=null){
- when(item.itemId){
- R.id.home->{
- Toast.makeText(applicationContext,"Home",Toast.LENGTH_SHORT).show()
- }
- R.id.refresh->{
- Toast.makeText(applicationContext,"Refresh",Toast.LENGTH_SHORT).show()
- }
- R.id.delete->{
- Toast.makeText(applicationContext,"Delete",Toast.LENGTH_SHORT).show()
- alert()
- }
- }
- }
- return super.onOptionsItemSelected(item)
- }
- private fun alert()
- {
- val builder=AlertDialog.Builder(this)
- builder.setTitle("Simple Alert")
- builder.setMessage("Alerrt Msg")
- builder.setPositiveButton("yes")
- {
- dialog, which -> Toast.makeText(applicationContext,"ButtonClick",Toast.LENGTH_SHORT).show()
- }
- val dialog:AlertDialog=builder.create()
- dialog.show()
- }
- }
- Activitymain.kt
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Hello World!"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintTop_toTopOf="parent"/>
- </android.support.constraint.ConstraintLayout>
- second slip
- 1-AlertDialog
- activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
- <Button android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="show alert"
- android:textAllCaps="false"
- android:id="@+id/dialog"/>
- </RelativeLayout>
- Mainactivity.kt
- package com.example.loginpage
- import android.support.v7.app.AppCompatActivity
- import android.os.Bundle
- import android.support.v7.app.AlertDialog
- import android.widget.Toast
- import kotlinx.android.synthetic.main.activity_main.*
- class MainActivity : AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- }
- override fun onBackPressed() {
- val builder =AlertDialog.Builder(this@MainActivity)
- builder.setTitle("are you sure")
- builder.setMessage("are you want to close app")
- builder.setPositiveButton("yes",{dialogInterfac,i->finish()})
- builder.setNegativeButton("no",{dialogInterfac,i->finish()})
- builder.show();
- }
- }
- 2-pass data from one page to another using intent
- MainActivity.kt
- package com.example.intent
- import android.content.Intent
- import android.support.v7.app.AppCompatActivity
- import android.os.Bundle
- import android.widget.Button
- import android.widget.EditText
- import kotlinx.android.synthetic.main.activity_main.*
- class MainActivity : AppCompatActivity() {
- val name = null
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- val btn=findViewById<Button>(R.id.btn1)
- var message=findViewById<EditText>(R.id.edttxt)
- btn.setOnClickListener{
- val intent=Intent(this,Main2Activity::class.java)
- var name= message.text.toString()
- intent.putExtra("message",message.text.toString())
- startActivity(intent)
- }
- }
- }
- Activitymain.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Enter Name!"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- android:id="@+id/txt"
- android:textSize="25dp"
- app:layout_constraintTop_toTopOf="parent"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/edttxt"
- android:layout_toRightOf="@+id/txt"
- />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/btn1"
- android:text="Submit"
- android:textSize="20dp"
- android:layout_below="@+id/txt"
- android:layout_marginTop="20dp"
- />
- </RelativeLayout>
- ActivitySecond.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".Main2Activity">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="25dp"
- android:textStyle="bold"
- android:layout_centerInParent="true"
- android:id="@+id/text1"
- />
- </RelativeLayout>
- main2activity.kt
- package com.example.intent
- import android.support.v7.app.AppCompatActivity
- import android.os.Bundle
- import android.widget.TextView
- class Main2Activity : AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_second)
- var strUser: String = intent.getStringExtra("message")
- val n = findViewById<TextView>(R.id.text1)
- n.text="Welcome " + strUser
- }
- }
- ..3 Android resource program:
- colors.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <color name="colorPrimary">#008577</color>
- <color name="colorPrimaryDark">#00574B</color>
- <color name="colorAccent">#D81B60</color>
- </resources>
- styles.xml
- <resources>
- <!-- Base application theme. -->
- <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
- <!-- Customize your theme here. -->
- <item name="colorPrimary">@color/colorPrimary</item>
- <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
- <item name="colorAccent">@color/colorAccent</item>
- </style>
- </resources>
- string.xml
- <resources>
- <string name="Hello_kotlin"></string>
- <string name="numbers">
- <item>1</item>
- <item>2</item>
- <item>3</item>
- </string>
- </resources>
- AndroidMAnifest.xml
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.hello_kotlin">
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/numbers"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN"/>
- <category android:name="android.intent.category.LAUNCHER"/>
- </intent-filter>
- </activity>
- </application>
- </manifest>
- Acitvitymain.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@drawable/download"
- tools:context=".MainActivity">
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Click"
- android:layout_centerInParent="true"
- android:id="@+id/button" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/numbers"
- android:textSize="20dp"
- android:layout_below="@id/button"
- android:textColor="#ffff"
- />
- </RelativeLayout>
- activity_main.xml
- 4. create calculator using grid
- <?xml version="1.0" encoding="utf-8"?>
- <GridLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity"
- android:layout_gravity="center"
- android:padding="16dp"
- android:rowCount="6"
- android:columnCount="4">
- <EditText
- android:layout_width="353dp"
- android:layout_height="wrap_content"
- android:inputType="textPersonName"
- android:ems="10"
- android:id="@+id/et_number"
- android:layout_columnSpan="4"/>
- <Button
- android:text="DEL"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:id="@+id/btn_del"
- android:layout_row="1"
- android:layout_column="0"
- />
- <Button
- android:text="C"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:id="@+id/btn_clear"
- android:layout_row="1"
- android:layout_column="1"/>
- <Button
- android:text="%"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:id="@+id/btn_mod"
- android:layout_row="1"
- android:layout_column="2"/>
- <Button
- android:text="+"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:id="@+id/btn_add"
- android:layout_row="1"
- android:layout_column="3"/>
- <Button
- android:text="1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/btn_no1"
- android:layout_row="2"
- android:layout_column="0"/>
- <Button
- android:text="2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:id="@+id/btn_no2"
- android:layout_row="2"
- android:layout_column="1"/>
- <Button
- android:text="3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:id="@+id/btn_no3"
- android:layout_row="2"
- android:layout_column="2"/>
- <Button
- android:text="/"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:id="@+id/btn_divide"
- android:layout_row="2"
- android:layout_column="3"/>
- <Button
- android:text="4"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:id="@+id/btn_no4"
- android:layout_row="3"
- android:layout_column="0"/>
- <Button
- android:text="5"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:id="@+id/btn_no5"
- android:layout_row="3"
- android:layout_column="1"/>
- <Button
- android:text="6"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:id="@+id/btn_no6"
- android:layout_row="3"
- android:layout_column="2"/>
- <Button
- android:text="-"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:id="@+id/btn_subtract"
- android:layout_row="3"
- android:layout_column="3"/>
- <Button
- android:text="7"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:id="@+id/btn_no7"
- android:layout_row="4"
- android:layout_column="0"/>
- <Button
- android:text="8"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:id="@+id/btn_no8"
- android:layout_row="4"
- android:layout_column="1"/>
- <Button
- android:text="9"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:id="@+id/btn_no9"
- android:layout_row="4"
- android:layout_column="2"/>
- <Button
- android:text="x"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:id="@+id/btn_multiply"
- android:layout_row="4"
- android:layout_column="3"/>
- <Button
- android:text="Button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:id="@+id/button18"
- android:layout_row="5"
- android:layout_column="0"/>
- <Button
- android:text="0"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_row="5"
- android:layout_column="1"
- android:id="@+id/btn_no0"/>
- <Button
- android:text="="
- android:layout_width="171dp"
- android:layout_height="wrap_content" android:id="@+id/btn_answer"
- android:layout_row="5"
- android:layout_column="2"
- android:layout_columnSpan="2"/>
- </GridLayout>
- MainActivity.kt
- import android.support.v7.app.AppCompatActivity
- import android.os.Bundle
- import android.widget.Toast
- import kotlinx.android.synthetic.main.activity_main.*
- class MainActivity : AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- var num_1 = 0
- var num_2 = 0
- var operator =""
- var answer = 0
- var answer_2 = 0.0
- //Button 0-9
- btn_no1.setOnClickListener {
- et_number.append("1").toString()
- }
- btn_no2.setOnClickListener {
- et_number.append("2").toString()
- }
- btn_no3.setOnClickListener {
- et_number.append("3").toString()
- }
- btn_no4.setOnClickListener {
- et_number.append("4").toString()
- }
- btn_no5.setOnClickListener {
- et_number.append("5").toString()
- }
- btn_no6.setOnClickListener {
- et_number.append("6").toString()
- }
- btn_no7.setOnClickListener {
- et_number.append("7").toString()
- }
- btn_no8.setOnClickListener {
- et_number.append("8").toString()
- }
- btn_no9.setOnClickListener {
- et_number.append("9").toString()
- }
- btn_no0.setOnClickListener {
- et_number.append("0").toString()
- }
- btn_add.setOnClickListener {
- num_1 = et_number.text.toString().toInt()
- et_number.setText("")
- et_number.setHint("+")
- operator = "+"
- }
- btn_divide.setOnClickListener {
- num_1 = et_number.text.toString().toInt()
- et_number.setText("")
- et_number.setHint("/")
- operator = "/"
- }
- btn_multiply.setOnClickListener {
- num_1 = et_number.text.toString().toInt()
- et_number.setText("")
- et_number.setHint("*")
- operator = "*"
- }
- btn_subtract.setOnClickListener {
- num_1 = et_number.text.toString().toInt()
- et_number.setText("")
- et_number.setHint("-")
- operator = "-"
- }
- btn_mod.setOnClickListener {
- num_1 = et_number.text.toString().toInt()
- et_number.setText("")
- et_number.setHint("%")
- operator = "%"
- }
- btn_clear.setOnClickListener {
- num_1 = 0
- num_2 = 0
- answer = 0
- operator = ""
- et_number.setHint("")
- et_number.setText("")
- }
- btn_del.setOnClickListener {
- var temp = et_number.text.toString().toInt()
- var ans = temp /10
- et_number.setText(ans.toString())
- }
- btn_answer.setOnClickListener {
- num_2 = et_number.text.toString().toInt()
- et_number.setText("")
- et_number.setHint("")
- if (operator.equals("+")){
- answer = num_1 + num_2
- Toast.makeText(this, answer.toString(), Toast.LENGTH_LONG).show()
- }
- else if (operator.equals("-")){
- answer = num_1 - num_2
- Toast.makeText(this, answer.toString(), Toast.LENGTH_LONG).show()
- }
- else if (operator.equals("/")){
- answer = num_1 / num_2
- Toast.makeText(this, answer.toString(), Toast.LENGTH_LONG).show()
- }
- else if (operator.equals("*")){
- answer = num_1 * num_2
- Toast.makeText(this, answer.toString(), Toast.LENGTH_LONG).show()
- }else if (operator.equals("%")){
- answer_2 = num_1.toDouble() % num_2.toDouble()
- Toast.makeText(this, answer_2.toString(), Toast.LENGTH_LONG).show()
- }else{
- Toast.makeText(this, "Wrong Input", Toast.LENGTH_LONG).show()
- }
- }
- }
- }
- 5: create areoplane mode
- activity_main.XML
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
- <ImageView
- android:id="@+id/imageview"
- android:layout_width="40dp"
- android:layout_height="40dp"
- android:layout_marginTop="8dp"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:srcCompat="@drawable/ic_flight_black_24dp"
- tools:ignore="VectorDrawableCompat"/>
- <TextView
- android:id="@+id/textview1"
- android:layout_width="300dp"
- android:layout_height="36dp"
- android:text="Flight Mode"
- android:textSize="24dp"
- android:textColor="@android:color/black"
- android:layout_toRightOf="@+id/imageview"
- android:layout_marginLeft="45dp"/>
- </RelativeLayout>
- androidManifest.xml
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.flight_mode">
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN"/>
- <category android:name="android.intent.category.LAUNCHER"/>
- </intent-filter>
- </activity>
- <receiver android:name=".myreciver"
- android:enabled="true"
- android:exported="true">
- <intent-filter>
- <action android:name="android.intent.action.AIRPLANE_MODE"></action>
- </intent-filter>
- </receiver>
- </application>
- </manifest>
- myreciver.kt
- package com.example.flight_mode
- import android.content.BroadcastReceiver
- import android.content.Context
- import android.content.Intent
- import android.widget.Toast
- class myreciver:BroadcastReceiver() {
- override fun onReceive(context: Context, intent: Intent) {
- Toast.makeText(
- context, "Broadcast : Flight mode changed.",
- Toast.LENGTH_LONG
- ).show()
- }
- }
- MainActivity.kt
- package com.example.flight_mode
- import android.content.IntentFilter
- import android.os.Bundle
- import android.support.v7.app.AppCompatActivity
- import java.util.logging.Filter
- class MainActivity : AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- val filter=IntentFilter()
- filter.addAction("com.example.flight_mode")
- filter.addAction("android.intent.action.AIRPLANE_MODE")
- val reciver=myreciver()
- registerReceiver(reciver,filter)
- }
- }
- 6: create application using image view
- activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <GridLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity" android:columnCount="3" android:rowCount="3">
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" app:srcCompat="@mipmap/ic_launcher_round" android:id="@+id/imageView"
- android:layout_row="0" android:layout_column="0"/>
- <ImageView
- android:layout_width="40dp"
- android:layout_height="40dp" app:srcCompat="@drawable/ic_assignment_ind_black_24dp"
- android:id="@+id/imageView2" android:layout_row="0" android:layout_column="1"/>
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" app:srcCompat="@mipmap/ic_launcher_round"
- android:id="@+id/imageView3"
- android:layout_row="0" android:layout_column="2"/>
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" app:srcCompat="@mipmap/ic_launcher_round"
- android:id="@+id/imageView4"
- android:layout_row="1" android:layout_column="0"/>
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" app:srcCompat="@mipmap/ic_launcher_round"
- android:id="@+id/imageView5"
- android:layout_row="1" android:layout_column="1"/>
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" app:srcCompat="@mipmap/ic_launcher_round"
- android:id="@+id/imageView6"
- android:layout_row="1" android:layout_column="2"/>
- </GridLayout>
- MainActivity.kt
- package com.trackrider.android.gridviewimage
- import android.support.v7.app.AppCompatActivity
- import android.os.Bundle
- import android.widget.Toast
- import kotlinx.android.synthetic.main.activity_main.*
- class MainActivity : AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- //change text in toast as required
- imageView.setOnClickListener {
- Toast.makeText(this, "Image 1", Toast.LENGTH_LONG).show()
- }
- imageView2.setOnClickListener {
- Toast.makeText(this, "Image 2", Toast.LENGTH_LONG).show()
- }
- imageView3.setOnClickListener {
- Toast.makeText(this, "Image 3", Toast.LENGTH_LONG).show()
- }
- imageView4.setOnClickListener {
- Toast.makeText(this, "Image 4", Toast.LENGTH_LONG).show()
- }
- imageView5.setOnClickListener {
- Toast.makeText(this, "Image 5", Toast.LENGTH_LONG).show()
- }
- imageView6.setOnClickListener {
- Toast.makeText(this, "Image 6", Toast.LENGTH_LONG).show()
- }
- }
- }
- 7: life cycle program
- ActivityMain.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Welcome!"
- android:textSize="30dp"
- android:layout_centerInParent="true"
- />
- </RelativeLayout>
- MainActivity.kt
- package com.example.life_cycle
- import android.content.Intent
- import android.support.v7.app.AppCompatActivity
- import android.os.Bundle
- import android.widget.Button
- import android.widget.Toast
- class MainActivity : AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- }
- override fun onStart() {
- super.onStart()
- Toast.makeText(applicationContext, "app Started..", Toast.LENGTH_LONG).show()
- }
- override fun onResume() {
- super.onResume()
- Toast.makeText(applicationContext,"Resumed",Toast.LENGTH_LONG).show()
- }
- override fun onRestart() {
- super.onRestart()
- Toast.makeText(applicationContext,"Activity restarted",Toast.LENGTH_LONG).show()
- }
- override fun onPause() {
- super.onPause()
- Toast.makeText(applicationContext,"Activity Paused",Toast.LENGTH_LONG).show()
- }
- override fun onDestroy() {
- super.onDestroy()
- Toast.makeText(applicationContext,"Activity destroyed",Toast.LENGTH_LONG).show()
- }
- override fun onStop() {
- super.onStop()
- Toast.makeText(applicationContext,"Activity stoped",Toast.LENGTH_LONG).show()
- }
- }
- ActivityOne.xml
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".Activity_one">
- </android.support.constraint.ConstraintLayout>
- Activityone.kt
- package com.example.life_cycle
- import android.support.v7.app.AppCompatActivity
- import android.os.Bundle
- class Activity_one : AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_one)
- }
- }
- 8:list view program
- list view
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <TextView android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="ListView"
- android:textSize="40dp"/>
- <ListView android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:entries="@array/insert_list">
- </ListView>
- </RelativeLayout>
- string.xml
- <resources>
- <string name="app_name">Linear_Layout</string>
- <resources>
- <array name="insert_list">
- <item>one</item>
- <item>two</item>
- <item>three</item>
- <item>four</item>
- <item>five</item>
- <item>six</item>
- <item>seven</item>
- <item>eight</item>
- <item>nine</item>
- <item>ten</item>
- </array>
- </resources>
- </resources>
- 9:login page program
- activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
- <EditText
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:inputType="textPersonName"
- android:ems="10"
- android:id="@+id/et_username"
- android:hint="Username" app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintStart_toStartOf="parent" android:layout_marginTop="260dp"
- app:layout_constraintEnd_toEndOf="parent"/>
- <EditText
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:inputType="textPassword"
- android:ems="10"
- android:id="@+id/et_password"
- android:hint="Password" app:layout_constraintStart_toStartOf="@+id/et_username"
- android:layout_marginTop="22dp" app:layout_constraintTop_toBottomOf="@+id/et_username"/>
- <TextView
- android:text="Login"
- android:layout_width="86dp"
- android:layout_height="61dp"
- android:id="@+id/textView" android:textSize="30sp"
- android:textColor="@color/colorPrimaryDark" android:textStyle="bold"
- app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
- android:layout_marginBottom="14dp" app:layout_constraintBottom_toTopOf="@+id/et_username"/>
- <Button
- android:text="Submit"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/btn_submit"
- app:layout_constraintStart_toStartOf="@+id/et_password"
- app:layout_constraintBaseline_toBaselineOf="@+id/btn_reset"/>
- <Button
- android:text="Reset"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/btn_reset" android:layout_marginTop="39dp"
- app:layout_constraintTop_toBottomOf="@+id/et_password" app:layout_constraintEnd_toEndOf="@+id/et_password"/>
- <android.support.constraint.Constraints android:layout_width="wrap_content" android:layout_height="wrap_content"
- android:id="@+id/constraints" app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintEnd_toStartOf="parent"
- app:layout_constraintBottom_toTopOf="parent"/>
- </android.support.constraint.ConstraintLayout>
- MainActivity.kt
- import android.support.v7.app.AppCompatActivity
- import android.os.Bundle
- import android.widget.Button
- import android.widget.EditText
- import android.widget.Toast
- class MainActivity : AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- var mUsername = findViewById<EditText>(R.id.et_username)
- var mPassword = findViewById<EditText>(R.id.et_password)
- var mBtnSubmit = findViewById<Button>(R.id.btn_submit)
- var mBtnReset = findViewById<Button>(R.id.btn_reset)
- mBtnReset.setOnClickListener {
- mUsername.setText("")
- mPassword.setText("")
- }
- mBtnSubmit.setOnClickListener {
- var uname = mUsername.text.toString()
- var pwd = mPassword.text.toString()
- if(uname.equals("abcd") && pwd.equals("abcd")){
- Toast.makeText(this, “Login Successful”, Toast.LENGTH_LONG).show()
- }else{
- Toast.makeText(this, "Login Denied", Toast.LENGTH_LONG).show()
- }
- }
- }
- }
- 10: permission program
- ActivityMain.kt
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/button1"
- tools:ignore="MissingConstraints" tools:layout_editor_absoluteY="134dp"
- tools:layout_editor_absoluteX="16dp"
- android:text="Request permission At Runtime"/>
- </android.support.constraint.ConstraintLayout>
- MainActivity.kt
- package com.example.permission
- import android.Manifest
- import android.content.Context
- import android.os.Build
- import kotlinx.android.synthetic.main.activity_main.*
- import android.support.v7.app.AppCompatActivity
- import android.os.Bundle
- import android.widget.Toast
- class MainActivity : AppCompatActivity() {
- private val PermissionsRequestCode = 123
- private lateinit var managePermissions: ManagePermissions
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- val list = listOf<String>(
- Manifest.permission.CAMERA,
- Manifest.permission.READ_CONTACTS,
- Manifest.permission.READ_EXTERNAL_STORAGE,
- Manifest.permission.SEND_SMS,
- Manifest.permission.READ_CALENDAR
- )
- managePermissions = ManagePermissions(this, list, PermissionsRequestCode)
- button1.setOnClickListener{
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
- managePermissions.checkPermissions()
- }
- }
- override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>,
- grantResults: IntArray) {
- when (requestCode) {
- PermissionsRequestCode ->{
- val isPermissionsGranted = managePermissions
- .processPermissionsResult(requestCode,permissions,grantResults)
- if(isPermissionsGranted){
- // Do the task now
- toast("Permissions granted.")
- }else{
- toast("Permissions denied.")
- }
- return
- }
- }
- }
- }
- // Extension function to show toast message
- fun Context.toast(message: String) {
- Toast.makeText(this, message, Toast.LENGTH_LONG).show()
- }
- MAnagepermissions.kt
- package com.example.permission
- import android.app.Activity
- import android.app.AlertDialog
- import android.content.pm.PackageManager
- import android.support.v4.app.ActivityCompat
- import android.support.v4.content.ContextCompat
- class ManagePermissions(val activity: Activity,val list: List<String>,val code:Int) {
- // Check permissions at runtime
- fun checkPermissions() {
- if (isPermissionsGranted() != PackageManager.PERMISSION_GRANTED) {
- showAlert()
- } else {
- activity.toast("Permissions already granted.")
- }
- }
- // Check permissions status
- private fun isPermissionsGranted(): Int {
- // PERMISSION_GRANTED : Constant Value: 0
- // PERMISSION_DENIED : Constant Value: -1
- var counter = 0;
- for (permission in list) {
- counter += ContextCompat.checkSelfPermission(activity, permission)
- }
- return counter
- }
- // Find the first denied permission
- private fun deniedPermission(): String {
- for (permission in list) {
- if (ContextCompat.checkSelfPermission(activity, permission)
- == PackageManager.PERMISSION_DENIED) return permission
- }
- return ""
- }
- // Show alert dialog to request permissions
- private fun showAlert() {
- val builder = AlertDialog.Builder(activity)
- builder.setTitle("Need permission(s)")
- builder.setMessage("Some permissions are required to do the task.")
- builder.setPositiveButton("OK", { dialog, which -> requestPermissions() })
- builder.setNeutralButton("Cancel", null)
- val dialog = builder.create()
- dialog.show()
- }
- // Request the permissions at run time
- private fun requestPermissions() {
- val permission = deniedPermission()
- if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) {
- // Show an explanation asynchronously
- activity.toast("Should show an explanation.")
- } else {
- ActivityCompat.requestPermissions(activity, list.toTypedArray(), code)
- }
- }
- // Process permissions result
- fun processPermissionsResult(requestCode: Int, permissions: Array<String>,
- grantResults: IntArray): Boolean {
- var result = 0
- if (grantResults.isNotEmpty()) {
- for (item in grantResults) {
- result += item
- }
- }
- if (result == PackageManager.PERMISSION_GRANTED) return true
- return false
- }
- }
- AndoridManifest.xml
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.permission">
- <uses-permission android:name="android.permission.INTERNET"></uses-permission>
- <uses-permission android:name="android.permission.CAMERA"></uses-permission>
- <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
- <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
- <uses-permission android:name="android.permission.READ_CALENDAR"></uses-permission>
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN"/>
- <category android:name="android.intent.category.LAUNCHER"/>
- </intent-filter>
- </activity>
- </application>
- </manifest>
- string.xml
- <resources>
- <string name="app_name">Permission</string>
- </resources>
- 12: ringtone program
- activity.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:background="@color/colorAccent"
- tools:context=".MainActivity">
- <Button
- android:id="@+id/button_play"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Play Default Ringtone"
- android:layout_gravity="center_horizontal"
- android:layout_margin="10dp"
- />
- <Button
- android:id="@+id/button_stop"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Stop"
- android:layout_gravity="center_horizontal"
- android:layout_margin="10dp"
- />
- <Button
- android:id="@+id/button_media_play"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Play Using Media Player"
- android:layout_gravity="center_horizontal"
- android:layout_margin="10dp"
- />
- <Button
- android:id="@+id/button_media_stop"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Stop Media Player"
- android:layout_gravity="center_horizontal"
- android:layout_margin="10dp"
- />
- </LinearLayout>
- activity.kt
- package com.example.back_sound_service
- import android.content.Context
- import android.media.MediaPlayer
- import android.media.Ringtone
- import android.media.RingtoneManager
- import android.media.audiofx.BassBoost
- import android.support.v7.app.AppCompatActivity
- import android.os.Bundle
- import android.provider.Settings
- import kotlinx.android.synthetic.main.activity_main.*
- class MainActivity : AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- val ringtone: Ringtone = defaultRingtone
- button_play.setOnClickListener{
- if (!ringtone.isPlaying){
- ringtone.play()
- }
- }
- button_stop.setOnClickListener{
- if (ringtone.isPlaying){
- ringtone.stop()
- }
- }
- val mediaPlayer:MediaPlayer = defaultRingtonePlayer()
- button_media_play.setOnClickListener{
- if (!mediaPlayer.isPlaying)mediaPlayer.start()
- }
- button_media_stop.setOnClickListener{
- if (mediaPlayer.isPlaying){
- mediaPlayer.stop()
- }
- }
- }
- }
- val Context.defaultRingtone:Ringtone
- get() {
- val uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE)
- return RingtoneManager.getRingtone(this, uri)
- }
- fun Context.defaultRingtonePlayer():MediaPlayer{
- return MediaPlayer.create(this, Settings.System.DEFAULT_RINGTONE_URI)
- }
- 13:table layout program
- table layout
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#e6f0ff"
- tools:context=".MainActivity">
- <TableLayout android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="50dp"
- android:layout_marginTop="150dp"
- android:background="#cce0ff">
- <TableRow>
- <Button
- android:id="@+id/btn1"
- android:text="1"
- android:layout_gravity="center"
- />
- <Button
- android:id="@+id/btn2"
- android:text="2"
- android:layout_gravity="center"
- />
- <Button
- android:id="@+id/btn3"
- android:text="3"
- android:layout_gravity="center"
- />
- </TableRow>
- <TableRow>
- <Button
- android:id="@+id/btn4"
- android:text="4"
- android:layout_gravity="center"
- />
- <Button
- android:id="@+id/btn5"
- android:text="5"
- android:layout_gravity="center"
- /><Button
- android:id="@+id/btn6"
- android:text="6"
- android:layout_gravity="center"
- />
- </TableRow>
- <TableRow>
- <Button
- android:id="@+id/btn7"
- android:text="7"
- android:layout_gravity="center"
- />
- <Button
- android:id="@+id/btn8"
- android:text="8"
- android:layout_gravity="center"
- /><Button
- android:id="@+id/btn9"
- android:text="9"
- android:layout_gravity="center"
- />
- </TableRow>
- </TableLayout>
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/linearl"
- android:text="LinearLayout"
- />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/relativel"
- android:layout_toRightOf="@+id/linearl"
- android:text="RelativeLayou"
- />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/fragmentl"
- android:layout_toRightOf="@+id/relativel"
- android:text="fragmentLayout" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/listview"
- android:layout_below="@+id/linearl"
- android:text="ListView" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/gridview"
- android:layout_toRightOf="@+id/listview"
- android:layout_below="@+id/linearl"
- android:text="ListView" />
- </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement