android - best way to shared the action bar - android

I aim to shared the action bar in all activities. So I create a based class call "MyActionBarActivity" as follows"
public class MyActionBarActivity extends ActionBarActivity {
public final static int TL = Toast.LENGTH_LONG;
#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 super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
Toast.makeText(getApplicationContext(), "I am settings", TL).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
and other activities inherit from this class to share the action bar.
public class Activity1 extends MyActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
}}
public class Activity2 extends MyActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
}}
Is this a good way to implement the action bar?

In my opinion this is a fine way to apply the common action bars in more than one activity.
But inflating in separate activities also won't make much difference.
You can use either ways.

Related

How to dismiss overflow menu when home button is pressed android?

I am displaying overflow menu list when we click the three dots. When I press home button and again when I launch the app, overflow menu list is still displaying. How to dismiss overflow menu list window?
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.v("RAMKUMARV ONCREATEOPTION", "RAMKUMARV");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
// Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.action_settings);
item.setVisible(true);
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);
}
}
You can have an activity field that stores the options/overflow menu whenever the onCreateOptionsMenu() gets triggered, and then use close() method to dismiss the menu when the home button is clicked i.e. in onUserLeaveHint().
public class MainActivity extends AppCompatActivity {
// Field to store the overflow menu
private Menu mOverflowMenu;
// omitted rest of your code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
mOverflowMenu = menu;
// omitted rest of your code
return true;
}
// Dismissing the overflow menu on home button click
#Override
protected void onUserLeaveHint() {
super.onUserLeaveHint();
mOverflowMenu.close();
}
}
Declare the menu item like at MainActivity level, like this:
public class MainActivity extends AppCompatActivity {
private MenuItem overflowItem;
...
}
Now you should instanciate the object inside the onCreateOptionsMenu method, like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
...
overflowItem = menu.findItem(R.id.action_settings);
overflowItem.setVisible(true);
return true;
}
Then you can use the onPause method to set the overflowItem not visible anymore:
#Override
public void onPause() {
super.onPause();
overflowItem.setVisible(false);
}
Finally remember to replace your MenuItem item object with overflowItem where it is used.
To dismiss the overflow menu you can simply call closeOptionsMenu() inside Activity.
closeOptionsMenu()
Progammatically closes the options menu. If the options menu is
already closed, this method does nothing.
You can call it in onPause() method like below:
#Override
public void onPause() {
super.onPause();
closeOptionsMenu();
}
or in case you want to call it only when the user presses the Home key you can use onUserLeaveHint() like below:
#Override
protected void onUserLeaveHint() {
super.onUserLeaveHint();
closeOptionsMenu();
}

Open screen from Button in Android

I am very new to Java and android apps.
I am trying to open a screen when the user clicks on a button. The button is called "Company"
I have a MainActivity.java and a Company.java
MainActivity looks like:
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// If your minSdkVersion is 11 or higher, instead use:
// getActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
/** Called when the user clicks the Company button */
public void onClick(View view) {
// Do something in response to button
Intent intent = new Intent(this, TheCompany.class);
startActivity(intent);
}
}
But nothing happens when the user clicks on the button Company.
I have a Company Java file and a Company XML file, but they don't get called. I suspect it is the way they are being called from here.
Would really appreciate some help. Also let me know if I need to post other bits of code, like the activity_main.xml.
Thanks!
If you want to open a new activity with his own java.class by a button, just replace this:
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button)findViewById(R.id.Company);
btn.setOnClickListner(new View.OnClickListner(){
#Override
public void onClick(View v){
Intent activityChangeIntent = new Intent(MainActivity.this,TheCompany.class)
MainActivity.this.starActivity(activityChangeIntent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
the onclick() method is never called!
You have forgotten to set android:onClick in you layout xml file or to set the OnClickListener programmatically in code (see below):
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View button = findViewById(R.id.myButtonId);
button.setOnClickListener(new OnClickListener(){
public void onClick (View v){
startIntent();
}
});
//getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// If your minSdkVersion is 11 or higher, instead use:
// getActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
/** Called when the user clicks the Company button */
private void startIntent() {
// Do something in response to button
Intent intent = new Intent(this, TheCompany.class);
startActivity(intent);
}
}
You seem to say you have Company.java, but in your intent you are trying to access TheCompany.class. The file name needs to match the public class in your Company.java. So you either need to have a Company.java file with a Company class or TheCompany.java file with TheCompany class.
I suspect this is setup correctly, otherwise if you've added android:onClick in your XML file you'd be getting errors.
It's worth posting the XML for the button, sockeqwe is most likely right.

how to add onlclick listener android

Ok so I have multiple buttons on the home page of my android application.
I have seem to have got confused along the way as I have coded the app like this.
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 aboutus (final View view) {
setContentView(R.layout.aboutus);
System.out.println("about us clicked");
}
And I have put the on click in the xml.
However I have things that I need to write code for in the "about us" page.
In my about us class I have the following
public class AboutUs extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aboutus);
}
#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 onBackPressed() {
setContentView(R.layout.activity_main);
}
}
Here is the xml code for the button that is being clicked
<ImageButton
android:id="#+id/ImageButton01"
android:layout_width="134dp"
android:layout_height="97dp"
android:layout_x="16dp"
android:layout_y="150dp"
android:onClick="aboutus"
android:src="#drawable/aboutus" />
Any code I am writing isn't working on that page? I don't know why? can any one help?
Thank you.
Change your MainActivity like this:
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 aboutus(final View view)
{
startActivity(new Intent(MainActivity.this, AboutUs.class));
}
and AboutUs.java is like:
public class AboutUs extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aboutus);
}
#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;
}
/* No need of this method */
public void onBackPressed() {
setContentView(R.layout.activity_main);
}
}
NOTE:
android:onClick is for API level 4 onwards, so if you're targeting < 1.6, then you can't use it.
You can't just call setContentView in your aboutus method, you need to create an intent that starts a new activity. Calling setContentView just sets the view in the current main activity, and doesn't instantiate your AboutUs activity class. Your aboutus method should look similar to:
public void aboutus (final View view)
{
Intent intent = new Intent(this, AboutUs.class);
startActivity(intent);
}
I think you are using setContentView() to call another Activity.
You must use Intents
Intent in = new Intent(this,AboutUs.class);
startActivity(in);

finish activity from an other one

I have an activity for my menu events:
public class GlobalMenu extends Activity{
private MenuItem item;
public boolean event(MenuItem item){
this.item = item;
// Handle item selection
switch (this.item.getItemId()) {
case R.id.menu_stop:
finish();
return true;
}
return true;
}
}
And I use it like this
GlobalMenu gm = new GlobalMenu();
#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, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return gm.event(item);
}
But the finish didn't work, I think I need to link it with an application but I don't know how to do
Thanks
First of All, You can't make a Object or Instance of Android Activity.
like this
GlobalMenu gm = new GlobalMenu();
You have to pass a Context of GlobalMenu Activity to other Activity or Class and then call finish on this.
Like,
((GlobalMenu)mContext).finish();
Here mContext is a reference of GlobalMenu Activity.

Hiding some action items in ActionBarSherlock in some activities

I am not getting how to show only selected action items on some activities. Below is my code:
public abstract class BaseActivity extends SherlockActivity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Login")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
menu.add("Save")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case 0:
Intent loginIntent = new Intent(BaseActivity.this,LoginForm.class);
startActivity(loginIntent);
return true;
case 1:
Intent saveIntent = new Intent(BaseActivity.this,SaveForm.class);
startActivity(saveIntent);
return true;
}
return false;
}
}
In the above code I am setting action items so that I can reuse this action items in some activities without repeating the code. Whenever I want these action items to be displayed on particular activity, I am extending this BaseActivity on the present activity as below.
public class FirstActivity extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
....
....
}
So, whenever I need these action items on the particular activities, I am extending the baseactivity to the activities on which I need. Now my problem is how to set only selected action items on particular activity. Suppose I don't want to show up "login" action Item on one activity. How can I get this done? Code snippets would be appreciated.
Now it's a reasonable approach but I would only have items that were reused for all in the baseactivity. That said I think the correct way of handling this would be to override the methods with the implementation you want in these other activities.
You could have some variables that is set in onCreate(), if these are true/false handle it accordingly in onCreateOptionsMenu()
Edit
To override in your own activity you simply do the same thing as in the baseactivity but without calling initializing those buttons you don't need.
If you instead want to make toggle if buttons should be there or not simply add the variables to your baseactivity:
protected boolean mIsLoginButton = true;
Change the onCreateOptionsMenu() method in baseactivity to something like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if(mIsLoginButton) {
menu.add("Login")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
menu.add("Save")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true;
}
Then in your firstActivity you simply do this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mIsLoginButton = false;
}

Categories

Resources