package com.example.mobilewebpages
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
WebPages()
}
}
}
@Composable
fun WebPages() {
val context = LocalContext.current // dostęp do kontekstu w komponencie
val urls = listOf(
"https://cez.lodz.pl",
"http://www.palacmlodziezy.lodz.pl",
"http://pracownia.palacmlodziezy.lodz.pl",
"http://www.bbc.com"
)
val buttonLabels = listOf(
"cez",
"pałac",
"młodzi programiści",
"bbc"
)
Column {
urls.forEachIndexed { index, url -> // petla generująca przyciski
val label = buttonLabels[index]
//Log.i("==== przycisk ======", "Przycisk $label na URL: $url")
Button(
colors = ButtonDefaults.buttonColors(
containerColor = Color((0xFF shl 24) or (0xFFFFFF * Math.random()).toInt()), // Kolor tła
contentColor = Color.White // Kolor tekstu
),
shape = MaterialTheme.shapes.large,
onClick = {
val intent = Intent(Intent.ACTION_VIEW).apply { // tworzymy nowy Intent z akcją Intent.ACTION_VIEW,
data = Uri.parse(url) // jest to standardowy sposób na otwieranie stron internetowych.
}
context.startActivity(intent) // uruchamiamy przeglądarkę
},
modifier = Modifier.fillMaxWidth() // przycisk wypełnia całą szerokość
) {
Text(text = "$index $label") // tekst na przycisku
}
}
}
}
@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
WebPages()
}
source https://deepai.org/chat