Android ActionBar/Toolbar Disappears in my App - android

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.

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.

Manifest file structured in android studio for different activities

I want to know how to set multiple activities by using different layouts and through intent filters
How to set pages in application and what we have to specify in section of activity in manifest file for action and category for different pages of activity.
Please help me on that. What should I follow, So that I can make proper flow of my application.
Any suggestion or references for the same would be appreciated.
Mainifest.xml is having more information about application.
Activity defining is also one part of Manifest.xml, So define all the activities which are used in your app flow here. Main launcher activity will have tag with action - MAIN and category type - LAUNCHER. And other activities are defined in a simple manner.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example">
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.LauncherActivity"
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.ActivityA" >
</activity>
<activity android:name="com.example.ActivityB" >
</activity>
....
</application>
There are several other attributes that you can include in this element, to define properties such as the label for the activity, an icon for the activity, or a theme to style the activity's UI.
Follow for more: Activity
Labels in Activity: Activity labels in Manifest.xml

Does Android need a default layout file called activity_main.xml

I'm an Android noob and I'm having difficulty finding out something basic about Android.
I currently have an activity_main.xml file.
I want to use this layout when I first start the Android emulator, using Android Studio
Does Android look for a file activity_main.xml and use it as the default layout?
Here's my AndroidManifest.xml:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
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>
</application>
So I understand that this specifies that my .MainActivity will respond to an action.MAIN intent call. What I don't know is what the action.MAIN intent call actually is, and how my activity_main.xml relates to this.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Can anyone provide an explanation or a link to a good primer that explains these basic concepts?
From manifest :
<action android:name="android.intent.action.MAIN" />
Means this activity is the entry point of the application. In your case, the MainActivity starts.
The MainActivity sets up the layout for itself - the line responsible is
setContentView(R.layout.activity_main);
Where it looks for layout file activity_main.xml. This is just the naming convention - feel free to rename the layout file and call the new one from setContentView. It's not required to be called activity_main

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.

Android 5.0 Back icon parent activity

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

Categories

Resources