0

I have been trying to figure this out but it's been a disaster.

I have this menu which populates and I can't change the background or I can't add menu icons to it. can someone help me to figure this out please..!

this is my main activity.xml

<LinearLayout
    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:orientation="vertical"
    tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:minHeight="?android:attr/actionBarSize" />

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white" />

        <com.vproductions.xinxilanka.views.DrawerNavigationListView
            android:id="@+id/drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@android:color/white"/>


    </android.support.v4.widget.DrawerLayout>




</LinearLayout>

this is the mainactivity.java

package com.vproductions.xinxilanka.activities;

import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Build;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import com.vproductions.xinxilanka.utils.EventBus;
import com.squareup.otto.Subscribe;
import com.vproductions.xinxilanka.R;
import com.vproductions.xinxilanka.events.DrawerSectionItemClickedEvent;
import com.vproductions.xinxilanka.fragments.AdvancedSearchFragment;
import com.vproductions.xinxilanka.fragments.NearMapFragment;
import com.vproductions.xinxilanka.fragments.QuickSearchFragment;
import com.vproductions.xinxilanka.repository.FieldRepository;

public class MainActivity extends ActionBarActivity {

  private DrawerLayout mDrawerLayout;
  private ActionBarDrawerToggle mActionBarDrawerToggle;
  private String mCurrentFragmentTitle;
  private static Boolean firstInit = true;

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

    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
      getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
    }

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mActionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawer_opened, R.string.drawer_closed) {
      @Override
      public void onDrawerOpened(View drawerView) {
        super.onDrawerOpened(drawerView);
        if (getSupportActionBar() != null)
          getSupportActionBar().setTitle(R.string.drawer_opened);
      }

      @Override
      public void onDrawerClosed(View drawerView) {
        super.onDrawerClosed(drawerView);
        if (getSupportActionBar() != null)
          getSupportActionBar().setTitle(R.string.drawer_closed);
      }
    };

    mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);


    if (firstInit || getSupportFragmentManager().getFragments() == null)
      displayInitialFragment();
    firstInit = false;
  }

  private void displayInitialFragment() {
    getSupportFragmentManager().beginTransaction().replace(R.id.container, QuickSearchFragment.getInstance()).commit();
    mCurrentFragmentTitle = getString(R.string.section_quick_search);
  }

  @Override
  protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    mActionBarDrawerToggle.syncState();
  }

  @Override
  public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mActionBarDrawerToggle.onConfigurationChanged(newConfig);
  }

  @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) {
    // 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.

    if (mActionBarDrawerToggle.onOptionsItemSelected(item))
      return true;

    return super.onOptionsItemSelected(item);
  }

  @Override
  protected void onStart() {
    super.onStart();
    EventBus.getInstance().register(this);
  }

  @Override
  protected void onStop() {
    EventBus.getInstance().unregister(this);
    super.onStop();
  }

  @Subscribe
  public void onDrawerSectionItemClickEvent(DrawerSectionItemClickedEvent event) {
    mDrawerLayout.closeDrawers();

    if (event == null || TextUtils.isEmpty(event.section) || event.section.equalsIgnoreCase(mCurrentFragmentTitle)) {
      //return;
    }

    //Toast.makeText(this, "MainActivity: Section Clicked: " + event.section, Toast.LENGTH_SHORT).show();

    if (event.section.equalsIgnoreCase(getString(R.string.section_map))) {
      getSupportFragmentManager().beginTransaction().replace(R.id.container, NearMapFragment.getInstance()).commit();
    } else if (event.section.equalsIgnoreCase(getString(R.string.section_quick_search))) {
      getSupportFragmentManager().beginTransaction().replace(R.id.container, QuickSearchFragment.getInstance()).commit();
    } else if (event.section.equalsIgnoreCase(getString(R.string.advanced_search))) {
      if (FieldRepository.getInstance().getSeted()) {
        getSupportFragmentManager().beginTransaction().replace(R.id.container, AdvancedSearchFragment.getInstance()).commit();
      } else {
        Toast.makeText(this, getString(R.string.fields_not_loaded), Toast.LENGTH_LONG).show();
      }
    } else if (event.section.equalsIgnoreCase(getString(R.string.add_property))) {

      // go to external website
      Intent intent = new Intent();
      intent.setAction(Intent.ACTION_VIEW);
      intent.addCategory(Intent.CATEGORY_BROWSABLE);
      intent.setData(Uri.parse("http://xinxilanka.com/index.php/admin/user/login/"));
      startActivity(intent);

    } else if (event.section.equalsIgnoreCase(getString(R.string.open_website))) {

      // go to external website
      Intent intent = new Intent();
      intent.setAction(Intent.ACTION_VIEW);
      intent.addCategory(Intent.CATEGORY_BROWSABLE);
      intent.setData(Uri.parse(getString(R.string.script_url)));
      startActivity(intent);

    } else {
      return;
    }

    mCurrentFragmentTitle = event.section;
  }

  public void open(View view) {
    Intent browserIntent = (new Intent(Intent.ACTION_VIEW, Uri.parse("http://xinxilanka.com/index.php/admin/user/login/")));
    startActivity(browserIntent);
  }
}

the navigation menu that shows now is just white background and no icons.

I want to add a nice header and some icons to the menu items.

this menu is a auto populated menu.

basically the navigation menu which populates menu items without defining them in a menu file.

see below for drawernavigationlistview class

package com.vproductions.xinxilanka.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.vproductions.xinxilanka.R;
import com.vproductions.xinxilanka.adapters.DrawerNavigationListAdapter;
import com.vproductions.xinxilanka.events.DrawerSectionItemClickedEvent;
import com.vproductions.xinxilanka.utils.EventBus;

/**
 * Created by sandi on 04.01.2016..
 */
public class DrawerNavigationListView extends ListView implements AdapterView.OnItemClickListener {
  public DrawerNavigationListView(Context context) {
    this(context, null);
  }

  public DrawerNavigationListView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public DrawerNavigationListView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    // Populate menu
    DrawerNavigationListAdapter adapter = new DrawerNavigationListAdapter(getContext(), 0);
    adapter.add(getContext().getString(R.string.section_quick_search));
    adapter.add(getContext().getString(R.string.section_map));
    adapter.add(getContext().getString(R.string.advanced_search));
    adapter.add(getContext().getString(R.string.howto_use));

    if (getContext().getString(R.string.website_enabled).equalsIgnoreCase("true")) {
      adapter.add(getContext().getString(R.string.add_property));
      adapter.add(getContext().getString(R.string.open_website));
    }

    setAdapter(adapter);

    setOnItemClickListener(this);
  }

  @Override
  public void onItemClick(AdapterView << ? > parent, View view, int position, long id) {
    //Toast.makeText(getContext(), "SectionClicked: " + parent.getItemAtPosition(position), Toast.LENGTH_SHORT).show();

    EventBus.getInstance().post(new DrawerSectionItemClickedEvent((String) parent.getItemAtPosition(position)));
  }
}

navigation_drawer_list_item.xml file

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
          android:background="@drawable/navigation_list_selector"
    android:gravity="left"
    android:padding="8dp"
    android:textSize="14sp"
    android:drawableStart="@mipmap/ic_stars_black_24dp"
    android:drawableLeft="@mipmap/ic_stars_black_24dp"
    android:drawablePadding="3dp"
    android:textColor="@color/black">


</TextView>
Pete V
  • 1
  • 2
  • You can take look https://stackoverflow.com/questions/19659637/how-to-change-the-background-color-of-action-bars-option-menu-in-android-4-2/19659779 it may helpfull to you – Akash Jul 09 '17 at 19:37
  • HI akash, thanks for your reply. but my question is not the options menu. its about the navigation menu, which populates the menu items in a drawer layout without defining them in the menu file. – Pete V Jul 09 '17 at 20:06
  • can you provide `com.vproductions.xinxilanka.views.DrawerNavigationListView` class? –  Jul 09 '17 at 22:26
  • @ibrahim - I just added the DrawerNavigationListView class above. and also I managed to add icons in the navigation_drawer_list_item.xml file but if I add from there I can add only one icon for all menu items that populates. can you help me on this? is there a way to add icons from java class file? – Pete V Jul 10 '17 at 06:56
  • @ibrahim - see attached navigation_drawer_list_item.xml file too – Pete V Jul 10 '17 at 06:56
  • also provide `DrawerNavigationListAdapter ` –  Jul 10 '17 at 21:35

1 Answers1

0

You have to define the background into the DrawerNavigationListAdapter XML.