no titlebar in fragment activity for android - android

I want to hide title bar from swipe tab fragment activity , but when i write no title bar theme in manifest and run the app it have errors and don`t do anything , what should i do ? here is code:
public class Fragment1 extends FragmentActivity implements ActionBar.TabListener {
ActionBar actionbar;
ViewPager viewpager;
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.fragment);
viewpager= (ViewPager) findViewById(R.id.pager);
viewpager.setAdapter(new Myadapter(getSupportFragmentManager()));
viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
actionbar.setSelectedNavigationItem(arg0);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});;
actionbar=getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1=actionbar.newTab();
tab1.setText("All Stuffs");
tab1.setTabListener(this);
ActionBar.Tab tab2=actionbar.newTab();
tab2.setText("Top Stuffs");
tab2.setTabListener(this);
ActionBar.Tab tab3=actionbar.newTab();
tab3.setText("New Stuffs");
tab3.setTabListener(this);
actionbar.addTab(tab1);
actionbar.addTab(tab2);
actionbar.addTab(tab3);
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
viewpager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
class Myadapter extends FragmentPagerAdapter
{
public Myadapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
#Override
public android.support.v4.app.Fragment getItem(int arg0) {
// TODO Auto-generated method stub
android.support.v4.app.Fragment fragment=null;
if(arg0==0)
{
fragment=new FragmentA();
}
if(arg0==1)
{
fragment=new FragmentB();
}
if(arg0==2)
{
fragment=new FragmentC();
}
return fragment;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
}
and it is my manifest code:
activity android:name=".FragmentA"
android:theme="#android:style/Theme.NoTitleBar"

In onCreate method try:
getSupportActionBar().hide();
It will hide title bar

First of all please check your manifest:
activity android:name=".FragmentA"
android:theme="#android:style/Theme.NoTitleBar"
Your activity name is Fragment1. So this line is pointless. You cannot give theme to fragment is xml.
Before setcontentview method place this line:
requestWindowFeature(Window.FEATURE_NO_TITLE);
This will hide the title bar.

Related

how can I change the color of the tabBar in tab fragment (Viewpager)

I tried many times but it's not OK.
I try to change actionBar in MainActivity but the TabBar doesn't change, so I don't no how to change it.
This is my code :
MainActivivty
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private ActionBar actionBar;
private TabsPagerAdapter mAdapter;
private String[] tabs = { "Bài Hát", "Yêu Thích"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar ac = getActionBar();
ac.setBackgroundDrawable(new ColorDrawable(Color.rgb(72, 209, 204)));
//ac.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#E64260")));
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
if (position == 1) {
TabYeuThich frag = (TabYeuThich) mAdapter
.getFragmentTabYeuThich(1);
frag.resetPage();
} else {
TabBaiHat frag = (TabBaiHat) mAdapter
.getFragmentTabBaiHat(0);
frag.resetPage();
}
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
This is class : TabsPagerAdapter
public class TabsPagerAdapter extends FragmentPagerAdapter {
FragmentManager fm;
TabYeuThich fragYeuThich;
TabBaiHat fragBaiHat;
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
this.fm = fm;
}
#Override
public Fragment getItem(int index) {
// TODO Auto-generated method stub
switch (index) {
case 0:
fragBaiHat = new TabBaiHat();
return fragBaiHat;
case 1:
fragYeuThich = new TabYeuThich();
return fragYeuThich;
}
return null;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
public Fragment getFragmentTabYeuThich(int post) {
return fragYeuThich;
}
public Fragment getFragmentTabBaiHat(int post) {
return fragBaiHat;
}
Please help me ! Thanks all !
Useally I set up the action bar from themes.xml
I recommend you follow up the both tutorials
developer android
action bar explanation

Android - Swiping without Tabs

NoteActivity Code:
public class NoteActivity extends FragmentActivity implements ActionBar.TabListener
{
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = {"Note", "Note Info"};
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note);
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
TabsPageAdapter.Java
public class TabsPagerAdapter extends FragmentPagerAdapter
{
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index)
{
case 0:
return new NoteFragment();
case 1:
return new NoteInfoFragment();
}
return null;
}
#Override
public int getCount()
{
// get item count - equal to number of tabs
return 2;
}
}
So basically, I used an example of Tabs View and got the Tabs working and it looks like this:
What would I have to do to make it just swipeable without the Tabs showing. An example is like Snapchat. It definitely uses the Swipe view control but the tabs are hidden. Can someone please show me how to get this done?
Since you already have a ViewPager in your code, all you have to do is remove the code that creates the ActionBar tabs (under the comment // Adding tabs), as well as the code that synchronizes the tab selection with the current page (start with the ViewPager.OnPageChangeListener and the ActionBar.TabListener callbacks and see if anything breaks).

ActionBar tabs not working with android:theme="#android:style/Theme.Light.NoTitleBar"

I want my application with no title bar, but there is an activity which has two swipe tabs given below
public class APlus_Post extends FragmentActivity implements TabListener {
ViewPager viewPager;
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.post);
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
actionBar.setSelectedNavigationItem(arg0);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
ActionBar.Tab tab1 = actionBar.newTab(); //First Tab
tab1.setText("QAs Wall");
tab1.setTabListener(this);
ActionBar.Tab tab2 = actionBar.newTab(); //Second Tab
tab2.setText("Track Companies");
tab2.setTabListener(this);
actionBar.addTab(tab1);
actionBar.addTab(tab2);
}
#Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
}
#Override
public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
viewPager.setCurrentItem(arg0.getPosition());
}
#Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
}
}
class MyAdapter extends FragmentPagerAdapter{
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int arg0) {
Fragment fragment=null;
if(arg0==0){
fragment=new APlus_PostFragment1();
}
if(arg0==1){
fragment=new APlus_PostFragment2();
}
return fragment;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
}
with layout associated post.xml given below
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Whenever i run these above code with android:theme="#android:style/Theme.Holo.Light" (in AndroidMenifesto.xml) it works fine, but when i use android:theme="#android:style/Theme.Light.NoTitleBar" it doesn't work.
I want those swipe tabs on action bar but not the title bar in any activity of my application.
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);

App crashes when setting the current item in tab selected method

I have got a problem, My app crashes when i try to use the set Current Item(arg0.get Position()) in on Tab selected method while implement a the functionality that i click on tabs the fragments below should also change with it..... it works the other way around that is when i swipe, Tabs changes with it. Any kind of help will be appreciable.. thanks in advance. the code is given below:
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class AboutUs extends FragmentActivity implements TabListener {
private ActionBar actionBar;
ViewPager viewPager;
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.aboutus);
intialization();
}
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void intialization() {
// TODO Auto-generated method stub
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1 = actionBar.newTab();
tab1.setText("MUMTAZ ANWAR");
tab1.setTabListener(this);
ActionBar.Tab tab2 = actionBar.newTab();
tab2.setText("CREDIENTIALS");
tab2.setTabListener(this);
actionBar.addTab(tab1);
actionBar.addTab(tab2);
viewPager = (ViewPager) findViewById(R.id.pagger);
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
actionBar.setSelectedNavigationItem(arg0);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
//Log.d("HAMZA", "onTabReselected at " + "position" + tab.getPosition()
// + "name" + tab.getText());
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
//Log.d("HAMZA", "onTabselected at " + "position" + tab.getPosition()
// + "name" + tab.getText());
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
//Log.d("HAMZA", "onTabUnselected at " + "position" + tab.getPosition()
// + "name" + tab.getText());
}
}
class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
#Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
Fragment fragment = null;
if (arg0 == 0) {
fragment = new BlankFragment();
}
if (arg0 == 1) {
fragment = new BlankFragmenta();
}
return fragment;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
}
I can't find fragment transaction in your code
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment); // pass your fragment as second parameter
transaction.addToBackStack(null);// it is optional if you want to put it in stack
// Commit the transaction
transaction.commit();
how you replace fragment . it is missing

Unable to start activity component info (nullpointer)

im trying to make a basic ui in android with tabs. however i keep getting a nullpointer exception whenever i try to run it. the error originates from initializing viewpager. (i dont know what else to type and i keep receiving an error whenever i try to post.)
public class MainActivity extends FragmentActivity implements TabListener {
ActionBar actionBar;
ViewPager viewPager;
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.activity_main);
viewPager=(ViewPager) viewPager.findViewById(R.id.tabs);
viewPager.setAdapter(new adapter(getSupportFragmentManager()));
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab stocks = actionBar.newTab();
stocks.setText("Stocks");
stocks.setTabListener(this);
ActionBar.Tab market = actionBar.newTab();
market.setText("Market");
market.setTabListener(this);
ActionBar.Tab portfolio = actionBar.newTab();
portfolio.setText("Portfolio");
portfolio.setTabListener(this);
actionBar.addTab(stocks);
actionBar.addTab(market);
actionBar.addTab(portfolio);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mainsearch, menu);
return true;
}
#Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
class adapter extends FragmentPagerAdapter {
public adapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
public Fragment getItem(int arg0) {
Fragment fragment = null;
if(arg0==0) {
fragment = new StocksFragment();
}
if (arg0==1){
fragment = new MarketFragment();
}
if (arg0==2){
fragment = new PortfolioFragment();
}
return fragment;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
}
}
Of course this line will give a NPE
viewPager=(ViewPager) viewPager.findViewById(R.id.tabs);
you are calling a method on an object that you are trying to initialize (calling findViewById() on null viewPager)
If tabs is in activity_main.xml then just remove viewPager
viewPager=(ViewPager) findViewById(R.id.tabs);
findViewById is a method of the activity, not of the view. So it should be:
viewPager=(ViewPager) this.findViewById(R.id.tabs);

Categories

Resources