Is it possible to add a swipe functionality to an existing Tab Menu? i.e. swipe left or right to move between the tabs rather than clicking on a tab.
I already have an exisiting 3 tab menu and do not want to greatly alter it's structure, but would love to add the swipe functionality.
How can I do so?
Main Menu/TabHost:
public class MainMenu extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
TabSpec tab1 = tabHost.newTabSpec("Games");
Intent tab1Intent = new Intent(this, firstActivity.class);
tab1.setContent(tab1Intent);
tab1.setIndicator("Games");
TabSpec tab2 = tabHost.newTabSpec("Search");
Intent tab2Intent = new Intent(this, secondActivity.class);
tab2.setContent(tab2Intent);
tab2.setIndicator("Search");
TabSpec tab3 = tabHost.newTabSpec("Summary");
Intent tab3Intent = new Intent(this, thirdActivity.class);
tab3.setContent(tab3Intent);
tab3.setIndicator("Summary");
tabHost.addTab(tab1);
tabHost.addTab(tab2);
tabHost.addTab(tab3);
}
#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;
}
}
Example of tab (middle tab):
public class secondActivity extends ActionBarActivity implements View.OnClickListener {
TextView instruction;
Button date;
Button game;
Button med;
Button att;
Button score;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_tab);
initialiseVars();
}
/**
* Initialises vars and sets onclick listeners for buttons
*/
public void initialiseVars() {
// setting up vars
instruction = (TextView) findViewById(R.id.tvSearchHome);
date = (Button) findViewById(R.id.btnSearchHomeDate);
game = (Button) findViewById(R.id.btnSearchHomeGame);
med = (Button) findViewById(R.id.btnSearchHomeMedValues);
att = (Button) findViewById(R.id.btnSearchHomeAttValues);
score = (Button) findViewById(R.id.btnSearchHomeScore);
// set on click listeners for the buttons
date.setOnClickListener(this);
game.setOnClickListener(this);
med.setOnClickListener(this);
att.setOnClickListener(this);
score.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnSearchHomeDate:
Intent openDateSearch = new Intent(
"com.example.brianapp.SearchDate");
// Start activity
startActivity(openDateSearch);
break;
case R.id.btnSearchHomeGame:
Intent openGameSearch = new Intent(
"com.example.brianapp.SearchGame");
// Start activity
startActivity(openGameSearch);
break;
case R.id.btnSearchHomeMedValues:
// change this to make sure it opens the med screen
Intent openMedSearch = new Intent(
"com.example.brianapp.MeditationSearchHome");
// Start activity
startActivity(openMedSearch);
break;
case R.id.btnSearchHomeAttValues:
// change this to make sure it opens the med screen
Intent openAttSearch = new Intent(
"com.example.brianapp.AttentionSearchHome");
// Start activity
startActivity(openAttSearch);
break;
case R.id.btnSearchHomeScore:
// change this to make sure it opens the med screen
Intent openScoreSearch = new Intent(
"com.example.brianapp.SearchScore");
// Start activit
startActivity(openScoreSearch);
break;
}// switch end
}
}
I can just provide you with the following snippet. Please note that I haven't tested it. I just wanted to show how I would approach this problem.
public class MainMenu extends TabActivity implements GestureDetector.OnGestureListener{
...
#Override
public boolean onFling(MotionEvent e0, MotionEvent e1, float arg2, float arg3) {
...
int current = getCurrentTab(); // TabHost.getCurrentTab()
int next;
if(e0.getRawX() - e1.getRawX() < 0 ){
//fling right
next = current + 1; //TODO check for next = n, n=number of tabs
}
else{
next = current - 1; //TODO check for next = -1
}
setCurrentTab(next); // TabHost.setCurrentTab()
}
}
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 have a Activity named as Wallet and have a Activity named as CRechargeMain which adds two frgament named as "Mobile","Data";what I want in Wallet screen I have a Listview in which in case 0: when I click I want to go to CRechargeMain and show"Mobile" tab and in case 1 : when I click I want to go CRechareMain and open tab " data" .how can I do that
code for wallet:-
m_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
Intent mMobileRecharges = new Intent(CMyWalletScreen.this,CRechargeMain.class);
startActivity(mMobileRecharges);
break;
case 1:
Intent mDataRecharge = new Intent(CMyWalletScreen.this,CRechargeMain.class);
startActivity(mDataRecharge);
break;
case 2:
Intent m_Earning= new Intent(CMyWalletScreen.this,CWalletTransactionScreen.class);
startActivity(m_Earning);
break;
}
}
});
code for CRechargeMain:-
public class CRechargeMain extends AppCompatActivity {
View m_Main;
private ViewPager m_ViewPager;
private Toolbar m_ToolBar;
private String[]actonBar={"Mobile Recharge","Mobile Data Recharge"};
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recharge_main);
init();
}
public void init() {
m_ToolBar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(m_ToolBar);
//noinspection ConstantConditions
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
m_ToolBar.setTitle("Mobile Recharge");
TabLayout m_TabLayout = (TabLayout) findViewById(R.id.tab_layout);// finding Id of tablayout
m_TabLayout.addTab(m_TabLayout.newTab().setText("Mobile"));// add deal listin tab
m_TabLayout.addTab(m_TabLayout.newTab().setText("Data Card"));// add stories tab
m_TabLayout.setTabGravity(TabLayout.GRAVITY_FILL);// setting Gravity of Tab
m_ViewPager = (ViewPager) findViewById(R.id.pager);//finding Id of ViewPager
CRechargePager m_oMobilePager = new CRechargePager
(getSupportFragmentManager(), m_TabLayout.getTabCount());
m_ViewPager.setAdapter(m_oMobilePager);// adiing adapter to ViewPager
m_ViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(m_TabLayout));// performing action of page changing
m_TabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
m_ViewPager.setCurrentItem(tab.getPosition());
m_ToolBar.setTitle(actonBar[tab.getPosition()]);
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_wallet, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
You can use the same intent and select tab based on the intent argument in your activity.
As my assumption you are having two activity, activity1 contains list and activity2 contains fragments.from activity1 based on conditions you have to jump to activity2 (fragments).Just call as below
Intent i =New Intent(this,CRechargeMain.class);
i.putExtra("",0);
startActivity(i);
In Activty2 on create method based on conditions you have to set fragment to load.
FragmentManager FM = getFragmentManager();
FM.beginTransaction().replace(R.id.content_frame, detail).commit();
Intent mMobileRecharges = new Intent(CMyWalletScreen.this,CRechargeMain.class);
intent.putExtra("doWhat", 0);
Intent mDataRecharge = new Intent(CMyWalletScreen.this,CRechargeMain.class);
intent.putExtra("doWhat", 1);
In your receiving activity:
int iDoWhat = intent.getIntExtra("doWhat", -1);
Then make your decision based on the value of iDoWhat.
int iDoWhat = getIntent().getIntExtra("doWhat",-1);
switch (iDoWhat) {
case -1:
//select tab 0
break;
case 0:
//select tab 0
break;
case 1:
//select tab 1
break;
}
You have to write the code for selecting the tab now you know which one.
I have 2 tab while select first tab the accept menu icon should not visible.
while select second tab the accept menu icon should visible. I tried but i am getting null pointer exception.
And also should get the text from the second activity while select accept menu.
How can i do this ?
Here is my Code :
public class DetailTabActivity extends TabActivity implements OnTabChangeListener {
String name,address,rating,reference,lat,lng;
StringConstants constants;
Float rate;
MenuItem menuitem;
String url;
TabHost tabHost;
Menu menu1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.overridePendingTransition(R.anim.anim_out,
R.anim.anim_in);
constants=new StringConstants();
setContentView(R.layout.layout_detail_tab);
Intent intent = getIntent();
name = intent.getStringExtra("name");
Log.e("name",name+"");
address = intent.getStringExtra("address");
Log.e("address",address+"");
rating = intent.getStringExtra("rating");
Log.e("rating",rating+"");
reference = intent.getStringExtra("reference");
Log.e("reference",reference+"");
lat = intent.getStringExtra("lat");
Log.e("lat",lat+"");
lng = intent.getStringExtra("lng");
Log.e("lng",lng+"");
ActionBar actionBar = getActionBar();
// Enabling Up / Back navigation
actionBar.setDisplayHomeAsUpEnabled(true);
tabHost = getTabHost();
tabHost.setOnTabChangedListener(this);
// Tab for Photos
TabSpec detailspec = tabHost.newTabSpec("Detail");
// setting Title and Icon for the Tab
detailspec.setIndicator("Detail");
Intent detailintent = new Intent(this, DetailActivity.class);
detailintent.putExtra("name",name);
detailintent.putExtra("address",address);
detailintent.putExtra("rating",rating);
detailintent.putExtra("reference",reference);
detailspec.setContent(detailintent);
// Tab for Songs
TabSpec reviewspec = tabHost.newTabSpec("Review");
reviewspec.setIndicator("Review/Rating");
Intent songsIntent = new Intent(this, ReviewAndRating.class);
songsIntent.putExtra("name",name);
songsIntent.putExtra("lat",lat);
songsIntent.putExtra("lng",lng);
reviewspec.setContent(songsIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(detailspec); // Adding photos tab
tabHost.addTab(reviewspec); // Adding songs tab
tabHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.parseColor("#ffffff"));
// Set Tab1 as Default tab and change image
tabHost.getTabWidget().setCurrentTab(0);
tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#134960"));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.layout_detail_tab, menu);
menuitem = menu.findItem(R.id.action_accept);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_accept:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onTabChanged(String tabId) {
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
if(i==0)
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#ffffff"));
else if(i==1)
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#ffffff"));
}
Log.i("tabs", "CurrentTab: "+tabHost.getCurrentTab());
if(tabHost.getCurrentTab()==1)
{
menuitem.setVisible(true);
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#134960"));
}
else if(tabHost.getCurrentTab()==0)
{
menuitem.setVisible(false);
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#134960"));
}
}
}
set invalidateOptionsMenu();as,
if(tabHost.getCurrentTab()==1)
{
menuitem.setVisible(true);
invalidateOptionsMenu();
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#134960"));
}
else if(tabHost.getCurrentTab()==0)
{
menuitem.setVisible(false);
invalidateOptionsMenu();
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#134960"));
}
I have a tabhost and I add there 3 Activities(one activity per tab).
I need to know how to call a new intern in an activity each time i change a tab.
I added a listener for the tabhost.When I use the clearAllTabs(); method and add all the tabs again inside the listener then the app crash.
when I use code toto delete from the view the specific tab that the user clickes tabHost.getTabWidget().removeView(tabHost.getTabWidget().getChildTabViewAt(i));
tabHost.addTab(the tab I want to replace);
then the new tab is positioned in the end of the tabhost.
I just need an example of how to reload the proportionate activity each time the user clickes the specific tab.
my code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ActionBar bar = getSupportActionBar();
// requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Resources res = getResources();
LocalActivityManager mlam = new LocalActivityManager(this, false);
final TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
mlam.dispatchCreate(savedInstanceState);
tabHost.setup(mlam);
TabHost.TabSpec spec;
Intent intent;
// TabHost tabHost = getTabHost();
// tabHost.setup();
TabSpec specAll = tabHost.newTabSpec("All");
specAll.setIndicator("All");
Intent allIntent = new Intent(this, allActivity.class);
specAll.setContent(allIntent);
// specAll.setContent(R.id.allList);
Log.d("SpecAll",""+specAll.setContent(allIntent));
TabSpec specIn = tabHost.newTabSpec("in");
specIn.setIndicator("In");
Intent inIntent = new Intent(this, inActivity.class);
specIn.setContent(inIntent);
TabSpec specOut = tabHost.newTabSpec("Out");
specOut.setIndicator("Out");
Intent outIntent = new Intent(this, outActivity.class);
specOut.setContent(outIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(specAll); // Adding all tab
tabHost.addTab(specIn); // Adding in tab
tabHost.addTab(specOut); // Adding out tab
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
int i = tabHost.getCurrentTab();
//Log.i("######## ANN CLICK TAB NUMBER", "------" + i);
if (i == 0) {
Log.d("TAB","" +i);
} else if (i == 1) {
Log.d("TAB","" +i);
}
else
Log.d("TAB", ""+i);
}
});
}
It seems there is a matter with activity and tabhost.in order to reload an activity you just have to do:
specAll.setContent(yourIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
Just before the tabHost.addTab
In my simple case when I do not need to persist tab fragments I use the next code
int currentTabId = mTabHost.getCurrentTab();
mTabHost.clearAllTabs();
setupTabs();
mTabHost.setCurrentTab(currentTabId);
I also face the same problem but i resolve this issue like this way...
This is my TabActivity....
public class MainActivity extends TabActivity {
TabHost tabhost;
String cTab = "0";
String nTab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
tabhost = getTabHost();
TabSpec one = tabhost.newTabSpec("0");
// setting Title and Icon for the Tab
one.setIndicator("", getResources().getDrawable(R.drawable.ic_launcher));
Intent songsIntent = new Intent(this, FirstActivity.class);
one.setContent(songsIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
one.setContent(songsIntent);
TabSpec two = tabhost.newTabSpec("1");
// setting Title and Icon for the Tab
two.setIndicator("", getResources().getDrawable(R.drawable.ic_launcher));
Intent songsIntent1 = new Intent(this, SecondActivity.class);
two.setContent(songsIntent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
two.setContent(songsIntent1);
TabSpec three = tabhost.newTabSpec("2");
// setting Title and Icon for the Tab
three.setIndicator("",
getResources().getDrawable(R.drawable.ic_launcher));
Intent songsIntent4 = new Intent(this, ThirdActivity.class);
three.setContent(songsIntent4.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
three.setContent(songsIntent4);
TabSpec four = tabhost.newTabSpec("3");
// setting Title and Icon for the Tab
four.setIndicator("", getResources()
.getDrawable(R.drawable.ic_launcher));
Intent songsIntent5 = new Intent(this, FourthActivity.class);
four.setContent(songsIntent5.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
four.setContent(songsIntent5);
tabhost.addTab(one);
tabhost.addTab(two);
tabhost.addTab(three);
tabhost.addTab(four);
tabhost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String arg0) {
cTab = "" + tabhost.getCurrentTab();
}
});
int numberOfTabs = tabhost.getTabWidget().getChildCount();
for (int t = 0; t < numberOfTabs; t++) {
tabhost.getTabWidget().getChildAt(t)
.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
String currentSelectedTag = MainActivity.this
.getTabHost().getCurrentTabTag();
nTab = currentSelectedTag;
System.out.println(" nTab " + nTab);
System.out.println(" cTab " + cTab);
if (cTab.equals(nTab)) {
if (nTab.equals("0")) {
Intent intent = new Intent();
intent.setAction("com.ensis.first");
MainActivity.this.sendBroadcast(intent);
}
if (nTab.equals("1")) {
Intent intent = new Intent();
intent.setAction("com.ensis.second");
MainActivity.this.sendBroadcast(intent);
}
if (nTab.equals("2")) {
Intent intent = new Intent();
intent.setAction("com.ensis.third");
MainActivity.this.sendBroadcast(intent);
}
if (nTab.equals("3")) {
Intent intent = new Intent();
intent.setAction("com.ensis.fourth");
MainActivity.this.sendBroadcast(intent);
}
}
}
return false;
}
});
}
}
#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;
}}
This is my FirstActivity.java
public class FirstActivity extends ActivityGroup{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ok();
IntentFilter filter = new IntentFilter("com.ensis.first");
registerReceiver(myReceiver, filter);
/**/
}
private void ok() {
// TODO Auto-generated method stub
setContentView(R.layout.firstscreen);
Button bt=(Button)findViewById(R.id.button1);
bt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent it=new Intent(FirstActivity.this,SubActivity.class);
replaceContentView("activity3", it);
}
});
}
public void replaceContentView(String id, Intent newIntent) {
View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
this.setContentView(view);
}
private BroadcastReceiver myReceiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent) {
Log.v("22222222222222222", "22222222222");
ok();
}
};}
I have three buttons on my main page. Something weird happens when I try to click on them. For example, when I click on the NewGame button, it first displays what the scores button should display, and then if I click the back button it will proceed to display the activity that it was meant to. With the About button, I have to click back twice (it displays the newGame activity and the scores activity. Is there a reason why this is happening?
public class Sakurame extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
//set up click listeners for buttons
View HighScoreButton = findViewById(R.id.highscore_button);
HighScoreButton.setOnClickListener(this);
View newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(this);
View aboutButton = findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.settings:
startActivity(new Intent(this, Prefs.class));
return true;
// More items go here (if any)
}
return false;
}
public void onClick(View v){
switch(v.getId()){
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
case R.id.new_button:
Intent i2 = new Intent(this, HighScore.class);
startActivity(i2);
case R.id.highscore_button:
Intent i3 = new Intent(this, DisplayScores.class);
startActivity(i3);
//break;
// more buttons go here (if any)
}
}
Try adding break; after each startActivity within the onClick method.
Edit to clarify. This ensures that once the case has been met, the switch statement is broken from instead of moving on to the next case statement.
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
case R.id.new_button:
Intent i2 = new Intent(this, HighScore.class);
startActivity(i2);
break;
case R.id.highscore_button:
Intent i3 = new Intent(this, DisplayScores.class);
startActivity(i3);
break;