android


how to call an intent with my swipe buttons


Here I have a code where you can swipe the tab menus, but I want that after a swipe another activity is opened, for example Simmering.class.
I think this should go with an intent, but how or where should I put it.
Here is the code:
import java.util.Locale;
import android.os.Bundle;
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.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
#SuppressWarnings("unused")
public class MainActivity extends FragmentActivity {
/**
* The {#link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {#link android.support.v4.app.FragmentPagerAdapter} derivative, which
* will keep every loaded fragment in memory. If this becomes too memory
* intensive, it may be best to switch to a
* {#link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {#link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
// Show 3 total pages.
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
case 3:
return getString(R.string.title_section4).toUpperCase(l);
}
return null;
}
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);
TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
}
Try adding below code in onCreateView method of your fragment class. check for the position after which you want to start new activity. And in it call the startActivity method.
Intent intent = new Intent(getApplicationContext(), Simmering.class);
getApplicationContext().startActivity(intent);
Do you know, that starting new activity after swipe hide page you're swiping to? If your answer is yes, you can use ViewPager's onPageChangeListener. It's methods are invoked when page is changed/updated etc. Usage is following:
mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int index) {
// TODO Auto-generated method stub
}
#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
}
})
i already found an option to achieve what i wanted, here is the code:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends FragmentActivity {
ViewPager viewPager=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager= (ViewPager) findViewById(R.id.pager);
FragmentManager fragmentManager=getSupportFragmentManager();
viewPager.setAdapter(new MyAdapter(fragmentManager));
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// action with ID action_refresh was selected
case R.id.action_refresh:
Toast.makeText(this, R.string.refresh_clicked, Toast.LENGTH_SHORT).show();
break;
// action with ID action_settings was selected
case R.id.action_settings:
Toast.makeText(this, R.string.settings_clicked, Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return true;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
class MyAdapter extends FragmentStatePagerAdapter
{
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
Fragment fragment=null;
switch(i){
case 0: fragment = new Home(); break;
case 1: fragment = new Simmering(); break;
case 2: fragment = new LugnerCity(); break;
case 3: fragment = new Gmunden(); break;
case 4: fragment = new Salzburg(); break;
case 5: fragment = new Linz(); break;
case 6: fragment = new Saalbach(); break;
case 7: fragment = new Innsbruck(); break;
case 8: fragment = new Reutte(); break;
case 9: fragment = new Bregenz(); break;
case 10: fragment = new Kufstein(); break;
case 11: fragment = new Bratislava(); break;
}
return fragment;
}
#Override
public int getCount() {
return 12;
}
#Override
public CharSequence getPageTitle(int position) {
switch(position) {
case 0: return "Home";
case 1: return "Simmering";
case 2: return "Lugner City";
case 3: return "Gmunden";
case 4: return "Salzburg";
case 5: return "Linz";
case 6: return "Saalbach";
case 7: return "Innsbruck";
case 8: return "Reutte";
case 9: return "Bregenz";
case 10: return "Kufstein";
case 11: return "Bratislava";
}
return null;
}
}

Related Links

How to stop Progress Bar Animation in Android?
How to set buttons at the bottom of a scrollView such that the buttons are covered by the softkeyboard
Picasso Target has been garbage collected
Scroll Problemas with Viewpager and Tabs
Definning a reference to method attribute for custom view
NoSuchAlgorithmException in FacebookSDK.GraphResponse.fromHTTPConnection
SQLite Join Table Mapping
Android tabs : Don't swipe tabs until current tab is filled
Window Overlay dispatch back button event to foreground Activity of other app from service
Meteor while running on android device gives this error
Android Requires MinSdk <=9
Nested fragment with viewpager where viewpager not loading the fragments
Android prevent appearing sensitive data at task switcher without secure flag
Crashlytics NDK symbols and Gradle tasks
On the Marshmallow,the MediaMetadataRetriever does not working
Cordova Build fails, buildToolsVersion is not specified

Categories

HOME
azure-stream-analytics
cheerio
linkedin
phpmyadmin
pclxl
owl-carousel
pdfbox
parameters
jetty
android-json
title
barcode
accessibility
java.util.logging
cmdb
naivebayes
supervisor
mongodb-csharp
gruntfile
internationalization
azure-iot-hub
powershell-v5.0
forgot-password
pyramid
symbols
exit
event-store
gmock
mount
onload
moses
opentk
soot
winexe
browser-sync
testcomplete
eviews
quickfixj
jett
fossil
pop3
indy10
video-capture
amazon-ses
mongoose-schema
numberpicker
android-ibeacon
outputcache
wc
clang++
double-click
openresty
dotnet-httpclient
reportservice2010
corruption
context-sensitive-grammar
boxing
java-6
android-testing
headless
builtins
webjars
microstation
resourcemanager
watch-os-2
beamer
model-associations
http-status-code-401
redis-cluster
realsense
phpgrid
toran-proxy
gocql
rewrite
flock
zscript
ocra
rdflib
graphics2d
git-ftp
aspectj-maven-plugin
entity-framework-4.3
diff3
gamekit
big-ip
cuba
codeplex
matrix-inverse
jquery-slider
vsx
twitter-bootstrap-rails
accesscontrolexception
xmltodict
openargs
notorm
kendo-dataviz
inputmismatchexception
inappsettingskit
nevron
facebook-timeline
argouml
midlet
apache-whirr
httponly
flex-mobile
entity-attribute-value
denied
jquery-tools
umra
cac
message-passing
isapi-extension
strtod
google-wave
entitykey
great-circle

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App