mari
mari

Reputation: 51

Action bar and menu are not showing on the fragment activity

I have created a fragment activity for google map but the action bar and menu are not showing I tried different solution but none of theme has solved the problem

public class FitnessTrackerActivity extends FragmentActivity {
    private GoogleMap map;
    GpsTracker gps;
    TrackDBHelper mDBHelper;
    // Fields for flash functionality
    Camera camera;
    Camera.Parameters parameters;

    String date;
    TextView transportation;
    TextView distance;
    TextView calories;
    Chronometer chrono;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fitness_tracker);



        Time today = new Time(Time.getCurrentTimezone());
        today.setToNow();
        date = today.format("%k:%M:%S");
        if (isFlashSupported()) {
            camera = Camera.open();
            parameters = camera.getParameters();
        } else {
            showNoFlashAlert();
        }

    }
    @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_fitness_tracker, menu);
        return true;
    }

    @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.
        switch (item.getItemId()) {
            case R.id.led_on:
                // Turn on LED
                parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                camera.setParameters(parameters);
                camera.startPreview();
                return true;
            case R.id.led_off:
                // Turn off LED
                parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                camera.setParameters(parameters);
                camera.stopPreview();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void showNoFlashAlert() {
        new AlertDialog.Builder(this)
                .setMessage("Your device hardware does not support flashlight!")
.setIcon(android.R.drawable.ic_dialog_alert).setTitle("Error")
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        finish();
                    }
                }).show();
    }

    private boolean isFlashSupported() {
        PackageManager pm = getPackageManager();
        return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
    }
}

and this is the menu that I'm trying to use:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".FitnessTrackerActivity">
    <item android:id="@+id/led_on"
        android:title="LED On"
        app:showAsAction="withText"/>
    <item android:id="@+id/led_off"
        android:title="LED Off"
        app:showAsAction="withText"/>
</menu>

this is the FitnessTrackerActivity XML

<RelativeLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".FitnessTrackerActivity"
    tools:showIn="@layout/activity_fitness_tracker">
    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_marginBottom="150dp"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btnStartStop"
        android:onClick="onButtonStartStopClick"
        android:layout_alignParentBottom="true"
        android:id="@+id/btnStartStop"
        android:layout_marginBottom="40dp"
        android:layout_marginLeft="20dp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/caloriesVal"
        android:text="0"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="40dp"
        android:layout_marginRight="20dp"
        android:textSize="20sp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/caloriesText"
        android:text="@string/caloriesText"
        android:layout_alignParentBottom="true"
        android:layout_toLeftOf="@+id/caloriesVal"
        android:layout_marginBottom="40dp"
        android:layout_marginRight="5dp"
        android:textSize="20sp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/timerText"
        android:text="@string/timer"
        android:layout_above="@id/caloriesText"
        android:layout_toLeftOf="@+id/timerVal"
        android:layout_marginRight="5dp"
        android:textSize="20sp"/>

    <Chronometer
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/timerVal"
        android:layout_above="@id/caloriesText"
        android:layout_alignParentRight="true"
        android:layout_marginRight="20dp"
        android:textSize="20sp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/distanceVal"
        android:text="0"
        android:layout_above="@id/timerText"
        android:layout_alignParentRight="true"
        android:layout_marginRight="20dp"
        android:textSize="20sp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/distanceText"
        android:text="@string/distanceText"
        android:layout_above="@id/timerText"
        android:layout_toLeftOf="@id/distanceVal"
        android:layout_marginRight="5dp"
        android:textSize="20sp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/transportationSel"
        android:layout_above="@id/distanceText"
        android:layout_alignParentRight="true"
        android:layout_marginRight="20dp"
        android:textSize="20sp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/transportationText"
        android:text="@string/transportationText"
        android:layout_above="@id/distanceText"
        android:layout_toLeftOf="@id/transportationSel"
        android:layout_marginRight="5dp"
        android:textSize="20sp"/>
</RelativeLayout>

Upvotes: 1

Views: 159

Answers (2)

Joel Kanyi
Joel Kanyi

Reputation: 511

Extend AppCompatActivity instead of FragmentActivity

This will give your Map the flexibility to have an ActionBar or a Toolbar

Upvotes: 1

Aron
Aron

Reputation: 9

ActionBar work with AppCompatActivity , BUT you can put the googleMap in fragment , with setChildFragmentMenger and put your layoutFragment in the AppCompatActivity layout, and the ActionBar in AppCompatActivity , hope its help

Upvotes: 0

Related Questions