I want to use spinner in the action bar in my activity below is the onCreateOptionsMenu: I use this tutorial to achieve this approach. My problem is when the activity is lunch, the onNavigationItemSelected method fires and the code on the switch/case run and the activity that I set for the position 0 opens. what should I do to prevent to run the switch/case when the activity is lunch?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.main, menu);
SpinnerAdapter mSpinnerAdapter;
if(Build.VERSION.SDK_INT <= 10)
{
mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.spinner_data,android.R.layout.simple_spinner_item);
}
else
{
mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.spinner_data,android.R.layout.simple_spinner_dropdown_item);
}
ActionBar.OnNavigationListener mOnNavigationListener = new ActionBar.OnNavigationListener()
{
#Override
public boolean onNavigationItemSelected(int position, long itemId)
{
switch (position)
{
case 0:
Intent searchIntent = new Intent(ActivitySearchBusiness.this, ActivityFindBusinessCity.class);
startActivity(searchIntent);
break;
case 2:
Intent dealsIntent = new Intent(ActivitySearchBusiness.this, ActivityDeals.class);
startActivity(dealsIntent);
break;
case 3:
Intent eventsIntent = new Intent(ActivitySearchBusiness.this, ActivityEvents.class);
startActivity(eventsIntent);
break;
}
return true;
}
};
actionBar.setListNavigationCallbacks(mSpinnerAdapter,
return super.onCreateOptionsMenu(menu);
}
You don't need the following code inside onCreateOptionsMenu(Menu):
....
....
Remove it and place it in your activity's onCreate(Bundle) method.
Edit:
Declare a global boolean variable:
boolean initializing = true;
Place the following code inside onCreate(Bundle):
SpinnerAdapter mSpinnerAdapter;
if(Build.VERSION.SDK_INT <= 10)
{
mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.spinner_data,android.R.layout.simple_spinner_item);
}
else
{
mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.spinner_data,android.R.layout.simple_spinner_dropdown_item);
}
ActionBar.OnNavigationListener mOnNavigationListener = new ActionBar.OnNavigationListener()
{
#Override
public boolean onNavigationItemSelected(int position, long itemId)
{
if (initializing) {
initializing = false;
} else {
switch (position)
{
case 0:
Intent searchIntent = new Intent(ActivitySearchBusiness.this, ActivityFindBusinessCity.class);
startActivity(searchIntent);
break;
case 2:
Intent dealsIntent = new Intent(ActivitySearchBusiness.this, ActivityDeals.class);
startActivity(dealsIntent);
break;
case 3:
Intent eventsIntent = new Intent(ActivitySearchBusiness.this, ActivityEvents.class);
startActivity(eventsIntent);
break;
}
}
return true;
}
};
//actionBar.setListNavigationCallbacks(mSpinnerAdapter, mOnNavigationListener);
getActionBar().setListNavigationCallbacks(mSpinnerAdapter, mOnNavigationListener);
Related
I'm new in Android programming. I'm working on an application that have multiple activities. I've created a custom menu with ListView. I would like to put this menu in a base activity to be available in all activities. How should I do this?
Till now, I have something like this:
This is for the button to toggle the menu
menuToggelIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Hide layouts if VISIBLE
if(menuLayout.getVisibility() == View.VISIBLE)
{
menuLayout.setVisibility(View.GONE);
}
// Show layouts if they're not VISIBLE
else
{
menuLayout.setVisibility(View.VISIBLE);
}
}
});
And this is for the menu
menuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String name = menuArray[position];
Context context = getApplicationContext();
switch (name) {
case "CASE1":
Intent case1Intent = new Intent(context, Activity1.class);
startActivity(case1Intent);
break;
case "CASE2":
Intent case2Intent = new Intent(context, Activity2.class);
startActivity(case2Intent);
break;
case "CASE3":
Intent case3Intent = new Intent(context, Activity3.class);
startActivity(case3Intent);
break;
case "CASE4":
Intent case4Intent = new Intent(context, Activity4.class);
startActivity(case4Intent);
break;
case "CASE5":
Intent case5Intent = new Intent(context, Activity5.class);
startActivity(case5Intent);
break;
case "CASE6":
Intent case6Intent = new Intent(context, Activity6.class);
startActivity(case6Intent);
break;
case "CASE7":
Intent case7Intent = new Intent(context, Activity7.class);
startActivity(case7Intent);
break;
default:
break;
}
}
});
Android custom menu
make one BaseActivity class and all activity extends by BasyActivity class.
BaseActivity class define your main things that show all the screen like menu and other thing. for example
public class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.manu_file_name, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.icon) {
Toast.makeText(getApplicationContext(), "Hello World", 0).show();
}
return super.onOptionsItemSelected(item);
}
}
and this activity extends all other activity.
I'm trying to build a drop down menu in the Action Bar Compat, as per the dev example: http://developer.android.com/guide/topics/ui/actionbar.html#Dropdown
I can't get onNavigationItemSelected to fire though.
The spinner dropdown item in the action bar is being generated, to the right of the actionbar's Title. When I press it I do see the triangle icon turn blue. The OnNavigationListener and Sting[]strings are being ran in OnCreate.
How would I get this to work?
public class Main extends ActionBarActivity {
private ActionBar actionBar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
spinner = new Spinner(this);
SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this,
R.array.operating_systems, android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(mSpinnerAdapter);
ActionBar.OnNavigationListener mOnNavigationListener = new ActionBar.OnNavigationListener() {
String[] strings = getResources().getStringArray(R.array.operating_systems);
#Override
public boolean onNavigationItemSelected(int position, long itemId) {
ListContentFragment newFragment = new ListContentFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container, newFragment, strings[position]);
ft.commit();
return true;
}
};
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_share:
Toast.makeText(this, "Refresh selected", Toast.LENGTH_SHORT)
.show();
break;
case R.id.action_settings:
Toast.makeText(this, "Settings selected", Toast.LENGTH_SHORT)
.show();
break;
default:
break;
}
return true;
}
}
Doesn't look like you're calling ActionBar.setListNavigationCallbacks.
From step 4 in the example:
Set the callback for the drop-down list with setListNavigationCallbacks(). For example:
actionBar.setListNavigationCallbacks(mSpinnerAdapter, mNavigationCallback);
How can I refer to onListItemClick within onDialogPositiveClick? So the page does not open before/while the dialog box opens..Can I literally put onListItemClick in onDialogPositiveClick, or do I have to do something completely different? Here is my code...thanks for all/any help!
public class MainActivity extends ListActivity implements TheDialog.NoticeDialogListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
String[] sites = {"Google", "Amazon", "Ebay" , "Reddit", "SmashingMag", "CCC"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.mylist_item, R.id.textView1, sites);
setListAdapter(adapter);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
DialogFragment newFragment = new TheDialog();
newFragment.show(getFragmentManager(), "Confirm");
Intent i = null;
switch(position){
case 0:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(i); break;
case 1:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.amazon.com"));
startActivity(i); break;
case 2:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.ebay.com"));
startActivity(i); break;
case 3:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.reddit.com"));
startActivity(i); break;
case 4:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.smashingmag.com"));
startActivity(i); break;
case 5:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.myccc.corning-cc.edu"));
startActivity(i); break;
}
}
#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 void onDialogPositiveClick(DialogFragment dialog) {
// TODO Auto-generated method stub
}
#Override
public void onDialogNegativeClick(DialogFragment dialog) {
// TODO Auto-generated method stub
}
}
What you can do is not fire intent on list item click and move the switch code to
onDialogPositiveClick(DialogFragment dialog){
Intent i = null;
switch(position){
case 0:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(i); break;
case 1:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.amazon.com"));
startActivity(i); break;
case 2:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.ebay.com"));
startActivity(i); break;
case 3:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.reddit.com"));
startActivity(i); break;
case 4:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.smashingmag.com"));
startActivity(i); break;
case 5:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.myccc.corning-cc.edu"));
startActivity(i); break;
}}
This way user will only go to the next page when positive button is clicked, hope you want this kind of behavior.
So, I'm using jfeinstein10 library for my SlidingMenu. It works fine but I'm having a problem to toggle the menu when the user taps in one of the menu options.
I have the MainActivty where the slidingmenu is and a fragment called SampleListFragment where I set the menu options.
What I'm trying to do is call a function from the MainActivity when I click the option. This function should only toggle the menu, but instead I get a NullPointException error.
My MainActivity
public class MainActivity extends BaseActivity implements SlidingActivityBase {
private SlidingMenu menu;
private ImageButton btn_pesquisa;
private ImageButton btn_toggle;
private MakeMateria makeMat = new MakeMateria();
private static final int SCREEN_ORIENTATION_PORTRAIT = 1;
String id_test;
SampleListFragment listFragment = new SampleListFragment();
public MainActivity() {
super(R.string.title_bar_content);
}
public void mainToggle() {
Log.d("1", "" + this);
toggle();
Log.d("2", "" + this);
}
public static Intent newInstance(Activity activity, int pos) {
Intent intent = new Intent(activity, MainActivity.class);
intent.putExtra("pos", pos);
return intent;
}
public void testeEvent(){
Log.d("Funciona","works");
toggle();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
BitmapDrawable bg = (BitmapDrawable) getResources().getDrawable(
R.drawable.titlebar);
bg.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
getSupportActionBar().setBackgroundDrawable(bg);
BitmapDrawable bgSplit = (BitmapDrawable) getResources()
.getDrawable(R.drawable.titlebar);
bgSplit.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
getSupportActionBar().setSplitBackgroundDrawable(bgSplit);
}
int pos = 0;
if (getIntent().getExtras() != null) {
pos = getIntent().getExtras().getInt("pos");
}
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.abs_layout);
menu = new SlidingMenu(this);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setShadowWidthRes(R.dimen.shadow_width);
menu.setShadowDrawable(R.drawable.shadow);
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
menu.setFadeDegree(0.35f);
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
menu.setBehindScrollScale((float) 1.0);
menu.setMenu(R.layout.menu_frame);
// set the Above View
setContentView(R.layout.content_frame);
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, new MainFragment()).commit();
setSlidingActionBarEnabled(true);
btn_pesquisa = (ImageButton) findViewById(R.id.btnPesquisa);
btn_toggle = (ImageButton) findViewById(R.id.btn_menu);
btn_toggle.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
toggle();
}
});
btn_pesquisa.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(),
SearchActivity.class);
//startActivity(intent);
overridePendingTransition(R.anim.view_transition_in_left,
R.anim.view_transition_out_left);
}
});
}
public void getMenu(){
menu.toggle();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
toggle();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
This part is from my fragment:
#Override
public void onListItemClick(ListView lv, View v, int position, long id) {
Fragment newContent = null;
android.support.v4.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();
switch (position) {
case 0:
Log.d("1", "1");
getFragmentManager().beginTransaction().replace(R.id.content_frame, new MainFragment()).commit();
mainActivity.getMenu();
break;
case 1:
Log.d("2", "2");
toggle();
break;
case 2:
Log.d("3", "3");
toggle();
break;
case 3:
Log.d("4", "4");
toggle();
break;
case 4:
Log.d("5", "5");
toggle();
break;
case 5:
Log.d("6", "6");
toggle();
break;
case 6:
Log.d("7", "7");
toggle();
break;
case 7:
Log.d("8", "8");
toggle();
break;
case 8:
Log.d("9", "9");
toggle();
break;
}
if (newContent != null)
switchFragment(newContent);
}
MainActivity mainActivity is global, and the insance of it I did in the onCreateView.
The NPE points to the lines where I call the function and where I call the toggle inside the function.
Many thanks.
I did this -> Android : Accessing container activity object from fragment using putExtra?
The problem was that I passing a null object, but when I did like this ->
((MainActivity) this.getActivity()).getMenu()
I was able to get the correct value from the object.
#Wenger thanks for the help.
I'm trying to implement a drop down list as navigation for the action bar in Android.
I can see the drop down list and the items, but I can't get the clicking event.
I'm not sure what I'm missing since I was following the tutorial in http://developer.android.com/guide/topics/ui/actionbar.html
This is my code:
public void onCreate(Bundle savedInstanceState) {
OnNavigationListener mOnNavigationListener;
super.onCreate(savedInstanceState);
// setContentView(R.layout.info_layout);
// getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setNavigationMode(getSupportActionBar().NAVIGATION_MODE_LIST);
SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.navigation_array, android.R.layout.simple_dropdown_item_1line);
mOnNavigationListener = new OnNavigationListener() {
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
switch (itemPosition) {
case 1:
Intent i = new Intent();
i.setClass(getApplicationContext(), ZoekAndBoekActivity.class);
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
}
// return super.onOptionsItemSelected(itemPosition);
return true;
}
};
getSupportActionBar().setListNavigationCallbacks(mSpinnerAdapter, mOnNavigationListener);
}
Thanks a lot in advance!
Are you sure that you don't get click events? You're creating intent but doesn't do anything with it. Try something like this:
switch (itemPosition) {
case 1:
Intent i = new Intent();
i.setClass(getApplicationContext(), ZoekAndBoekActivity.class);
startActivity(i);
break;
...
}
or add writing to log to be sure:
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
Log.d("SomeTag", "Get click event at position: " + itemPosition);
switch (itemPosition) {
...
}
}
and see in the logcat output for message with "SomeTag" when you click on items.
I think the return statement must be false inside the switch case, and your case must have brackets.. Hope it helps :)))