1

Hey I work with Kotlin and Android Studio

Here is my activity_main.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/background"
    tools:context=".MainActivity">

<!--    <TextView-->
<!--        android:text="Food Lovers"-->
<!--        android:gravity="center"-->
<!--        android:textSize="30sp"-->
<!--        android:layout_marginTop="45dp"-->
<!--        android:textStyle="bold|normal|italic"-->
<!--        android:textColor="@color/white"-->
<!--        android:layout_width="match_parent"-->
<!--        android:layout_height="wrap_content"/>-->
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="200dp"
        android:layout_height="161dp"
        android:layout_gravity="center"
        android:paddingTop="50dp"
        app:srcCompat="@drawable/logo" />

    <androidx.cardview.widget.CardView
        app:cardCornerRadius="25dp"
        app:cardElevation="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:layout_marginStart="38dp"
        android:layout_marginEnd="38dp"
        app:cardUseCompatPadding="true">
        
        <LinearLayout
            android:id="@+id/cardView"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <LinearLayout
                android:orientation="horizontal"
                android:gravity="center"
                android:layout_gravity="center"
                android:weightSum="2"
                android:layout_marginTop="30dp"
                android:layout_marginStart="20dp"
                android:layout_marginEnd="20dp"
                android:background="@drawable/switch_tumbs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView
                    android:id="@+id/logIn"
                    android:fontFamily="@font/iransans_fanum"
                    android:text="@string/log_In"
                    android:gravity="center"
                    android:layout_gravity="center"
                    android:paddingBottom="5dp"
                    android:paddingTop="5dp"
                    android:textAlignment="center"
                    android:textColor="@color/textColor"
                    android:textSize="16sp"
                    android:textStyle="bold|normal"
                    android:paddingRight="15dp"
                    android:paddingLeft="15dp"
                    android:background="@drawable/switch_trcks"
                    android:layout_weight="1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"/>
                <TextView
                    android:id="@+id/singUp"
                    android:text="ثبت نام"
                    android:gravity="center"
                    android:fontFamily="@font/iransans_fanum"
                    android:layout_gravity="center"
                    android:paddingTop="5dp"
                    android:paddingRight="15dp"
                    android:paddingLeft="15dp"
                    android:paddingBottom="5dp"
                    android:textAlignment="center"
                    android:textColor="@color/pinkColor"
                    android:textSize="16sp"
                    android:textStyle="bold|normal"
                    android:layout_weight="1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"/>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/logInLayout"
                android:orientation="vertical"
                android:background="@color/white"
                android:padding="25dp"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <com.google.android.material.textfield.TextInputLayout
                    android:hint="شماره تلفن"
                    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
                    android:layout_marginStart="5dp"
                    android:layout_marginEnd="5dp"
                    android:layout_marginTop="5dp"
                    app:shapeAppearanceOverlay="@style/button4"
                    app:boxStrokeColor="#4E4A4A"
                    app:boxStrokeWidth="1dp"
                    app:prefixText="+۹۸"
                    app:endIconMode="clear_text"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <com.google.android.material.textfield.TextInputEditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:inputType="phone"
                        android:id="@+id/phoneNumber"
                        android:ems="10"
                        android:textSize="13sp"
                        android:textColor="@color/black"
                        android:textStyle="normal"
                        android:fontFamily="@font/iransans_fanum"
                        />
                </com.google.android.material.textfield.TextInputLayout>

                <com.google.android.material.textfield.TextInputLayout
                    android:hint="رمز عبور"
                    android:layoutDirection="rtl"
                    android:textDirection="ltr"
                    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
                    android:layout_marginStart="5dp"
                    android:layout_marginEnd="5dp"
                    android:layout_marginTop="5dp"
                    app:shapeAppearanceOverlay="@style/button4"
                    app:boxStrokeColor="#4E4A4A"
                    app:boxStrokeWidth="1dp"
                    app:boxCornerRadiusTopEnd="9dp"
                    app:boxCornerRadiusTopStart="9dp"
                    app:boxCornerRadiusBottomEnd="9dp"
                    app:boxCornerRadiusBottomStart="9dp"
                    app:endIconMode="password_toggle"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <com.google.android.material.textfield.TextInputEditText

                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:inputType="textPassword"
                        android:id="@+id/passwords"
                        android:ems="10"
                        android:textSize="13sp"
                        android:textColor="@color/black"
                        android:textStyle="normal"
                        android:fontFamily="@font/iransans_fanum"
                        />
                </com.google.android.material.textfield.TextInputLayout>

                <TextView
                    android:textSize="11sp"
                    android:text="رمز عبور خود را فراموش کرده‌اید؟   "
                    android:textAlignment="textEnd"
                    android:textColor="#616161"
                    android:textStyle="normal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/signUpLayout"
                android:visibility="gone"
                android:orientation="vertical"
                android:background="@color/white"
                android:padding="25dp"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <com.google.android.material.textfield.TextInputLayout
                    android:hint="شماره تلفن"
                    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
                    android:layout_marginStart="5dp"
                    android:layout_marginEnd="5dp"
                    android:layout_marginTop="5dp"
                    app:shapeAppearanceOverlay="@style/button4"
                    app:boxStrokeColor="#4E4A4A"
                    app:boxStrokeWidth="1dp"
                    app:prefixText="+۹۸"
                    app:endIconMode="clear_text"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <com.google.android.material.textfield.TextInputEditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:inputType="phone"
                        android:id="@+id/phoneNumbersi"
                        android:ems="10"
                        android:textSize="13sp"
                        android:textColor="@color/black"
                        android:textStyle="normal"
                        android:fontFamily="@font/iransans_fanum"
                        />
                </com.google.android.material.textfield.TextInputLayout>
                <com.google.android.material.textfield.TextInputLayout
                    android:hint="رمز عبور"
                    android:layoutDirection="rtl"
                    android:textDirection="ltr"
                    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
                    android:layout_marginStart="5dp"
                    android:layout_marginEnd="5dp"
                    android:layout_marginTop="5dp"
                    app:shapeAppearanceOverlay="@style/button4"
                    app:boxStrokeColor="#4E4A4A"
                    app:boxStrokeWidth="1dp"
                    app:endIconMode="password_toggle"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <com.google.android.material.textfield.TextInputEditText

                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:inputType="textPassword"
                        android:id="@+id/passwordssi"
                        android:ems="10"
                        android:textSize="13sp"
                        android:textColor="@color/black"
                        android:textStyle="normal"
                        android:fontFamily="@font/iransans_fanum"
                        />
                </com.google.android.material.textfield.TextInputLayout>
                <com.google.android.material.textfield.TextInputLayout
                    android:hint="تکرار رمز عبور"
                    android:layoutDirection="rtl"
                    android:textDirection="ltr"
                    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
                    android:layout_marginStart="5dp"
                    android:layout_marginEnd="5dp"
                    android:layout_marginTop="5dp"
                    app:shapeAppearanceOverlay="@style/button4"
                    app:boxStrokeColor="#4E4A4A"
                    app:boxStrokeWidth="1dp"
                    app:endIconMode="password_toggle"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <com.google.android.material.textfield.TextInputEditText

                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:inputType="textPassword"
                        android:id="@+id/confirmpasswordssi"
                        android:ems="10"
                        android:textSize="13sp"
                        android:textColor="@color/black"
                        android:textStyle="normal"
                        android:fontFamily="@font/iransans_fanum"
                        />
                </com.google.android.material.textfield.TextInputLayout>


            </LinearLayout>

            <Button
                android:id="@+id/signIn"
                android:text="@string/log_In"
                android:textAllCaps="false"
                android:textSize="17sp"
                android:elevation="15dp"
                android:fontFamily="@font/iransans_fanum"
                android:gravity="center"
                android:layout_gravity="center"
                app:shapeAppearanceOverlay="@style/button5"
                app:backgroundTint="@color/pinkColor"
                android:layout_marginStart="24dp"
                android:layout_marginEnd="24dp"
                android:textColor="@color/textColor"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
            <LinearLayout
                android:id="@+id/OTPLayout"
                android:visibility="visible"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="یا می‌توانید از طریق زیر وارد شوید"
                android:fontFamily="@font/iransans_fanum"
                android:textSize="12sp"
                android:layout_marginTop="16dp"
                android:layout_marginBottom="16dp"
                android:textStyle="normal"
                android:textAlignment="center"
                android:textColor="#474646"
                android:layout_gravity="center"
                android:gravity="center" />

            <Button
                android:id="@+id/signInOTP"
                android:text="ورود با شماره تلفن همراه"
                android:fontFamily="@font/iransans_fanum"
                android:textAllCaps="false"
                android:textSize="17sp"
                android:elevation="15dp"
                android:gravity="center"
                android:layout_gravity="center"
                app:shapeAppearanceOverlay="@style/button5"
                app:backgroundTint="#0932FF"
                android:layout_marginStart="24dp"
                android:layout_marginEnd="24dp"
                android:layout_marginBottom="24dp"
                android:textColor="@color/textColor"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/></LinearLayout>

        </LinearLayout>

    </androidx.cardview.widget.CardView>

</LinearLayout>

And here is my MainActivity.kt:

package com.example.sepehr1

import android.annotation.SuppressLint
import android.app.AlertDialog
import android.content.DialogInterface

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

import android.view.View

import android.widget.Button
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast



class MainActivity : AppCompatActivity() {

@SuppressLint("UseCompatLoadingForDrawables", "CutPasteId")
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val signIn = findViewById<Button>(R.id.signIn)
    val singUp = findViewById<TextView>(R.id.singUp)
    val logIn = findViewById<TextView>(R.id.logIn)
    val signUpLayout = findViewById<LinearLayout>(R.id.signUpLayout)
    val logInLayout = findViewById<LinearLayout>(R.id.logInLayout)
    val otpLayout = findViewById<LinearLayout>(R.id.OTPLayout)
    val cardView = findViewById<LinearLayout>(R.id.cardView)
    val phoneNumber = findViewById<EditText>(R.id.phoneNumber)
    var username = findViewById<EditText>(R.id.phoneNumber)
    var password = findViewById<EditText>(R.id.passwords)
    singUp.setOnClickListener{
        singUp.background = resources.getDrawable(R.drawable.switch_trcks,null)
        singUp.setTextColor(resources.getColor(R.color.textColor,null))
        logIn.background = null
        signIn.text="ثبت نام"
        cardView.setPadding(0,0,0,30)
        signUpLayout.visibility = View.VISIBLE
        logInLayout.visibility = View.GONE
        logIn.setTextColor(resources.getColor(R.color.pinkColor,null))
        otpLayout.visibility=View.GONE
    }

    logIn.setOnClickListener{
        singUp.background = null
        signIn.text="ورود"
        cardView.setPadding(0,0,0,0)
        singUp.setTextColor(resources.getColor(R.color.pinkColor,null))
        logIn.background = resources.getDrawable(R.drawable.switch_trcks,null)
        signUpLayout.visibility = View.GONE
        logInLayout.visibility = View.VISIBLE
        logIn.setTextColor(resources.getColor(R.color.textColor,null))
        otpLayout.visibility=View.VISIBLE
    }

}
}

My AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Sepehr1"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

And My build.gradle:

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-parcelize'
}

android {
    namespace 'com.example.sepehr1'
    compileSdk 34

    defaultConfig {
        applicationId "com.example.sepehr1"
        minSdk 23
        targetSdk 34
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.10.1'
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.9.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

My app does not open in android 8 or 7 but opens in android 12 and I can not find the reason of this problem and make my app compatible with lower android versions! (I have chosen android 7 as the lowest version of my app while creating the project)

compileSDK is 34 and minSdk is 23 (my app still does not work with lower version of minSDK! And also I made another app with these details and it worked in android 7)

gioravered
  • 1,758
  • 3
  • 19
  • 30
Amir Elahi
  • 31
  • 5
  • Can you add your AndroidManifest.xml and your build.gradle files ? – ItzDavi Aug 09 '23 at 06:48
  • I added both as you wanted – Amir Elahi Aug 09 '23 at 06:56
  • Can you also add gradle-wrapper.properties ? – ItzDavi Aug 09 '23 at 07:05
  • `#Mon Aug 07 12:54:44 IRST 2023 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists` – Amir Elahi Aug 09 '23 at 07:08
  • 2
    What do you mean by does not open? Does it install? If it does, when you run it it must be crashing. Get us the log of the crash. If it doesn't install, get us the reason why from adb – Gabe Sechan Aug 09 '23 at 07:09
  • Just tested your configuration in an emulator and my app starts without problems so it must be something else. Is there any type of error in the console / logcat ? – ItzDavi Aug 09 '23 at 07:13
  • it installs without problem but when I click on that it says the app stopped – Amir Elahi Aug 09 '23 at 07:23
  • There is gonna be an error displayed on the logcat of Android Studio which will tell us the cause of "crash"/ANS. – Rigo Regalado Aug 09 '23 at 07:52
  • can you explain more what should I do? – Amir Elahi Aug 09 '23 at 08:00
  • 1
    @AmirElahi there is a window called "Logcat". It simply is a console where Android reports debug informations like warnings and errors. When you open the app and it crashes it should report what's went wrong. (See https://developer.android.com/studio/debug/logcat for more information) – ItzDavi Aug 09 '23 at 08:04
  • I saw the error: `FATAL EXCEPTION: main Process: com.example.sepehr1, PID: 31064 java.lang.OutOfMemoryError: Failed to allocate a 752277792 byte allocation with 16766368 free bytes and 413MB until OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method)` I saw also the solutions below but they did not work `android:hardwareAccelerated="false" android:largeHeap="true"` in Manifest – Amir Elahi Aug 09 '23 at 10:16

0 Answers0