The main step to being a xamarin developer is to have some knowledge on C# and the MVVM architecture. I will be doing a seperate article on the MVVM architecture. Its almost similar to the javas MVC architecture. The IDE you would be using can be either Visual Studio or Xamarin Studio. Personally I use Visual Studio as it seemed more like our Android Studio and obviously the Android sdk set up. I use most of the android sdk tools during development - the ddms, uiautomater etc.
Android Mazes
An android developer since 2 years, currently working on the wearable platform in a reputed firm. Im a tech geek, an author and an immense reader by hobby. One fine day it dawned on me, "why dont I write a blog on what I know". Android Mazes was the result. As they say, knowledge is to be shared and here you go. I have started another hobby recently, i.e. to learn swift programming so expect a blog soon on that :)
Monday, 23 January 2017
Xamarin.Android - a change in the pattern
Alas, I found my blog back. Its been while since I lost in touch with my buddy and a lot has happened. My career switched from being a native developer to cross platform and my medium of development became Xamarin.Android. So, Im gonna post on all my xamarin encounters. And another good thing is. Whatever solutions are posted in Xamarin.Android it can be used up with syntax change sin native android as well. Happy coding :)
Friday, 19 June 2015
Error Solving time - Android Studio - bad class file magic (cafebabe) or version (0034.0000) android studio
My friend got this error when he tried to import a project from Github, he asked me to try and see and I got the same issue as well. Finally we found that the issue was caused by the java compiler version.
Android supports 1.7 jdk version and if you are using a 1.8 version you might face this error.
This error came into picture when we tried to run the application, the normal gradle build was clean. On running the app gradle popped up with,
Local path doesn't exist
and the cause was shown as
bad class file magic (cafebabe) or version (0034.0000) android studio
The fix:
1) Go to File > Other Settings > Default Settings > Build, Execution, Deployment > Compiler > Java Compiler, and change the Project bytecode version to 1.7
2)Go to File > Project Structure > SDK Location, here change the JDK locations path to that of a 1.7 version as below, for mac users give the jdk path till the Home folder.
Now try to build and run the project, it will be successfull. Thanks
Tuesday, 16 June 2015
Error Solving time - Android Studio - peer not authenticated
I had earlier worked on a studio project in a different machine, and after a while I thought of doing some modifications. Thankfully, I had a back up and I tried to open that project in studio in my new machine. It came up with the following error after the gradle build:
peer not authenticated
Here isthe solution, try changing the your root build.gradle's repository block as followsrepositories {
jcenter {
url "http://jcenter.bintray.com/" }
}
repositories { maven { url "http://repo1.maven.org/maven2" } }
and rebuild your project, hopefully your error might be gone.
Happy coding :)
Tuesday, 9 June 2015
Error Solving time - Android Studio - aidl is missing
Today, my gradle came up with an error as
aidl is missing
If any of you has faced this, I would like to share as to how I solved it. There are two ways:
1) Right click on the app and select Open Module Settings and in Properties change the Build Tools Version to 22.0.1 from 23.0.0rc1. Click Ok and check, the error might be gone. But, with this method, you are not using the latest gradle version.
2) If you want to stick to using the latest gradle version (23.0.0rc1), you need to change the root gradle file's classpath as follows:
aidl is missing
If any of you has faced this, I would like to share as to how I solved it. There are two ways:
1) Right click on the app and select Open Module Settings and in Properties change the Build Tools Version to 22.0.1 from 23.0.0rc1. Click Ok and check, the error might be gone. But, with this method, you are not using the latest gradle version.
2) If you want to stick to using the latest gradle version (23.0.0rc1), you need to change the root gradle file's classpath as follows:
classpath 'com.android.tools.build:gradle:1.3.0-beta2'
The andriod studio latest version 1.3 preview beta version 2 is the latest that has come out and if you are going to use build toll version 23.0.0rc1 in the module file settings then you need to change accordingly in your root gradle as well.
Note: I will be posting random error fixes that I might have faced, so stay tuned.
Friday, 5 June 2015
Adding gradle dependencies when library package name is not known
Im an android developer who recently moved to Android Studio. I had this problem of finding a library dependency name that I needed to declare in the gradle. The problem started when I was trying to add the Volley library to my project, I searched all over to find the relevant package of volley which needs to be declared in the dependency set and when I tried to use the
You will see the dependency added in your gradle and the gradle auto sync has been initiated.
https://android.googlesource.com/platform/frameworks/volleyThe gradle build failed, and I couldnt find a proper package name. So, I thought to share with you how to add a dependency whose package name you don't know in the android studio gradle. Please follow these steps:
- Right click your project name (say app)
- Open Module Settings
- Select Dependencies tab
- Click on the + sign in the top right
- Choose Library Dependency
- In the window that opens type the library file's name(say volley)
- Double click on any library file and click Ok
Wednesday, 27 May 2015
Stepping stones to becoming a developer - Android Studio project structure
Hi all, so previously we had discussed in brief about the Gradle and studio, we had also gone through the steps for environment setup and helloworld coding. We will now climb each step to the different aspects of android programming with studio, so, fasten your seat belts
Understand the android studios project structure
Lets start by creating a new project(click here if you want to know how), say the same hello world program for now which we discussed previously. For any android application, the main areas to focus is the activities and layouts.
An activity can be considered as a presentation layer, it will decide the behaviour of the app from its start to end. Each activity is a class which will control how the app will interact with the user. For example, when you open any android app what do you see?, you would see a complex view with clickable, non clickable, editable, non editable components in a single page, the entire page or view is called a layout. On the other hand, try interacting with the layout, say, clicking a button or typing in your user name / password etc. all such functional behaviours as to what each individual component in a layout is to do is controlled by the activity class. You will learn more while start coding.
Moving on, lets take a look at our project, you will see an app directory which has a few sub-directories namely manifests, java, res. We will begin with the java folder; on expanding which you would see a package name in the form "com.somthing.somethingelse".
A package name is kind of a unique identifier for an application. It is a mandatory part of any application, 2 applications may exist in the same device with same name and different package names, but if 2 applications with same package names are tried to be installed, the system will come up with a failure message. Also if you are uploading an app to the Google play store with whatever package name and after a while you want to release the same app with updated features and bug fixes, you have to use the same package name you used previously, if you are going to upload the apk with a different package name it will be treated as a new app.
In android each class files are grouped under packages. Within an application , you can declare multiple packages for grouping different types of class files such as a com.example.db for all the database classes or com.example.constant to hold all the classes which declare constants, but, only one package will act as the unique id for the application, mostly it will be the package you declared while creating the project unless you change the package forcibly.
As said activities and layouts form the main parts of an android app, you can see that under your package there is the MainActivity.java file. Open the java file and you will see something like
package androidstudio.test.com.myapplication; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class MainActivity2Activity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_activity2); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main_activity2, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // 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. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
As you can see the MainActivity extends the Activity class. The Activity class contains a series that defines each state of the app. An activity goes through a series of lifecycle as you navigate through the app, like if the app is in started or the activity is in foreground, the onCreate and onResume method are called.
Activity Life cycle methods
onCreate() - called when an activity is created or launched. This method will be overridden in our activity to create view, initialise views, bind data etc
onStart() - called right after onCreate(), this may be used to implement any task before the UI becomes visible to the user such as refreshing or reloading UI contents etc. This is immediately followed by onResume()
onResume() - called when the UI is ready for user interaction.This is one of the important method to be overridden. It is method that get called whenever the activity comes to foreground hence it always is the state that may happen after onPause(). This method can be used to bind services, listeners etc.
onPause() - This method is called whenever the activity goes in background like when another activity is opened on top of the current activity or when home button is pressed etc. i.e. when the activity is no longer visible but is not killed. This can be overridden to unbind services or listeners, release system resources, save application data etc. It can be considered as the opposite of onResume(). The onPause() is always guaranteed to be called whenever the apps goes in background or turns invisible, but after that the Android system has the right to kill your app so make sure your app data is saved.
onStop() - called when the app is completely invisible and happens after onPause(). After this the onRestart(), onDestroy() or nothing may be called. We cannot depend on this system, as the system may or may not call it considering low memory scenarios. Hence write all the commit codes in your onPause().
onDestroy() - called when the activity is temporarily or permanently killed. This happens when either the system kills your application instance when it is low on resources as well as when the activity is finished by the user, the two scenarios can be differentiated with the help of the isFinishing() method. It is the last method that would be called in an activity's life cycle. Like onStop() this method also cannot be relied on as it is up to the system whether it will be called or not. This method is usually implemented to free resources that may be used by the activity.
Before we go into doing an example on this, I would like to talk about one more important object, called Intent.
An intent can be described as a message carrier between components. In programming world it can be considered as an object that can carry information or data between components belonging to the same application. Intents are mainly used for three major functions:
Intent will mainly carry three important parts; an action, data, component name. An action is a String that generally describes what action the Intent is intended for such as ACTION_VIEW, which is used to display information through an activity to the user such as displaying video in the video player, or images in the gallery etc.
- To start another activity
- To start a service
- To pass messages to broadcast receivers
A data is a bundle of information that is required to be communicated between components.
A component name is the package name of the other component to which the intent is passed. For example, you want to pass the intent to MainActivity.java residing in package com.example.sampleintent package, then, its component name is com.example.sampleintent.MainActivity which can be said as the fully qualified class name.
There are two types of Intents
Explicit intents are those in which the component names are specified. These components might be activities or other components within your application and whose component names you are aware of, mostly used when starting another activity from your current activity. Implicit intents are those in which the the component is not specified, whereas an action will be declared with it. These types of intents are usually used if you want to use a specific function on an in built component whose component name you are not aware of such as to call a particular number you can use the phones default caller with the help of the ACTION_CALL intent action.
- Explicit intents
- Implicit intents
Lets move on to an example to understand these aspects. I hope you know how to create a project and all, if any confusions please refer to earlier posts. Assuming you would have started a new project, you will already have a MainActivity.java, lets create one more Activity. For this:
A new activity and its corresponding layout file will be created
- Right click your application package
- Goto New >> Activity >> Blank Activity
- Name your activity as SecondActivity(you may name as you wish)
Once this is done, let's move back to the MainActivity.java and override our lifecycle methods. To override in Android Studio, point and click the cursor to any location you like to create your methods and from the Keyboard press Alt+Insert, you can also right click on the emoty space and select Generate, It will open up a small window with a list of actions, select Override Methods...
From the list that follows, keep pressing CTRL from your keyboard and click the following methods:
onResume()
onStart()
onPause()
onRestart()
onStop()
onDestroy()
and click OK.
The methods will get added up at the point where your cursor was clicked. Inside each method, we will write a Log statement(a log is used to print out a text while a specific code is being run, it is basically used for debug purposes, in our case to check when each method is being called, based on the log that appears). A log statement is in the form:
Log.d("",""); //d for debug
Log.i("",""); //i for info
Log.e("",""); //e for error
There are many types of log, we will use Log.d
Next, goto activity_main.xml the layout file for this activity. Go to the Design mode
If your see any Helloword text in the mobile screen, just click and press Delete from keyboard.
From the pallete available in the left hand side, drag and drop the button into the mobile screen.
Double click on the button and a window will appear, in the text field type "Click to goto next activity", in the id give "click_button"(you may give any, but you should remember it). Now click in the empty space outside the window and the button label will get updated. Beside the Design field at the bottom left , there is another label called Text, click on that and you will see the xml file of the layout you just edited.
Come back to the MainActivity.java. In the declaration area, declare a button as
private Button clickButton; //name your variable as you wish
inside onCreate lets initialize the button as
clickButton=(Button)findViewById(R.id.click_button); //this is he std format to declare any view components
Next, we will implement the click functionality for the button as
clickButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } });
inside the onClick method write the following code to pass control to the next activity when the button is clicked. The onClick is an event and whatever you write inside of it will define the buttons function:
clickButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent goToSecondActivity=new Intent(MainActivity.this,SecondActivity.class); goToSecondActivity.putExtra("data","dataToBePassed"); startActivity(goToSecondActivity); } });
code snippet
public class MainActivity extends ActionBarActivity { public static final String TAG = "ACTIVITY_LIFECYCLE"; private Button clickButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "onCreate called"); setContentView(R.layout.activity_main); clickButton=(Button)findViewById(R.id.click_button); clickButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent goToSecondActivity=new Intent(MainActivity.this,SecondActivity.class); goToSecondActivity.putExtra("data","dataToBePassed"); startActivity(goToSecondActivity); } }); } @Override protected void onDestroy() { super.onDestroy(); Log.d(TAG,"onDestroy called"); } @Override protected void onRestart() { super.onRestart(); Log.d(TAG, "onRestart called"); } @Override protected void onStop() { super.onStop(); Log.d(TAG, "onStop called"); } @Override protected void onStart() { super.onStart(); Log.d(TAG, "onStart called"); } @Override protected void onResume() { super.onResume(); Log.d(TAG, "onResume called"); } @Override protected void onPause() { super.onPause(); Log.d(TAG, "onPause called"); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // 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. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }Now in your SecondActivity, write the below snippet in its onCreate, below the setContentView method.
Let's set the received string content in the TextView present in the activity's layout. Inorder to access any view(TextView in our case), make sure the view has an id defined in its xml. So, go the layout and in the TextView xml, declare a unique id(if not present), say,
android:id="@+id/my_textview"
Now you need to initialize your TextView, inorder to set the content. For this, create an object of the TextView class say,
private TextView mTextView;
and then we can initialize it as
mTextView = (TextView)findViewById(R.id.my_textview);
Now the mTextView object will officially represent the my_textview.
So, we can set its text with the receivedText as
mTextView.setText(receivedText);
Lets also override the life cycle methods here as well so that we can understand the state of this activity when we try to navigate fro here,
code snippet
SecondActivity.java
public class SecondActivity extends ActionBarActivity { public static final String TAG = "SECONDACTIVITY_LIFECYCL"; private TextView mTextView; String receivedText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "onCreate called"); setContentView(R.layout.activity_second); receivedText=getIntent().getStringExtra("data"); System.out.println(receivedText); mTextView=(TextView)findViewById(R.id.my_textview); mTextView.setText(receivedText); } @Override protected void onDestroy() { super.onDestroy(); Log.d(TAG,"onDestroy called"); } @Override protected void onRestart() { super.onRestart(); Log.d(TAG, "onRestart called"); } @Override protected void onStop() { super.onStop(); Log.d(TAG, "onStop called"); } @Override protected void onStart() { super.onStart(); Log.d(TAG, "onStart called"); } @Override protected void onResume() { super.onResume(); Log.d(TAG, "onResume called"); } @Override protected void onPause() { super.onPause(); Log.d(TAG, "onPause called"); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_second, menu); return true; } }
Layout for the activity - activity_second.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="androidstudio.test.com.myapplication.SecondActivity"> <TextView android:id="@+id/my_textview" android:text="@string/hello_world"android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
Vola, we are done with our project. Lets now run and check the ouput's. To run the project, click the green play button onthe top, or click Run>>Run 'app'
:::Output::::
Check your logcat to see the out put, to view logcat you can see the Android tab in the bottom left of your studio, and inside it you can see log cat.
Remember the TAG string representing the logs from the MainActivity is ACTIVITY_LIFECYCLE and that from the SecondActivity is SECONDACTIVITY_LIFECYCL, this will help us differentiate the output from each activity.
When the app is launched, you will see
D/ACTIVITY_LIFECYCLE﹕ onCreate called //represent the log we have written in the onCreate of MainActivity, D represents that it is a debug log
D/ACTIVITY_LIFECYCLE﹕ onStart called
D/ACTIVITY_LIFECYCLE﹕ onResume called
Press your device's home button and check the logcat, the following comes up
D/ACTIVITY_LIFECYCLE﹕ onPause called
D/ACTIVITY_LIFECYCLE﹕ onStop called
Resume the app from your recent app list(long press the home button)
D/ACTIVITY_LIFECYCLE﹕ onRestart called
D/ACTIVITY_LIFECYCLE﹕ onStart called
D/ACTIVITY_LIFECYCLE﹕ onResume called
Click Button > CLICK TO GOTO NEXT ACTIVITY, the SecondActivity Opens up through the Intent and you can see the message that was passed through the Intent has been set in the TextView, the logcat will show
D/ACTIVITY_LIFECYCLE﹕ onPause called
D/SECONDACTIVITY_LIFECYCL﹕ onCreate called //logs from SecondActivity
D/SECONDACTIVITY_LIFECYCL﹕ onStart called
D/SECONDACTIVITY_LIFECYCL﹕ onResume called
D/ACTIVITY_LIFECYCLE﹕ onStop called //MainActivity Stopped after SecondActivity is visible
Press device's back button
D/SECONDACTIVITY_LIFECYCL﹕ onPause called
D/ACTIVITY_LIFECYCLE﹕ onRestart called
D/ACTIVITY_LIFECYCLE﹕ onStart called
D/ACTIVITY_LIFECYCLE﹕ onResume called
D/SECONDACTIVITY_LIFECYCL﹕ onStop called
D/SECONDACTIVITY_LIFECYCL﹕ onDestroy called
There you go, now you know what goes on when you navigate through an application. Have a good day. I will be back soon with another topic. Please see below if you wish to see the app screenshots.
Subscribe to:
Posts (Atom)