11 January 2017

Android: Android Working with Action Bar (P1) (Android Studio)

activity_main.xml
Java Android 2017
<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=".MainActivity">

    <TextView android:text="@string/count" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:textSize="18sp"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>
menu_main.xml
Java Android 2017
<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=".MainActivity">
    <item
        android:id="@+id/add"
        android:icon="@android:drawable/ic_menu_add"
        android:title="@string/add"
        app:showAsAction="always" />
    <item
        android:id="@+id/reset"
        android:icon="@android:drawable/ic_menu_revert"
        android:title="@string/reset"
        app:showAsAction="always|withText" />
    <item
        android:id="@+id/about"
        android:icon="@android:drawable/ic_dialog_info"
        android:title="@string/about"
        app:showAsAction="never" />
    <item
        android:id="@+id/exit"
        android:title="@string/exit"
        app:showAsAction="never" />
</menu>
strings.xml
Java Android 2017
<resources>
    <string name="app_name">ActionBar</string>
    <string name="count">Nothing is selected</string>
    <string name="action_settings">Settings</string>
    <string name="reset">Speak</string>
    <string name="about">About</string>
    <string name="exit">Exit</string>
    <string name="about_toast">About is clicked</string>
    <string name="add">Add</string>
</resources>
ApplicationTest
Java Android 2017
package com.example.lonely.demo;

import android.app.Application;
import android.test.ApplicationTestCase;

/**
 * Created by Lonely on 1/11/2017.
 */

public class ApplicationTest extends ApplicationTestCase<Application> {
    public ApplicationTest() {
        super(Application.class);
    }
}
MainActivity.java
Java Android 2017
package com.example.lonely.demo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

    @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) {
        switch (item.getItemId()) {
            case R.id.add:
                count = (TextView) findViewById(R.id.textView);
                count.setText("Add is clicked");
                return (true);
            case R.id.reset:
                count = (TextView) findViewById(R.id.textView);
                count.setText("Nothing is selected");
                return (true);
            case R.id.about:
                Toast.makeText(this, R.string.about_toast, Toast.LENGTH_LONG).show();
                return (true);
            case R.id.exit:
                finish();
                return (true);

        }
        return (super.onOptionsItemSelected(item));
    }
}

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang