Create a menu with 5 options and selected option should appear in textbox.

/*Create a menu with 5 options and selected option should appear in textbox.

*/

File Name: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.fuzzycode.options_menu" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/ThemeOverlay.AppCompat.ActionBar" > <activity android:name=".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>
             

File Name: activity_main.xml

<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/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="30dp"/>
</RelativeLayout>  



File Name: menu_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=".MainActivity">
    <item
        android:id="@+id/settings"
        android:title="@string/action_settings" />
    <item
        android:id="@+id/help"
        android:title="Help"/>
    <item
        android:id="@+id/search"
        android:title="Search"/>
    <item
        android:id="@+id/contact_us"
        android:title="Your Contacts"/>
    <item
        android:id="@+id/take_care"
        android:title="Bye"/>
</menu>
 
File Name: MainActivity.java
package com.fuzzycode.options_menu;


import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.Settings;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.search:
                Uri ur = Uri.parse("https://www.thefuzzycode.blogspot.com");
                Intent ab = new Intent(Intent.ACTION_VIEW, ur);
                startActivity(ab);
                break;

            case R.id.help:
                break;

            case R.id.settings:
                Intent intent = new Intent(Settings.ACTION_SETTINGS);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(in);
                break;

            case R.id.contact_us:
                Intent cn = new Intent();
                cn.setAction(Intent.ACTION_VIEW);
                cn.setData(ContactsContract.Contacts.CONTENT_URI);
                startActivity(cn);
                break;

            case R.id.take_care:
                Toast.makeText(this, "Take Care child", Toast.LENGTH_SHORT).show();
                this.finish();
                break;

            default:
                return super.onOptionsItemSelected(item);
        }
        return true;
    }
}

No comments:

Post a Comment

Featured post

Amazon Interview Process

On July 5, 1994, Jeff Bezos started the world's most "customer-centric" firm out of his garage in Bellevue, Washington. The A...