SCRUMble ! Hello Blog Readers! Thank you for all your support and encouragement. I have something exciting for you all. I have recently written and published a new book called 'SCRUMble !'. It is currently available on pothi store. It will be soon available on Amazon and Flipkart as well. Please get your copy and do let me know your reviews. -Abhishek Sathe SCRUMble ! Written and Published by: Abhishek Sathe Distributed by: pothi.com Order your copy now: https://store.pothi.com/book/abhishek-sathe-scrumble/ Coming soon on Amazon and Flipkart About the book: Scrum is a framework for solving complex problems largely adapted by Software Development field. There are multiple ag...
Important things you will want to know while developing an android application:
1.In res/layout folder, there is main.xml or activity_main.xml file which defines the UI of your application. The default view is the layout view which lays out the activity graphically.
2.When you create a new project, following files are created by default:
-Project name
-Create activity
-Min. SDK Version: minimum version of SDK that your project is targeting.
3.MainActivity is usually entrypoint of the application which is displayed when the application is loaded. In android, an activity is a window that contains UI. An application can have zero or more activities.
4.Various folders and their files:
-src
-Android 2.3 library
-gen
-assets
-res
-AndroidManifest.xml contains detailed information about the application.
5.The code that connects tha activity to the UI is setContentView( ) method, which is in MainActivity.java.
setContentView(R.layout.main);
Here, R.layout.main refers to the main.xml file located in the res/layout folder. onCreate( ) is one of the many methods that are fired when an activity is loaded.
6.To apply a style to an activity, simply modify the <Activity> element in the AndroidManifest.xml file by adding android:theme attribute.
ex. android:theme="@android.style/Theme.Dialog"
This will make the activity appear as a dialog.
7.If the activity you want to invoke is defined within the same project, we can write:
startActivity(new Intent(this,Activity2.class));
8.If we want to return a result from an activity back to the calling activity, we can use startActivityForResult( ).
ex.
startActivityForResult(new Intent("net.learn2develop.ACTIVITY2"),request_code);
In addition to passing in an intent object, you also need to pass in request_code as well. The request_code is simply an integer value that identifies an activity you are calling. This is needed when an activity returns a value, you need a way to identify it. When calling multiple activities, this is useful to determine which activity has returned. In order for an activity to return a value, to the calling activity, you use an Intent object to send data back via the setData( ) method:
Intent data=new Intent( );
EditText txt_username=(EditText)findViewById(R.id.txt_username);
data.setData(Uri.parse(txt_username.getText( ).toString( )));
setResult(RESULT_OK,data);
finish( ); //closes activity
The setRedult( ) sets a result code(either RESULT_OK or RESULT_CANCELLED) and the data ( an Intent object) to be returned back to the calling activity. The finish( ) method closes the activity and returns control back to the calling activity. In the calling activity, you need to implement the onActivityResult( ) method, which is invoked whenever an activity returns. Here, you check the appropriate request code and display the returned result:
public void onActivityResult(int requestCode,int resultCode,Intent data)
{
if(requestCode==request_Code)
{
if(resultCode==RESULT_OK)
{
Toast.makeText(this,data.getData( ).toString( ),Toast.LENGTH_SHORT).show( );
}
}
}
9.We can also pasd data to an activity. To make Intent object carry data, we use Bundle object.
Bundle extras=new Bundle( );
extras.putString("Name","Your name here");
Intent i=new Intent("net.learn2develop.ACTIVITY2");
i.putExtras(extras);
A bundle object is basically a dictionary object that enables you to set data in key/value pairs. In this case, you created a key named Name and assigned it a value of "Your name here". The bundle object is then added to intent object using putExtras( ).
In the target activity, you can use getExtras( ) to obtain the bundle objects:
Bundle extras=getIntent.getExtras( );
if(extras != null)
{
defaultName=extras.getString("Name");
}
10.Units of measurements:
-dp:density independent picture. 160 dp is equivalent to one inch of physical screen size. This is the recommended unit of measurement when specifying the dimension of views in your layout. You can either specify dp or dip for referring to it.
-sp:scale independent pixel. This is similar to dp and is recommended for specifying font sizes.
-pt:Point. A point is defined to be 1/72 of an inch, based on the physical screen size.
-px:pixel. Corresponding to actual pixels on screen. Using this is not recommended as your UI may not render correctly on devices with different screen sizes.
1.In res/layout folder, there is main.xml or activity_main.xml file which defines the UI of your application. The default view is the layout view which lays out the activity graphically.
2.When you create a new project, following files are created by default:
-Project name
-Create activity
-Min. SDK Version: minimum version of SDK that your project is targeting.
3.MainActivity is usually entrypoint of the application which is displayed when the application is loaded. In android, an activity is a window that contains UI. An application can have zero or more activities.
4.Various folders and their files:
-src
-Android 2.3 library
-gen
-assets
-res
-AndroidManifest.xml contains detailed information about the application.
5.The code that connects tha activity to the UI is setContentView( ) method, which is in MainActivity.java.
setContentView(R.layout.main);
Here, R.layout.main refers to the main.xml file located in the res/layout folder. onCreate( ) is one of the many methods that are fired when an activity is loaded.
6.To apply a style to an activity, simply modify the <Activity> element in the AndroidManifest.xml file by adding android:theme attribute.
ex. android:theme="@android.style/Theme.Dialog"
This will make the activity appear as a dialog.
7.If the activity you want to invoke is defined within the same project, we can write:
startActivity(new Intent(this,Activity2.class));
8.If we want to return a result from an activity back to the calling activity, we can use startActivityForResult( ).
ex.
startActivityForResult(new Intent("net.learn2develop.ACTIVITY2"),request_code);
In addition to passing in an intent object, you also need to pass in request_code as well. The request_code is simply an integer value that identifies an activity you are calling. This is needed when an activity returns a value, you need a way to identify it. When calling multiple activities, this is useful to determine which activity has returned. In order for an activity to return a value, to the calling activity, you use an Intent object to send data back via the setData( ) method:
Intent data=new Intent( );
EditText txt_username=(EditText)findViewById(R.id.txt_username);
data.setData(Uri.parse(txt_username.getText( ).toString( )));
setResult(RESULT_OK,data);
finish( ); //closes activity
The setRedult( ) sets a result code(either RESULT_OK or RESULT_CANCELLED) and the data ( an Intent object) to be returned back to the calling activity. The finish( ) method closes the activity and returns control back to the calling activity. In the calling activity, you need to implement the onActivityResult( ) method, which is invoked whenever an activity returns. Here, you check the appropriate request code and display the returned result:
public void onActivityResult(int requestCode,int resultCode,Intent data)
{
if(requestCode==request_Code)
{
if(resultCode==RESULT_OK)
{
Toast.makeText(this,data.getData( ).toString( ),Toast.LENGTH_SHORT).show( );
}
}
}
9.We can also pasd data to an activity. To make Intent object carry data, we use Bundle object.
Bundle extras=new Bundle( );
extras.putString("Name","Your name here");
Intent i=new Intent("net.learn2develop.ACTIVITY2");
i.putExtras(extras);
A bundle object is basically a dictionary object that enables you to set data in key/value pairs. In this case, you created a key named Name and assigned it a value of "Your name here". The bundle object is then added to intent object using putExtras( ).
In the target activity, you can use getExtras( ) to obtain the bundle objects:
Bundle extras=getIntent.getExtras( );
if(extras != null)
{
defaultName=extras.getString("Name");
}
10.Units of measurements:
-dp:density independent picture. 160 dp is equivalent to one inch of physical screen size. This is the recommended unit of measurement when specifying the dimension of views in your layout. You can either specify dp or dip for referring to it.
-sp:scale independent pixel. This is similar to dp and is recommended for specifying font sizes.
-pt:Point. A point is defined to be 1/72 of an inch, based on the physical screen size.
-px:pixel. Corresponding to actual pixels on screen. Using this is not recommended as your UI may not render correctly on devices with different screen sizes.
Good and informative
ReplyDelete