I have a menu upside my page and it has an action item. like this:
action item
I set app:showAsAction="always" for it.
I have invisible view (in UI) in my page too.
I want to if I click on it, my invisible view appears. so I set a boolean parameter for that.
My boolean set true but my object doesn't appear at all. How can I fix it?
here is my code:
public static boolean myBoolean=false;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this if doesn't check after change parameter
if(myBoolean)
findViewById(R.id.checkBox).setVisibility(View.VISIBLE);
}
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_setting: {
myBoolean=true;
return true;
}}}
What I am getting from this is you want to show a view based on the click of some menu item in action bar right?. if this is true then do like this.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_setting:
if(myBoolean){
findViewById(R.id.checkBox).setVisibility(View.GONE);
myBoolean = false;
}else{
findViewById(R.id.checkBox).setVisibility(View.VISIBLE);
myBoolean = true;
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
you can't decide the visibility earlier in your code since click may happen sometime after onCreate() only.
I'm building an app with a navigation drawer, and an action bar with a button which pressed should take users to a log in page. The log in page is an activity.
I have inserted what I believe to be the appropriate code, however the section in bold comes back with the following error: Annotations are not allowed here
This code previously worked, and now it has this error and my app obviously wont launch. Does anyone know why? Thanks!
#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_LoginPage) {
Toast.makeText(getApplicationContext(), "Welcome to the Employee Log-in Page", Toast.LENGTH_LONG).show();
switch (item.getItemId()){
case R.id.action_LoginPage:
Intent intent = new Intent(this, LoginPageMain.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
return super.onOptionsItemSelected(item);
}
**#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Code for Nav Bar Item Clicks (Button Clicks)
int id = item.getItemId();**
I have two buttons on my action bar, a logout button, and a create new message button. However, if i click the create new message button (where nothing is supposed to happen yet), it logs me back out. My code is set as what it is supposed to do. Any suggestions?
#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();
switch(id) {
case R.id.action_newPool:
Toast.makeText(getApplicationContext(),"Being Implemented", Toast.LENGTH_LONG).show();
case R.id.action_logOut:
ParseUser.logOut();
Intent leaveIntent = new Intent (MyPoolsActivity.this, DispatchActivity.class);
leaveIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(leaveIntent);
}
You need a break keyword after your first case, otherwise it will execute the next case. This allows it to "break" out of the switch block.
switch(id) {
case R.id.action_newPool:
Toast.makeText(getApplicationContext(),"Being Implemented", Toast.LENGTH_LONG).show();
break;
case R.id.action_logOut:
ParseUser.logOut();
Intent leaveIntent = new Intent (MyPoolsActivity.this, DispatchActivity.class);
leaveIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(leaveIntent);
break;
}
I have an application which can be localized to many languages. But I have one big trouble. I have default file values/strings.xml where I store string in English by default and I have folder values-uk/strings.xml (for Ukrainian localization). I am saving the selected localization in SharedPreferences. The trouble is when the application starts without any selected language preference (but the system language of device is Ukrainian, I checked) my app must load Ukrainian strings from values-uk/strings.xml, but it loads English values from values/strings.xml. Can anyone explain me why this happens and how can I solve this problem. Thank you very much!
Also always check your build.gradle (app) android/defaultConfig, if there set resConfigs("en", "de", "ru"...). If yes only listed resources will leave in the project.
I had the same problem. Then I followed many tutorials. and finally I have used localization in my android application for all Indian Language. Have look at my code snippet:
#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
//SharedPreferences.Editor edit=getSharedPreferences("pref",MODE_PRIVATE).edit();
switch (id){
case R.id.action_assamese:
setLocale("as");
return true;
case R.id.action_bihari:
setLocale("bh");
return true;
case R.id.action_english:
setLocale("en");
return true;
case R.id.action_gujarati:
setLocale("gu");
return true;
case R.id.action_hindi:
setLocale("hi");
return true;
case R.id.action_kannada:
setLocale("kn");
return true;
case R.id.action_kashmiri:
setLocale("ks");
return true;
case R.id.action_malayalam:
setLocale("ml");
return true;
case R.id.action_marathi:
setLocale("mr");
return true;
case R.id.action_oriya:
setLocale("or");
return true;
case R.id.action_sanskrit:
setLocale("sa");
return true;
case R.id.action_tamil:
setLocale("ta");
return true;
case R.id.action_telgu:
setLocale("te");
return true;
case R.id.action_urdu:
setLocale("ur");
return true;
}
return super.onOptionsItemSelected(item);
}
public void setLocale(String lang) {
Locale myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
Intent refresh = new Intent(this, MainActivity.class);
startActivity(refresh);
finish();
}
For more details you can follow this tutorial.
I hope it may help you.
I want to customize the activity back button in action bar, not in hard key back button. I have overriden the onBackPressed() method. It works with my emulator back button, but not with action bar back button.
I want it to happen with action bar. How can I do this?
Here is my code:
#Override
public void onBackPressed() {
Toast.makeText(getApplicationContext(),"Back button clicked", Toast.LENGTH_SHORT).show();
return;
}
I have used this toast whether back pressed is working or not but the actual implementation changes like to move back to previous activity. But this is not working with the button present on top of action bar (besides title of the activity).
Please any one could specify me the problem.
I think you want to override the click operation of home button. You can override this functionality like this in your activity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Toast.makeText(getApplicationContext(),"Back button clicked", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
If you want ActionBar back button behave same way as hardware back button:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onBackPressed();
return true;
}
return false;
}
Two things to keep in mind that the user can either press back button or press the actionbar home button.
So, if you want to redirect him to the same destination then you can do this.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return false;
}
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
startActivity(intent);
finish();
}
This will take the user to the intent pressing either key or the action bar button.
Sorry mine is a late answer, but for anyone else arriving at this page with the same question, I had tried the above:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
...
if (item.getItemId() == android.R.id.home) {
....
}
....
}
but this failed to catch the "Back" button press.
Eventually I found a method that worked for me on https://stackoverflow.com/a/37185334/3697478 which is to override the "onSupportNavigateUp()" as I am using the actionbar from the "AppCompatActivity" support library. (There is an equivalent "onNavigateUp()" for the newer actionbar/toolbar library.)
#Override
public boolean onSupportNavigateUp(){
finish();
return true;
}
and I removed the "android:parentActivityName=".MainActivity" section from the manifest file.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
}
return true;
}
(1) Add Parent activity for your child activity (AndroidManifest.xml)
<activity
android:name=".ParentActivity" />
(2) override the onSupportNavigateUp method inside the child activity
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return false;
}
I have achieved this, by using simply two steps,
Step 1: Go to AndroidManifest.xml and in the add the parameter in tag - android:parentActivityName=".home.HomeActivity"
example :
<activity
android:name=".home.ActivityDetail"
android:parentActivityName=".home.HomeActivity"
android:screenOrientation="portrait" />
Step 2: in ActivityDetail add your action for previous page/activity
example :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
If you want to return to the previous instance of an Activity by pressing of ActionBar home button, without recreating it, you can override getParentActivityIntent method to use the one from the back stack:
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
#Override
public Intent getParentActivityIntent() {
return super.getParentActivityIntent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
EDIT:
Also you can achieve the same result by
setting the launchMode of your parent activity to singleTop.
So setandroid:launchMode="singleTop" to parent activity in your manifest.
Or you can use flag FLAG_ACTIVITY_CLEAR_TOP with the UP intent.
reference: Providing Up Navigation
#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 == android.R.id.home) {
onBackPressed();
return true;
}
//noinspection SimplifiableIfStatement
if (id == R.id.signIn) {
return true;
}
return super.onOptionsItemSelected(item);
}
///////////////////
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
There are several ways how to set up back button in bar:
1) method .setDisplayHomeAsUpEnabled(true); will do it, and then you can simply override android.R.id.home
2) adding <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="my.package.parrent" /> in Android Manifest, but in this case you can not override android.R.id.home in OnOptionsMenuSelected.
.. for those who wonder why it doesn't work for them...