Android 5.0 Back icon parent activity - android

I'm migrating my projects to new Material design / Android 5.0 Lollipop now.
In previous Android versions it was easy to create Activity with back ActionBar button (arrow) using android:parentActivityName in AndroidManifest.xml. But it seems that in doesn't work any more on new API with support libraries com.android.support:appcompat-v7:21.+.
Below is my code and screenshots from previous and updated to Lollipop support example project:
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="org.serge.androidprobe.app.MainActivity"
android:label="Activity1:Parent" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="org.serge.androidprobe.app.SecondActivity"
android:parentActivityName="org.serge.androidprobe.app.MainActivity"
android:label="Activity2" >
</activity>
</application>
Before migration to Lollipop:
public class MainActivity extends Activity {/**/ }
public class SecondActivity extends Activity {/**/ }
<resources>
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar"/>
</resources>
After migration to Lollipop:
public class MainActivity extends android.support.v7.app.ActionBarActivity {/**/ }
public class SecondActivity extends android.support.v7.app.ActionBarActivity {/**/ }
<resources>
<style name="AppTheme" parent="#style/Theme.AppCompat"/>
</resources>
Note: before migration to Lollipop I haven't used support/appcompat libs at all.
How to make back arrow visible & clickable on the top-left corner using new v21 of support/appcompat libraries?

In yours onCreate try calling
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
You then handle the click by checking android.R.id.home in onOptionsItemSelected

Related

How Up Navigation works

I've implemented inside my app the Up Navigation button in this way, inside my AndroidManifest.xml I wrote this:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".EventDetails"
android:label="#string/category_events"
android:parentActivityName=".MainActivity"
android:screenOrientation="portrait"/>
</application>
I didn't do anything else and everything works very good.
I read the official documentation and about the
android:parentActivityName=".MainActivity"
they say:
With the parent activity declared this way, you can navigate Up to the appropriate parent using the NavUtils APIs, as shown in the following sections.
This is my child activity code:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class EventDetails extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content of the activity
setContentView(R.layout.event_details);
}
}
So you can see that I don't implement NavUtils APIs, etc...
Now my question is this, why everything works?
NavUtils are a set of convenience methods for doing certain tasks, like launching your parent as a new task. They are not required for any android app, and there is basic functionality like back stack navigation present without any need to use them.
Also, I see you tagged this fragments- it has nothing to do with fragments. NavUtils is about navigating between Activities, not Fragments of a single Activity.

Android ActionBar/Toolbar Disappears in my App

So I have an app. It has 3 activities at the moment... I am using intents to launch the activities from one and another
MainActivity > ChallongeLogin > ChallongeEvents
In my MainActivity and my ChallongeLogin, there is a large bar at the top of the app that lists the name of my app. However, when I eventually read the ChallongeEvents, this bar disappears... I don't remember doing anything special to make this bar disappear. Why did it go away?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testing.testingapp">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:screenOrientation="portrait"
android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:screenOrientation="portrait"
android:name=".ChallongeLogin" />
<activity
android:screenOrientation="portrait"
android:name=".ChallongeEvents" />
</application>
</manifest>
According to your requirement you must extends AppCompatActivity instead of Activity .
public class ActivityName extends AppCompatActivity {
// ...
}
AppCompatActivity is from the appcompat-v7 library. Principally, this
offers a backport of the action bar. Since the native action bar was
added in API Level 11, you do not need AppCompatActivity for that.
However, current versions of appcompat-v7 also add a limited backport
of the Material Design aesthetic, in terms of the action bar and
various widgets. There are pros and cons of using appcompat-v7, well
beyond the scope of this specific Stack Overflow answer.
Reference
Activity, AppCompatActivity, FragmentActivity, and ActionBarActivity: When to Use Which?
It is called ActionBar/ToolBar.
Extend the AppCompatActivity class instead of plain Activity class in that activity's java class.

Activity does not show title bar in Android Studio

I have create demo application in Android Studio with two Activity. In First Activity Show Title Bar in preview as well as in emulator but on second Activity does not show Title Bar in Emulator but it display in preview. I have used API 22 in Android Studio.
Please help me....thanx in advn
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/BaseTheme">
<activity
android:name=".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=".DrawingActivity"
android:label="#string/app_name"
android:theme="#style/BaseTheme"
android:screenOrientation="portrait" />
</application>
Theme style file like this....
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="BaseTheme" parent="AppTheme">
<item name="colorPrimary">#color/action_bar_backgound</item>
</style>
I have also try this...
First Activity Work's fine so I have set that in Second Activity as a setContentView(R.layout.activity_main);
MainActivity.java
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void buttonAction(View v) {
Intent intent = new Intent(MainActivity.this, DrawingActivity.class);
startActivity(intent);
}
}
DrawActivity.java
public class DrawingActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
What you are facing is a bug related to the API 22(5.1) SDK.
Kindly refer the following links for the details on the bug in which the Action Bar is not displayed:
https://code.google.com/p/android/issues/detail?id=159793
https://code.google.com/p/android/issues/detail?id=159780
What you can do for now
Either revert back to API 21 if possible or,
Select API 21 in the compile with (render with) option in Android Studio.
Hope this helps.

getActionBar() is always returning null

I have gone through lots of similar posts in Stackoverflow, but nothing seems to resolve my issue.
I am trying to get the ActionBar and invoke setDisplayHomeAsUpEnabled(true).
I can see the black ActionBar (as the theme is black background) with the Activity name displayed as title. But in my activity code, if I try to do getActionBar() it always returns null.
I tried the code in onResume() and onCreate() (after inflating the layout). Everywhere it returns null. Is there anything I am missing?
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".ActivityMain"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
This is my styles definition. I am using Theme.AppCompat.Light.DarkActionBar theme
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
Also, I am using following dependency:
compile 'com.android.support:support-v4:22.1.0'
compile 'com.android.support:appcompat-v7:22.2.0'
and my activity class definition is:
MainActivity extends AppCompatActivity
Try using getSupportActionBar(). The latest appcompat-v7 seems to disable the systembar.
If your class defenition is MainActivity extends AppCompatActivity, you should try using getSupportActionBar()
If your class defenition is MainActivity extends Activity, you can try using getActionBar() (this call requires API 11)

Remove Action Bar Android

Hi to android stack overflow communities,
I want to remove the action bar which contain the title and the three dots. I have tried some of the solutions such as android:theme="#android:style/Theme.NoTitleBar" but, the app went crash.
Is there any possible other solution? Thanks
This is the nuclear option.
Replace
public class MainActivity extends ActionBarActivity
with
public class MainActivity extends Activity
in all the Activitys where you don't want ActionBar.
And to remove the three-dots button, add
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
return false;
}
to your Activity.
Try this. This will work.
You have to use Activity not ActionBarActivity. So extends your javaclass from Activity. For removing the three dots remove onCreateOptionsMenu method from your Activity.
Create Your code like this
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Enter this line android:theme="#android:style/Theme.Black.NoTitleBar" in Activity
You just need to call function
getActionBar().hide();
Tthis will hide action bar for you simply.

Categories

Resources