Mobile App Dev Diary

Android (Java, Kotlin), iOS (Swift), Xamarin (C#)

Objective-C / show and hide indicator in status bar

With objective-c, show and hide indicator in status bar, use this.

Show indicator in status bar.

UIApplication *application = [UIApplication sharedApplication];
[application setNetworkActivityIndicatorVisible:YES];

Hide indicator from status bar.

UIApplication *application = [UIApplication sharedApplication];
[application setNetworkActivityIndicatorVisible:NO];
Remove all ads

Swift 3.0 / unwrap optional values with if let statement

if let statement unwrap optional values.

var value : Int?
value = 5

if let value = value {
    //do something here
    print(String(value))
}

If you never use unwrapped optional value inside if let statement, you can use underscore with if let .

var value : Int?
value = 5

if let _ = value {
    //do something here
}
Remove all ads

Compare BigDecimal value with zero (Android, Java)

use signum() .

BigDecimal bigDecimal = BigDecimal.valueOf(0.0);

if (bigDecimal.signum() == 0) {
    Log.i("compare", "is zero");
} else {
    Log.i("compare", "not zero");
}
Remove all ads

Xamarin.Forms / Prevent iOS statusbar is filled with background color

When you use BackgroundColor = Color.Black; or something, status bar is also filled with the color.

To prevent this, add these two properties to iOS Info.plist.

<key>UIStatusBarStyle</key>                                                                                       
<string>UIStatusBarStyleLightContent</string>
<key>UIViewControllerBasedStatusBarAppearance</key>
Remove all ads

Xamarin.Forms / Prevent iOS statusbar is overwrapped

To avoid iOS statusbar is hidden by UI components, use this.

<?xml version="1.0" encoding="utf-8"?>
<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:calc"
    x:Class="calc.calcPage">

    <!-- for iOS -->
    <!-- prevent statusbar is overwrapped -->
    <ContentPage.Padding>
        <OnPlatform x:TypeArguments="Thickness" iOS="0, 20, 0, 0" />
    </ContentPage.Padding>

</ContentPage>
Remove all ads

Swift 3.0 / Convert String to Unix Timestamp

Convert String to Unix Timestamp in Swift version 3.0.

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateString = "1970-01-01 09:00:00"
let date:Date? = dateFormatter.date(from: dateString)
let dateUnix: TimeInterval? = date?.timeIntervalSince1970

Related entry:

python.hatenablog.jp

Remove all ads

Swift 3.0 / Convert Unix Timestamp to String

Convert Unix Timestamp to String in Swift version 3.0.

let dateUnix:TimeInterval = TimeInterval()
let date = Date(timeIntervalSince1970: dateUnix)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy/MM/dd HH:mm:ss"
let dateString = dateFormatter.string(from: date)

Related entry:

python.hatenablog.jp

Remove all ads

Kotlin 1.1 / Screen transition with Intent

Screen transition in Kotlin version 1.1.

When you transite to “NextActivity”, use this.

val intent = Intent(applicationContext, NextActivity::class.java)
startActivity(intent)

Or you may want to define package-level function to instantiate Intent. Simply speaking, “package-level” means outside class definition.

//package-level functiono
fun getNextActivityIntent(context: Context) : Intent {
    return Intent(context, NextActivity::class.java)
}

You can call this function from Activity class.

startActivity(getDetailActivityIntent(applicationContext))
Remove all ads

Kotlin 1.1 / Display Toast

Display Toast in Kotlin version 1.1.

Short-duration.

Toast.makeText(this, "Good morning!", Toast.LENGTH_SHORT).show()

Long-duration.

Toast.makeText(this, "Good evening!", Toast.LENGTH_LONG).show()
Remove all ads

Kotlin 1.1 / setOnClickListener

Set “setOnClickListener” to Button in Kotlin version 1.1.

val myButton: Button = findViewById(R.id.menu_camera) as Button
myButton.setOnClickListener {
    //onClick
}
Remove all ads