CREATE A PROJECT FROM EMPTY ACTIVITY
- Create an empty activity project using Android Studio.; the Manifest file contains only the following items
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gcarabel.brembillaexercisefromemptyactivity">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme" />
</manifest>
- if you run this app the error: "Default Activity not found" is reported during gradle compilation. It is therefore necessary to add a default activity in the Manifest file as follows:
<!-- Main Activity-->
<activity android:name=".MainActivity" >
<intent-filter>
<!-- MAIN represents that it is the Main Activity-->
<action android:name="android.intent.action.MAIN" />
<!-- Launcher Denotes that it will be the first launching activity-->
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
- At this point the final Manifest file will look as follows:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gcarabel.brembillaexercisefromemptyactivity">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<!-- Main Activity-->
<activity android:name=".MainActivity" >
<intent-filter>
<!-- MAIN represents that it is the Main Activity-->
<action android:name="android.intent.action.MAIN" />
<!-- Launcher Denotes that it will be the first launching activity-->
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
- Now it is necessary to create the res/layout directory, since no layout has been created when running an empty activity. below an example of how the activity_main.xml file looks like using the
ConstraintLayout
with a simple TextView
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context="com.example.gcarabel.brembillaexercisefromemptyactivity.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="brembillaexercisefromemptyactivity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
- Now we have to create a
MainActivity.java
class as follows
package com.example.gcarabel.brembillaexercisefromemptyactivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
- Be also sure that the gradle file has the following dependencies. (version depends on the available one). This because in my case I am using ConstraintLayout. Different dependencies might be required for different layouts.
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'com.android.support:appcompat-v7:26.0.0-beta2'
implementation "com.android.support:support-core-utils:26.0.0-beta2"
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.google.android.gms:play-services-vision:11.0.2'
testImplementation 'junit:junit:4.12'
}