hello
could you please tell me why on landscape it is not acquiring equal width as in portrait mode ?? is there any way to provide equal width into tabs in portrait mode.
I do like this
landscape
potrait
here is my code ..in portrait it is taking equal width but on landscape it is not taking the equal width why ?
here is my code
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
ActionBar actionBar;
ViewPager viewPager;
PageAdapter pageAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager);
pageAdapter =new PageAdapter(getSupportFragmentManager());
viewPager.setAdapter(pageAdapter);
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar.newTab().setText("Tab1").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Tab2").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Tab3").setTabListener(this));
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int i, float v, int i1) {
}
#Override
public void onPageSelected(int i) {
actionBar.setSelectedNavigationItem(i);
}
#Override
public void onPageScrollStateChanged(int i) {
}
});
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
}
Adapter
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class PageAdapter extends FragmentPagerAdapter{
public PageAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
switch (i){
case 0:
return new Fragmentone();
case 1:
return new Fragmenttwo();
case 2:
return new FragmentThree();
default:
break;
}
return null;
}
#Override
public int getCount() {
return 3;
}
}
Related
I am new to Android, Designing an Android Application that contains "Tabs".
I have designed this:
But I want to design like this,
I think there must be way, as like of populating lists using Custom Adapters, But I am not finding any thing to do this, the tutorials that are available on Android Web are out of scope of my mind, because I am new..
My code:
Tabs.xml
Tabs.java
package com.example.medbleep;
import TabsManager.TabPagerAdapter;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
import android.widget.TextView;
public class Tabs extends FragmentActivity implements ActionBar.TabListener {
private ViewPager viewPager;
private TabPagerAdapter mAdapter;
private android.app.ActionBar actionBar;
private int numberOfTabs = 5;
// Tab titles
private int[] tabIcons = {R.drawable.home,R.drawable.home,R.drawable.home,R.drawable.home,R.drawable.home,R.drawable.home};
private String[] tabText = { "Home", "units", "conferences", "couses", "more" };
ImageView imageView;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);
imageView = (ImageView)findViewById(R.id.tab_icon);
textView = (TextView)findViewById(R.id.tab_title);
// Initialization
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setBackgroundColor(getResources().getColor(R.color.darkLoginBlue));
actionBar = getActionBar();
mAdapter = new TabPagerAdapter(getSupportFragmentManager(),
numberOfTabs);
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (int i = 0; i<5 ; i++) {
actionBar.addTab(actionBar.newTab().setText(tabText[i])
.setIcon(getResources().getDrawable(tabIcons[i]))
.setTabListener(this));
//actionBar.addTab(actionBar.newTab().setCustomView(R.layout.tab_layout).setText(tabText[i]).setIcon(tabIcons[i]).setTabListener(this));
//actionBar.setLogo(tabIcons[4]);
//actionBar.setIcon(tabIcons[i]);
//actionBar.setTitle(tabText[i]);
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction arg1) {
// TODO Auto-generated method stub
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
}
TabPagerAdapter.java
package TabsManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class TabPagerAdapter extends FragmentStatePagerAdapter{
int mNumOfTabs;
public TabPagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
HomeTabFragment homeTabFragment = new HomeTabFragment();
return homeTabFragment;
case 1:
UnitTabFragment unitTabFragment = new UnitTabFragment();
return unitTabFragment;
case 2:
ConferencesTabFragment conferencesTabFragment = new ConferencesTabFragment();
return conferencesTabFragment;
case 3:
CoursesTabFragment coursesTabFragment = new CoursesTabFragment();
return coursesTabFragment;
case 4:
MoreTabFragment moreTabFragment = new MoreTabFragment();
return moreTabFragment;
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
For making this, your have to create Custom Adaptor and set width and height According to your requirement of each tab.
Please Follow this:
http://mobikul.com/make-custom-tabs-icons-android/
I have viewpager with 5 scroll-able fragments, i would like to make
fragment 1 load by default
fragment 2 accessed by button clicked in fragment 1
then allow user to scroll to frag.4(without bottoms)
frag. 5 displayed when user click button in frag. 4
is it possible to do that?
thanks in advance
main activity::
package com.example.s_meter.all.horizental;
import com.example.siemenssmartmeter.R;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.RadioGroup;
public class SMeterAllHorrzental extends ActionBarActivity{
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.s_meter_all_h);
MyPageAdapter_all_h pageAdapter;
ViewPager pager;
pageAdapter = new MyPageAdapter_all_h(getSupportFragmentManager());
pager =(ViewPager)findViewById(R.id.viewpager);
final RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radiogroup);
pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
switch (position){
case 0:
radioGroup.check(R.id.radioButton);
break;
case 1:
radioGroup.check(R.id.radioButton2);
break;
case 2:
radioGroup.check(R.id.radioButton3);
break;
case 3:
radioGroup.check(R.id.radioButton4);
break;
case 4:
radioGroup.check(R.id.radioButton5);
break;
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
pager.setAdapter(pageAdapter);
}
}
adapter::::
package com.example.s_meter.all.horizental;
import com.example.horizental.tabs_s_old.*;
import com.example.s.meter.tabs.Location_tab;
import com.example.s.meter.tabs.New_meter;
import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MyPageAdapter_all_h extends FragmentPagerAdapter {
//private List<Fragment> fragments;
// public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
public MyPageAdapter_all_h(FragmentManager fm) {
super(fm);
// this.fragments = fragments;
}
#Override
public Fragment getItem(int position) {
// return this.fragments.get(position);
Fragment f=null;
if(position ==0){
f=new Location_tab();
}
else if(position ==1){
f=new Old_meter_frag1();
}
else if (position==2){
f=new Old_meter_frag2();
}
else if (position==3){
f=new Old_meter_frag3();
}
else if (position==4){
f=new New_meter();
}
return f;
}
#Override
public int getCount() {
return 5;
}
}
In my fragment class, I create an object of my Activity class:
MainActivity mainActivity;
and in my fragment's OnCreateView(), I initialize it:
mainActivity = new MainActivity();
However, when I debug my app, mainActivity is always null. Why is this?
My Activity class:
package com.example.brettrosen.atls;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
public ActionBar actionBar;
// Tab titles
private String[] tabs = {"Pre-arrival Plan", "Primary Survey", "Secondary Survey"};
#TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialization
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));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
updateTabTitles(0, 12);
updateTabTitles(1, 2);
updateTabTitles(2, 14);
}
public void updateTabTitles(int pageNum, int remaining) {
String text = tabs[pageNum] + " (" + remaining + ")";
System.out.println(actionBar);
actionBar.getTabAt(pageNum).setText(text);
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
};
I see your problem is that you want to access MainActivity from within a fragment that it hosts.
To do that, you could just call this:
getActivity();
And it will return your MainAcitvity (or any other Activity that is currently hosting your Fragment).
To access a method from it, you'd have to cast the return of that method.
MainActivity yourMain = (MainActivity) getActivity();
yourMain.method1();
Remember to do this inside of your fragment.
I am trying to learn Swipe tabs in android fragments using 3 tabs by referring to a tutorial. I am getting null pointer exception in the line:
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
since the the following line is returning null
actionBar=getActionBar();
Here is the full code.
MainActivity.java
package com.example.pricababa.learningswipetabs;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
ActionBar actionBar;
ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager=getSupportFragmentManager();
viewPager= (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new MyAdapter(fragmentManager));
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int i, float v, int i1) {
}
#Override
public void onPageSelected(int i) {
actionBar.setSelectedNavigationItem(i);
}
#Override
public void onPageScrollStateChanged(int i) {
}
});
actionBar=getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1=actionBar.newTab();
tab1.setText("Tab 1");
tab1.setTabListener(this);
ActionBar.Tab tab2=actionBar.newTab();
tab1.setText("Tab 2");
tab1.setTabListener(this);
ActionBar.Tab tab3=actionBar.newTab();
tab1.setText("Tab 3");
tab1.setTabListener(this);
actionBar.addTab(tab1);
actionBar.addTab(tab2);
actionBar.addTab(tab3);
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
}
class MyAdapter extends FragmentPagerAdapter
{
public MyAdapter(FragmentManager fm){
super(fm);
}
#Override
public Fragment getItem(int i) {
Fragment fragment=null;
if(i==0)
fragment=new FragmentA();
else if(i==1)
fragment=new FragmentB();
else if(i==2)
fragment=new FragmentC();
return fragment;
}
#Override
public int getCount() {
return 3;
}
}
I tried to apply the following fixes from similar SO threads:
1) getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
2) using getSupportActionBar()
That didn't help. It would be very helpful if somebody can explain the reason behind this and state a possible fix.
Thanks!
EDIT: I am following this tutorial:
See here
I have an app with 4 actionbar's tabs. Although I imported android.support.v7.app. ActionBar library, it's only displayed in API 16 device but not in API 10 device. What's the solution here?
Main Activity:
import android.app.ActionBar;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.app.ActionBar.TabListener;
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends ActionBarActivity {
public android.support.v7.app.ActionBar actionbar;
public ViewPager viewpager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionbar = getSupportActionBar();
actionbar.setDisplayShowHomeEnabled(false);
actionbar.setDisplayShowTitleEnabled(false);
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
viewpager = (ViewPager) findViewById(R.id.pager);
FragmentManager fm = getSupportFragmentManager();
addPageChangeListener();
MyFragmentPagerAdapter fragmentPagerAdapter = new MyFragmentPagerAdapter(fm);
viewpager.setAdapter(fragmentPagerAdapter);
addActionBarListener();
}
public void addPageChangeListener() {
ViewPager.OnPageChangeListener pageChangeListener = new OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionbar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
};
viewpager.setOnPageChangeListener(pageChangeListener);
}
public void addActionBarListener() {
android.support.v7.app.ActionBar.TabListener listener = new TabListener() {
#Override
public void onTabUnselected(
android.support.v7.app.ActionBar.Tab arg0,
android.support.v4.app.FragmentTransaction arg1) {
}
#Override
public void onTabSelected(android.support.v7.app.ActionBar.Tab tab,
android.support.v4.app.FragmentTransaction arg1) {
viewpager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabReselected(
android.support.v7.app.ActionBar.Tab arg0,
android.support.v4.app.FragmentTransaction arg1) {
}
};
android.support.v7.app.ActionBar.Tab tab = actionbar.newTab()
.setTabListener(listener);
tab.setCustomView(R.layout.search);
actionbar.addTab(tab);
tab = actionbar.newTab().setTabListener(listener);
tab.setCustomView(R.layout.filter);
actionbar.addTab(tab);
tab = actionbar.newTab().setTabListener(listener);
tab.setCustomView(R.layout.favorite);
actionbar.addTab(tab);
tab = actionbar.newTab().setTabListener(listener);
tab.setCustomView(R.layout.info);
actionbar.addTab(tab);
}
}
MyPagerFragmentAdapter:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
public final int PAGE_COUNT = 4;
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
// TODO Auto-generated method stub
Bundle data = new Bundle();
switch (i) {
case 0:
SearchFragment searchFragment = new SearchFragment();
return searchFragment;
case 1:
FilterFragment filterFragment = new FilterFragment();
return filterFragment;
case 2:
FavoriteFragment favoriteFragment = new FavoriteFragment();
return favoriteFragment;
case 3:
InfoFragment infoFragment = new InfoFragment();
return infoFragment;
}
return null;
}
#Override
public int getCount() {
return PAGE_COUNT;
}
}
Main Activity Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<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" />
Try with replacing import android.app.ActionBar;
with this one import android.support.v7.app.ActionBar;.
Also check your application theme should be android:theme="#style/Theme.AppCompat" in your AndroidManifest.xml file
Refer below link for more details...
http://wptrafficanalyzer.in/blog/swipable-navigation-tabs-using-actionbarcompat-library/
Finally, I solved my problems. Just because of my fault while customing the view for tabs. I extended wrong theme:
<style name="ActionBarTabStyle" parent="#android:Widget.ActionBar.TabView">
instead of:
<style name="ActionBarTabStyle" parent="#style/Widget.AppCompat.ActionBar.TabView">
Thanks everyone for supporting me!