actionbarsherlock getSupportActionBar() return null in android4.0,but in 2.3.3 is ok - actionbarsherlock

application use actionbarsherlock,but main activity get getsupportactionbar returns null.
AndroidManifest.xml:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Light1Theme" >
<activity
android:name=".activity.LogonActivity"
android:configChanges="orientation|keyboardHidden"
android:windowSoftInputMode="adjustUnspecified|stateHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
...
themes.xml:
<style name="Light1Theme" parent="Theme.Sherlock.Light">
LogonActivity.ava
extends SherlockActivity implements OnSharedPreferenceChangeListener {
...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//requestWindowFeature(Window.FEATURE_NO_TITLE);
//getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.logon);
try{
final ActionBar ab = getSupportActionBar();
//return null
ab.setBackgroundDrawable(getResources().getDrawable(R.drawable.ab_bg_black));
getSupportActionBar().setIcon(R.drawable.hospital);
}catch(Exception e){
Log.e("", e.getMessage());
}

I was facing this same issue a few hours ago, and it turned out, the root of the problem was that I used a theme with no title bar.
This reply from Jake Wharton, the developer of actionbarsherlock will help you sort it out.
In short: on android versions ICS and newer, setting the attribute android:windowNoTitle will affect the action bar's display (as it is a core feature in those versions).
The ABS has wrapped it into windowNoTitle (with no namespace / default abs namespace), so depending on the android version your application is installed on, you will get the proper display.
If you have the line
<item name="android:windowNoTitle">true</item>
in your style definition, just change it to
<item name="windowNoTitle">true</item>
or completely leave it out / remove it from your theme (abs will handle it correctly!)

I was facing this same issue now , and I found that use android:theme="#style/Theme.Sherlock" is enough.
if you use
<item name="windowNoTitle">true</item>
or
<item name="android:windowNoTitle">true</item>
It will crash application , may be Jake fix this in Theme.Sherlock.
So Here is my solution now:
<application
android:name="XXXXApplication"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.Sherlock" >
And it works well on ICS+ and pre-ICS , test on android 2.3.5,2.3.7,CM7 , android 4.0,4.1

In my case, I face this issue when I set the theme android:theme="#style/Theme.Sherlock.NoActionBar", then call getSupportActionBar() to get the actionbar. If you want to manage the action bar, just turn the them to android:theme="#style/Theme.Sherlock.

I had the same problem. I have simple removed below line of code and its work like a charm.
requestWindowFeature(Window.FEATURE_NO_TITLE);

Related

App Bar is not showing in Android Studio but showing on an actual android device

I have already checked similar problems like mine but none of it helped. I also checked my previous project with the same AppTheme, they are the same but I have noticed everytime I create a new android project, the App bar is not showing even after several "Rebuild Project".
This is what my styles.xml shows
<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>
This is what my AndroidManifest.xml shows
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mycontact">
<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>
</application>
</manifest>
My MainActivity.java (Haven't put code yet)
package com.example.mycontact;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
And my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
</LinearLayout>
I have also tried changing the layout from ContraintLayout, RelativeLayout but nothing has changed. Zooming in and zooming out the layout doesn't change at all. Here is a snapchat.. Thank you for your help.
activity_main.xml page
Your styles.xml and other files seem to be in order, so try the following out.
Click the "Eye" icon on the toolbar above the XML Layout Preview, and then select "Show Layout Decorations" if it is unchecked. This should make it display both the AppBar and the system navigation bar.
Screenshots for reference, taken on Android Studio 3.3:
1. Layout with decorations missing
2. Press the 'Eye' icon, and select the option
3. The decorations (AppBar and system navigation bar) should now be visible

Android Studio Not Applying My Design Changes

I'm trying to change the theme of some activities by removing the action bar or changing the title of it. At first i was doing it using the AppTheme option in the design tab. However, after searching online i knew that i was doing it the wrong way, so i proceeded to the AndroidManifest file to do my changes manually. Unfortunately, it's not working either. Here's the code of my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.wonderheart">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<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:label="Sign Up"/>
<activity
android:name=".Login"
android:label="Log In"/>
<activity android:name=".drawable.welcome">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".UserProfile"
android:theme="#style/AppTheme.NoActionBar"/>
<activity android:name=".HomePage" >
</activity>
</application>
</manifest>
Here are some of my activities that I'm trying to change:
Action Bar Title Changed Successfully
Action Bar Title Didn't Change
I'm using Android Studio 3.0 Preview and Kotlin. I hope you can help because i can't progress on my work without fixing this design issue!
You can fix problem two way.
1. Use common one theme for all activities and use code for hiding and showing action bar in onCreate method
// Set up the toolbar
val toolbar = findViewById(R.id.toolbar) as Toolbar
setSupportActionBar(toolbar)
var actionBar = supportActionBar
//For hiding android actionbar
actionBar.hide();
//For showing android actionbar
//actionBar.show();
Use custom style for activity where you don’t want to show action bar in example below I did it for application level but you can use it your way with your any other activities
style.xml
<resources>
<style name = "AppTheme" parent = "android:Theme.Holo.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name = "NoActionBar" parent = "#android:style/Theme.Holo.Light">
<item name = "android:windowActionBar">false</item>
<item name = "android:windowNoTitle">true</item>
</style>
</resources>
AndroidManifest.xml
<application
android:allowBackup = "true"
android:icon = "#drawable/ic_launcher"
android:label = "#string/app_name"
android:theme = "#style/NoActionBar" <!--This is the important line-->
>
<activity

I want gradle to build only single class instead of the whole project

In Android Studio project I have a separate class where I experiment with java code. The class has method public static void main(String[] args){}
Right click on this class in project tree -> Run (Ctrl+Shift+F10)
And I noticed that gradle starts building the whole project. And it takes certain time but I want it to build and run only this one class. So it is as fast as possible.
How can I achieve this?
You can try modifying your AndroidManifest file to run only the class you want as a main-launcher class:
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="#drawable/my_icon"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme"
tools:replace="android:icon,android:theme">
<activity
android:name=".RUN_YOUR_ACTIVITY"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Questions regarding Camera2 class in Android Studio and API level to develop Flashlight App

I'm just starting to study Android Studio.
Trying to practice by creating custom flashlight application, but am having hard time doing so. There were many related questions here on S.O that I thought would be a perfect to take it as example. But those questions were asked few years ago and Its not working for me.
My problem is that when I try to use Camera object, Android Studio tells me that the class is deprecated, and that it should use camera2 class.
In the Android Studio website, it says to use CameraManager to work with camera. However, when I try to do it, it says "call requires API level 23 (current Min is 22).... " I'm testing the app with my phone which is a Galaxy S5, and in the options tab to choose the device or emulator. It shows my phone device is at API level 22.
Well. I'd think then that my phone is too old. But I have downloaded different FlshLight apps from Google Play store and my phone is able to run it just fine. Could anyone guide me to the right direction?
Here is my code.
import android.app.Activity;
import android.content.Context;
import android.graphics.Camera;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button button ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.mButton);
}
// When user press the button, turn the Flashlight On.
public void buttonPressed(View view){
CameraManager manager = (CameraManager)getSystemService(Context.CAMERA_SERVICE);
try {
String [] list = manager.getCameraIdList();
for(int i = 0; i < list.length; i ++){
manager.setTorchMode(list[i],true);
}
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.johnlee.myflash" >
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-sdk android:minSdkVersion="22"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<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>
</application>
</manifest>
You could use an if statement that check if the os version is below or above API level 23, and then use the appropriate camera.
if (android.os.Build.VERSION.SDK_INT >=android.os.Build.VERSION_CODES.THE_REQUIRE_VERSION) {
// only for a specific and newer versions
}
else{
//for deprecated versions
}

How do you get the hardware back button on VideoCastControllerActivity to work?

Once my app starts playing and VideoCastControllerActivity shows up, the hardware back button won't take it back to my activity. The actionbar back button works just fine. I don't see any errors on the logs or anything and looking at the VideoCastControllerActivity I didn't see anything obvious trying to capture the back button behaviour or anything. To start it I am calling startCastControllerActivity.
Here is the chunk of my manifest declaring it:
<activity
android:name="com.google.sample.castcompanionlibrary.cast.player.VideoCastControllerActivity"
android:label="#string/app_name"
android:launchMode="singleTask"
android:parentActivityName="com.mypackage.myactivity"
android:screenOrientation="portrait"
android:theme="#style/Theme.AppCompat" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mypackage.myactivity" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
Thanks.
The CastCompanionLibrary doesn't try to capture the back button at all, so it should work just fine. Please try the CastVideos-andrid (which uses that library) to confirm that.

Resources