Skip to content

Instantly share code, notes, and snippets.

@mkodekar
Created November 9, 2015 08:55
Show Gist options
  • Save mkodekar/a20b0707ea0513b2b92e to your computer and use it in GitHub Desktop.
Save mkodekar/a20b0707ea0513b2b92e to your computer and use it in GitHub Desktop.
Material Drawer with dynamically adding categories to the nav drawer -mike penz method
package com.emagic.newproject;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;
import butterknife.ButterKnife;
import butterknife.InjectView;
import com.emagic.newproject.FastIntents.FastIntent;
import com.emagic.newproject.FastIntents.FastTask;
import com.emagic.newproject.utils.ServiceHandler;
import com.mikepenz.materialdrawer.AccountHeader;
import com.mikepenz.materialdrawer.AccountHeaderBuilder;
import com.mikepenz.materialdrawer.Drawer;
import com.mikepenz.materialdrawer.DrawerBuilder;
import com.mikepenz.materialdrawer.model.PrimaryDrawerItem;
import com.mikepenz.materialdrawer.model.ProfileDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IProfile;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.Toolbar;
import android.text.style.LineHeightSpan.WithDensity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends FragmentActivity {
@InjectView(R.id.toolbar)
Toolbar toolbar;
@SuppressWarnings("rawtypes")
public FastIntent fi;
ProgressDialog pDialog;
private AccountHeader headerResult = null;
@SuppressWarnings("unused")
private Drawer result = null;
String reponse3;
public String app, appby, appdt, appres,tt;
int success1;
String app1, res;
public ArrayList<NameValuePair> param4 = new ArrayList<NameValuePair>();
private String url = "http://e-magicindia.com/ExpenseDetails.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@SuppressWarnings("rawtypes")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ButterKnife.inject(this);
fi = new FastIntent(this);
new Attempt3().execute();
}
@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;
}
@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();
// noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
class Attempt3 extends AsyncTask<String, String, String> {
/** * Before starting background thread Show Progress Dialog * */
boolean failure = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... params1) {
// TODO Auto-generated method stub
// here Check for success tag
// for( int i=0;i<=cut1-1;i++){
//
// gid = sygpid.get(i);
// gname = sygpname.get(i);
// glname = sygpcrtby.get(i);
//
//
//
// params2.add(new BasicNameValuePair("gid", gid));
// params2.add(new BasicNameValuePair("gname", gname));
// params2.add(new BasicNameValuePair("glname", glname));
//
// Servicehandler service = new Servicehandler();
// responce1 = service.getJSONfromURL(LOGIN_URL1, params2);
// }
Log.d("request!", "starting");
System.out.println("Data :"+url+" ");
System.out.println("Dad4 :"+param4+" ");
try {
ServiceHandler service = new ServiceHandler();
res = service.getJSONfromURL(url, param4);
System.out.print("outp3:" + res +"");
// checking log for json response
if(res!=null){
JSONObject jobj = new JSONObject(res);
JSONObject obj1 = jobj.getJSONObject("0");
/* aid= obj1.getString("Expense_Id");
crby= obj1.getString("Created_By");*/
app = (String) obj1.get("Approved_By");
appby = (String) obj1.get("Approved_By");
appdt = (String) obj1.get("Approved_Date");
appres = (String) obj1.get("ApprovalReason");
success1 = jobj.getInt(TAG_SUCCESS);
System.out.println("add:" + app);
if (success1 == 1)
{
Log.d("Successfully Login!", jobj.toString());
return jobj.getString(TAG_MESSAGE);
}
else{
return jobj.getString(TAG_MESSAGE);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* * Once the background process is done we need to Dismiss the progress
* dialog asap *
**/
protected void onPostExecute(String message) {
pDialog.dismiss();
if (message != null) {
if (success1 == 1)
{
/*Database usal1 = new Database(MainActivity.this);
usal1.open();
usal1.updatsalcol(aid,crby,app,appby,appdt,appres);
usal1.close();
Intent ii = new Intent("com.example.compny.MENU");
startActivity(ii); */
/* finish();*/
// new Attempt2().execute();
tt = app;
System.out.println("appt" + tt);
final IProfile profile = new ProfileDrawerItem().withName("Emagic")
.withIcon(R.drawable.ic_launcher);
headerResult = new AccountHeaderBuilder().withActivity(MainActivity.this)
.withCompactStyle(true)
.withHeaderBackground(R.drawable.background_poly)
.addProfiles(profile)
.build();
// Create the drawer
result = new DrawerBuilder()
.withActivity(MainActivity.this)
.withTranslucentStatusBar(true)
.withFullscreen(true)
.withToolbar(toolbar)
.withActionBarDrawerToggle(true)
.withAccountHeader(headerResult)
// set the AccountHeader we created earlier for the header
.addDrawerItems(
new PrimaryDrawerItem().withName(app)
.withIcon(R.drawable.home).withIdentifier(1),
new PrimaryDrawerItem().withName(appby)
.withIcon(R.drawable.home).withIdentifier(2),
new PrimaryDrawerItem().withName(appdt)
.withIcon(R.drawable.home).withIdentifier(3),
new PrimaryDrawerItem().withName(appres)
.withIcon(R.drawable.home).withIdentifier(4))
.withOnDrawerItemClickListener(
new Drawer.OnDrawerItemClickListener() {
@Override
public boolean onItemClick(View view, int position,
IDrawerItem drawerItem) {
if (drawerItem != null) {
if (drawerItem.getIdentifier() == 1) {
fi.toast("Item Clicked");
;
} else if (drawerItem.getIdentifier() == 2) {
fi.toast("Item Click");
;
} else if (drawerItem.getIdentifier() == 3) {
fi.toast("Item Clicked");
;
} else if (drawerItem.getIdentifier() == 4) {
fi.toast("Item Clicked");
;
}
}
return false;
}
}).build();
}
// this finish() method is used to tell android os that we are done with current
//activity now! Moving to other activity
// startActivity(ii);
// return json.getString(TAG_MESSAGE);
}
else
{
//return json.getString(TAG_MESSAGE);
}
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG)
.show();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment