Set home up button - java

I want to set the Home Up Button in my PreferenceScreen so i tried with this code
if (android.os.Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB){
getActionBar().setDisplayHomeAsUpEnabled(true);
}
My App require minSdkVersion10 (GB) so i check the android version with Build.VERSION.SDK_INT. The problem is that eclipse give me an error on getActionBarMethod() because "Call requires API level 11 (current min is 10): android.preference.PreferenceActivity#getActionBar" How can i solve?

add this to the your method above where you use this code,
In this case i used onCreate method.
#SuppressLint("NewApi") or #TargetApi(HONEYCOMB)
public void onCreate(Bundle savedInstance){
if (android.os.Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB){
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}

For you to use actionbar on gingerbread your best solution would be to use ActionBarSherlock library by Jake Wharton

Related

How to setTheme to dark on one Button

Guys are possible setTheme from ThemeUtils with one button ?? look at screenshot just one button 'invert' to set Light to Dark and Dark to Light ? if possible how to do it ?? pls help me
my code :
case R.id.Invert:
ThemeUtils.setTheme(this, "dark");
return true;
look this image : https://drive.google.com/file/d/0B5SpWSpauPDuSGtWREgybnB6aEk/view?usp=drivesdk
You have to call the setTheme method on the Activity before calling SetContentView
Therefore to change theme of an Activity that is already open, you would need to restart it.
For example:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(YOUR_THEME_FROM_SHARED_PREFS);
setContentView(...)
}
and
case R.id.Invert:
// Set theme in shared Prefs here
this.recreate(); // restart the activity
return true;
Yes, sure. Define theme, you want to set in style folder. And instead of your string write code R.style.YourOwnTheme

onCreate android os bundle is never used

I'm a noob in java android but I get this error when I hover onCreate method onCreate(android.os.Bundle) is never used. Should I worry about this error?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionbar.addTab(actionbar.newTab().setText("Feed").setTabListener(this));
actionbar.addTab(actionbar.newTab().setText("Browse").setTabListener(this));
actionbar.addTab(actionbar.newTab().setText("Settings").setTabListener(this));
}
Thanks
thats not an error its a warning. the bundle is used for putting information in when the app changes state ie. orientation so you can continue where the user left off.
there is no harm in not using it however the activity will be shown just as it would if you were to launch it from the launcher.
Got it! I just changed extends FragmentActivity into extends Activity and imported android.app.Activity.

Are Activity/Fragment Transitions compatible with pre-Lollipop devices?

I'm trying to make an Activity Transition using Shared Elements on a pre-Lollipop device (4.x). Is it possible? So far, I'm trying this:
public class RewardDetail extends ActionBarActivity {
#Override
public void onCreate(final Bundle savedInstanceState) {
...
ViewCompat.setTransitionName(imageView, TRANSITION_NAME);
}
...
public static void launch(ActionBarActivity activity, View transitionView, WelcomeReward detailData) {
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(activity, transitionView, TRANSITION_NAME);
Intent intent = new Intent(activity, RewardDetail.class);
intent.putExtra(PARAM_DATA, detailData);
ActivityCompat.startActivity(activity, intent, options.toBundle());
}
}
called by:
#Override
public void onClick(final View v) {
int position = recyclerView.getChildPosition(v);
WelcomeReward welcomeReward = data.get(position);
RewardDetail.launch(WelcomeRewardActivity.this, v.findViewById(R.id.reward_view), welcomeReward);
}
But it results in a "regular" transition (no shared element). Any ideas?
EDIT
According to this video, it could be done:
https://www.youtube.com/watch?v=RhiPJByIMrM&index=8&list=WL
Is there a library already implementing this for pre Lollipop ?
No, Activity/Fragment Transitions are not possible on pre-Lollipop devices. According to the documentation:
Start an activity with additional launch information, if able.
In Android 4.1+ additional options were introduced to allow for more control on activity launch animations. Applications can use this method along with ActivityOptionsCompat to use these animations when available. When run on versions of the platform where this feature does not exist the activity will be launched normally.
See also George Mount's answer to this StackOverflow question.
You can check out this library for activity and fragment transitions for pre lollipop devices
dependencies {
compile 'com.albinmathew:PreLollipopTransition:1.1.2'
}
https://github.com/albinmathew/PreLollipopTransition
Although the fancy Lollipop Activity/Fragment transitions are not available pre-Lollipop (without the use of a 3rd party library), you can still override the animation used to transition between activities.
Just before/after you start invoke startActivity() you can make a call to [Activity.overridePendingTransition](http://developer.android.com/reference/android/app/Activity.html#overridePendingTransition(int, int)). When you leave your activity, call the same method.
Similarly you can use ActivityOptionsCompat to define a custom animation to use during a transition.
ActivityOptionsCompat opts =
ActivityOptionsCompat.makeCustomAnimation(getActivity(), R.anim.in, R.anim.out);
startActivity(intent, opts.toBundle());
There is a support library, but it does not support (all) transitions on Android versions below 5.0. There are however some alternatives:
Unofficial Compatibility libraries https://github.com/andkulikov/transitions-everywhere
https://github.com/takahirom/PreLollipopTransition
https://github.com/lgvalle/Material-Animations
Android KitKat http://www.doubleencore.com/2013/11/new-transitions-framework/ and a
sample found in your SDK samples folder.
Posted earlier to a duplicate of this question here: https://stackoverflow.com/a/27344471/1683141

Android action bar home

Simple enough,
At the top left hand corner of the action bar sits the default icon for the application. In most apps, it is clicked and returns you to the homepage. I'm working with 2 devices, a 3.2 and a 2.3.3 and I am trying to implement the action bar on the 3.2 without affecting the other.
I imagine its implemented like this:
case android.R.id.home:
Intent intent = new Intent(this, ActOnThisActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
One last thing is to set the flag
getActionBar().setHomeButtonEnabled(true);
This should work but as anyone familiar with android will know, this cant be run on an API of 11 or below. So it will run on 3.2+ but not the 2.3.3. Is there a way to specify this method to only work on api11 and above?
NOTE that #TargetApi(11) annotation might work but I've had some weird errors with it.
Is there a way to specify this method to only work on api11 and above?
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setHomeButtonEnabled(true);
}
Taking a look at ActionBarCompat from the sdk samples will give you fair idea of how to do it.
The following code in the ActionBarHelper class(from the sample) decides the instance for different versions.
public static ActionBarHelper createInstance(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
return new ActionBarHelperICS(activity);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
return new ActionBarHelperHoneycomb(activity);
} else {
return new ActionBarHelperBase(activity);
}
}
Have a look at ActionbarSherlock - I use it in a few apps and it works flawlessly. Mimics the Action Bar perfectly in Android versions below Honeycomb (3.0).
http://actionbarsherlock.com/
Main API for nearly all interaction with the action bar. This is the exact API getSupportActionBar() exposes.

Samsung Galaxy Tab 7.0 restarts app on return from camera intent

My code works as expected on smaller and bigger devices (Motorola Xoom, Samsung Galaxy Player 4.0, Kyocera Digno), but for the Samsung Galaxy Tab 7.0, after launching an ACTION_IMAGE_CAPTURE intent and taking a picture, when the app returns onDestroy() is called, followed by onCreate(), then onActivityResult() is called, and finally, onDestroy() and onCreate() are called again, which is of course undesireable - only onActivityResult() should be called.
Possibles clues:
The Galaxy Tab 7.0 has a screen size that is explicity not supported in the manifest file (and this is the only device I have tested with an unsupported screen size), so the user may choose scretch-to-fit or zoom-to-fit. Both UIs have the same (bad) behavior.
The camera activity seems to switch orientation when previewing a picture. My app only supports portrait mode (edit: on smaller screens - on non-xlarge screens, it supports orientation changes). Maybe the orientation change is destroying my activity, somehow.
I have tried launching and returning from a different intent (email intent), and my app is not destroyed and re-created in that case.
Let me know if more information or a code sample is needed.
Edit: the issue has been narrowed down to the orientation change. As per Karthik's answer, setting android:configChanges="orientation" fixes the issue. The only problem is, my app supports orientation changes on xlarge screens. This setting breaks this functionality on those devices. I've tried using android:configChanges="#string/config_changes" and providing a different string depending on the screen size, but now I'm getting an "Installation error: INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION". According to this, Android Activity, how to override manifest's android:configChanges with Java code?, there is no way to set it programmatically. Is my only option left to handle all orientation changes in my app manually?
You are right, it is due to the orientation change. Camera works in Landscape mode in Galaxy Tab.
So you can add android:configChanges="orientation" to your <activity> tag in manifiest file.
This would solve your problem. onDestroy() and onCreate() will not be called upon return from camera.
I discovered that the reason my app restarts is because the device runs out of memory when starting the camera app and the OS recycled my main Activity. That wouldn't be a problem, except I had a Fragment-based layout and some Fragment initialization was being done in onCreate(), regardless of the savedInstanceState. This caused the automatic Fragment restoration to be discarded and made the app look like it was restarting from the beginning when in fact it was just trying to be restored.
Ex:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// add main menu -- WRONG!
MainMenuFragment mainMenu = new MainMenuFragment();
FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
ft.add(R.id.contents, mainMenu);
ft.commit();
if (savedInstanceState != null) {
// <restore state>
}
else {
// <initialize stuff>
}
}
To fix it, I skipped the Fragment initialization when savedInstanceState was not null and made sure that the state was being saved correctly in onSaveInstanceState() and restored in onCreate(), and implemented the normal handling for onActivityResult().
Ex:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
// <restore state>
}
else {
// <initialize stuff>
// add main menu -- CORRECT!
MainMenuFragment mainMenu = new MainMenuFragment();
FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
ft.add(R.id.contents, mainMenu);
ft.commit();
}
}

Categories

Resources