I'd like to make a menu with a delete option. The actual delete functionality isn't made yet because at the moment I can't see the top bar in my app.
Main layout (activity_main.xml):
<?xml version="1.0" encoding="utf-8"?>
<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="billy.cs436.placebadgesapp.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/newPlace"
android:text="#string/newPlaceButton"
/>
</RelativeLayout>
Menu layout (main.xml):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/deleteMenu"
android:icon="#drawable/clear"
android:title="#string/deleteMenu"
app:showAsAction="ifRoom"
/>
</menu>
Main activity (MainActivity.java):
package billy.cs436.placebadgesapp;
import android.content.Intent;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity {
Button newPlace;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
newPlace = (Button) findViewById(R.id.newPlace);
newPlace.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), setLocation.class);
startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the main; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);//Menu Resource, Menu
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id == R.id.deleteMenu) {
// if there are no badges, toast message saying so (needs implementing)
Toast.makeText(this, "There are no badges to delete!", Toast.LENGTH_SHORT).show();
//else clear all badges
} else {
Toast.makeText(this, "Badges cleared!", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
}
Can anyone tell my why the New Place button is the only thing that shows up when this is run?
EDIT:
#T.S has the correct answer. Thank you.
Change your class to extend AppCompatActivity instead of Activity.
Related
I'm building an app where I need an information activity with another Android Actionbar to popup when the info ActionBar Item is clicked on.
Here is the screenshot of the activity_main_menu.xml:
activity_main_menu.xml code:
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">
<item
android:id="#+id/menu_item_info"
android:icon="#drawable/info_outline"
android:title="#string/information"
android:titleCondensed="#string/info"
app:showAsAction="ifRoom" />
<item
android:id="#+id/menu_item_share"
android:title="#string/share"
app:showAsAction="ifRoom"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider" />
</menu>
When the "menu_item_info" is clicked, here is what I want to happen:
Desired Result
activity_main.xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:background="#color/colorPrimary"
android:orientation="horizontal"
tools:context=".MainActivity">
<!-- <clip xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clipOrientation="vertical"
android:drawable="#drawable/bitcoin"
android:gravity="top" /> -->
<TextView
android:id="#+id/tvTotal"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:padding="10dp"
android:text="Text"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:textStyle="bold"
app:fontFamily="sans-serif"
app:lineHeight="60dp" />
<ImageView
android:id="#+id/ivBitcoin"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:contentDescription="You own text!"
android:padding="10dp"
app:srcCompat="#drawable/bitcoin" />
</LinearLayout>
MainActivity.java code:
package com.shikhar_mainalee.iownallbitcoin;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.ShareActionProvider;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONObject;
import java.util.Collection;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
private TextView tvTotal;
private ImageView ivBitcoin;
private ShareActionProvider mShareActionProvider;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvTotal = findViewById(R.id.tvTotal);
ivBitcoin = findViewById(R.id.ivBitcoin);
Main.ivBitcoin = ivBitcoin;
tvTotal.setText("You Own\n21,000,000 / 21,000,000\nBTC!");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate menu resource file.
getMenuInflater().inflate(R.menu.activity_main_menu, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.menu_item_share);
// Fetch and store ShareActionProvider
ShareActionProvider shareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
shareActionProvider.setShareIntent(createShareIntent());
new ShareActionProvider(this).setShareIntent(null);
// Return true to display menu
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_info:
// What code should go here?
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
}
What is the best way to go about this? Any help is appreciated! Thank you!
If you want to go to another activity here is what you should do:
Create another activity
Create an Intent to go to that activity
Intent myIntent = new Intent(this, SecondActivity.class);
you can call that Intent from anywhere in your main activity. In your case:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_info:
startActivity(myIntent); // switch to second activity
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
to change the second activity action bar menu you should create a new Menu and like your main activity Inflate it in onCreateOptionsMenu
getMenuInflater().inflate(R.menu.activity_second_menu, menu);
try this:
#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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
this is a example of a template from android studio.
It's apparently a question that has been asked multiple times, but only has solutions on a case-by-case basis.I am making an aap with toolbar and when i go to second activity i want the toolbar to display icon as well as settings menu but unfortunately toolbar is appearing but icons are not showing also android monitor shows null pointer exception on these lines of code
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
Error pic
This is my activity_main
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.zaina.toolbar.MainActivity">
<include android:id="#+id/aap_bar" layout="#layout/aap_bar"></include>
<TextView
android:layout_below="#id/aap_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
This is my MainActivity.java
package com.example.zaina.toolbar;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.aap_bar);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu item) {
getMenuInflater().inflate(R.menu.menu_main, item);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem menuItem){
int id=menuItem.getItemId();
if(R.id.action_favorite==id){
startActivity(new Intent(this,aap.class));
return true;
}
if(R.id.action_settings==id){
Toast.makeText(MainActivity.this, "Hey you hit", Toast.LENGTH_SHORT).show();
return true;
}
return onOptionsItemSelected(menuItem);
}
}
This is my aapbar_xml.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:aap="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/colorPrimary"
aap:Theme="#style/MyCustom"
app:popupTheme="#style/ThemeOverlay.AppCompat.Dark">
</android.support.v7.widget.Toolbar>
This is my activity_aap.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.zaina.toolbar.aap">
<include android:id="#+id/app_bar" layout="#layout/aap_bar"></include>
</RelativeLayout>
This is aap.java
package com.example.zaina.toolbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
public class aap extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aap);
Toolbar toolbar= (Toolbar) findViewById(R.id.aap_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu item) {
getMenuInflater().inflate(R.menu.menu_sub, item);
return true;
}
}
This is my menu_sub.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aap="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/za"
android:icon="#drawable/ic_chevron_left_black_24dp"
android:title="left"
aap:showAsAction="ifRoom"/>
<item android:id="#+id/zee"
android:title="sett"
aap:showAsAction="never"/>
</menu>
There may be few reasons of NullPointException, try to:
You are probably using a Theme that doesn't support ActionBar.
Trying using this theme:
android:theme="#android:style/Theme.Holo.Light".
try to catch:
if (getSupportActionBar() != null)
{
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
Or, try to change the id of app_bar in aap.java
findViewById method returning null.
Add id attribute to your toolbar
android:id="#+id/aap_bar
You are also using wrong namespace aap
Use app
xmlns:app="http://schemas.android.com/apk/res-auto"
I've just done a fresh re-install/extract of Eclipse off Android's website, hardly added much code or XML to my project, yet nothing's being recognized outside of my MainActivity.java. And before you ask-yes, I've tried cleaning and restarting Eclipse.
(see comments for where the errors are) For example, in the first method (onCreate), the second like there's activity_main that Eclipse is saying it can't resolve, as if it doesn't exist. Consequently, my CreateLG button in the onCreateLGClick method isn't resolved/recognized either. Neither is the main (.xml) or createlg_menu (.xml) resolved..
MainActivity.java:
package com.example.groceryrunner;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.R;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // not resolved or isn't a field
}
#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); // main isn't recognized
return true;
}
public void onCreateLGClick(View v) {
final int id = v.getId();
switch (id) {
case R.id.CreateLG: // CreateLG button from activity_main isn't recognized either
//findViewById(R.id.GetStarted).setVisibility(View.INVISIBLE);
createLGPopup(v);
break;
/*case R.id.ListsButton:
findViewById(R.id.GetStarted).setVisibility(View.INVISIBLE);
createLGMenu(v);
break;*/
}
}
public void createLGPopup(View v) {
PopupMenu LGMenu = new PopupMenu(this, v); // createlg_menu isn't recognized as well
LGMenu.getMenuInflater().inflate(R.menu.createlg_menu, LGMenu.getMenu());
LGMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
String choice = new String((String) item.getTitle());
if (choice == "Create_List") {
//createListDialog();
}
else if (choice == "Create_Group") {
//createListDialog();
}
return false;
}
});
LGMenu.show();
}
}
Here's the other files:
activity_main.xml:
<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=".MainActivity" >
<Button
android:id="#+id/CreateLG"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="37dp"
android:layout_marginTop="21dp"
android:text="+"
android:textSize="40sp" />
</RelativeLayout>
createlg_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/createList" android:title="Create List"></item>
<item android:id="#+id/createGroup" android:title="Create Group"></item>
</menu>
main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
</menu>
When i encounter this issue, the first step is to remove the import android.R line. That is the wrong R.
Next, comment out any line referencing R and build the project. That will make new gen folder with the R values.
Most likely your issue is importing android.R so the app looks in there instead of in your R gen file. But Eclipse gets silly sometimes and won't build without commenting those out, so try both steps to be sure.
I am getting this error. It says "edit_message can not be ressolved or is not a field" Can somebody tell me why I am getting this error is how can I fix it.
LAYOUT OF MAIN ACTIVITY: -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal">
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
</LinearLayout>
CODE OF MAIN ACTIVITY
package com.practice.firstapp1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE= "com.practice.fristapp1.MESSAGE";
#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;
}
/** Called when the user clicks the Send button */
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message); ///********ERRORR***********
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
LAYOUT OF SECOND ACTIVITY: -
<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=".DisplayMessageActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
CODE OF SECOND ACTIVITY: -
package com.practice.firstapp1;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
public class DisplayMessageActivity extends Activity {
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
STRING RESOURCES:-
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My First App</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
<string name="action_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string name="title_activity_display_message">My Message</string>
<string name="hello_world">Hello world!</string>
</resources>
It looks like your code is right. Check the following things:
1) Save the layouts in xml, one reason could be its not saved.
2) Check there is no error in your xmls and project is build.
3) If you are using eclipse, go to Projects->Clean All Projects and then projects->Build Automatically.
4) Open your activity and press "cmd + shift + o" or ctrl+ shift+o (in windows). It shall import the R.java, select the com..R in the options.
I hope it shall solve the error.
You're missing import com.practice.firstapp1.R in MainActivity.java.
See the docs on Accessing Resources for more information.
I have 2 layouts in named first_activity and second_activity...
the first_activity has a single button button1 on which i have applied onclick listener to make a simple toast and i have used this first_activity in the FirstActivity...
the second_activity has a single button along with that i have included the first_activity in the second_activity so the second_activity has 2 buttons...
now have coded and run the application using FirstActivity has the main activity and the i am getting the toast onclick of 1st button...
Now the SecondActivity extends FirstActivity along its own button 2 click event...
now i have changed the start activity as SecondActivity and after running i am getting the click event of only 2nd button not of 1st button..
so where i am making mistake..
my intention is to make code reuse for example i have an app in which there are 30 layout and all the layout have common menu which is included in each layout so i just want to code for that menu once and reuse that code in all the other layout...
here are the code of my app....
first_activity:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="200dip"
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=".FirstActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1st Button" />
second_activity:
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=".FirstActivity" >
android:id="#+id/layout"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/layout"
android:text="2nd Button" />
FirstActivity:
package com.example.codereuse;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class FirstActivity extends Activity {
Button b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_activity);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Button 1 Clicked", Toast.LENGTH_LONG).show();
}
});
}
#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;
}
}
SecondActivity:
package com.example.codereuse;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class SecondActivity extends FirstActivity {
Button b2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
b2 = (Button) findViewById(R.id.button2);
b2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Button 2 Clicked", Toast.LENGTH_LONG).show();
}
});
}
#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;
}
}
AndroidMenifest:
package="com.example.codereuse"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="8"
android:targetSdkVersion="17" />
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
android:name="com.example.codereuse.SecondActivity"
android:label="#string/app_name" >
android:name="com.example.codereuse.FirstActivity"
android:label="#string/app_name" >
Readers Reply very soon...
If you have 30 layouts which have the same content, then you can use one xml file for all those.
If you are using an id from one activity, there is no problem using the same id for the element in different activity.