Search code examples
javaandroideclipsebuttonmenu

how to change buttons to menu in android eclipse


I have searched high and low to find a simple way to turn buttons into a menu (any menu style) I currently have buttons in my app and I would like to turn them into a menu (i.e. the 3 dots in the right corner) for a more simple UI

I would like the following to appear in a menu: bCheckOnline bSettings bLogout1

If this is alot more complicated than i thought please contact me and I will pay someone to modify my code. (however, any help is appreciated!)

I have included my res/layout/activity_main.xml below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"             
    android:orientation="vertical"
    android:scrollbars="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:background="@color/black"   
    android:paddingBottom="@dimen/sixteen"
    android:paddingTop="@dimen/sixteen"
    android:weightSum="10" >

        <TextView
            android:id="@+id/tvSyncDate_temp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:visibility="gone"  />
        
        <TextView
            android:id="@+id/tvVersion"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="9.06"
            android:gravity="center_vertical"
            android:paddingLeft="13dp"
            android:singleLine="true"
            android:textColor="@color/white"
            android:text="TAP+REPORT"
            android:textSize="@dimen/twentysize"
            android:textStyle="bold" />

        <Button
            android:id="@+id/bSettings"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.55"
            android:background="@drawable/btn2"
            android:text="Sync"
            android:textColor="@color/white"
            android:textSize="@dimen/twentysize" />

    </LinearLayout>

    <ScrollView
        android:id="@+id/svMain"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="10"
        android:fillViewport="true" >    

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >           
        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:scrollbars="vertical" >
        
                <LinearLayout
                    android:layout_width="match_parent"
                    android:background="@color/white"   
                    android:layout_height="wrap_content" >
                
                <TextView
                    android:id="@+id/tvVersion"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingBottom="5dp"
                    android:paddingLeft="@dimen/thirteen"
                    android:paddingRight="@dimen/thirteen"
                    android:paddingTop="5dp"
                    android:text="Tap tag now with top-back of phone and hold to report. Upon completion tap sync."
                    android:textColor="@color/darkgrey"
                    android:textSize="@dimen/seventeen" />
                    
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:paddingTop="10dp" >

                </LinearLayout>

                 <LinearLayout
                     android:layout_width="match_parent"
                     android:layout_height="wrap_content"
                     android:layout_gravity="top"
                     android:layout_weight="4.76"
                     android:gravity="top|center_vertical|center_horizontal"
                     android:orientation="vertical" >

                     <ImageView
                         android:id="@+id/myanimation"
                         android:layout_width="match_parent"
                         android:layout_height="338dp"
                         android:padding="15dp"
                         android:scaleType="fitCenter"
                         android:src="@anim/anim_android" />

                 </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:gravity="bottom"
                    android:orientation="horizontal"
                    android:paddingBottom="@dimen/twentysize"
                    android:paddingLeft="@dimen/twentysize"
                    android:paddingRight="@dimen/twentysize" >
            
                        <Button
                            android:id="@+id/bCheckOnline"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_weight="8.55"
                            android:background="@drawable/btn3"
                            android:text="View Online"
                            android:textColor="@color/darkgrey"
                            android:textSize="@dimen/seventeen" />
                        
                        <Button
                            android:id="@+id/bLogout1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_weight="4"
                            android:background="@null"
                            android:gravity="right|center_horizontal"
                            android:paddingRight="5dp"
                            android:text="Logout"
                            android:textColor="@color/holobluelight"
                            android:textSize="@dimen/seventeen" />

                </LinearLayout>                 

        </LinearLayout>
    </RelativeLayout>       
    </ScrollView>
</LinearLayout>

I have included the java file:

package com.test.test.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
//import android.provider.Contacts.Intents;
import android.widget.TextView;

import java.util.Random;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import android.net.Uri;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.client.methods.HttpPost;

import java.io.IOException;

import android.nfc.NdefMessage;
import android.nfc.NfcAdapter;
import android.os.Parcelable;
import android.util.Log;

//import org.ndeftools.Message;
//import org.ndeftools.Record;
//import org.ndeftools.wellknown.SmartPosterRecord;
//import org.ndeftools.wellknown.TextRecord;
//import org.ndeftools.wellknown.UriRecord;
 
import android.app.PendingIntent;

import android.content.IntentFilter;
import android.content.IntentFilter.MalformedMimeTypeException;

import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.graphics.drawable.AnimationDrawable;
import android.widget.ImageView;

public class ScanInputActivity extends Activity implements OnClickListener {
    SharedPreferences app_preferences;
    TextView tvSyncDate_temp;
    Vehicle vehicle;
    
    private NfcAdapter mNfcAdapter;
    public static final String MIME_TEXT_PLAIN = "text/plain";
    public static final String TAG = "NfcDemo";
    
     @Override
     public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
        ImageView myAnimation = (ImageView)findViewById(R.id.myanimation);
        final AnimationDrawable myAnimationDrawable
        = (AnimationDrawable)myAnimation.getDrawable();
    
        myAnimation.post(
        new Runnable(){
        
            
          @Override
          public void run() {
           myAnimationDrawable.start();
          }

        });

      Button bSettings= (Button) findViewById(R.id.bSettings);
      bSettings.setOnClickListener(this);
      Button bCheckOnline= (Button) findViewById(R.id.bCheckOnline);
      bCheckOnline.setOnClickListener(this);
      Button bLogout1 = (Button) findViewById(R.id.bLogout1);
      bLogout1.setOnClickListener(this);
      

Solution

  • You need to extends ActionBarActivity insted of Activity.

    Then make a function for onCreateOptionsMenu

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

    Then make onOptionsItemSelected

    @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();
        if (id == R.id.action_settings) {
    
            // Do something when Settings button is clicked.
    
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    

    and in you Resourses folder there must be a menu folder with main.xml something like

    <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.example.test.MainActivity" >
    
        <item
            android:id="@+id/action_settings"
            android:orderInCategory="100"
            android:title="@string/action_settings"
            app:showAsAction="never"/>
    
    </menu>
    

    You can play with the above code to experiment with it and modify the same to get your desired results. I hope the information was helpful. Read more about it here.