2

I'm newbie in this and I'm wondering if I can make my activity to extend two java classes

Why i need that?

  • In my activity I need a Map and also a Navigation Drawer which will provide all the option where the user will be able to work with.

This is the code from my MainActivity:

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    Toolbar toolbar;
    DrawerLayout drawerLayout;
    NavigationView navigationView;

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

        setTitle("");

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        navigationView = (NavigationView) findViewById(R.id.navigation_view);

        setSupportActionBar(toolbar);
        navigationView.setNavigationItemSelectedListener(this);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open_drawer, R.string.close_drawer);
        drawerLayout.setDrawerListener(toggle);
        toggle.syncState();


    }

    @Override
    public void onBackPressed() {
        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }

    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {

        int id = item.getItemId();

        switch (id) {



        }

        drawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }
}
  • I also found out that two classes can't be extended on one activity. Any other solution how can I achieve it. Тhanks in advance.
Majid Ahmadi Jebeli
  • 517
  • 1
  • 6
  • 22
Jovan
  • 306
  • 6
  • 20

2 Answers2

2

In Java, there is no way of extending from two classes but you can get all the features of that class by creating a new object from that class and use it in your program.

You can also look at this link, it may help you

Update:

I used this method to use Google Maps APIs in my program without Extended class.

public class MapInfoFragment extends Fragment implements OnMapReadyCallback {
    private static GoogleMap mMap;


    public MapInfoFragment() {
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_travelling_info, container, false);
        SupportMapFragment mMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.frmap);
        lnl_map_fragment = rootView.findViewById(R.id.lnl_map_fragment);
        mMapFragment.getMapAsync(this);
        return rootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        googleMap.setMapType(1);
        googleMap.getUiSettings().setMapToolbarEnabled(false);
        googleMap.setIndoorEnabled(true);
        googleMap.setBuildingsEnabled(true);
        googleMap.setTrafficEnabled(false);
        googleMap.getUiSettings().setZoomControlsEnabled(false);
        googleMap.getUiSettings().setRotateGesturesEnabled(false);
        googleMap.getUiSettings().setAllGesturesEnabled(false);
        googleMap.getUiSettings().setTiltGesturesEnabled(false);
        LatLngBounds.Builder builder = new LatLngBounds.Builder();

        origin_marker_option.position(origin_ltln)
                .title("Origin")
                .icon(BitmapDescriptorFactory.fromBitmap(btm_origin))
                .flat(false);
        googleMap.addMarker(origin_marker_option);

        if (ar_dest_lat_lon.size() > 0) {
            dest_marker_option_1.position(ar_dest_lat_lon.get(0))
                    .title("Dest1")
                    .icon(BitmapDescriptorFactory.fromBitmap(btm_dest_1))
                    .flat(false);
            googleMap.addMarker(dest_marker_option_1);
        } else {
            builder.include(new LatLng(origin_ltln.latitude - 0.003, origin_ltln.longitude - 0.003));
            builder.include(new LatLng(origin_ltln.latitude + 0.003, origin_ltln.longitude + 0.003));
        }


        builder.include(origin_ltln);
        for (LatLng dest_lat_lon : ar_dest_lat_lon)
            builder.include(dest_lat_lon);
        LatLngBounds bounds = builder.build();
        int width = getResources().getDisplayMetrics().widthPixels;
        int height = getResources().getDisplayMetrics().heightPixels / 3;
        int padding = (int) (width * 0.12); // offset from edges of the map 12% of screen
        int w = lnl_map_fragment.getWidth();
        int h = lnl_map_fragment.getHeight();
        int p = (int) (w * 0.12);
        try {
            mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, padding));
        } catch (Exception e) {
            e.printStackTrace();
        }
        initMapCameraPosition();
    }

    private void initMapCameraPosition() {
        if (mMap == null) return;

    }
}
  • At the request of the respectable questioner, the code was placed
Majid Ahmadi Jebeli
  • 517
  • 1
  • 6
  • 22
  • For ex. how can I use the onMapReady() method if the activity does not extends the FragmentActivity java class and does not implements the OnMapReadyCallback. – Jovan Jan 06 '18 at 21:00
  • Please wait a few minutes to copy my project code from my gitlab repository to show you – Majid Ahmadi Jebeli Jan 06 '18 at 21:06
1

I'm newbie in this and I'm wondering if I can make my activity to extend two java classes.

No, sorry.

In my activity I need a Map and also a Navigation Drawer which will provide all the option where the user will be able to work with

That does not require inheriting from two Java classes.

Assuming that by "Map" you mean the Maps V2 API from the Play Services SDK, you can use a MapFragment for displaying the map.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491