Add options menu to QR reader application in Android Studio? - java

I'm learning to code apps for android and I'm trying to code an app that reads QR codes, but I want to add an options menu. I have a small code that does it, but when I try to add it the QR reader app the menu does not show. Can you tell me what is wrong with what I have so far?
MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import android.view.View;
import android.app.AlertDialog;
import android.util.Log;
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class MainActivity extends Activity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// Here I added the menu to the qr app
#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) {
switch (item.getItemId()) {
case R.id.item1:
Toast.makeText(getApplicationContext(),"Item 1 Selected",Toast.LENGTH_LONG).show();
return true;
case R.id.item2:
Toast.makeText(getApplicationContext(),"Item 2 Selected",Toast.LENGTH_LONG).show();
return true;
case R.id.item3:
Toast.makeText(getApplicationContext(),"Item 3 Selected",Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
//Here ends the part of the menu
public void onClick(View v){
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
#Override
protected void onPause() {
super.onPause();
mScannerView.stopCamera();
}
#Override
public void handleResult(Result result) {
Log.w("handleResult", result.getText());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Scan result");
builder.setMessage(result.getText());
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
Menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/item1"
android:title="Item 1"/>
<item android:id="#+id/item2"
android:title="Item 2"/>
<item android:id="#+id/item3"
android:title="Item 3"/>
</menu>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<GridLayout
android:layout_width="match_parent"
android:layout_height="114dp">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="3"
android:onClick="onClick"
android:text="Scan QR" />
</GridLayout>
</LinearLayout>
I know that the options menu works by itself, because I tested alone as a different app, and also the qr reader works by itself, but if I try to combine them the menu is not there. I'm a newbie on coding android apps, so I will appreciate your guidance. Thanks

your menu name you supplied is menu.xml but instead you are giving getMenuInflater().inflate(R.menu.main, menu);
you should replace
getMenuInflater().inflate(R.menu.main, menu);
with
getMenuInflater().inflate(R.menu.menu, menu);

Related

Using a toggle button to start and stop a menu item

This is the code inside of my MainActivity.java:
I am trying to use a toggle button to toggle a menu item to be on and off. The menu item when checked sets the background to red. When the item is not selected the background color is default. I am new to java so I am unsure what to put in the if and else statement under onCheckedChange. Any suggestions would help.
Thanks
import android.graphics.Color;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button showFragBut = (Button)findViewById(R.id.button);
showFragBut.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
String butText = showFragBut.getText().toString();
FragmentManager fg = getSupportFragmentManager();
FragmentTransaction ft = fg.beginTransaction();
if (butText.equals("Add fragment")){
Frag1 frag = new Frag1();
ft.add(R.id.dyframe, frag, null);
ft.addToBackStack(null);
ft.commit();
showFragBut.setText("Remove fragment");
} else {
getSupportFragmentManager().popBackStackImmediate();
showFragBut.setText("Add fragment");
}
MenuItem redMenuItem = (MenuItem)findViewById(R.id.menu_red);
}
});
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ToggleButton toggle = (ToggleButton) findViewById(R.id.toggleButton4);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
} else {
}
}
});
}
#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_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.
ConstraintLayout mainLayout = (ConstraintLayout)findViewById(R.id.content1);
int id = item.getItemId();
//noinspection SimplifiableIfStatement
switch (id){
case R.id.action_settings:
if (item.isChecked())
item.setChecked(false);
else
item.setChecked(true);
Toast.makeText(getApplicationContext(), "Setting is clicked", Toast.LENGTH_LONG).show();
return true;
case R.id.menu_red:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
mainLayout.setBackgroundColor(Color.RED);
return true;
case R.id.menu_green:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
mainLayout.setBackgroundColor(Color.GREEN);
return true;
}
return super.onOptionsItemSelected(item);
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/content1"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="#layout/activity_main">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="128dp"
android:layout_marginTop="116dp"
android:text="#string/button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Add Fragment" />
<FrameLayout
android:id="#+id/dyframe"
android:layout_width="185dp"
android:layout_height="63dp"
android:layout_marginStart="128dp"
android:layout_marginTop="44dp"
android:textAlignment="center"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button"/>
<ToggleButton
android:id="#+id/toggleButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="148dp"
android:layout_marginBottom="110dp"
android:text="#string/togglebutton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.brettbanich.menuactivity.MainActivity">
<group android:checkableBehavior="single" >
<item
android:id="#+id/menu_red"
android:title="#string/red"
app:showAsAction="never" />
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
</group>
</menu>
I think that this should work:
public void setColor(int color, boolean checked) {
ConstraintLayout mainLayout = (ConstraintLayout)findViewById(R.id.content1);
mainLayout.setBackgroundColor(color);
redMenuItem.setChecked(checked);
}
and for the toggle:
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
setColor(Color.GREEN, true);
} else {
setColor(Color.RED, false);
}
}
});
I'm not sure if the colors are swapped or not, the idea is something like this. I assume that the code in setColor() that you used in the onOptionsImetSelected() works as you expect.

Handling of pushing menu item. Android

I have the following code:
MainActivity.java:
package asus.example.com.fitnessapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.nav_items,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.home:
textView.setText("Home");
return true;
case R.id.notification:
textView.setText("Notification");
return true;
case R.id.profile:
textView.setText("Profile");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="52dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/nav_items"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"/>
</android.support.constraint.ConstraintLayout>
nav_items.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/home"
android:title="#string/home"
android:icon="#drawable/home" />
<item
android:id="#+id/notification"
android:title="#string/notifications"
android:icon="#drawable/notification" />
<item
android:id="#+id/profile"
android:icon="#drawable/person"
android:title="#string/profile" />
</menu>
When user clicks on any element of the bottom navigation bar the program should add to the textview the name of this element of menu. That's why there's the following lines in code, such as textView.setText("Home"); and other. But when user clicks on any elements, there's nothing shows int textview. What's the matter?
You are using a BottomNavigationView not a menu. You need to implement the BottomNavigationView.OnNavigationItemSelectedListener like this:
public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener{
and then you need to set the listener in the code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
BottomNavigationView bottomNav = findViewById(R.id.bottomNav);
bottomNav.setNavigationItemSelectedListener(this);
}
For this to work you need to add android:id="#+id/bottomNav" to your layout file:
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottomNav"
android:layout_width="match_parent"
android:layout_height="52dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/nav_items"/>
And lastly the method to handle the OnNavigationItemSelectedListener event:
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
switch (item.getItemId()){
case R.id.home:
textView.setText("Home");
return true;
case R.id.notification:
textView.setText("Notification");
return true;
case R.id.profile:
textView.setText("Profile");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You will not need onOptionsItemSelected() or onCreateOptionsMenu() if you just want to use the BottomNavigationView.

Image not getting displayed in emulator

I am implementing a simple page similar to any other app, a 'header image' and content below.
I've placed a wedhead.png file manually into all the drawable folders that I want to display.
The layout I wish to implement displays correctly on the graphical editor, but not on the emulator. The emulator comes out blank. Emulator is ARM CPU based.
Emulator screen -
Here's my code
XML File:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.wwmk1.MainActivity" >
<ImageView
android:id="#+id/img1"
android:layout_width="match_parent"
android:layout_height="160dp"
android:layout_marginBottom="19dp"
android:contentDescription="header"
android:fadingEdge="vertical"
android:src="#drawable/wedhead"
android:visibility="visible" />
</RelativeLayout>
The Main activity java file
package com.example.wwmk1;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView iv = (ImageView)findViewById(R.id.img1);
}
#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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
What place did I go wrong?
Please help. Thanks

the codes are not working in the second xml file

i am just working on a test app.
this is the xml file of the home page.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivityTEST2" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/button12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="72dp"
android:text="Button12" />
</RelativeLayout>
i have added a button listener to go to other page. here is my code for that.
package com.example.test2;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivityTEST2 extends Activity {
Button but;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity_test2);
but=(Button)findViewById(R.id.button12);
but.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
setContentView(R.layout.activity123);
}
});
}
#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_activity_test2, menu);
return true;
}
when i add a button listener in the second page to display text in the textfield, nothing works.
xml file of the second page
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<EditText
android:id="#+id/edittext1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
when i add a button listerner to display text in the textfiled, nothing works.
the app neighter stops working nor does anything after that.
my second window's java content is
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.example.test2.R;
public class activity_main_activty_test2 extends Activity {
Button but2;
EditText edit1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity123);
edit1=(EditText)findViewById(R.id.edittext1);
but2=(Button)findViewById(R.id.button2);
but2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
edit1.setText("hellow");
}
});
}
#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_activity_test2, menu);
return true;
}
}
am i missing something. do i have to address something. please help i am just a biggener.
i have added a button listener to go to other page No, apparently you haven't.
You cannot go to the other activity through this code
setContentView(R.layout.activity123);
You need to use Intent
#Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivityTEST2.this,activity_main_activty_test2.class);
startActivity(intent);
finish(); //optional
}
ALSO remove import com.example.test2.R; from your second activity.
if you want to go to the next view, you dont use setContentView(R.layout.activity123); in your button listener it will not transfer you to the next activity, instead make use of intent since you have a 2nd activity.
Intent intent = new Intent(getBaseContext(), activity_main_activty_test2.class);
startActivity(intent)
in your MainActivityTEST2
but.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(getBaseContext(), activity_main_activty_test2.class);
startActivity(intent)
}
});
Please Change your first page as following. Android use Intent to jump the activity from one to another.
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivityTEST2 extends Activity {
Button but;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity_test2);
but = (Button) findViewById(R.id.button12);
but.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setClass(this, activity_main_activty_test2.class);
startActivity(intent);
finish();
}
});
}
#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_activity_test2, menu);
return true;
}
}

App crashes when changing intent

I'm new to android and the problem feels simple, but I can't place it. I tried to look in logcat, but I can't follow.
Here is the main activity which loads just fine.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Intent intent;
Button newsButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
public void sendMessage(View view) {
Intent intent = new Intent(this, NewsActivity.class);
startActivity(intent);
}
}
Here is the corresponding xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:orientation="vertical" >
<Button
android:id="#+id/newsButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="104dp"
android:onClick="#layout/news_activity"
android:text="News" />
<Button
android:id="#+id/contactButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Contact Us"
android:onClick="#layout/contact_activity" />
<Button
android:id="#+id/findUs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Social Media"
android:onClick="#layout/social_activity" />
<Button
android:id="#+id/fundButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fundraising" />
</LinearLayout>
<TextView
android:id="#+id/titleName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:text="Send the Scots to Scotland"
android:textSize="24sp" />
</RelativeLayout>
The crash happens on the API 14 device I debug with. After clicking any one of my buttons to change to a new activity that looks like this :(which are all essentially the same as the one below)
package com.gconcode.scotstoscotland;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
public class NewsActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_activity);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.news, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up- vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
Sorry I removed the logcat, I wasn't sure if I posted it right. Didn't want too cause the post to be inconvenient thanks for the help!
I think you misunderstand android:onClick. It looks like you are expecting it to load a certain layout file when you click the Button but that is not how it works. The name you give android:onClick should be the name of a function which doesn't return anything and accepts a View as its one and only parameter. It looks like you pulled that from the example in the docs with
public void sendMessage(View view) {
Intent intent = new Intent(this, NewsActivity.class);
startActivity(intent);
}
There are different ways you could do this. Since you already have the android:onClick and you have a method in your Activity, you could change that to look like
android:onClick="sendMessage"
then add a switch statement to that method to open the correct Activity. So something like
public void sendMessage(View view)
{
switch (view.getId()) // get the id of the View clicked
{
case (R.id.newsButton):
Intent intent = new Intent(this, NewsActivity.class);
startActivity(intent);
break;
case (R.id.contactButton):
// assuming ContactActivity.java is an Activity name
Intent intent = new Intent(view.getContext(), ContactActivity.class);
startActivity(intent);
break;
...
}
}
This answer shows a little different way to do the Intent so you cut down on some of the reusable code. If it looks too complicated then don't worry about it until you have a better understanding but just an idea.

Categories

Resources