Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addition of more Actions #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion actionbar/default.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@

android.library=true
# Project target.
target=android-10
target=android-13
3 changes: 3 additions & 0 deletions actionbar/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
<string name="hello">Hello World!</string>
<string name="app_name">ActionBar</string>
<string name="actionbar_activity_not_found">Failed to open intent...</string>
<string name="actionbar_listener_unimplemented">This Activity does not implement OnActionClickListener.</string>
<string name="actionbar_listener_error">The OnActionClickListener was not instantiated properly.</string>
<string name="actionbar_search_error">OnSearchRequested() could not be called for your Activity.</string>
</resources>
120 changes: 118 additions & 2 deletions actionbar/src/com/markupartist/android/widget/ActionBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@

import java.util.LinkedList;

import com.markupartist.android.widget.actionbar.R;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
Expand All @@ -36,6 +35,8 @@
import android.widget.TextView;
import android.widget.Toast;

import com.markupartist.android.widget.actionbar.R;

public class ActionBar extends RelativeLayout implements OnClickListener {

private LayoutInflater mInflater;
Expand Down Expand Up @@ -291,6 +292,121 @@ public void performAction(View view) {
}
}
}

/**
* An Action which calls up a dialog from the host {@link Activity}, using {@link Activity.onCreateDialog(int id)}
* @author Espiandev
*
*/
public static class DialogAction extends AbstractAction {

private Activity mHost;
private int dialogId;

/**
*
* @param context the context to invoke a dialog in
* @param dialogId an int id to determine which dialog to invoke
* @param drawable the drawable to use for the action
*/
public DialogAction(Activity host, int dialogId, int drawable) {
super(drawable);
mHost = host;
this.dialogId = dialogId;
}

@Override
public void performAction(View view) {
try {
mHost.showDialog(dialogId);
} catch (Exception e) {
Toast.makeText(mHost,
e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}

}

/**
* An Action which utilises an interface, allowing the host activity to handle the click.
* @author Espiandev
*
*/
public static class InterfaceAction extends AbstractAction {
private OnActionClickListener mListener = null;
private Activity mHost;

/**
* Create an InterfaceAction. <code>host</code> must implement {@link OnActionClickListener}.
* @param host the {@link Activity} which will handle the click
* @param drawable the icon for the {@link Action}
*/
public InterfaceAction(Activity host, int drawable) {
super(drawable);
mHost = host;
try {
mListener = (OnActionClickListener) mHost;
} catch (ClassCastException cce) {
Toast.makeText(host,
mHost.getText(R.string.actionbar_listener_unimplemented),
Toast.LENGTH_SHORT).show();
}
}

/**
* Set the OnActionClickListener
* @param listener The listener to bind to this InterfaceAction
* @return this InterfaceAction
*/
public InterfaceAction setOnActionListener(OnActionClickListener listener) {
this.mListener = listener;
return this;
}

@Override
public void performAction(View view) {
try {
mListener.onActionClick(this);
} catch (NullPointerException npe) {
Toast.makeText(mHost,
mHost.getText(R.string.actionbar_listener_unimplemented),
Toast.LENGTH_SHORT).show();
} catch (Exception ex) {
Toast.makeText(mHost,
mHost.getText(R.string.actionbar_listener_error),
Toast.LENGTH_SHORT).show();
}
}

}

/**
* An Action which invokes onSearchRequested() in the hosting Activity
* @author Espiandev
*
*/
public static class SearchAction extends AbstractAction {

private Activity mHost;

public SearchAction(Activity host, int drawable) {
super(drawable);
mHost = host;
}

@Override
public void performAction(View view) {
try {
mHost.onSearchRequested();
} catch (Exception mnfe) {
Toast.makeText(mHost,
mHost.getText(R.string.actionbar_search_error),
Toast.LENGTH_SHORT).show();
}
}

}

/*
public static abstract class SearchAction extends AbstractAction {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.markupartist.android.widget;

public interface OnActionClickListener {

/**
* Called when a {@link InterfaceAction} is clicked
* @param a the {@link Action} which was clicked
*/
public void onActionClick(ActionBar.Action a);

}
3 changes: 3 additions & 0 deletions actionbarexample/res/layout/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remove share action" />
<Button android:id="@+id/add_search_action" android:layout_width="wrap_content" android:text="Add Search Action" android:layout_height="wrap_content"></Button>
<Button android:id="@+id/add_dialog_action" android:layout_width="wrap_content" android:text="Add Dialog Action" android:layout_height="wrap_content"></Button>
<Button android:id="@+id/add_inter_action" android:layout_width="wrap_content" android:text="Add Interface Action" android:layout_height="wrap_content"></Button>
</LinearLayout>
</ScrollView>
</LinearLayout>
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.markupartist.android.actionbar.example;

import com.markupartist.android.widget.ActionBar;
import com.markupartist.android.widget.ActionBar.Action;
import com.markupartist.android.widget.ActionBar.IntentAction;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
Expand All @@ -13,7 +11,15 @@
import android.widget.Button;
import android.widget.Toast;

public class HomeActivity extends Activity {
import com.markupartist.android.widget.ActionBar;
import com.markupartist.android.widget.ActionBar.Action;
import com.markupartist.android.widget.ActionBar.DialogAction;
import com.markupartist.android.widget.ActionBar.IntentAction;
import com.markupartist.android.widget.ActionBar.InterfaceAction;
import com.markupartist.android.widget.ActionBar.SearchAction;
import com.markupartist.android.widget.OnActionClickListener;

public class HomeActivity extends Activity implements OnActionClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -44,7 +50,7 @@ public void onClick(View v) {
actionBar.setProgressBarVisibility(View.GONE);
}
});

Button removeActions = (Button) findViewById(R.id.remove_actions);
removeActions.setOnClickListener(new OnClickListener() {
@Override
Expand All @@ -69,6 +75,30 @@ public int getDrawable() {
});
}
});

Button addSAction = (Button) findViewById(R.id.add_search_action);
addSAction.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
actionBar.addAction(new SearchAction(HomeActivity.this, android.R.drawable.ic_menu_search));
}
});

Button addDAction = (Button) findViewById(R.id.add_dialog_action);
addDAction.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
actionBar.addAction(new DialogAction(HomeActivity.this, 0, android.R.drawable.ic_menu_info_details));
}
});

Button addIAction = (Button) findViewById(R.id.add_inter_action);
addIAction.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
actionBar.addAction(new InterfaceAction(HomeActivity.this, android.R.drawable.ic_menu_view));
}
});

Button removeAction = (Button) findViewById(R.id.remove_action);
removeAction.setOnClickListener(new OnClickListener() {
Expand Down Expand Up @@ -101,4 +131,18 @@ private Intent createShareIntent() {
intent.putExtra(Intent.EXTRA_TEXT, "Shared from the ActionBar widget.");
return Intent.createChooser(intent, "Share");
}


@Override
public void onActionClick(Action a) {
Toast.makeText(this, "The InterfaceAction has been received", Toast.LENGTH_SHORT).show();
}

@Override
public Dialog onCreateDialog(int id) {
AlertDialog.Builder b = new AlertDialog.Builder(this);
b.setMessage("The DialogAction has been received. Hit back to clear this dialog.")
.setTitle("DialogAction");
return b.create();
}
}