Skip to main content

Posts

Showing posts from February, 2018

android kotlin - ViewPager example

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.viewpager.widget.ViewPager class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Get the widgets reference from XML layout val viewPager = findViewById<ViewPager>(R.id.viewPager) // Initialize a list of string values val alphabets = listOf("a","b","c","d","e","f") // Initialize a new pager adapter instance with list val adapter = AlphabetPagerAdapter(alphabets) // Finally, data bind the view pager widget with pager adapter viewPager.adapter = adapter } } activity_main.xml <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widge...

android kotlin - Handler and Runnable example

MainActivity.kt package com.cfsuman.kotlinexamples import android.graphics.Color import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.os.Handler import kotlinx.android.synthetic.main.activity_main.* import java.util.* class MainActivity : AppCompatActivity() { private lateinit var mRandom:Random private lateinit var mHandler: Handler private lateinit var mRunnable:Runnable override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Initialize a new Random instance mRandom = Random() // Initialize the handler instance mHandler = Handler() // Set a click listener for run button button_run.setOnClickListener{ mRunnable = Runnable { // Do something here root_layout.setBackgroundColor(randomHSVColor()) text_view.text = "Handler Example\n...

android kotlin - SwipeRefreshLayout example

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import androidx.swiperefreshlayout.widget.SwipeRefreshLayout import kotlin.random.Random class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Get the widgets reference from XML layout val swipeRefreshLayout = findViewById<SwipeRefreshLayout>(R.id.swipeRefreshLayout) val textView = findViewById<TextView>(R.id.textView) // Set an on refresh listener for swipe refresh layout swipeRefreshLayout.setOnRefreshListener { textView.text = "Refreshed & Generated Random Number\n" + "${Random.nextInt(500)}" // Hide swipe to refresh icon animation swipeRefresh...

android kotlin - Snackbar example

MainActivity.kt package com.cfsuman.kotlintutorials import android.graphics.Color import android.graphics.Typeface import android.os.Bundle import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import androidx.constraintlayout.widget.ConstraintLayout import com.google.android.material.snackbar.Snackbar class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Get the widgets reference from XML layout val rootLayout = findViewById<ConstraintLayout>(R.id.rootLayout) val buttonSimple = findViewById<Button>(R.id.buttonSimple) val buttonAction = findViewById<Button>(R.id.buttonAction) val buttonStyle = findViewById<Button>(R.id.buttonStyle) // Button click listener buttonSimple.setOnClickListener{ Snackbar.make...

android kotlin - Toolbar example

MainActivity.kt package com.cfsuman.kotlintutorials import android.graphics.Color import android.os.Bundle import android.view.Menu import android.view.MenuItem import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.widget.Toolbar class MainActivity : AppCompatActivity() { private lateinit var textView: TextView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Get the widgets from XML layout val toolbar = findViewById<Toolbar>(R.id.toolbar); textView = findViewById(R.id.textView); // Configure the Toolbar toolbar.apply { title = "Toolbar title" setTitleTextColor(Color.WHITE) setLogo(R.drawable.ic_action_help) //subtitle = "Toolbar Subtitle" //setSubtitleTextColor(Color.WHITE) } // Set the t...

android kotlin - Menu and onCreateOptionsMenu example

Kotlin Menu & onCreateOptionsMenu The Menu is a component of the native android SDK. The Menu is an android user interface component. Android SDK’s Menu API use to present user actions and other options in an Activity. The options menu is the primary menu for an activity where developers place the actions. The following android kotlin tutorial will demonstrate how we can create an options Menu and place it in the android app user interface. This tutorial is written in Kotlin programming language with native android SDK tools. Android SDK provides a standard XML format to define menu items. Instead, kotlin code developers should define the menu and its items in an XML menu resource. Then android developers can inflate the menu resource in the activity or fragment it as a Menu object. Menu resource is easy to visualize and also easy to organize the menu structure in XML. The Menu resource separates content from Kotlin code. It allows us to create altern...

android kotlin - Convert dp to px to dp

MainActivity.kt package com.example.jetpack import android.app.Activity import android.content.Context import android.os.Build import android.os.Bundle import android.util.DisplayMetrics import android.util.TypedValue import androidx.appcompat.app.AppCompatActivity import kotlinx.android.synthetic.main.activity_main.* import kotlin.math.roundToInt import kotlin.random.Random class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // generate random integer and float value val random = Random.nextInt(50,100) val randomFloat = Random.nextFloat() * 100 // show the screen density textView.text = "Screen Density: ${displayMetrics.density}" // convert integer dp value to equivalent pixels value textView.append("\n\nRandom Number : $random") val pixels = random.dpToPixels(t...

android kotlin - TimePickerDialog example

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.widget.* import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Get the widgets reference from XML layout val button = findViewById<Button>(R.id.button) // Set a click listener for button widget button.setOnClickListener{ // Initialize a new TimePickerFragment val newFragment = TimePickerFragment() // Show the time picker dialog newFragment.show( supportFragmentManager, "Time Picker" ) } } } TimePickerFragment.kt package com.cfsuman.kotlintutorials import android.app.TimePickerDialog import android.os.Bundle import android.widget.TextView import android.ap...

android kotlin - DatePickerDialog example

MainActivity.kt package com.cfsuman.kotlintutorials import android.content.Context import android.os.Bundle import android.widget.Button import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { private lateinit var context: Context override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Get the widgets reference from XML layout val button = findViewById<Button>(R.id.button) // Set a click listener for button widget button.setOnClickListener{ // Initialize a new DatePickerFragment val datePickerFragment = DatePickerFragment() // Show the date picker dialog datePickerFragment.show( supportFragmentManager, "Date Picker" ) } } } DatePickerFragment.kt package com.cfsuman.kotlintutorials import android.ap...

android kotlin - DatePicker example

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.widget.* import androidx.appcompat.app.AppCompatActivity import java.text.DateFormat import java.util.* import kotlin.random.Random class MainActivity : AppCompatActivity() { private lateinit var calendar:Calendar override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Get the widgets reference from XML layout val textView = findViewById<TextView>(R.id.textView) val datePicker = findViewById<DatePicker>(R.id.datePicker) val buttonGet = findViewById<Button>(R.id.buttonGet) val buttonSet = findViewById<Button>(R.id.buttonSet) // Initialize a new calendar instance calendar = Calendar.getInstance() // Get the Calendar current year, month and day of month val thisYear = calendar.get(Calendar.YEAR) ...

android kotlin - TimePicker example

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.widget.* import androidx.appcompat.app.AppCompatActivity import kotlin.random.Random class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Get the widgets reference from XML layout val textView = findViewById<TextView>(R.id.textView) val timePicker = findViewById<TimePicker>(R.id.timePicker) val buttonGet = findViewById<Button>(R.id.buttonGet) val buttonSet = findViewById<Button>(R.id.buttonSet) val buttonToggle = findViewById<Button>(R.id.buttonToggle) // Set a time change listener for time picker widget timePicker.setOnTimeChangedListener{ view,hourOfDay,minute-> textView.text = "Time Changed ${getHourAMPM(hourOfDay)} " ...

android kotlin - Create CardView programmatically

MainActivity.kt package com.cfsuman.kotlintutorials import android.graphics.Color import android.os.Bundle import android.widget.* import androidx.appcompat.app.AppCompatActivity import androidx.cardview.widget.CardView import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintLayout.LayoutParams class MainActivity : AppCompatActivity() { private lateinit var context:MainActivity override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Get the context context = this // Get the widgets reference from XML layout val rootLayout = findViewById<ConstraintLayout>(R.id.rootLayout) val button = findViewById<Button>(R.id.button) // Set a click listener for button widget button.setOnClickListener{ // Disable the button itself it.isEnabled = false ...

android kotlin - EditText example

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.Editable import android.text.TextWatcher import android.widget.* import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Get the widgets reference from XML layout val tvOutput = findViewById<TextView>(R.id.tvOutput) val editText = findViewById<EditText>(R.id.editText) val buttonSet = findViewById<Button>(R.id.buttonSet) val buttonGet = findViewById<Button>(R.id.buttonGet) val buttonClear = findViewById<Button>(R.id.buttonClear) // Set some text to the EditText buttonSet.setOnClickListener{ editText.setText("Jones") } // Get the EditText text on button click event butt...

android kotlin - AutoCompleteTextView example

AutoCompleteTextView The AutoCompleteTextView is an editable text view. The AutoCompleteTextView shows completion suggestions automatically while the app user is typing. The AutoCompleteTextView’s list of suggestions is displayed in a drop-down menu. The android app users can choose an item from the suggestions to replace the content of the edit box with. The android app users can dismiss the AutoCompleteTextView suggestions drop-down menu any time by pressing the back key, or if no item is selected in the drop-down, by pressing the enter key. So, from where did the AutoCompleteTextView suggestions are coming from? The AutoCompleteTextView suggestions list is obtained from a data adapter. The AutoCompleteTextView suggestions appear only after a given number of characters defined by the ‘threshold’. So, how we implemented the AutoCompleteTextView in our tutorial? At first, we create an array of String values. Then we create an instance of ArrayAdapter with...

android kotlin - Fragment example

MainActivity.kt package com.cfsuman.kotlinexamples import android.support.v7.app.AppCompatActivity import android.os.Bundle import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Set a click listener for first button widget button1.setOnClickListener { // Get the text fragment instance val textFragment = TextFragment() // Get the support fragment manager instance val manager = supportFragmentManager // Begin the fragment transition using support fragment manager val transaction = manager.beginTransaction() // Replace the fragment on container transaction.replace(R.id.fragment_container,textFragment) transaction.addToBackStack(null) // Finishing the transition ...