Convert Pixels to DP in an Android App Using Kotlin

Azhar
Updated on 05-Nov-2020 14:40:46

795 Views

This example demonstrates how to convert pixels to dp in an Android App using Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.             Step 3 − Add the following code to src/MainActivity.ktimport android.content.Context import android.os.Bundle import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import kotlin.math.roundToInt class MainActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {       super.onCreate(savedInstanceState)       setContentView(R.layout.activity_main)       title = "KotlinApp" ... Read More

Show Current Location on Google Map in Android using Kotlin

Azhar
Updated on 05-Nov-2020 14:40:08

5K+ Views

This example demonstrates how to show current location on a Google Map on Android using Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.Step 3 − Add the given dependency in the build.gradle (Module: app)implementation 'com.google.android.gms:play-services-maps:17.0.0' implementation 'com.google.android.gms:play-services-location:17.0.0' implementation 'com.google.android.gms:play-services-maps:17.0.0'Step 4 − Add the following code to src/MainActivity.ktimport android.Manifest import android.content.pm.PackageManager import android.location.Location import android.os.Bundle import android.widget.Toast import androidx.core.app.ActivityCompat import androidx.fragment.app.FragmentActivity import com.google.android.gms.location.FusedLocationProviderClient import com.google.android.gms.location.LocationServices import com.google.android.gms.maps.CameraUpdateFactory import com.google.android.gms.maps.GoogleMap import com.google.android.gms.maps.OnMapReadyCallback import com.google.android.gms.maps.SupportMapFragment import com.google.android.gms.maps.model.LatLng ... Read More

Get IP Address of Android Device Programmatically Using Kotlin

Azhar
Updated on 05-Nov-2020 14:37:08

4K+ Views

This example demonstrates how to get the IP address of the Android device programmatically using Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.         Step 3 − Add the following code to src/MainActivity.ktimport android.content.Context import android.net.wifi.WifiManager import android.os.Bundle import android.text.format.Formatter import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {       super.onCreate(savedInstanceState)       setContentView(R.layout.activity_main)       title = "KotlinApp"   ... Read More

Retrieve Android API Version Programmatically using Kotlin

Azhar
Updated on 05-Nov-2020 14:36:13

859 Views

This example demonstrates how to retrieve Android API version programmatically using Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.             Step 3 − Add the following code to src/MainActivity.ktimport android.os.Build import android.os.Bundle import android.widget.Button import android.widget.TextView import android.widget.Toast import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() {    lateinit var button: Button    lateinit var textView: TextView    var androidVersion = 0    override fun onCreate(savedInstanceState: Bundle?) {     ... Read More

Draw a Line in Android Using Kotlin

Azhar
Updated on 05-Nov-2020 14:35:29

1K+ Views

This example demonstrates how to draw a line in Android using Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.         Step 3 − Add the following code to src/MainActivity.ktimport android.graphics.Bitmap import android.graphics.Canvas import android.graphics.Color import android.graphics.Paint import android.os.Bundle import android.widget.Button import android.widget.ImageView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() {    lateinit var button: Button    lateinit var imageView: ImageView    override fun onCreate(savedInstanceState: Bundle?) {       super.onCreate(savedInstanceState)   ... Read More

Rotate Image in ImageView by Angle on Android using Kotlin

Azhar
Updated on 05-Nov-2020 14:34:35

1K+ Views

This example demonstrates how to rotate an image in imageview by an angle on Android using Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.         Step 3 − Add the following code to src/MainActivity.ktimport android.os.Bundle import android.widget.Button import android.widget.ImageView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() {    lateinit var imageView: ImageView    lateinit var btnRotate: Button    override fun onCreate(savedInstanceState: Bundle?) {       super.onCreate(savedInstanceState)       setContentView(R.layout.activity_main) ... Read More

Format Decimal to Two Places or Integer in C#

Nizamuddin Siddiqui
Updated on 05-Nov-2020 13:56:19

4K+ Views

Converts the value of objects to strings based on the formats specified and inserts them into another string.Namespace:System Assembly:System.Runtime.dllEach overload of the Format method uses the composite formatting feature to include zero-based indexed placeholders, called format items, in a composite format string. At run time, each format item is replaced with the string representation of the corresponding argument in a parameter list. If the value of the argument is null, the format item is replaced with String.Empty.Exampleclass Program{    static void Main(string[] args){       int number = 123;       var s = string.Format("{0:0.00}", number);     ... Read More

Convert String to Title Case in C#

Nizamuddin Siddiqui
Updated on 05-Nov-2020 13:55:21

745 Views

Title case is any text, such as in a title or heading, where the first letter of major words is capitalized. Title case or headline case is a style of capitalization used for rendering the titles of published works or works of art in English.When using title case, all words are capitalized except for "minor" words unless they are the first or last word of the title.The current implementation of the ToTitleCase in the example yields an output string that is the same length as the input string.Example 1class Program{    static void Main(string[] args){       string myString ... Read More

Set Property Value by Reflection in C#

Nizamuddin Siddiqui
Updated on 05-Nov-2020 13:53:37

6K+ Views

The System. Reflection namespace contains classes that allow you to obtain information about the application and to dynamically add types, values, and objects to the application.Reflection objects are used for obtaining type information at runtime. The classes that give access to the metadata of a running program are in the System. Reflection namespace.Reflection allows view attribute information at runtime.Reflection allows examining various types in an assembly and instantiate these types.Reflection allows late binding to methods and properties.Reflection allows creating new types at runtime and then performs some tasks using those types.ExampleGetProperty(String)Searches for the public property with the specified name.GetType(String, Boolean)Gets ... Read More

Rethrow InnerException Without Losing Stack Trace in C#

Nizamuddin Siddiqui
Updated on 05-Nov-2020 13:52:36

1K+ Views

In c#, the throw is a keyword and it is useful to throw an exception manually during the execution of the program and we can handle those thrown exceptions using try−catch blocks based on our requirements.By using throw keyword in the catch block, we can re-throw an exception that is handled in the catch block. The re-throwing an exception is useful when we want to pass an exception to the caller to handle it in a way they want.Following is the example of re−throwing an exception to the caller using throw keyword with try-catch blocks in c#.Exampleclass Program{    static ... Read More

Advertisements