Up Button in Toolbar doesn't do anything - android

I set up Toolbar in my app (for the first time) but the I can't set the Home (/Up) button to do anything when clicked.
I have 3 activitives (MainActivity , Second , Third) and I've defined the toolbar in all 3 but nothing happens when I click the Home button in each of the activitis I want the button to do his (in my understanding) default action which is go back to the Home Activity which is "MainActivity".
Here is some relevant code:
Toolbar layout (named: app_bar.xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/toolBar_background"
android:elevation="5dp"
android:title="#string/app_name" />
</RelativeLayout>
my 3 activities layout xmls:
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:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.example.noamm_000.finaltoolbarproject.MainActivity">
<include layout="#layout/app_bar"/>
//More layout code...
Activity_Second.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.noamm_000.finaltoolbarproject.Second">
<include layout="#layout/app_bar"/>
//More layout code...
Activity_Third.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.noamm_000.finaltoolbarproject.Third">
<include layout="#layout/app_bar"/>
//More layout code
Actually all the 3 activities simply include the toolbar layout which names "app_bar.xml".
Here is part of my Mainfest file where I configured the app_parent:
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Second"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
<intent-filter>
<action android:name="com.example.noamm_000.finaltoolbarproject.Second" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Third"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
<intent-filter>
<action android:name="com.example.noamm_000.finaltoolbarproject.Third" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
and ofcourse parts of my java code for each of my 3 activities:
MainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar_id);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
mainBtn = (Button) findViewById(R.id.btn_main);
openSecondActivity();
}
public void openSecondActivity(){
mainBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent("com.example.noamm_000.finaltoolbarproject.Second");
startActivity(intent);
}
});
SecondActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
btnSecond = (Button) findViewById(R.id.btn_second);
toolbar = (Toolbar) findViewById(R.id.toolbar_id);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
openThirdActivity();
}
public void openThirdActivity(){
btnSecond.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent("com.example.noamm_000.finaltoolbarproject.Third");
startActivity(intent);
}
});
}
and ThirdActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
toolbar = (Toolbar) findViewById(R.id.toolbar_id);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
}
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.toolbar_menu,menu);
return super.onCreateOptionsMenu(menu);
}
You can see that all 3 Activities "onCreate" functions are pretty much the same..
When I start my app I can see the toolbar in all 3 activities and I can see the back arrow in all but clicking it do nothing.
I know I can set the toolbar.setNavigationOnClickListener but I just want it to make it default action which is go to home activity....
Thank you very much,
Noam

Try like this...
#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) {
Intent i = new Intent(CurrentActivity.this, HomeActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
return true;
}
return super.onOptionsItemSelected(item);
}

Try this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//finish(); or Do wahtever you wnat to do
return true;
}
return true;
}

If you are having parent activity,Follow this code,
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
if(NavUtils.getParentActivityIntent(this)== null) {
onBackPressed();
}else{
NavUtils.navigateUpFromSameTask(this);
}
break;
}
return super.onOptionsItemSelected(item);
}

Try this in your activities
#Override
public boolean onOptionsItemIdSelected(int id) {
switch (id) {
case R.id.home:
//do stuff
break;
}
return true;
}
In your case, I recommend to you create a AbstractActivity which inherit all your activities. So you can factor a number of behavior , including management of the toolbar button

Related

Android - Preventing duplicate window on Intent & StartActivity

I am trying to solve an issue with my android application.
The issue is that when i start a new instance or class by calling an Intent and StartActivity, a duplicate window or view opens.
I want to keep the same activity or view but execute/run a new class without affecting the view. The intent is simply to execute an extended class but i dont want it recreating or opening a duplicate view.
I have tried using android:Launchmode="singleTop" but to no effect.
I have used the standard android navigation drawer example xml and classes. You will see the content_main.xml contains a viewswitcher which includes 2 other xml files which doesn't need to load a new instance or activity...if that makes sense.
Im not sure if the issue lies with the BeaconTracking.java where it calls the super.onCreate(...) event again maybe causing the parent view to reopen??
Any ideas where i am going wrong?
Thanks in advance!
AndroidManifest.xml
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.NoActionBar">
</activity>
<activity
android:name=".BeaconTracking"
android:label="#string/app_name"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:theme="#style/AppTheme">
</activity>
MainActivity.java
public class MainActivity extends AppCompatActivity implements ...
public ViewSwitcher switcher;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
...
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_vehicle_tracking) {
Intent intent = new Intent(getApplicationContext(), BeaconTracking.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
}
} else if (id == R.id.nav_vehicle_info) {
//SWITCH TO BEACON SCREEN
switcher.setDisplayedChild(2);
}
//CLOSE NAVIGATION DRAWER WHEN BUTTON IS PRESSED
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
BeaconTracking.java
public class BeaconTracking extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
switcher.setDisplayedChild(1);
}
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ViewSwitcher
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/content_frame"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<include
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="#layout/content_beacons" />
<include
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="#layout/content_map" />
</ViewSwitcher>
android:Launchmode="singleTop" will not help here, why do you want to open another activity you can manage it in your current activity with view switcher, still you want to open another activity without duplicate window. In that case you can make the transparent ui of the opening activity.
<activity android:name = "BeaconTracking"
android:label = "#string/app_name"
android:theme = "#android:style/Theme.NoDisplay" >
or with theme #android:style/Theme.Translucent.NoTitleBar"
And replace the parent class to AppCompatActivity of BeaconTracking. But still BeaconTracking activity will be added in back stack.
Each Activity has its own views. If you don't want to open new views, you shouldn't start a new Activity. Just move the functionality of your BeaconActivity into the MainActivity.
Try using android:launchMode="singleInstance" in Manifest.

How to make the 'back-arrow' work in the toolbar?

I have the following part of the xml code that defines my toolbar:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:columnCount="5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:useDefaultMargins="true"
android:alignmentMode="alignBounds"
android:columnOrderPreserved="false">
<android.support.v7.widget.Toolbar android:id="#+id/toolbar_setting"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"/>
... // other code here
and the code in the SettingsActivity (derived from AppCompatActivity) is as follows:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
// Set toolbar, allow going back.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_setting);
toolbar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Settings");
When compiling and running the code I see a toolbar as follows:
but a click on the left-arrow does not get me back to the previous menu. What am I missing here?
You can access that little arrow by android.R.id.home :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return(super.onOptionsItemSelected(item));
}
#Override
public void onBackPressed() {
super.onBackPressed();
}
If you try this code , when you click arrow, it will act like your back button pressed.
in onOptionsItemSelected you need to listen for the click then do something
case android.R.id.home:
// do something with the click
break;
As you need to go one level up, make the following changes in your AndroidManifest.xml.
<activity
android:name=".CurrentActivity"
android:parentActivityName=".OneLevelUpActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".OneLevelUpActivity" />
</activity>
In onCreate() add toolbar.setHomeButtonEnabled(true);
The <meta-data> is to support earlier API versions (<API level 16).

How to go back by using action bar in android

I have to get this as a result:
Moreover, how do I add colour in the action bar's background?
Try this
Create toolbar.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="50dp"
android:background="#color/orange"
android:theme="#style/AppTheme.Toolbar.Theme"/>
Include this toolbar in your activitys xml file
<?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:background="#color/app_bg"
android:orientation="vertical"/>
<include
android:id="#+id/tool_bar"
layout="#layout/toolbar" />
</LinearLayout>
create your activity.java
public class MainActivity extends AppCompatActivity {
protected ActionBar actionBar;
protected Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_profile);
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish(); //here add your back function code.
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Click here to add back action button in your actionbar and add onClick methocd for that button.
For adding background to your actionbar refer this.
Hope this helps.

Adding ActionBar with the arrow "GoBack" to PreferenceActivity?

I have a PreferenceActivity. I'm looking for a way to add ActionBar with the arrow "GoBack" to it. All the examples I found so far have been, to my mind, overcomplicated because if I had a simple Activity I could add ActionBar to it with one line of java code and that would be it.
I wonder, isn't there a simple way to add ActionBar with the arrow "GoBack" to PreferenceActivity?
UPDATE:
Here's my Preference activity:
public class PreferenceActivity123 extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new MainPreferenceFragment()).commit();
}
public static class MainPreferenceFragment extends PreferenceFragment {
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
}
// Add this in your androidmanifest.xml file
<activity
android:name=".SecondActivity"
android:label="#string/app_name"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
You have to set DisplayHomeasUpEnabled in your actionbar activity like
getSupportActionBar().setDisplayHomeAsUpEnabled(true);//Which will show back button
Define the parent activity in AndroidManifest.xml where the activity(PreferenceActivity) will be called once the back button in the action bar is pressed.
In your definition on the Manifest, add the line:
<activity
android:parentActivityName="com.example.activities.PreferenceActivity"
</activity>
or
Just listen the optionItemSelected method
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(this, PreferenceActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Edit :
In order to achieve same in Preference activity You need to make custom action bar style with back button in styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="PrefTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/PrefActionBar</item>
</style>
<style name="PrefActionBar" parent="#android:style/Widget.Holo.ActionBar">
<item name="android:displayOptions">showHome|homeAsUp|showTitle</item>
</style>
</resources>
Call the style in manifest like :
<application android:theme="#style/PrefTheme">
Call the action bar in activity
getActionBar().setDisplayHomeAsUpEnabled(true);
Just use android.support.v7.widget.Toolbar
public class SettingsActivity extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
Toolbar actionbar = (Toolbar) findViewById(R.id.actionbar);
actionbar.setTitle("Settings");
actionbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_action_back));
actionbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
YourActivity.this.finish();
}
});
}
}
and your activity_settings.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".SettingsActivity"
tools:menu="settings"
tools:actionBarNavMode="standard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/actionbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimaryDark"
/>
<FrameLayout
android:id="#id/content"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>

Relative Layout with a text field and Image field doesn't display on Android Application

I start an activity from main activity at startup and finish that activity using finish(). Now the issue is that contents of the activity being called are not visible. Heres the code snippet.
MainActivity onCreate()
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, SplashActivity.class);
startActivity(intent);
// Set up the action bar.
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(
actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
SplashActivity onCreate()
public class SplashActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
//setContentView(R.layout.fragment_splash);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
finish();
}
SplashActivity Fragment LAYOUT 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:gravity="center"
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.daq.SplashActivity$PlaceholderFragment" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="192dp"
android:layout_height="146dp"
android:layout_gravity="center"
android:contentDescription="#string/desc"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
SplashActivity activity_splash.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.daq.SplashActivity"
tools:ignore="MergeRootFrame" >
</FrameLayout>
Android Manifest FIle
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.daq"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.daq.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.daq.SplashActivity"
android:label="#string/title_activity_splash"
android:parentActivityName="com.example.daq.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.daq.MainActivity" />
</activity>
</application>
</manifest>
I donot know why 2 layout files are created when I make one activity (fragment_splash.xml and activity_splash.xml)
I would like to mention that the code works without errors and activities are shown but without content.
you can first start SplashActivity after that MainActivity It will be better
You are calling finish() as soon as the fragment is placed in the container so the activity will finish and go back to the activity that called it.
public class SplashActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
//setContentView(R.layout.fragment_splash);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finish();
}

Categories

Resources