Custom slide menu dashboard


This is an attempt to create a slide menu dashboard in a test application,the code is very simple and uses the library SlidingMenu and a personaly class to manage history data of the app.

here the complete project: Download PRJ
here the sample code of CommonActivity:

public class BaseActivitySideDashboard extends SlidingActivity {

	String strActActive = "";

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

		//set the Above View
		setContentView(R.layout.act_init);

		//set the Behind View
		setBehindContentView(R.layout.sidedashboard);

		//customize the SlidingMenu
		this.setSlidingActionBarEnabled(true);
		getSlidingMenu().setShadowWidthRes(R.dimen.shadow_width);
		getSlidingMenu().setShadowDrawable(R.drawable.shadow);
		getSlidingMenu().setBehindOffsetRes(R.dimen.actionbar_home_width);
		getSlidingMenu().setBehindScrollScale(0.25f);

		//customize the ActionBar
		ActionBar actionBar = getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
		actionBar.setDisplayHomeAsUpEnabled(true);

		//update activity active
		Intent intent = getIntent();
		if (intent.hasExtra(getPackageName() + ".strActActive")) {
			strActActive = intent.getStringExtra(getPackageName()
					+ ".strActActive");
		}

		//set event button
		Button btn1 = (Button) findViewById(R.id.btn_sidedashboard_1);
		btn1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {

				ManageHistoryFile.getIstance(getApplicationContext())
						.addAction("Button 1 pressed",
								Tools.getIstance().getCurrentTime());

				ManageHistoryFile.getIstance(getApplicationContext())
						.saveListAction();

				loadHistoryDashboard(ManageHistoryFile.getIstance(
						getApplicationContext()).getAllAction());
			}

		});

		Button btn2 = (Button) findViewById(R.id.btn_sidedashboard_2);
		btn2.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {

				ManageHistoryFile.getIstance(getApplicationContext())
						.addAction("Button 2 pressed",
								Tools.getIstance().getCurrentTime());

				ManageHistoryFile.getIstance(getApplicationContext())
						.saveListAction();

				loadHistoryDashboard(ManageHistoryFile.getIstance(
						getApplicationContext()).getAllAction());
			}

		});

		//add activity into navigation dashboard
		loadNavigationDashboard(new DataActivity[] {
				new DataActivity("Act One", ActOne.class,
						R.drawable.ic_sd_act1_unselected),
				new DataActivity("Act Two", ActTwo.class,
						R.drawable.ic_sd_act2_unselected),
				new DataActivity("Act Three", ActThree.class,
						R.drawable.ic_sd_act3_unselected) });
	}

	@Override
	public void onResume() {
		super.onResume();

		//load history action
		loadHistoryDashboard(ManageHistoryFile.getIstance(
				getApplicationContext()).getAllAction());
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			toggle();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return true;
	}

	private void loadHistoryDashboard(
			ArrayList<ManageHistoryFile.DataHistory> arlT) {
		LinearLayout llHistory = (LinearLayout) findViewById(R.id.ll_sidedashboard_history);
		llHistory.removeAllViews();
		LinearLayout.LayoutParams lpPartial = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.MATCH_PARENT);

		if (!arlT.isEmpty()) {
			Iterator<DataHistory> index = arlT.iterator();
			while (index.hasNext()) {
				DataHistory elmHistory = (DataHistory) index.next();

				LinearLayout llActionRec = new LinearLayout(this);
				llActionRec.setClickable(true);

				lpPartial.weight = 1.0f;

				TextView tvAction = new TextView(this);
				tvAction.setText(elmHistory.getAction());
				tvAction.setGravity(Gravity.LEFT);

				TextView tvTimeAction = new TextView(this);
				tvTimeAction.setText(elmHistory.getTimeAction());
				tvTimeAction.setGravity(Gravity.RIGHT);

				llActionRec.addView(tvAction, lpPartial);
				llActionRec.addView(tvTimeAction, lpPartial);

				llHistory.addView(llActionRec);
			}

		} else {
			TextView tvNoHistory = new TextView(this);
			tvNoHistory.setText(R.string.tv_sidedashboard_nohistory);
			tvNoHistory.setGravity(Gravity.CENTER);

			llHistory.addView(tvNoHistory);
		}

		llHistory.invalidate();
	}

	private void loadNavigationDashboard(DataActivity[] dataT) {
		for (final DataActivity data : dataT) {
			LinearLayout llNav = (LinearLayout) findViewById(R.id.ll_sidedashboard_navigation);
			LinearLayout llAct = new LinearLayout(this);
			llAct.setClickable(true);
			llAct.setBackgroundResource(R.drawable.background_ll);

			LinearLayout.LayoutParams lpPartial = new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.MATCH_PARENT,
					LinearLayout.LayoutParams.WRAP_CONTENT);

			ImageView imvAct = new ImageView(this);
			imvAct.setImageResource(data.resIcon);

			TextView tvAct = new TextView(this);
			tvAct.setText(data.strName);
			tvAct.setGravity(Gravity.CENTER_VERTICAL);

			ImageView imvActActive = new ImageView(this);
			if (strActActive.equals(data.strName)) {
				imvActActive.setImageResource(R.drawable.selector);
			}

			lpPartial.weight = 2.0f;
			llAct.addView(imvAct, lpPartial);
			lpPartial.weight = 1.0f;
			llAct.addView(tvAct, lpPartial);
			lpPartial.weight = 4.0f;
			llAct.addView(imvActActive, lpPartial);

			llAct.setOnClickListener(new View.OnClickListener() {
				public void onClick(View view) {

					toggle();

					if (!strActActive.equals(data.strName)) {
						finish();

						ManageHistoryFile.getIstance(getApplicationContext())
								.addAction("Access to " + data.strName,
										Tools.getIstance().getCurrentTime());

						ManageHistoryFile.getIstance(getApplicationContext())
								.saveListAction();

						/*
						 * set actvity active
						 */
						Intent i = new Intent(view.getContext(), data.actClass);
						i.putExtra(getPackageName() + ".strActActive",
								data.strName);

						view.getContext().startActivity(i);
					}

				}
			});

			llNav.addView(llAct);
		}
	}

	public void addCommonAction(String strAct) {
		ManageHistoryFile.getIstance(getApplicationContext()).addAction(strAct,
				Tools.getIstance(getApplicationContext()).getCurrentTime());

		ManageHistoryFile.getIstance(getApplicationContext()).saveListAction();

		loadHistoryDashboard(ManageHistoryFile.getIstance(
				getApplicationContext()).getAllAction());

	}

	private class DataActivity {
		public String strName;
		public Class<?> actClass;
		int resIcon;

		DataActivity(String strT, final Class<?> actT, int resT) {
			this.strName = strT;
			this.actClass = actT;
			this.resIcon = resT;
		}
	}
}

here the sample code of history data manage:

public class ManageHistoryFile {
	private static String HISTORY_FILE_NAME = "historydata";

	private static ManageHistoryFile Instance = null;
	private static Context context;

	private ArrayList<DataHistory> arlHistory = new ArrayList<DataHistory>();

	private ManageHistoryFile() {
	}

	private ManageHistoryFile(Context contextT) {
		ManageHistoryFile.context = contextT;
	}

	public synchronized static ManageHistoryFile getIstance() {
		if (Instance == null) {
			Instance = new ManageHistoryFile();
		}
		return Instance;
	}

	public static synchronized ManageHistoryFile getIstance(Context contextT) {
		if (Instance == null) {
			Instance = new ManageHistoryFile(contextT);
		}
		return Instance;
	}

	public void setContext(Context contextT) {
		ManageHistoryFile.context = contextT;
	}

	public void addAction(String strAct, String strTimeAct) {
		arlHistory.add(0,getIstance().new DataHistory(strAct, strTimeAct));
		saveListAction();
	}

	public ArrayList<DataHistory> getAllAction() {
		if(arlHistory.isEmpty()){
			loadListAction();
		}
		return arlHistory;
	}

	public void saveListAction() {
		StringBuffer strbufferT = new StringBuffer("");
		if (!arlHistory.isEmpty()) {
			Iterator<DataHistory> index = arlHistory.iterator();

			while (index.hasNext()) {
				DataHistory elmHistory = (DataHistory) index.next();
				strbufferT.insert(0, elmHistory.getAction() + "<action-time>"
						+ elmHistory.getTimeAction() + "\n");
			}

			Tools.getIstance(ManageHistoryFile.context).saveToFile(
					HISTORY_FILE_NAME, strbufferT);
		}
	}

	public void loadListAction() {
		// parse the content of file
		Scanner scannerF = new Scanner(Tools.getIstance(context).loadFromFile(
				HISTORY_FILE_NAME));
		try {
			// first use a Scanner to get each line
			while (scannerF.hasNextLine()) {
				Scanner scannerL = new Scanner(scannerF.nextLine());
				scannerL.useDelimiter("<action-time>");

				if (scannerL.hasNext()) {

					arlHistory.add(0,getIstance().new DataHistory(
							scannerL.next(), scannerL.next()));
				}
			}

		} finally {
			scannerF.close();
		}
	}

	public class DataHistory {
		public String strAction;
		public String strTimeAction;

		DataHistory(String strAct, String strTimeAct) {
			this.strAction = strAct;
			this.strTimeAction = strTimeAct;
		}

		public String getAction() {
			return this.strAction;
		}

		public String getTimeAction() {
			return this.strTimeAction;
		}
	}

}

Lascia un commento

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...