0%
Loading ...

Kotlin for Android

Toast in Android using Kotlin

Toast is used to show small text messages on the screen for short time. The toast message stays for about 2, 3, or 5 seconds on screen after that it disappears automatically. The main motive of the toast message is to inform the user such as to warn a user who is trying to enter the number in the name field of the form. There is a dedicated Toast class available in Android SDK to make and show toast messages. The Toast class contains a static method called makeText() that is used to make a toast message. Check out the syntax to create and show a toast message. Toast.makeText(Context, String message, Toast duration).show(); As in the above syntax, you can clearly see we have used another method called show() to show a toast message on the screen. To mention toast duration static variables LENGTH_SHORT or LENGTH_LONG are available in the Toast class.  Example program to show simple toast message:  Toast.makeText(this, “Hello! This is a Toast message.”, Toast.LENGTH_LONG).show()   Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Toast in Android using Kotlin Read More »

EditText in Android using Kotlin

EditText is one of the most commonly used widgets in Android applications. EditText widget is used to get input from the user, for example, you have a login form in your application and you want to ask for an email and password from the user, for this purpose you can use EditText to get data. Android provides us wide range of properties to customize the EditText widget. Follow these steps to add an EditText widget in Android. Add an EditText widget in the XML layout file. Add required attributes such as hint, textColor, textSize, inputType, background to the EditText widget. Open Kotlin file to modify attributes and to set Listeners for the EditText. Adding EditText in the layout file: <?xml version=”1.0″ encoding=”utf-8″?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” xmlns:app=”http://schemas.android.com/apk/res-auto” xmlns:tools=”http://schemas.android.com/tools” android:layout_width=”match_parent” android:layout_height=”match_parent” android:orientation=”vertical” android:padding=”16dp” tools:context=”.MainActivity”> <TextView android:layout_height=”wrap_content” android:layout_width=”wrap_content” android:text=”Login” android:textSize=”20sp” android:textStyle=”bold” android:layout_marginBottom=”15dp” /> <EditText android:layout_width=”match_parent” android:layout_height=”wrap_content” android:hint=”Enter email” android:id=”@+id/emailET” android:inputType=”textEmailAddress” android:layout_marginBottom=”15dp” /> <EditText android:layout_width=”match_parent” android:layout_height=”wrap_content” android:hint=”Enter password” android:id=”@+id/passwordET” android:inputType=”textPassword” android:layout_marginBottom=”15dp” /> <Button android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:paddingHorizontal=”15dp” android:paddingVertical=”10dp” android:text=”Click Me” android:textSize=”18sp” android:textColor=”@color/white” android:id=”@+id/loginB” android:background=”@color/blue” /> </LinearLayout> In the above example, you can see we have created a login form and in this form, we used two EditText widgets to ask for email and password from the user. We have used white for the text color and blue for the background in the button widget. You can add these two colors to your colors.xml file.  <color name=”white”>#FFFFFFFF</color> <color name=”blue”>#304FFE</color> List of available attributes for EditText widget: Attributes Description android:id This attribute is used to uniquely identify the control android:gravity This attribute is used to specify how to align the text like left, right, center, top, etc. android:hint This attribute is used to display the hint text when text is empty android:text This attribute is used to set the text of the EditText android:textSize This attribute is used to set the size of the text. android:textColor This attribute is used to set the color of the text. android:textStyle This attribute is used to set the style of the text. For example, bold, italic, bolditalic, etc. android:textAllCaps This attribute is used this attribute to show the text in capital letters. android:width This attribute makes the TextView be exactly this many pixels wide. android:height This attribute makes the TextView be exactly this many pixels tall. android:maxWidth This attribute is used to make the TextView be at most this many pixels wide. android:minWidth This attribute is used to make the TextView be at least this many pixels wide. android:background This attribute is used to set the background to this View. android:backgroundTint This attribute is used to set tint to the background of this view. android:clickable This attribute is used to set true when you want to make this View clickable. Otherwise, set false. android:drawableBottom This attribute is used to set drawable to the bottom of the text in this view. android:drawableEnd This attribute is used to set drawable to the end of the text in this view. android:drawableLeft This attribute is used to set drawable to the left of the text in this view. android:drawablePadding This attribute is used to set padding to be drawable of the view. android:drawableRight This attribute is used to set drawable to the right of the text in this view. android:drawableStart This attribute is used to set drawable to the start of the text in this view. android:drawableTop This attribute is used to set drawable to the top of the text in this view. android:elevation This attribute is used to set the elevation to this view. Set click listener to EditText using Kotlin: class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val button = findViewById<Button>(R.id.loginB) as Button val emailET = findViewById<EditText>(R.id.emailET) as EditText val passwordET = findViewById<EditText>(R.id.passwordET) as EditText button.setOnClickListener(View.OnClickListener { val email=emailET.text.toString() val password=passwordET.text.toString() if(email.equals(“abc@abc.com”) && password.equals(“12345”)) { Toast.makeText(this@MainActivity, “Login done”, Toast.LENGTH_SHORT) .show() }else{ Toast.makeText(this@MainActivity, “Wrong email or password”, Toast.LENGTH_SHORT) .show() } }) } } In the above example program, If a user will enter email= abc@abc.com and password= 12345. Then on button click, a toast message Login done will appear on the screen otherwise Wrong email or password message will appear.  Final Output: Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

EditText in Android using Kotlin Read More »

Button in Android using Kotlin

The button widget in Android can be used for various purposes. It totally depends on the requirements of your project. We often see buttons in the login and signup form. Android provides us wide range of properties to customize the button widget. Follow these steps to add a Button widget in Android activity.  Add a Button in the XML layout file of yours activity. Add required attributes such as text, textColor, textSize, background to the Button widget. Open Kotlin file to modify attributes of textView and to set Listeners for the textView. Adding Button in the layout file: <?xml version=”1.0″ encoding=”utf-8″?> <RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android” xmlns:app=”http://schemas.android.com/apk/res-auto” xmlns:tools=”http://schemas.android.com/tools” 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:paddingHorizontal=”15dp” android:paddingVertical=”10dp” android:text=”Click Me” android:textSize=”18sp” android:textColor=”@color/white” android:id=”@+id/button” android:background=”@color/blue” android:layout_centerInParent=”true” /> </RelativeLayout> In the above example, you can see we have added a Button widget in our XML file. Furthermore, we have also added some properties such as padding, textSize, textColor, and background to our Button widget. We have used white for the text color and blue for the background. you can add these two colors to your colors.xml file.  <color name=”white”>#FFFFFFFF</color> <color name=”blue”>#304FFE</color> List of available attributes for Button widget: Attributes Description android:id This attribute is used to set the id of the view. android:text This attribute is used to the display text of the button. android:textColor This attribute is used to the set color of the text. android:textSize This attribute is used to the set size of the text. android:textStyle This attribute is used to set the style of the text such as Bold, Italic, etc. android:textAllCaps This attribute is used to set text in Capital letters such as true, false android:background This attribute is used to set the background of the Button. android:padding This attribute is used to set the padding of the Button. android:visibility This attribute is used to set the visibility of the view such as visible, gone. android:gravity This attribute is used to specify the gravity of the view such as center, top, bottom, etc After adding this widget into the XML file our next step is to set a listener to our button. so to do this we have to move to our Kotlin file and there we will set an onClickListerner for our button widget.  Set click listener to Button using Kotlin: class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val button = findViewById<Button>(R.id.button) as Button button.setOnClickListener(View.OnClickListener { Toast.makeText(this@MainActivity, “You have Clicked on Button”, Toast.LENGTH_SHORT).show() }) } } As you can see in the above example program we have created a variable called the button and we assigned our XML widget to this variable with the help of findViewById method. After that, we have set a click listener to our button to show a toast message.  Output: Here is the final output. when we click on the Button widget. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Button in Android using Kotlin Read More »

TextView in Android using Kotlin

The Android TextView widget is used to display the text on the screen such as headings or paragraphs. Steps to add TextView in Android activity. Add a TextView in the XML layout file of yours activity. Add required attributes such as text, textColor, textSize, textStyle to textView widget. Open Kotlin file to modify attributes of textView and to set Listeners for the textView. Adding TextView in the layout file: As you can see here we have added a TextView into our layout file. We have also added some attributes to our TextView such as text, textSize, textStyle, etc (You can add attributes to the widget according to requirements of style). <?xml version=”1.0″ encoding=”utf-8″?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android=”http://schemas.android.com/apk/res/android” xmlns:app=”http://schemas.android.com/apk/res-auto” xmlns:tools=”http://schemas.android.com/tools” 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!” android:textSize=”25sp” android:textStyle=”bold” app:layout_constraintBottom_toBottomOf=”parent” app:layout_constraintLeft_toLeftOf=”parent” app:layout_constraintRight_toRightOf=”parent” app:layout_constraintTop_toTopOf=”parent” /> </androidx.constraintlayout.widget.ConstraintLayout> List of available attributes for TextView widget. Attributes Description android:text Sets the text of the Textview android:id Gives a unique ID to the Textview android:cursorVisible Use this attribute to make the cursor visible or invisible. The default value is visible. android:drawableBottom Sets images or other graphic assets below of the Textview. android:drawableEnd Sets images or other graphic assets to end of Textview. android:drawableLeft Sets images or other graphic assets to the left of Textview. android:drawablePadding Sets padding to the drawable(images or other graphic assets) in the Textview. android:autoLink This attribute is used to automatically detect URL or emails and show it as clickable links. android:autoText Automatically correct spelling errors in the text of the Textview. android:capitalize It automatically capitalizes whatever the user types in the Textview. android:drawableRight Sets drawable to the right of the text in the Textview. android:drawableStart Sets drawable to start of text in the Textview. android:drawableTop Sets drawable to top of the text in the Textview. android:ellipsize Use this attribute when you want the text to be ellipsize if it is longer than the Textview width. android:ems Sets width of the Textview in ems. android:gravity We can align the text of the Textview vertically or horizontally or both. android:height Use to set the height of the Textview. android:hint Use to show hints when there is no text. android:inputType Use to set input type of the Textview. It can be Number, Password, Phone, etc. android:lines Use to set the height of the Textview by the number of lines. android:maxHeight Sets the maximum height of the Textview. android:minHeight Sets the minimum height of the Textview. android:maxLength Sets maximum character length of the Textview. android:maxLines Sets maximum lines Textview can have. android:minLines Sets minimum lines Textview can have. android:maxWidth Sets maximum width Textview can have. android:minWidth Sets minimum lines Textview can have. android:textAllCaps Show all texts of the Textview in capital letters. android:textColor Sets color of the text. android:textSize Sets the font size of the text. android:textStyle Sets style of the text. For example, bold, italic, bolditalic. android:typeface Sets typeface or font of the text. For example, normal, sans, serif, etc android:width Sets width of the TextView.   Set click listener to TextView using Kotlin: In the following code firstly we linked a variable with our TextView widget with the help of the findViewById method, Then we set up an onClick listener to our TextView widget to show toast in case someone will click on the widget. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val textView = findViewById<TextView>(R.id.textView) as TextView textView.setOnClickListener(View.OnClickListener { Toast.makeText(this@MainActivity, “You have Clicked on TextView”, Toast.LENGTH_SHORT).show() }) } } Here is the final output: Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

TextView in Android using Kotlin Read More »

First project in Android using Kotlin

It is petty easy to start a project in Android. To start an Android project we assumed that you already have Android Studio Installed in your machine, If you haven't then please click here to install Android studio first. After installing the android studio please follow the steps mentioned below. Let's start creating the first project in Android using Kotlin. We hope you already have good knowledge of Kotlin. If you are new to Kotlin then please learn it first click here to start with Kotlin. Follow these steps: 1. Launch Android Studio in your machine then the following popup will appear on the screen. Click on create new project button to start the project. In case there is already a project open in Android Studio then you can click on File>Create New Project.  2. After clicking on create a new project the following popup will appear on the screen which will allow you to choose an activity template for your project you can choose any according to the requirement of your project. To keep it simple we are choosing here Empty Activity. Click next after choosing the activity template. 3. Now this new popup window will appear on the screen which will allow you to choose Application name, package name, save location(where you want to save your project in your machine), minimum SDK version and most important language(Don't forget to choose Kotlin here). After you are done click on the finish button. 4. Congratulations you have successfully created your First project in Android using Kotlin. To run this application on a virtual or physical device click on the play button. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

First project in Android using Kotlin Read More »

Scroll to Top
×