I have an advanced device with me. So I am going to be selfish here. I want to know what I can do with my device and see what my device is capable of. So I am going to try out stuffs that work good with my device. So if you are working with a device that is lesser than Andriod 3 and if you find this not helping you, you could probably visit developer.android.com for exhaustive neat documentation.
Running down what I have understood so far in respect to the previous posts Android Hello World Part 1 to 4
1. Define layout in activity_main.xml
2. Define ids to elements (they are the view objects as per SDK) use android:id="@+id/yourid"
3. + symbol comes behind the @ symbol for id declarations alone
4. The above said declaration will help me access my element using R.id.yourid
5. @symbol is required to refer any resources
6. setContentView(R.layout.activity_main); -> will look for the layout defined in the activity_main.xml file in the layout folder and render it .
7. Call the above mentioned line in the onCreate event of the activity to get the layout
8. Use findViewById(R.id.yourid) to find your components. Its equivalent to document.getElementById in javascript.
9. Use "Intent" objects to communicate between activities
10. Use Intent.putExtra(key,value) to transfer values
11. Use startActivity(Intent) in button clicks to start activites
Now its time for the action bars to come into the picture.
I intend to add a couple of actions to the action bar of my main activity page. In order to do that
create the following xml file - res/menu/main_activity_actions.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:showAsAction="never" />
</menu>
For some reason, the following line kept erroring out.
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
So I added the following line of mark up to the strings.xml file
<string name="action_settings">Settings</string>
And added an image by the name ic_action_search to the project file as illustrated by the following screen shot
To add the action to the action bar, Modify the onCreateOptionsMenu in the MainActivity.java call to the following
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return true;
}
To respond to the action button clicks add the following function to the mainActivity.java class. Note the way in which the ids of the action items are being used
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
// openSearch();
return true;
case R.id.action_settings:
// openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Now to ensure that we are able to navigate from the child activities to the parent activity, we need to add a up button in them.
Honestly, it got created automatically even before I realized the need for it. So may be the following code is required for some lol faulty advanced mobiles
Add this to the child activity class' onCreate function. In this case it is the DisplayMessageActivity.java class
getActionBar().setDisplayHomeAsUpEnabled(true);
Now I see an action button my action bar. Yeppieee
No comments:
Post a Comment