Juha Kuitunen avatar Juha Kuitunen committed a9a88e5

list branches in submenu and display Toast message when clicked

Comments (0)

Files changed (2)

res/menu/menu_source.xml

-<?xml version="1.0" encoding="utf-8"?>
-<menu xmlns:android="http://schemas.android.com/apk/res/android" >
-    <item android:id="@+id/menu_branch" android:title="@string/branch" android:showAsAction="always|withText" android:icon="@drawable/fork_28x32"></item>
-</menu>

src/com/saibotd/bitbeaker/SourceBrowserActivity.java

 
 import com.actionbarsherlock.app.ActionBar;
 import com.actionbarsherlock.view.Menu;
-import com.actionbarsherlock.view.MenuInflater;
 import com.actionbarsherlock.view.MenuItem;
+import com.actionbarsherlock.view.SubMenu;
 
 public class SourceBrowserActivity extends MyActivity {
 
 	private String scm;
 	private String owner;
 	private String subdir;
-	private String branches;
+	private String[] branches;
+	private SubMenu branchesMenu;
+	private Menu mainMenu;
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
         ActionBar actionBar = getSupportActionBar();
         setTitle(slug);
         actionBar.setSubtitle(subdir);
+
+        branches = new String[1];
+        if (scm.equals("git")) branches[0] = "master";
+        else branches[0] = "default";
+        listBranches();
+
         AsyncLoader asyncLoader = new AsyncLoader();
         if(scm.equals("git")) asyncLoader.execute("https://api.bitbucket.org/1.0/repositories/" + owner + "/"+slug+"/src/master" + encode(subdir));
         else asyncLoader.execute("https://api.bitbucket.org/1.0/repositories/" + owner + "/"+slug+"/src/tip" + encode(subdir));
-        listBranches();
     }
 
 	private void listBranches() {
 		JSONObject json;
 		try {
 			json = new JSONObject(result);
-			branches = json.names().toString();
+			JSONArray names = json.names();
+			branches = new String[names.length()];
+			for (int i=0; i<names.length(); i++)
+				branches[i] = names.getString(i);
+			setBranchesMenuItems(mainMenu);
 		} catch (JSONException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 		}
 	}
 
+	@Override
 	public boolean onCreateOptionsMenu(Menu menu) {
-		MenuInflater inflater = getSupportMenuInflater();
-		inflater.inflate(R.menu.menu_source, menu);
-		return true;
+
+		mainMenu = menu;
+		branchesMenu = menu.addSubMenu(R.string.branch);
+		setBranchesMenuItems(mainMenu);
+
+		MenuItem branchesMenuItem = branchesMenu.getItem();
+		branchesMenuItem.setIcon(R.drawable.fork_28x32);
+		branchesMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
+
+		return super.onCreateOptionsMenu(mainMenu);
+	}
+
+	private void setBranchesMenuItems(Menu menu) {
+		branchesMenu.clear();
+		for (int i=0; i<branches.length; i++)
+			branchesMenu.add(branches[i]);
 	}
 
 	@Override
 	public boolean onOptionsItemSelected(MenuItem item) {
-		if (item.getItemId() == R.id.menu_branch) {
-			Toast.makeText(this, branches, Toast.LENGTH_LONG).show();
+		if (!item.getTitle().equals(getString(R.string.branch))) {
+			Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
 			return true;
 		}
 		else return super.onOptionsItemSelected(item);
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.