PreferenceActivity Change In Android 6.0 (Android M, Android Marshmallow) - android

I have several activities that use custom layouts for a preference activity. The style is as follows.
public class SettingsActivity extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
//Configure toolsbar
//more custom stuff using "findViewById"
PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
setPreferenceScreen(root);
//Add preferences
}
The problem is that on Android 6.0, setContentView isn't working for me. findViewById returns null, where it returns the Toolbar on API 9-API22. Did this change? What is going on here?

Related

Android - FacebookSDK change AccountKitActivity title

I using the AccountKitActivity of the facebookAccountKitSdk.
Can I change the title of the AccountKitActivity? Currently, the title is "Enter your mobile number".
I've checked and find out this method, but don't know how to do it.
public AccountKitConfiguration.AccountKitConfigurationBuilder setUIManager(UIManager uiManager)
Thanks!
This is a shot in the dark because I don't have Android Studio in from of me right now. But because AccountKitActivity extends the AppCompatActivity, I'm guessing you would set the title in the same way?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_fancy_layout);
setTitle(R.string.my_fancy_layout_title);
}
Or if you're using tool bars...
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitle(R.string.your_title)

getSupportActionBar setSubtitle only shows after activity recreation

I have a generic
MyActivity extends AppCompatActivity
I don't override the toolbar with a custom xml defined toolbar, just use the generated one Android provides.
I can set the title via your normal
getSupportActionBar().setTitle("foo");
but setting the subtitle via
getSupportActionBar().setSubtitle("bar");
doesn't set it. It remains blank. I'm doing this onCreate()
(I feel I've done this many times before with no fail)
Although I've noticed if I visit another activity, then return, the subtitle would then show... not on orientation change, not on recreate() but only when I'm returning from an activity.
I'm experiencing this on 5.0 and 7.0
For the time being I'll likely define my own Toolbar and move forward since that seems where most people have solutions for this same problem.
Relevant code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_replenishment_list);
ButterKnife.bind(this);
MyApplication.getInstance().getComponent().inject(this);
setupUI();
}
private void setupUI() {
setupActionBar();
}
private void setupActionBar() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
//TODO: not working unless activity is recreated...
// explore custom xml defined toolbar
//actionBar.setTitle("different title than what is defined in manifest"); <-- this does work, but not this
actionBar.setSubtitle(UserUtil.getFormattedFirstNameLastName(userService.getUserFromJWT(), this));
}
}
I have put the below code in my onCreate() method.
ActionBar actionBar = getActionBar();
if (actionBar==null) {
System.out.println("TEST NULL");
} else {
System.out.println("TEST NOT NULL");
}
The result is null. When I add the toolbar first it works fine.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getActionBar();
actionBar.setSubtitle("TESTING");
Your getSupportActionBar or getActionBar will return null if you didn't set toolbar to it. You need to set the toolbar to your action bar before using getSupportActionBar or getActionBar.

I want to make tabs below custom action bar using tabhost on the activity where I used custom toolbar as action bar?

I am the android developer (freshers).
Here is my java code. where I have tried TabSpec.addTab(.......) but it gives me error of nullpointerexception. plz let me know what should I do? please provide me the code for tabhost with xml and java...thank you.
public class HomeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(null);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
Is this the error where I use supportactionbar methods?
please help me I want to use these tabs with the listview and searchview..
thank you.

Android TabActivity with Toolbar - setSupportActionBar() unknow

Is it possible to use setSupportActionBar() in an TabActivity?
Extending with AppCompatActivity is not possible...
public class TabHost extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabhost);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar); // this is unkown
getSupportActionBar().setDisplayHomeAsUpEnabled(true); //also
Do I have to switch from TabActivity to FragmentTabHost?
Thanks
no you can't. You have to extend AppCompatActivity, and you shouldn't use TabActivity in the first place. It was deprecated long time ago. You should use a solution based on a ViewPager and Fragments to achieve the same behavior

findViewById() returns null

There are a few questions already asking about this, but I'm not finding a solution. I have a LiveWallpaper that is using its own subclass of PreferenceFragment to specify preferences. The solution most often cited is to assure that setContentView() is called before findViewByID(). I am not calling setContentView() at all because I do not have a layout specified. This app originally implemented the preferences using the deprecated methods like PreferenceActivity.getPreferenceManager() and without using a layout and worked just fine. I am trying to bring the code up-to-date in using PreferenceFragment.
Am I required to have a layout and if so, what would I have when I don't really want one?
Or is there a another way to get/set the View?
public class SetPreferenceActivity extends Activity {
private CheckBox redCheckBox;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main); ???
redCheckBox = (CheckBox)findViewById(R.id.redCheckBox); // returns null
getFragmentManager().beginTransaction().replace(android.R.id.content, new LiveWallpaperPreferenceFragment()).commit();
}
}
You can create views dynamically :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
textView.setText("Hello");
setContentView(textView);//<----set the view !
}
}

Categories

Resources