Making the logo - android

This is my Code for the action bar i use.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.icon:
case R.id.Kur:
Intent Doviz = new Intent(MainActivity.this, MainActivity.class);
startActivity(Doviz);
finish();
return true;
case R.id.Hesap:
Intent Hesap = new Intent(MainActivity.this, Genel.class);
startActivity(Hesap);
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
I'm trying to make it so when someone clicks the icon at top ( which is in red circle at picture) it should do the same thing as my "Döviz Kuru" Button at action bar. I have 2 problems 1 is i cant seem to make same Intent work for 2 cases in the menu 2nd is R.id.İcon doesn't reach to icon. I also tried home and the name i give to that .png neither worked.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case android.R.id.home:
Intent Doviz = new Intent(MainActivity.this, MainActivity.class);
startActivity(Doviz);
finish();
return true;
case R.id.Kur:
Intent Doviz = new Intent(MainActivity.this, MainActivity.class);
startActivity(Doviz);
finish();
return true;
case R.id.Hesap:
Intent Hesap = new Intent(MainActivity.this, Genel.class);
startActivity(Hesap);
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
it gives an error abaut "Duplicate local variable Doviz". When i try to use the same Intent for 2 cases.
the answer for the making 2 cases with same job is done like this.
case android.R.id.home:
case R.id.Kur:
Intent Doviz = new Intent(Genel.this, MainActivity.class);
startActivity(Doviz);
finish();
return true;

To enable Home Icon of Actionbar...
Read This Document For More Info : Navigate Up to Parent Activity
You should Do following...
write these lines in OnCreate() method of your activity..
ActionBar actn = getActionBar();
actn.setHomeButtonEnabled(true); //set home button clickable...
actn.setDisplayHomeAsUpEnabled(true); //add up indicator with home button..
add this to onOptionsItemSelected()..
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case android.R.id.home:
//this calls Home Icon of Actionbar...
//your code..
return true;
default:
return super.onOptionsItemSelected(item);
}
}
for calling same code for both cases You can write cases like below..
switch (item.getItemId()) {
case android.R.id.home:
case R.id.Kur:
//this code runs for both cases..
//your code..
return true;
default:
return super.onOptionsItemSelected(item);
}
if you want to restart your activity....
you should use this code..this will restart current activity..
Intent intent = getIntent();
finish();
startActivity(intent);

How do I change the android actionbar title and icon
If you want to change it in code call
setTitle("My new title");
getActionBar().setIcon(R.drawable.my_icon);
And set the values to whatever you please.

Related

Back Arrow press exiting the App in Android [duplicate]

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...

How can I add an Action after clicking on an ActionBar's Button?

I want to add a new button to the ActionBar. When I click it, it does a specific Action. So I don't want a button that, after being pressed, opens a sub-menu (like the classic 3-dot menu).
I created a new button, this:
<item android:id="#+id/action_refresh"
android:icon="#drawable/refresh"
android:title="#string/refresh_string"
android:showAsAction="always"/>
and it's shown on the ActionBar, but if I click it, naturally, it doesn't do anything.
How can I manage to get an Action just pressing it?
Thanks!
You Need to override onOptionsItemSelected
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_refresh:
//do your stuff
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Same as Homo sapiens's answer, but with an if-structure.
Add this method to your Activity class:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (item.getItemId() == R.id.action_refresh)
{
// do your stuff
return true;
}
else if (item.getItemId() == R.id.otherItem)
{
// do other stuff
return true;
}
// ...
else
{
return super.onOptionsItemSelected(item);
}
}

SherlockActionBar up button won't work on API 10

I'm playing around with the sherlockactionbar library, there is a problem which is the up button won't work on API 10. The up icon can be shown on the actionbar, but it won't respond to my pressing. On higher API such as 16, it functions as expected without problems.
I tried several solutions I came across, but none of them work. Below is one of them.
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpTo(this, new Intent(this, test.class));
return true;
Make sure you have these set:
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
And that your switch is in this method:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case android.R.id.home:
/* Do Something //*/
return true;
default:
return false;
}
}

Using icon in top left of ActionBarSherlock to navigate

Using the developers guide found here, I am trying to make my icon navigate back to my home screen. I currently have a button which does this, and have copy and pasted the code in the onOptionsItemSelected() method. However tapping the icon never does anything. Is this a difference in ActionBar and ActionBarSherlock?
This is the code given as an example:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; go home
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
This is the code that I am using:
public boolean onOptionsItemSelected( MenuItem item ) {
switch( item.getItemId() ) {
case R.id.mainTopBluetoothState:
Toast.makeText( this, "BluetoothState", Toast.LENGTH_SHORT ).show();
return true;
case R.id.mainTopAppState:
Toast.makeText( this, "BluetoothState", Toast.LENGTH_SHORT ).show();
return true;
case android.R.id.home:
Log.i( "In Home", "In Home" );
killToasts();
dispatchKeyEvent(new KeyEvent( KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK ));
finish();
return true;
}
return super.onOptionsItemSelected( item );
}
When I tap the icon, nothing happens. The Log call in the code isn't ever shown in my LogCat either.
You are probably not enabling the ABS Activity logo to be clickable. Add this in onCreate()
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Also, if you haven't done so already, read Implementing Ancestral Navigation so you navigate up properly (ignore their use of getActionBar(), they're not using ABS, and that's the actual Android API Action Bar method).

android menu does not work properly

in an android app , I use menus for an activity
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/logout"
android:title="Logout" />
<item
android:id="#+id/compose"
android:title="Compose" />
<item
android:id="#+id/refresh"
android:title="Refresh" />
</menu>
and in activity :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.inboxmenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.logout:
SharedPreferences settings =getSharedPreferences("CASPreferences", MODE_PRIVATE);
SharedPreferences.Editor prefEditor = settings.edit();
prefEditor.putString("uid", "");
prefEditor.putString("stdno", "");
prefEditor.putString("firstname", "");
prefEditor.putString("lastname", "");
prefEditor.putString("lastname", "");
prefEditor.commit();
Intent myIntent = new Intent(this, LoginActivity.class);
startActivity(myIntent);
return true;
case R.id.refresh:
Log.e("menu" , "refresh");
//Intent intent=new Intent(this,MainActivity.class);
startActivity(getIntent());
finish();
case R.id.compose:
Log.e("menu" , "compose");
Intent intent1=new Intent(this,ComposeActivity.class);
startActivity(intent1);
default:
return super.onOptionsItemSelected(item);
}
}
but when I click on refresh button , it goes to compose activity ! like as you clicked on compose.
why does this happen?
add break to your switch statement, otherwise your code will continue into the third case
You haven`t added break; for each switch stmt.
Please try and share the result.
Rewrite your switch statement.
switch (item.getItemId()) {
case R.id.logout:
...
return true;
case R.id.refresh:
...
finish();
break; // <- Add this line
case R.id.compose:
...
break; // <- Add this line
default:
return super.onOptionsItemSelected(item);
}
Each case block must be enclosed by break.
The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered.
break doesn't required in first case block because flow interrupted by return statement.
Reference: http://download.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

Categories

Resources