I am trying to build my very first app in android studio, "hello, world." so I start a new project and add 2 lines of codes in the bottom.
fun main(args:Array<String>){ printHello()}
fun printHello () {println ("Hello World")}
(1)How do I use above 2 lines of kotlin codes to see the result in android emulator? (2)In another project, I can modify "0" => "hello, world." from res/layout/fragment_first.xml. But I see neither nor fragment_first?xml so where do I go to modify?
package com.example.hello
import android.os.Bundle
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.snackbar.Snackbar
import androidx.appcompat.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(findViewById(R.id.toolbar))
findViewById<FloatingActionButton>(R.id.fab).setOnClickListener { view ->
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return when (item.itemId) {
R.id.action_settings -> true
else -> super.onOptionsItemSelected(item)
}
}
}
fun main(args:Array<String>){ printHello()}
fun printHello () {println ("Hello World")}
To answer your question:
1.
fun main(args:Array<String>){ printHello()}
fun printHello () {println ("Hello World")}
the println would only show in andorid's log, to show something in the emulator, we can use Toast. https://developer.android.com/guide/topics/ui/notifiers/toasts
fun main(args:Array<String>){ printHello()}
fun printHello () {Toast.makeText(this,"Hello World", Toast.LENGTH_LONG).show()}
Full code here with minimal modification to your posted code. You should see the toast message when the app launch.
package com.example.hello
import android.os.Bundle
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.snackbar.Snackbar
import androidx.appcompat.app.AppCompatActivity
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)
setSupportActionBar(findViewById(R.id.toolbar))
findViewById<FloatingActionButton>(R.id.fab).setOnClickListener { view ->
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}
main(arrayOf())// notice the call to the function here
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return when (item.itemId) {
R.id.action_settings -> true
else -> super.onOptionsItemSelected(item)
}
}
fun main(args:Array<String>){ printHello()}
fun printHello () {Toast.makeText(this,"Hello World", Toast.LENGTH_LONG).show()}
}
For building your first app, i would suggest going through the official first app guide, https://developer.android.com/training/basics/firstapp
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.