Android Studio: Cara Membuat Android Apps 4

From OnnoWiki
Jump to navigation Jump to search

In this video tutorial I will cover how to make menus and dialog popups. I’ll specifically cover laying out menus in main.xml, DialogFragment, AlertDialog, Action Bars, Option Menus and more.

If you missed the first videos they are here How to Make Android apps 1, 2, and 3. Definitely watch them first. All of the code follows the video below. It is heavily commented to help you learn.


If you like videos like this, it helps me if you share on Google Plus with a click here

Code from the Video

main.xml

<menu 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"
   tools:context="com.newthinktank.menuexamples2.app.MainActivity" >


   <group android:checkableBehavior="single">
   <item android:id="@+id/action_settings"
       android:title="@string/action_settings"
       android:orderInCategory="100"
       app:showAsAction="never"
       android:icon="@drawable/sunny"/>
   <item android:id="@+id/exit_the_app"
       android:title="@string/options_exit_text"
       app:showAsAction="ifRoom|withText"
       android:orderInCategory="101"
       android:icon="@drawable/night"/>
   </group>

</menu>

MainActivity.java

package com.newthinktank.menuexamples2.app;

import android.app.DialogFragment; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
   }


   // This method creates the menu on the app
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
       // Inflate the menu; this adds items to the action bar if it is present.
       getMenuInflater().inflate(R.menu.main, menu);
       return true;
   }
   // Called when a options menu item is selected
   @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();
       // We check what menu item was clicked and show a Toast
       if (id == R.id.action_settings) {
           // A DialogFragment is a Fragment you can place over top
           // the current Activity. A Fragment is like an interface
           // block that you can place into an Activity.
           // The FrgamentManager allows you to interact with the
           // Fragment
           DialogFragment myFragment = new MyDialogFragment();
           myFragment.show(getFragmentManager(), "theDialog");
           return true;
           // If exit was clicked close the app
       } else if (id == R.id.exit_the_app) {
           finish();
           return true;
       }
       return super.onOptionsItemSelected(item);
   }


}

MyDialogFragment.java

package com.newthinktank.menuexamples2.app;

// This will be used to create a dialog window

import android.app.AlertDialog; import android.app.Dialog; import android.app.DialogFragment; import android.content.DialogInterface; import android.os.Bundle; import android.widget.Toast;

// If you get an error that the minimum must be 11, change the minimum in the manifest // and also change it in build.gradle

// To generate the onCreateDialog() right click on DialogFragment and Generate and // select onCreateDialog()

public class MyDialogFragment extends DialogFragment{

   @Override
   public Dialog onCreateDialog(Bundle savedInstanceState) {
       // We build the dialog
       // getActivity() returns the Activity this Fragment is associated with
       AlertDialog.Builder theDialog = new AlertDialog.Builder(getActivity());
       // Set the title for the Dialog
       theDialog.setTitle("Sample Dialog");
       // Set the message
       theDialog.setMessage("Hello I'm a Dialog");
       // Add text for a positive button
       theDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
           @Override
           public void onClick(DialogInterface dialogInterface, int i) {
               Toast.makeText(getActivity(), "Clicked OK", Toast.LENGTH_SHORT).show();
           }
       });
       // Add text for a negative button
       theDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
           @Override
           public void onClick(DialogInterface dialogInterface, int i) {
               Toast.makeText(getActivity(), "Clicked Cancel", Toast.LENGTH_SHORT).show();
           }
       });
       // Returns the created dialog
       return theDialog.create();
   }

}

AndroidManifest.xml

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

   package="com.newthinktank.menuexamples2.app" >
   <uses-sdk
       android:minSdkVersion="11"
       android:targetSdkVersion="19" />
   <application
       android:allowBackup="true"
       android:icon="@drawable/ic_launcher"
       android:label="@string/app_name"
       android:theme="@style/AppTheme" >
       <activity
           android:name="com.newthinktank.menuexamples2.app.MainActivity"
           android:label="@string/app_name" >
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
       </activity>
   </application>

</manifest>

- See more at: http://www.newthinktank.com/2014/06/make-android-apps-4/#sthash.2X7oJHNf.dpuf



Referensi