hi I had created an android app which contains actionbar with 3 tabs.I had created it using a view pager and adapter.It works successfully . But Now I had created a new activity with popup window.I added my activity_main as my contentview. But it doesn't shows action bar tabs.I don't know why.I am new to android . So please help me, thanks in advance .
here is my code
MainActivity.java
public class MainActivity1 extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Featured", "Games", "Entertainment" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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) {
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
Here is Code in which I want to popup
public class Main extends FragmentActivity implements
ActionBar.TabListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hii);
Button b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog = new Dialog(Main.this);
// Include dialog.xml file
dialog.setContentView(R.layout.activity_main);
// Set dialog title
dialog.setTitle("Custom Dialog");
// set values for custom dialog components - text, image and button
dialog.show();
}
});
}
#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
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
The main problem is in your Dialog view. You have to create a dialog which is custom and have to add style with actionbar tab. If you use that style in your dialog box then all those actionbar tabs will come.
I would reccommend instead of using dialog in your b1.setOnclick method create a separate Dialog class which will extend the Dialog class.There you set the content view and make a proper layout with actionbar tabs same as what you have done for your activity.
and inflate the layout from that dialog class.Something like this:--
b1.setOnClickListener(new View.setOnClickListener){
new actionbarDialog(MainActivity.this).show();
}
and create a class of actionbarDialog which will extend Dialog class.
and in onCreate() method of that class write the logic of actionbar that you have written in MainActivity.You will get actionbar tab in Dialog.
Related
Screenshot of my app:
I want to change the blue underline to white programmatically, that is by using java code not using xml styles. Can anyone suggest please how to do it?
Here is my java code. I am able to change tab background.
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Music", "Video"};
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
//actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#DF0174")));
actionBar.setStackedBackgroundDrawable(new ColorDrawable(Color.parseColor("#DF3A01")));//to change the tab colors
// 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) {
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
Just use a filter
This will be applied to the region that isn't transparent
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).getBackground().setColorFilter(Color.BLUE, PorterDuff.Mode.MULTIPLY);
One line of code - no need to change states
Just add this attribute to your TabLayout in your xml file:
app:tabIndicatorColor="#color/yourColor"
i want to operation on back button in main fragment class. i am use swipe tab with help of fragment. so in as par activity class i know function of back button. but this is not help of me. this is my code. i already done on action bar home function. so i want to do same as back button
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Metals","Forex" };
static int i = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
i=0;
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
// actionBar.setHomeButtonEnabled(true);
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) {
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case android.R.id.home:
Log.d("back", "backkk");
i = 1;
Toast.makeText(getApplicationContext(), "back"+i,100).show();
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
i think you want to perform an opration when back key is pressed in Fragment
So Create a public method in fragment
and from Fragment activity class , override onBackkeypressed call that method
example
#Override
public void onBackPressed() {
DetailFragment fragment = (DetailFragment) getFragmentManager()
.findFragmentById(R.id.listfragment);
fragment.doSomthing()
}
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).
I want to change the tab colour in the ViewPager. I search but didn't find any good solution for it. I want to change the tab colour from default that is from black to white. Is their any way to do this programatically for i have to modify style.xml. I don't know how to do as i am new to android. plz somebody help
public class HomeActivity extends FragmentActivity implements ActionBar.TabListener
{
public ViewPager viewPager;
private PageAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Calculate EMI", "EMI Schedule"};
String TabFragmentSchedule;
public void setTabFragmentSchedule(String t){
TabFragmentSchedule = t;
}
public String getTabFragmentSchedule(){
return TabFragmentSchedule;
}
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#0F7BFF"));
actionBar.setBackgroundDrawable(colorDrawable);
mAdapter = new PageAdapter(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) {
}
});
}
//method that is called to swipe viewpage on button click from calculation fragment
public void switchToFragmentSchedule(){
// viewPager.setCurrentItem(1);
viewPager.setCurrentItem(1, false);
}
#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
}
}
You should style your theme in your xml files. You can find all the information for this task here.
If you need to change it programmatically, you can use
mActionBar.setBackgroundDrawable(new ColorDrawable(0xffffffff));
mActionBar.setDisplayShowTitleEnabled(false);
mActionBar.setDisplayShowTitleEnabled(true);
As a beginner, you might also find this styling tool very helpful.
Hi in my project i am using a actionbar with three tabs which are fragments. now i have a button in fragment C, when i click the button i need to swipe back to fragment B, also i want to carry my data from fragment C to B this is secondary, can some one point me in right direction how can i achieve this .
Below is my MainActivty which has all the fragments
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private String[] tabs = { "fragA", "fragB", ""fragC", " };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main)
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
// 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) {
// 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 tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
I think this question is a duplicate, take a look here Android ActionBar Tabs - Swipe code is almost the same: a viewpager, an actionbar and you need transiction between fragments to be done with a swipe gesture.
Hope it helped you out ;)