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.
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?
i am developing android application. i extend my class to Tab Activity.
while i am using getActionBar() or getSupportActionBar(), the activity stops working. when i extend my activity to Activity or ActionBarActivity getTabHost() will not work. Any one pls help me. thanks in advance.
Here is my coding.
public class CommonTabActivity extends TabActivity
implements OnTabChangeListener {
TabHost tabHost;
Bundle response = new Bundle();
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_common_tab);
....
tabHost = getTabHost();
....
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
}
you can extend your activity to AppCompatActivity (ActionbarActivity is deprecated now), use tablayout from android design library to show tabs and associate viewpager to tablayout to switch between fragments corresponding to each tab.
your activity layout would be something like this.
android.support.v7.widget.Toolbar
android.support.design.widget.TabLayout
android.support.v4.view.ViewPager
There are plenty of examples available to implement this.
http://www.truiton.com/2015/06/android-tabs-example-fragments-viewpager/
I'm using SupportLibrary. Here is my MainActivity code:
public class MainActivity extends ActionBarActivity implements android.support.v7.app.ActionBar.TabListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
android.support.v7.app.ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
android.support.v7.app.ActionBar.Tab tab = bar.newTab();
tab.setText("Tab 1");
tab.setTabListener(this);
bar.addTab(tab);
}
How can I display some activity in the tab?
Thanks.
How can I display some activity in the tab?
You can't. First, that technique was deprecated three years ago. Second, ActionBarActivity does not extend the deprecated ActivityGroup.
You are welcome to use fragments for your tabs, though.
I was recommended to extend my Activity class from ActionBarActivity
Here is the previous code:
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
I wrote new application and followed advice.
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends ActionBarActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar =getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
If I use ACtionBarActivity instead of Activity, I get the following error on the phone, when I try to run it:
The method getSupportActionBar() is undefined for the type TaskActivity
Your class needs to extend from ActionBarActivity, rather than a plain Activity in order to use the getSupport*() methods.
Update [2015/04/23]: With the release of Android Support Library 22.1, you should now extend AppCompatActivity. Also, you no longer have to extend ActionBarActivity or AppCompatActivity, as you can now incorporate an AppCompatDelegate instance in any activity.
Here is another solution you could have used. It is working in my app.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
android.support.v7.app.ActionBar actionBar =getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.activity_main)
Then you can get rid of that import for the one line ActionBar use.
If you are already extending from ActionBarActivity and you are trying to get the action bar from a fragment:
ActionBar mActionBar = (ActionBarActivity)getActivity()).getSupportActionBar();
Here is the answer of my question.
I've asked that again with some remarks.
How to add support libraries?
If you are extending from an AppCompatActivity and are trying to get the ActionBar from the Fragment, you can do this:
ActionBar mActionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
You have to change the extends Activity to extends
AppCompactActivity then try set and getSupportActionBar()
Can you set the ActionBar before you set the Contient View? This order would be better:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar =getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}