Zum Inhalt

Android Studio Cheatsheet

Android Studio - Die offizielle IDE für Android Development

Android Studio ist das offizielle Integrated Development Environment (IDE) für das Android-Betriebssystem von Google, das auf der IntelliJ IDEA-Software von JetBrains basiert und speziell für Android-Entwicklung entwickelt wurde. < p>

generieren

Inhaltsverzeichnis

Installation

Systemanforderungen

# Windows
# 64-bit Microsoft Windows 8/10/11
# x86_64 CPU architecture; 2nd generation Intel Core or newer, or AMD CPU with support for Windows Hypervisor
# 8 GB RAM or more
# 8 GB of available disk space minimum (IDE + Android SDK + Android Emulator)
# 1280 x 800 minimum screen resolution

# macOS
# macOS 10.14 (Mojave) or higher
# ARM-based chips, or 2nd generation Intel Core or newer with support for Hypervisor.Framework
# 8 GB RAM or more
# 8 GB of available disk space minimum (IDE + Android SDK + Android Emulator)
# 1280 x 800 minimum screen resolution

# Linux
# Any 64-bit Linux distribution that supports Gnome, KDE, or Unity DE; GNU C Library (glibc) 2.31 or later
# x86_64 CPU architecture; 2nd generation Intel Core or newer, or AMD processor with support for AMD Virtualization (AMD-V) and SSSE3
# 8 GB RAM or more
# 8 GB of available disk space minimum (IDE + Android SDK + Android Emulator)
# 1280 x 800 minimum screen resolution
```_

### Download und Installieren
```bash
# Download from https://developer.android.com/studio
# Run the installer and follow the setup wizard

# Verify installation
android --version
adb --version
```_

### SDK Setup
```bash
# Open Android Studio
# Go to Tools > SDK Manager
# Install required SDK platforms and tools

# Command line SDK manager
sdkmanager --list
sdkmanager "platforms;android-33"
sdkmanager "build-tools;33.0.0"
sdkmanager "system-images;android-33;google_apis;x86_64"
```_

## Erste Schritte

### Neues Projekt erstellen
```bash
# File > New > New Project
# Choose a template (Empty Activity, Basic Activity, etc.)
# Configure project settings:
# - Name: MyApp
# - Package name: com.example.myapp
# - Save location: /path/to/project
# - Language: Java/Kotlin
# - Minimum SDK: API 21 (Android 5.0)
```_

### Projektvorlagen
```kotlin
// Empty Activity
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

// Basic Activity with Fragment
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        if (savedInstanceState == null) {
            supportFragmentManager.beginTransaction()
                .replace(R.id.container, MainFragment.newInstance())
                .commitNow()
        }
    }
}

// Bottom Navigation Activity
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val navView: BottomNavigationView = findViewById(R.id.nav_view)
        val navController = findNavController(R.id.nav_host_fragment)
        navView.setupWithNavController(navController)
    }
}
```_

## Projektstruktur

MyApp/ ├── app/ │ ├── src/ │ │ ├── main/ │ │ │ ├── java/com/example/myapp/ │ │ │ │ ├── MainActivity.kt │ │ │ │ ├── fragments/ │ │ │ │ ├── adapters/ │ │ │ │ ├── models/ │ │ │ │ └── utils/ │ │ │ ├── res/ │ │ │ │ ├── layout/ │ │ │ │ ├── values/ │ │ │ │ ├── drawable/ │ │ │ │ ├── mipmap/ │ │ │ │ └── menu/ │ │ │ └── AndroidManifest.xml │ │ ├── test/ │ │ └── androidTest/ │ ├── build.gradle │ └── proguard-rules.pro ├── gradle/ ├── build.gradle ├── settings.gradle └── gradle.properties ```_

Benutzeroberfläche

Aktivitäten

```kotlin // Basic Activity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main)

    // Initialize views
    val button = findViewById<Button>(R.id.button)
    button.setOnClickListener {
        // Handle click
        Toast.makeText(this, "Button clicked!", Toast.LENGTH_SHORT).show()
    }
}

override fun onStart() {
    super.onStart()
    // Activity is becoming visible
}

override fun onResume() {
    super.onResume()
    // Activity is now visible and interactive
}

override fun onPause() {
    super.onPause()
    // Activity is partially obscured
}

override fun onStop() {
    super.onStop()
    // Activity is no longer visible
}

override fun onDestroy() {
    super.onDestroy()
    // Activity is being destroyed
}

}

// Activity with Intent class SecondActivity : AppCompatActivity() { companion object { const val EXTRA_MESSAGE = "extra_message"

    fun newIntent(context: Context, message: String): Intent {
        return Intent(context, SecondActivity::class.java).apply {
            putExtra(EXTRA_MESSAGE, message)
        }
    }
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_second)

    val message = intent.getStringExtra(EXTRA_MESSAGE)
    findViewById<TextView>(R.id.textView).text = message
}

}

// Starting an Activity val intent = SecondActivity.newIntent(this, "Hello from MainActivity!") startActivity(intent)

// Starting Activity for Result private val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> if (result.resultCode == Activity.RESULT_OK) { val data = result.data?.getStringExtra("result_data") // Handle result } }

launcher.launch(intent) ```_

Duftstoffe

```kotlin // Basic Fragment class MainFragment : Fragment() {

companion object {
    fun newInstance() = MainFragment()
}

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    return inflater.inflate(R.layout.fragment_main, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    val button = view.findViewById<Button>(R.id.button)
    button.setOnClickListener {
        // Handle click
    }
}

}

// Fragment with Arguments class DetailFragment : Fragment() {

companion object {
    private const val ARG_ITEM_ID = "item_id"

    fun newInstance(itemId: String) = DetailFragment().apply {
        arguments = Bundle().apply {
            putString(ARG_ITEM_ID, itemId)
        }
    }
}

private val itemId: String by lazy {
    arguments?.getString(ARG_ITEM_ID) ?: ""
}

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    return inflater.inflate(R.layout.fragment_detail, container, false)
}

}

// Fragment Transaction supportFragmentManager.beginTransaction() .replace(R.id.container, DetailFragment.newInstance("123")) .addToBackStack(null) .commit() ```_

Layouts und Ansichten

Lineares Layout

```xml

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:textSize="18sp" />

<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Click Me" />

```_

Layout einschränken

```xml

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click Me"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView" />

```_

Recycling Blick

```kotlin // Adapter class MyAdapter(private val items: List) : RecyclerView.Adapter() {

class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
    val textView: TextView = view.findViewById(R.id.textView)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    val view = LayoutInflater.from(parent.context)
        .inflate(R.layout.item_layout, parent, false)
    return ViewHolder(view)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    holder.textView.text = items[position]
}

override fun getItemCount() = items.size

}

// Setup RecyclerView val recyclerView = findViewById(R.id.recyclerView) recyclerView.layoutManager = LinearLayoutManager(this) recyclerView.adapter = MyAdapter(listOf("Item 1", "Item 2", "Item 3")) ```_

ViewPager2 mit Fragmenten

```kotlin // Fragment Adapter class ViewPagerAdapter(activity: FragmentActivity) : FragmentStateAdapter(activity) {

override fun getItemCount(): Int = 3

override fun createFragment(position: Int): Fragment {
    return when (position) {
        0 -> FirstFragment()
        1 -> SecondFragment()
        2 -> ThirdFragment()
        else -> FirstFragment()
    }
}

}

// Setup ViewPager2 val viewPager = findViewById(R.id.viewPager) viewPager.adapter = ViewPagerAdapter(this)

// With TabLayout val tabLayout = findViewById(R.id.tabLayout) TabLayoutMediator(tabLayout, viewPager) { tab, position -> tab.text = "Tab ${position + 1}" }.attach() ```_

Ressourcen

Streicher

```xml

My App Hello World! Welcome, %1$s!

<plurals name="numberOfItems">
    <item quantity="one">%d item</item>
    <item quantity="other">%d items</item>
</plurals>

val appName = getString(R.string.app_name) val welcomeMessage = getString(R.string.welcome_message, "John") val itemCount = resources.getQuantityString(R.plurals.numberOfItems, count, count) ```_

Farben

```xml

#FFBB86FC #FF6200EE #FF3700B3 #FF03DAC5 #FF018786 #FF000000 #FFFFFFFF

val color = ContextCompat.getColor(this, R.color.purple_500) textView.setTextColor(color) ```_

Abmessungen

```xml

8dp 16dp 24dp 12sp 16sp 20sp

```_

Stile und Themen

```xml

<style name="CustomButton" parent="Widget.MaterialComponents.Button">
    <item name="android:textColor">@color/white</item>
    <item name="backgroundTint">@color/purple_500</item>
    <item name="cornerRadius">8dp</item>
</style>