Use Actionbarsherlock in Program - android

i use "Actionbarsherlock" in my project
First i import "Actionbarsherlock" in my project then delete folder "libs"
But my question is how can I change the code below؟
mainactivity
package info.androidhive.tabsswipe;
import info.androidhive.tabsswipe.adapter.TabsPagerAdapter;
import android.annotation.SuppressLint;
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;
#SuppressLint("NewApi")
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "water"," time" };
#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);
// 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) {
}}
MoviesFragment
package info.androidhive.tabsswipe;
import info.androidhive.tabsswipe.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MoviesFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_movies,container,false);
return rootView;
}
}
GamesFragment
package info.androidhive.tabsswipe;
import info.androidhive.tabsswipe.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class GamesFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_games, container,false);
return rootView;
}}
TabsPagerAdapte
package info.androidhive.tabsswipe;
import info.androidhive.tabsswipe.GamesFragment;
import info.androidhive.tabsswipe.MoviesFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
// Games fragment activity
return new GamesFragment();
case 1:
// Movies fragment activity
return new MoviesFragment();
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 2;
}}
Please help me to solve this problem

bro remove your email adress from the question.Now coming to your question your actionbar should be an object of supportActionbar.Change the getActionBar() to getSupportActionBar()l.And secondly,your main activity should extend sherlockActivity.Lastly,change the importation from the android actionbar to the sherlock actionbar

Related

Android: How to REPLACE or OVERLAY fragments when a FragmentPagerAdapter is used

I have an app with three tabs which I control with an FragmentPagerAdapter. Now I want to jump between those tabs when I click on a TextView of one fragment (the fragment with the TextView should be replaced with the destination fragment). And when i click on a Button another Fragment should be overlayed above this fragment with the Button.
I know that those are two different things but I also know that I have to use FragmentTransaction but I can't figure out how to do it.
Could you please help me?
Here is my MainActivy.java:
package letscho.workout;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
ViewPager viewPager;
TabLayout tabLayout;
FragmentManager fragmanager;
FragmentTransaction fragtrans;
Fragment1 fragment1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = (ViewPager) findViewById(R.id.viewpager);
ViewPagerAdapter1 viewPagerAdapter1 = new ViewPagerAdapter1(getSupportFragmentManager());
viewPager.setAdapter(viewPagerAdapter1);
tabLayout = (TabLayout) findViewById(R.id.tablayout);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
public static void changefragmentOnBtnClick(){
fragmanager = getFragmentManager();
fragtrans = fragmanager.beginTransaction();
fragtrans.replace(R.id.viewpager, getActivity());
fragtrans.commit();
}
}
And the ViewPager:
package letscho.workout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class ViewPagerAdapter1 extends FragmentPagerAdapter {
String [] tabtitlearray = {"Übungen", "Push", "Pull"};
public ViewPagerAdapter1 (FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0: return new Fragment1();
case 1: return new Fragment2();
case 2: return new Fragment3();
}
return null;
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
return tabtitlearray[position];
}
}
And the Fragment where all should start:
package letscho.workout;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Fragment2 extends Fragment {
View contentView2;
TextView textView;
Button btn1;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
contentView2 = inflater.inflate(R.layout.layout2_push, null);
return contentView2;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
textView = (TextView)contentView2.findViewById(R.id.push_text_eins);
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.changefragmentOnBtnClick();
}
});
btn1 = (Button) contentView2.findViewById(R.id.btn_timer1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
#Override
public void onStop() {
super.onStop();
}
}

What is ActionBar.setHomeButtonEnabled(boolean)

I want to create a viewpager which contains 2 tabs, I have 3 layout:
This is activity_chart.xml that contains paper:
<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">
</android.support.v4.view.ViewPager>
This is AccountsActivityChart.java:
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 com.rastari.salar.mymetarialbank.R;
import com.rastari.salar.mymetarialbank.adapter.TabsPagerAdapter;
/**
* Created by Salar on 9/5/2015.
*/
public class AccountsActivityChart extends FragmentActivity implements ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Accounts Activity", "Chart Activity" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chart);
// 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);
// 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(ActionBar.Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
}
this is TabsPagerAdapter.java class:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
/**
* Created by Salar on 26/4/2015.
*/
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
// Accounts Activity fragment activity
return new AccountsActivityFragment();
case 1:
// Chart Activity activity
return new ChartActivityFragment();
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 2;
}
}
this is AccountsActivityFragment.java:
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.rastari.salar.mymetarialbank.R;
/**
* Created by Salar on 25/4/2015.
*/
public class AccountsActivityFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_accounts_activity, container, false);
return rootView;
}
}
this is ChartActivityFragment.java:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.rastari.salar.mymetarialbank.R;
/**
* Created by Salar on 9/5/2015.
*/
public class ChartActivityFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_chart_activity, container, false);
return rootView;
}
}
this is manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rastari.salar.mymetarialbank" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/MyMetarialBank">
<activity
android:name=".activity.Login"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activity.MainActivity"/>
<activity android:name=".activity.AccountsActivityChart"/>
</application>
</manifest>
When I run the app I get this Error:
Attempt to invoke virtual method 'void
android.app.ActionBar.setHomeButtonEnabled(boolean)' on a null object
reference
How can I fix this?
I think you have applied the theme which is having no action bar. This is why when you are getting the actionbar instance, it will be null and you are calling the setHomeButtonEnabled on on the null actionbar instance. That is why you are getting the error.
try actionBar = getSupportActionBar();
instead of actionBar = getActionBar();

When adding a fragment Tabs on drawerOption

I have in my MainActivity one drawerOption. Where you can select multiple Fragment.
public class MainActivity extends ActionBarActivity {
private ListView drawerList;
private String[] drawerOptions;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
private Fragment[] fragments = new Fragment[]{
new MainFragment(),
new AcordesFragment(),
new AfinadorFragment(),
new AboutFragment(),
new ExitFragment(),
};
I would like to pray to select a Fragmet, this contains several tabs.
The source of my code fragment is as follows:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
public class AcordesFragment extends Fragment implements ActionBarActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionbar = getSupportActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
Toast.makeText(getApplicationContext(), tab.getText(),Toast.LENGTH_SHORT).show();
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
};
for (int i = 0; i < 2; i++){
actionbar.addTab(
actionbar.newTab()
.setText("Tab" +i)
.setTabListener(tabListener)
);
}
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_acordes, container, false);
}
}
But it does not work the following line: public class AcordesFragment extends Fragment implements ActionBarActivity
Thank you very much for your help. I'm learning a lot
man fragment can not implements a ActionBarActivity .Only,Activity can implements ActionBarActivity.
If, you want to add a tabs in Fagment you have to use TabSlidingTabLayout.
This is the xml part .
You Have to download this library.
Download The Zip.
developers.google.com/SlidingTabLayout

Add a dynamic listview to a fragment

In my app I need the possibility to create a dynamic listview populated by elements extracted from the db.
This in itself wouldn't be a big problem (there are a lot of tutorials about it, like this one), problem is, my activity is divided in tabs and, therefore, I have a Fragment for each tab (the code it's basically this).
There are lots of methods that I can't use in a Fragmentand I'm honestly pretty stuck.
How can I implement a dynamic layout solution inside a fragment?
Edit: code for clarity:
Fragment:
package it.sii.mywaiter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class AllerFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.aller_layout, container, false);
return rootView;
}
}
Activity:
package it.sii.mywaiter;
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.*;
import android.app.ActionBar;
public class UserPrefActivity extends FragmentActivity implements ActionBar.TabListener {
private ViewPager viewPager;
private PrefPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Ingredienti preferiti", "Allergie e Intolleranze" };
int tabsnum = tabs.length;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_pref_layout);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new PrefPagerAdapter(getSupportFragmentManager());
mAdapter.setCount(tabsnum);
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) {
}
}
Adapter
package it.sii.mywaiter;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class PrefPagerAdapter extends FragmentPagerAdapter {
private int count = 0;
public PrefPagerAdapter(FragmentManager fm) {
super(fm);
}
public void setCount(int count){
this.count = count;
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new PrefFragment();
case 1:
return new AllerFragment();
}
return null;
}
#Override
public int getCount() {
return count;
}
}
Fragment's layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#fa6a6a" >
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test test"
android:textSize="20sp"
android:layout_centerInParent="true"/>
</RelativeLayout>
As you said, you have Fragments. Every Fragment has its own class so you can create a CustomArrayAdapter and the use it in the Fragments onCreate Method. Inside the Fragment get your ListView, which you defined in the xml File for this Fragment, with findViewByID and then set its Adapter with setAdapter(new YourCustomAdapterClass()); then you have your custom dynamic ListView in a Fragment.

How to go back to the parent Fragment using webview?

I have used fragments here. But I cannot go back to the parent.
Swipeactivitydefault.java
package com.pokhara.nepal;
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 com.pokhara.nepal.json.TabsPagerAdapter;
public class Swipeactivitydefault extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Tab1", "Tab2", "Tab3" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewpager_news);
// 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);
// 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) {
}
}
Tab1.java
package com.pokhara.nepal;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.LinearLayout;
public class Tab1 extends Fragment {
private WebView webView;
private Bundle webViewBundle;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = (LinearLayout) inflater.inflate(
R.layout.news_activity_child, container, false);
webView = (WebView) ll.findViewById(R.id.webview);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new MyCustomWebViewClient());
if (webViewBundle == null) {
webView.loadUrl("http://www.tipfortechs.com/mobile");
} else {
webView.restoreState(webViewBundle);
}
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
return ll;
}
}
Tab2.java and Tab3.java are also same as Tab 1
Just wanted to make three tabs.
TabsPagerAdapter.java
package com.pokhara.nepal.json;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import com.pokhara.nepal.Ekantipur;
import com.pokhara.nepal.HimalayaTimes;
import com.pokhara.nepal.NepalNews;
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
// Top Rated fragment activity
return new Tab1();
case 1:
// Games fragment activity
return new Tab2();
case 2:
// Movies fragment activity
return new Tab3();
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 3;
}
}
news_activity_child.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Help me Please!!! I want to go back to the parent fragment after opening the link from that webview.
I know there are many answer out there. The one was overiding the back button. But I donot know how to implement. I am new to this. So, can somebody help me with the code.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//...
}
#Override
public void onBackPressed() {
this.getParent().onBackPressed();
}
write onBackPressed() override method on tab1/tab2/tab3 fragment pages. It will help you.
I am unfamiliar with webview but I have worked with tabbed fragments.
From my experience with tabbed fragments, one way to go would be to implement a method on the main activity that "exposes your viewpager functionality" and swtiches to a desired fragment. For example:
public void showTab(int n) {
viewPager.setCurrentItem(n);
}
Then, within the fragment you want to trigger the transition to the other fragment, you just get the reference to the acitivy and tell it to show the tab you want.
Swipeactivitydefault m = (Swipeactivitydefault )getActivity();
m.showTab(1);

Categories

Resources