Action Bar producing NullPointerException [duplicate] - android

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
Solution:
I tried everything available on internet and nothing really worked for me. Then I found a solution. I didnt have the values-v23 folder in my project. If you don't have that, DO IT! and add this in style.xml of the Values- v23 folder
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowActionBar">true</item>
<item name="windowNoTitle">false</item>
</style>
</resources>
I had splash screen and slider which was overlaying the action bar and android studi somehow got confused and my actionbar was returning null. After 2 weeks of research I found a tiny issue which no one seem to point out. I edited the line
android:theme="#style/AppTheme">
with the actual theme i was using i.e
<android:theme="#style/Theme.AppCompat.Light.DarkActionBar">
It worked for me..
Question
I had this app working totally fine until I made some changes.
I reverted back all of the changes but now I am getting NUllPointerException on ActionBar
ActionBar ab = getSupportActionBar();
if (ab != null) {
ab.setLogo(R.mipmap.icon);
ab.setDisplayUseLogoEnabled(true); //shows logo
ab.setDisplayShowHomeEnabled(true); //shows home page
}
else {
Toast.makeText(HomeScreen.this, "Feature not supported!", Toast.LENGTH_SHORT).show();
}
I put the if else statements to check where the problem was. It runs the else part only. The piece of Home Screen code is this
package com.example.dell.optasia;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.media.MediaPlayer;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.support.v4.view.GestureDetectorCompat;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Locale;
import static android.view.GestureDetector.OnDoubleTapListener;
import static android.view.GestureDetector.OnGestureListener;
import static com.example.dell.optasia.R.id.editText;
public class HomeScreen extends AppCompatActivity implements
OnGestureListener, OnDoubleTapListener {
//used for text to speech
TextToSpeech ttsobject;
int result;
TextView et;
String text;
String texttoplay;
//uptil here
private GestureDetectorCompat GestureDetect;
private static TextView Viewtext;
//used for speech to text
public static final int REQUEST_OK = 1;
public static TextView finalRes;
public static ImageView imgBtn;
//uptill here
public MediaPlayer mp1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
mp1 = MediaPlayer.create(HomeScreen.this, R.raw.startup);
mp1.start();
ActionBar ab = getSupportActionBar();
if (ab != null) {
ab.setLogo(R.mipmap.icon);
ab.setDisplayUseLogoEnabled(true); //shows logo
ab.setDisplayShowHomeEnabled(true); //shows home page
}
else {
Toast.makeText(HomeScreen.this, "Feature not supported!", Toast.LENGTH_SHORT).show();
}
//for tap and hold and all other gestures
Viewtext = (TextView) findViewById(R.id.textView);
//onlongpress code needs this
GestureDetect = new GestureDetectorCompat(this, this);
GestureDetect.setOnDoubleTapListener(this);
//text to speech code here
et = (TextView) findViewById(editText);
ttsobject = new TextToSpeech(HomeScreen.this, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
result = ttsobject.setLanguage(Locale.UK);
} else {
Toast.makeText(HomeScreen.this, "Feature not supported!", Toast.LENGTH_SHORT).show();
}
}
});
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
package="com.example.dell.optasia">
<uses-sdk android:minSdkVersion="11" />
<application
android:allowBackup="true"
android:icon="#mipmap/icon"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Splashscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".HomeScreen">
<intent-filter>
<action android:name="com.example.dell.optasia.HomeScreen" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".AboutUs">
<intent-filter>
<action android:name="com.example.dell.optasia.AboutUs" />
<category android:name="android.intent.category.About" />
</intent-filter>
</activity>
<activity android:name=".Help"></activity>
</application>
Style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="windowActionBar">true</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>

#Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
super.onCreate(savedInstanceState);
setContentView(R.layout...);
// experiment with the ActionBar
ActionBar actionBar = getSupportActionBar();
}

Related

Why Do see two toolbars when I created an app that has a navigation drawer?

I have created an app that has a navigation drawer, it works fine, but the only problem is that I see two toolbars. One in which there is Actionbartoggle button and other which is default. Now the tutorial I have seen has used a toolbar in the main content of drawer layout and obviously navigation contents are coming from menu.
Now the problem is I think we have to pass a toolbar reference in the constructor of Actionbardrawertoggle, and after I passed it properly it works fine, but I see two toolbars/actionbars. Basically my question is how do I get the toggle button on the main action bar and not get second tool bar created? I have tried using
<style name="MyTheme" parent="android:Theme.Material.Light.NoActionBar">
It helps me to hide the upper actionbar but I don't think it's the good way to do it. I am uploading my screenshot of output and code:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- Page Content -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#color/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
</LinearLayout>
<!-- Navigation View -->
<android.support.design.widget.NavigationView
android:id="#+id/navigationView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:itemTextColor="#5DADE2"
app:menu="#menu/drawer_items"/>
</android.support.v4.widget.DrawerLayout>
Mainactivity:
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Toast;
import java.util.HashMap;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
SessionManager session;
private Toolbar mToolbar;
private DrawerLayout mDrawerLayout;
ActionBarDrawerToggle drawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_area);
setupToolbarMenu();
setupNavigationDrawerMenu();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.lg:
session.logoutUser();
return true;
default:
return super.onOptionsItemSelected(item);
}
} private void setupToolbarMenu() {
mToolbar = (Toolbar) findViewById(R.id.toolbar);
mToolbar.setTitle("Home Page");
}
private void setupNavigationDrawerMenu() {
NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
navigationView.setNavigationItemSelectedListener(this);
navigationView.setItemIconTintList(null);
drawerToggle = new ActionBarDrawerToggle(this,
mDrawerLayout,mToolbar,
R.string.drawer_open,
R.string.drawer_close);
mDrawerLayout.addDrawerListener(drawerToggle);
drawerToggle.syncState();
} #Override // Called when Any Navigation Item is Clicked
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
closeDrawer();
switch (menuItem.getItemId()) {
case R.id.home:
// Put your Item specific Code here
break;
case R.id.invoice:
Intent intent = new Intent(this,details.class);
startActivity(intent);
// Put your item specific Code here
break;
}
return true;
}
// Close the Drawer
private void closeDrawer() {
mDrawerLayout.closeDrawer(GravityCompat.START);
}
// Open the Drawer
private void showDrawer() {
mDrawerLayout.openDrawer(GravityCompat.START);
}
#Override
public void onBackPressed() {
if (mDrawerLayout.isDrawerOpen(GravityCompat.START))
closeDrawer();
else{
new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Exit")
.setMessage("Are you sure?")
.setPositiveButton("yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
}).setNegativeButton("no", null).show();}
}
}
my styles.xml file:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
I know this might not be of some use, but here's my manifest file too:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET"/>
<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=".details" />
<activity android:name=".Login"
android:label="Login"/>
<activity android:name=".MainActivity" />
<activity android:name=".recycler" />
<activity android:name=".RegisterActivity" />
<activity android:name=".SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
And here's the output I am getting:
Try to follow this example: https://developer.android.com/training/appbar/setting-up.html
There you will find this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
}
the thing you are missing is: setSupportActionBar(myToolbar);

add action bar with back button in preference activity

Here is my preference activity:
package com.example.hms.test;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class PrefsActivity extends PreferenceActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
}
here I want to show an actionbar with name settings and a back button to home
You should do couple of things:
Add the following to your onCreate of PreferenceActivity:
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Override onOptionsItemSelected in PreferenceActivity:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
change the <activity> tag in manifest for your PreferenceActivity to look something like this:
<activity
android:name=".PrefsActivity"
android:label="#string/title_activity_settings"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.android.MainActivity" />
</activity>
Finally put android:launchMode="singleTop" in your MainActivity <activity> tag in manifest:
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:launchMode="singleTop"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
The answer Pooya gave won't work for a PreferenceActivity. Instead make your class extend AppCompatActivity, and use a PreferenceFragment to load up the preference. Here is my code for settings:
public class MyPrefsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onSupportNavigateUp(){
finish();
return true;
}
public static class MyPreferenceFragment extends PreferenceFragment {
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
}
Put the activity in your AndroidManifest.XML:
<activity android:name=".MyPrefsActivity"
android:label="Preferences"
android:theme="#style/AppTheme"/>
And now you can start the settings activity using an intent in my Main Activity (or whichever parent activity you have) as normal:
Intent prefsIntent = new Intent(activity, MyPrefsActivity.class);
activity.startActivity(prefsIntent);
I needed different action bar menu items in my activities so I created my MainActivity with a singleTop launchMode. That was great for getting my child activity's action bar set up, but it left my preferences activity without an action bar.
In the end, the key was in making sure the MainActivity theme had a parent of
Theme.AppCompat.Light.NoActionBar:
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:launchMode="singleTop"
android:theme="#style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
and the SettingsActivity theme had the parent Theme.AppCompat.Light.DarkActionBar:
<activity
android:name=".SettingsActivity"
android:label="#string/title_activity_settings"
android:theme="#style/SettingsTheme"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="net.deatrich.app.bodyandminddbt.MainActivity" />
</activity>
In styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="SettingsTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
There's probably a better way to style it but this works.
For anyone else reading, also remember to derive your SettingsActivity from AppCompatPreferenceActivity:
SettingsActivity.java:
public class SettingsActivity extends AppCompatPreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// I'm displaying a fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new GeneralPreferenceFragment())
.commit();
setupActionBar();
}
private void setupActionBar() {
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
if (!super.onMenuItemSelected(featureId, item)) {
NavUtils.navigateUpFromSameTask(this);
}
return true;
}
return super.onMenuItemSelected(featureId, item);
}
...
okay. now you still trouble then i have unique solution for you which work 100% with just minor changes.
so first of all create one style for only settings activity.
here is my toolbar styles code.
<style name="Toolbar_settings_style" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:colorControlNormal">#color/appTitleTextColor</item>
</style>
and here is my stlyes.xml looks like
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:colorControlNormal">#color/appTitleTextColor</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="Toolbar_settings_style" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:colorControlNormal">#color/appTitleTextColor</item>
</style>
</resources>
yes. you noticed i have create duplicate styles(appTheme and Toolbar_settings_style both are same styles attribute) for only settingsActivity.
it is very important to create duplicate styles.
Now go to your settingsActivity and paste below code inside onCreate().
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
After placing above code now my SettingsActivity looks like
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.RingtonePreference;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;
import settingspreferences.AppCompatPreferenceActivity;
public class User_Settings extends AppCompatPreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// load settings fragment
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.settings);
// gallery EditText change listener
bindPreferenceSummaryToValue(findPreference(getString(R.string.key_gallery_name)));
// notification preference change listener
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
// onBackPressed();
}
return super.onOptionsItemSelected(item);
}
private static void bindPreferenceSummaryToValue(Preference preference) {
preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(), ""));
}
/**
* A preference value change listener that updates the preference's summary
* to reflect its new value.
*/
private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String stringValue = newValue.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list.
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
// Set the summary to reflect the new value.
preference.setSummary(
index >= 0
? listPreference.getEntries()[index]
: null);
} else if (preference instanceof RingtonePreference) {
// For ringtone preferences, look up the correct display value
// using RingtoneManager.
if (TextUtils.isEmpty(stringValue)) {
// Empty values correspond to 'silent' (no ringtone).
preference.setSummary(R.string.pref_ringtone_silent);
} else {
Ringtone ringtone = RingtoneManager.getRingtone(
preference.getContext(), Uri.parse(stringValue));
if (ringtone == null) {
// Clear the summary if there was a lookup error.
// preference.setSummary(R.string.summary_choose_ringtone);
} else {
// Set the summary to reflect the new ringtone display
// name.
String name = ringtone.getTitle(preference.getContext());
preference.setSummary(name);
}
}
} else if (preference instanceof EditTextPreference) {
if (preference.getKey().equals("key_gallery_name")) {
// update the changed gallery name to summary filed
preference.setSummary(stringValue);
}
} else {
preference.setSummary(stringValue);
}
return true;
}
};
}
}
Now let's come to real important part.
go to manifest.xml file and find your settingsActivity and paste below code.
<activity android:name=".User_Settings"
android:theme="#style/Toolbar_settings_style"></activity>
so here is my AndroidManifest.xml looks like
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mypackage">
<uses-permission android:name="android.permission.INTERNET" />
<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"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- <intent-filter> -->
<!-- <action android:name="android.intent.action.SEARCH" /> -->
<!-- </intent-filter> -->
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
<activity android:name=".Add_Items" />
<activity android:name=".Product_details_view" />
<activity
android:name=".editProducts"
android:parentActivityName=".Product_details_view" />
<activity android:name=".User_Settings"
android:theme="#style/Toolbar_settings_style"></activity>
</application>
</manifest>
This is all you need to shows Toolbar on settingsActivity.

Android app fullscreen with no Title bar and no Action bar

I am trying to create my first Android app with a WebView. The last issue I am trying to resolve is to have the WebView in fullscreen with no title bar and no action bar.
My Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.domain.butlerv2">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme.NoActionBar">
<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>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
</manifest>
My MainActivity:
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
public class MainActivity extends AppCompatActivity {
String answer;
private GoogleApiClient client;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
ConnectivityManager cm = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (null != activeNetwork) {
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
answer = "You are connected to a WiFi Network";
if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
answer = "You are connected to a Mobile Network";
} else
answer = "No WIFI Connectivity";
Toast.makeText(getApplicationContext(), answer, Toast.LENGTH_LONG).show();
WebView webView = (WebView)
findViewById(R.id._webview);
WebSettings webSettings = webView.getSettings();
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.domain.com/app/index.php");
webView.setWebViewClient(new MyBrowser());
webView.requestFocus(View.FOCUSABLES_ALL);
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
#Override
public void onStart() {
super.onStart();
client.connect();
Action viewAction = Action.newAction(
Action.TYPE_VIEW,
"Main Page",
Uri.parse("android-app://com.domain.butlerv2/http/host/path")
);
AppIndex.AppIndexApi.start(client, viewAction);
}
#Override
public void onStop() {
super.onStop();
Action viewAction = Action.newAction(
Action.TYPE_VIEW,
"Main Page", shown.
Uri.parse("http://host/path"),
Uri.parse("android-app://com.domain.butlerv2/http/host/path")
);
AppIndex.AppIndexApi.end(client, viewAction);
client.disconnect();
}
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
My Style
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="generalnotitle" parent="ThemeOverlay.AppCompat.Light">
<item name="android:windowNoTitle">true</item>
</style>
<style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="#style/Theme.AppCompat.Light">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
</resources>
When I run the App on a Android phone the Title and Action bar are displayed. Can anyone see what I am doing wrong.
I have read so many Posts and tech papers all of which are different which has totally confused me.
Can you help.
Regards
Do this in Activity
if (Build.VERSION.SDK_INT < 16) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
else
{
View decorView = getWindow().getDecorView();
//Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
}
and do the following in AndroidManifest
<activity
......................................
android:screenOrientation="portrait" <!--if you want to lock orientation-->
android:theme="#style/Theme.AppCompat.NoActionBar"
..................................... >
</activity>
Hope this solve your issue,
Change you AndroidManifest.xml theme from,
android:theme="#style/AppTheme.NoActionBar"
TO
android:theme="#android:style/Theme.Holo.Light.NoActionBar.Fullscreen"

Activity doesnt show up

i want to make a Login page for the app. however the activity is not showing its only a blank page with a single title bar with a app name that is there. i tried different themes but the problem remains the same.
here is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="variofitness.com.schedulekeeper">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="#mipmap/img_logo"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".HomeActivity"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.NoActionBar">
</activity>
<activity android:name=".LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
And Here is my styles file
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/white</item>
<item name="android:textColorPrimary">#color/lightBlue</item>
<item name="android:windowBackground">#color/white</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="ToolBarStyle" parent="">
<item name="android:background">#android:color/transparent</item>
<item name="titleTextAppearance">?android:textAppearanceLarge</item>
</style>
<style name="ThemeOverlay.MyApp.ActionBar" parent="ThemeOverlay.AppCompat.ActionBar">
<!-- Parent theme sets colorControlNormal to textColorPrimary. -->
<item name="android:textColorPrimary">#color/lightBlue</item>
</style>
</resources>
Here is my java code..
package variofitness.com.schedulekeeper;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/**
* Created by vinod on 1/29/16.
*/
public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
EditText edtUserName,edt_password;
TextInputLayout input_application_password,input_application_userName;
TextView txt_Forgot_Password;
Button btn_login,btn_request_For_signUp;
#Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.layout_login);
initializeComponents();
}
private void initializeComponents() {
input_application_password = (TextInputLayout)findViewById(R.id.input_application_password);
input_application_userName = (TextInputLayout)findViewById(R.id.input_application_userName);
edtUserName = (EditText)findViewById(R.id.edtUserName);
edt_password = (EditText)findViewById(R.id.edt_password);
txt_Forgot_Password = (TextView)findViewById(R.id.txt_Forgot_Password);
btn_login = (Button)findViewById(R.id.btn_login);
btn_request_For_signUp = (Button)findViewById(R.id.btn_request_For_signUp);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.txt_Forgot_Password:
break;
case R.id.btn_login:
break;
case R.id.btn_request_For_signUp:
break;
default:
break;
}
}
}
can anyone help me to tell me what i am missing?
#Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
Replace this piece of code with
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
And you are good to go.

Disable a Android Title

1.Hello , i am trying to build a webView Full Screen App
But i can't understand how to disable the title bar
I change the theme in my manifest to this : Android:theme="#style/Theme.AppCompat.NoActionBar" >`
But that still keep showing me that Title .
2.From sdk 6 there is a like a email icon on the right bottom
of the app,there is way to disable that too ?
Thanks for all the help !!
code :
Manifest
:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.omermalka.webview" >
<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/Theme.AppCompat.NoActionBar" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
main Activity :
package com.example.omermalka.webview;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
/*FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});*/
WebView webView = (WebView)findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new MyBrowser());
webView.loadUrl("https://pizzahut.co.il");
}
private class MyBrowser extends WebViewClient{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
Activity.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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main" tools:context=".MainActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/webView"
android:layout_alignParentStart="true" />
</RelativeLayout>
Thanks again to all the helpers
You can use requestWindowFeature(Window.FEATURE_NO_TITLE); in your activity before setContentView(LayoutId); to hide title.
Note:
Reference to this answer in AppCompact and ActionBarSherkock libraries you should put requestWindowFeature(Window.FEATURE_NO_TITLE); before super.onCreate();.
Or
you can put this code into styles.xml
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
</resources>
and in your manifest file put below code in your activity tag
android:theme="#style/AppTheme.NoActionBar"
Like
<activity
android:name=".RSSActivity"
android:label="#string/title_activity_rss"
android:theme="#style/AppTheme.NoActionBar" >
</activity>

Categories

Resources