In this posting, we can learn how to use the Action Bar, simply. 


Action items appear directly in the action bar with an icon or text are known as action buttons. 

Actions that can't fit in the action bar or aren't important enough are hidden in the action overflow.


Now, here's how to add the Action Bar.


1. Specify the Actions in XML

First, we should define in an XML menu resource for all action buttons and other items available in the action overflow. 

we create a new XML file in our project's 'res/menu/' directory.

==== res/menu/main.xml ====

<menu xmlns:android="http://schemas.android.com/apk/res/android" >


    <item android:id="@+id/menu_refresh"

        android:title="@string/menu_refresh"

        android:icon="@drawable/menu_refresh"

        android:orderInCategory="101"

        android:showAsAction="always" />


  <item android:id="@+id/menu_search"

        android:title="@string/menu_search"

        android:icon="@drawable/menu_search"

        android:orderInCategory="102"

        android:showAsAction="always|withText" />

    

  <item android:id="@+id/menu_settings"

        android:title="@string/menu_settings"

        android:icon="@drawable/menu_settings"

        android:orderInCategory="103"

        android:showAsAction="never" />

</menu>

: Add an <item> element for each item you want to include in the action bar.

In icon case, we can use icons provided in "http://developer.android.com/design/downloads/index.html#action-bar-icon-pack"


2. Add the Actions to the Action Bar

@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;

}

: We should implement onCreateOptionsMenu() callback method in our activity to inflate the menu resource into the given Menu object.


3. Respond to Action Buttons

/**

* This method is called automatically, when ActionBar's menu is selected.

* @param item

* @return

*/

@Override

public boolean onOptionsItemSelected(MenuItem item)

{

switch(item.getItemId()){

case R.id.menu_refresh:

text01.setText("새로고침 메뉴 선택!!");

return true;

case R.id.menu_search:

text01.setText("검색 메뉴 선택!!");

return true;

case R.id.menu_settings:

text01.setText("설정 메뉴 선택");

return true;

}

return super.onOptionsItemSelected(item);

}

: When the user presses one of the action buttons, the Android system calls our activity's onOptionsItemSelected() callback method.

We can call getItemId() method to determine which item was pressed. And then The returned ID can match the value we declared in the corresponding <item> element's android:id attributes.



The result let us run the application is the following.




'Android > Activity' 카테고리의 다른 글

Staring Another Activity  (0) 2014.02.03

In this section, I'll start another Activity when I click the the Send button in Main Activity.


Let's see the following.



1. Respond to the Send Button.

==== activity_main.xml ====

<LinearLayout 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:orientation="horizontal"
   >
 <EditText 
     android:id="@+id/message"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:hint="Enter a message"
     android:layout_weight="1"
     />
 <Button 
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Send"
     android:textSize="20sp"
     android:onClick="sendMessage"
     />
</LinearLayout>

: the android:onClick attribute's value, "sendMessage" is the name of a method in MainActivity that the system calls when the user click the button.


2. Build an Intent

==== MainActivity.java ====

public void sendMessage(View view)
 {
  Intent intent = new Intent(getBaseContext(), DisplayMessageActivity.class);
  
  editText = (EditText)findViewById(R.id.message);
  String message = editText.getText().toString();
  
  intent.putExtra(EXTRA_MESSAGE, message);
  startActivity(intent); 

 }

- Add the corresponding method in the MainActivity.

- Inside the sendMessage method, create an Intent to start an activity called DisplaymessageActivity.



3. Create the second Activity

==== DisplayMessageActivity.java ====

protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  
  // Get the message from the Intent  
  Intent intent = getIntent();
  String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
  
  TextView textView = new TextView(this);
  textView.setTextSize(40);
  textView.setText(message);
 
 // Set the Textview as an activity layout
  setContentView(textView);
}

- We can now run the app.

- When it opens, type a message in the textfield, click the button, and the message in the textfield appears on the second activity.

'Android > Activity' 카테고리의 다른 글

Action Bar: Adding Action Buttons  (0) 2014.02.03

We can set the background color in the LinearLayout, but can't find the attribute about a border. 

Now Let's see the following about setting borders around the LinearLayout.


The First way is to use Shape Object.


==== header_border.xml ====

<?xml version="1.0" encoding="UTF-8"?>

 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">

   <padding android:left="10sp" android:right="10sp" android:top="10sp" android:bottom="10sp"/>

  <solid android:color="@android:color/transparent"/>

   <stroke

    android:width="1sp"

    android:color="#000000" />

   

 </shape>

: Define header_border.xml like this. Then Save it into '/res/drawable-mdpi'



==== activie_main.xml ====

<LinearLayout 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:orientation="vertical"

    android:background="ffffff"

     >

     <LinearLayout 

         android:layout_width="match_parent"

         android:layout_height="wrap_content"

         android:orientation="horizontal"

         android:background="@drawable/header_border"

         >

       <ImageButton 

           android:id="@+id/side_menu"

           android:layout_width="40sp"

           android:layout_height="40sp"

           android:background="@drawable/icon_list"

           android:contentDescription="사이드 메뉴"

           android:layout_marginTop="5sp"

           />

        <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="아이템 목록"

        android:textSize="20sp" 

        android:layout_marginTop="5sp"

        android:layout_weight="1"

        android:gravity="center"

        />

       <ImageButton 

           android:id="@+id/user_config"

           android:layout_width="37sp"

           android:layout_height="37sp"

           android:background="@drawable/icon_config"

           android:contentDescription="사용자 설정"

           android:layout_marginTop="5sp"

           android:layout_marginRight="5sp"

           />

     </LinearLayout>

: Set the reference of header_border object as a background attribute's value. then We can find borders around the LinearLayout.


But I'd like to do that only the LinearLayout appears only bottom border.

We can see the following for it.


 ==== activity_main.xml ====

<LinearLayout 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:orientation="vertical"

    android:background="ffffff"

     >

     <LinearLayout 

         android:layout_width="match_parent"

         android:layout_height="wrap_content"

         android:orientation="horizontal"

         android:background="f0fff0"

         android:padding="10sp"

         >

       <ImageButton 

           android:id="@+id/side_menu"

           android:layout_width="40sp"

           android:layout_height="40sp"

           android:background="@drawable/icon_list"

           android:contentDescription="사이드 메뉴"

          

           />

        <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="아이템 목록"

        android:textSize="20sp" 

        android:layout_marginTop="5sp"

        android:layout_weight="1"

        android:gravity="center"

        />

       <ImageButton 

           android:id="@+id/user_config"

           android:layout_width="37sp"

           android:layout_height="37sp"

           android:background="@drawable/icon_config"

           android:contentDescription="사용자 설정"

          

           />

     </LinearLayout>

     <!-- This is LinearLayout for bottom border. -->

     <LinearLayout 

         android:layout_width="match_parent"

         android:layout_height="2dp"

         android:background="000000"

         >

         

     </LinearLayout>

    

</LinearLayout>

: We add a LinearLayout for bottom border. It has no child view. It just has layout_height "2dp" and background "000000" as values of the attributes.

 

We can see the bottom border like this.



'Android > Layout' 카테고리의 다른 글

Filling an Listview with data  (0) 2014.02.17

+ Recent posts

출처: http://large.tistory.com/23 [Large]