Sorry for my bad English. I have BottomNavigationView in my app such as in Facebook and viewPager to scrool the fragments with swipe but I have some problems in function onPageSelected, it always set position to 4. This is the code of MainActivity:
private static final int NUM_PAGES = 5;
BottomNavigationView bottomNavigationView;
ViewPager viewPager;
PagerAdapter pagerAdapter;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_tests:
viewPager.setCurrentItem(0);
return true;
case R.id.navigation_courses:
viewPager.setCurrentItem(1);
return true;
case R.id.navigation_profile:
viewPager.setCurrentItem(2);
return true;
case R.id.navigation_leaderboard:
viewPager.setCurrentItem(3);
return true;
case R.id.navigation_settings:
viewPager.setCurrentItem(4);
return true;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = findViewById(R.id.pager);
pagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(pagerAdapter);
bottomNavigationView = findViewById(R.id.navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
viewPager.setCurrentItem(2);
bottomNavigationView.getMenu().findItem(R.id.navigation_profile).setChecked(true);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
switch (position) {
case 0:
setTitle("Tests");
bottomNavigationView.setSelectedItemId(R.id.navigation_tests);
case 1:
setTitle("Courses");
bottomNavigationView.setSelectedItemId(R.id.navigation_courses);
case 2:
setTitle("Profile");
bottomNavigationView.setSelectedItemId(R.id.navigation_profile);
case 3:
setTitle("Leaderboard");
bottomNavigationView.setSelectedItemId(R.id.navigation_leaderboard);
case 4:
setTitle("Settings");
bottomNavigationView.setSelectedItemId(R.id.navigation_settings);
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
}
#Override
public void onBackPressed() {
if (viewPager.getCurrentItem() == 2) {
super.onBackPressed();
} else if (viewPager.getCurrentItem() >= 3) {
viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
} else {
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
}
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new FragmentTests();
case 1:
return new FragmentCourses();
case 2:
return new FragmentProfile();
case 3:
return new FragmentLeaderboard();
case 4:
return new FragmentSettings();
}
return null;
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
This is the image of app
Then it it again move me to settings .And when I tap on profile
You need to write break statement in switch case of ViewPager onPageSelected method like below
#Override
public void onPageSelected(int position) {
switch (position) {
case 0:
setTitle("Tests");
bottomNavigationView.setSelectedItemId(R.id.navigation_tests);
break;
case 1:
setTitle("Courses");
bottomNavigationView.setSelectedItemId(R.id.navigation_courses);
break;
case 2:
setTitle("Profile");
bottomNavigationView.setSelectedItemId(R.id.navigation_profile);
break;
case 3:
setTitle("Leaderboard");
bottomNavigationView.setSelectedItemId(R.id.navigation_leaderboard);
break;
case 4:
setTitle("Settings");
bottomNavigationView.setSelectedItemId(R.id.navigation_settings);
break;
}
}
Related
I want to pass data from main activity to one tab of TabView.Here is my code and it isn't working,what to do now
There is my codes
MainActivity :
public class MainActivity extends AppCompatActivity {
Button button;
EditText text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button= (Button) findViewById(R.id.getInfoButton);
text= (EditText) findViewById(R.id.gitHubUserName);
Logger.addLogAdapter(new AndroidLogAdapter());
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final String user = text.getText().toString();
Intent in=new Intent(MainActivity.this,Tab.class);
in.putExtra("SS",user);
Logger.d(user);
Bundle bundle=new Bundle();
bundle.putString("NAME",user);
ProfileActivity pro = new ProfileActivity();
pro.setArguments(bundle);
startActivity(in);
}
});
}
}
my Adapter for tabViews :
public class PagerAdapter extends FragmentPagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
ProfileActivity profileActivity=new ProfileActivity();
return profileActivity;
case 1:
FollowersActivity followersActivity=new FollowersActivity();
return followersActivity;
case 2:
PublicRepos publicRepos=new PublicRepos();
return publicRepos;
default:
return null;
}
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Profile Info";
case 1:
return "Followers";
case 2:
return "Public Repos";
}
return null;
}
}
and this is ProfileActivity where I want to pass the data which I got from EditText in Main Activity :
public class ProfileActivity extends Fragment {
ImageView imageView;
TextView name;
TextView bio;
String text;
public void dataPass(String txt){
text = txt;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_profile, container, false);
name = rootView.findViewById(R.id.name);
bio = rootView.findViewById(R.id.bio);
Logger.addLogAdapter(new AndroidLogAdapter());
Bundle bundle = this.getArguments();
String nameString = bundle.getString("NAME");
Logger.d(nameString);
name.setText(nameString);
return rootView;
}
}
when I run the app, app stopped working and in logcat this error :
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
at com.salman.getgithubofgeek.Activity.Activity.Activites.ProfileActivity.onCreateView(ProfileActivity.java:43)
You cannot use setArguments like that.
Do this:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final String user = text.getText().toString();
Intent in=new Intent(MainActivity.this,Tab.class);
in.putExtra("SS",user);
in.putExtra("NAME",user);
Logger.d(user);
startActivity(in);
}
});
then in Tab activity, pass the extra to the adapter wherever you call it like so:
PagerAdapter adapter = new PagerAdapter(fm, user);
and make the following changes to the adapter class:
public class PagerAdapter extends FragmentPagerAdapter {
String nameString;
public PagerAdapter(FragmentManager fm, String nameString) {
super(fm);
this.nameString = nameString;
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
ProfileActivity profileActivity=new ProfileActivity();
Bundle bundle=new Bundle();
bundle.putString("NAME",nameString);
profileActivity.setArguments(bundle);
return profileActivity;
case 1:
FollowersActivity followersActivity=new FollowersActivity();
return followersActivity;
case 2:
PublicRepos publicRepos=new PublicRepos();
return publicRepos;
default:
return null;
}
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Profile Info";
case 1:
return "Followers";
case 2:
return "Public Repos";
}
return null;
}
}
The problem is you are trying to add the bundle in fragment that is in `
TabActivity
and inPager` you are again declaring ProfileActivity fragment that is with out
setArguments()
.
All you need to do is send the bundle to activity first and then before using ProfileActivity fragment in Pager set the bundle to the ProfileActivity fragment.
Button button;
EditText text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button= (Button) findViewById(R.id.getInfoButton);
text= (EditText) findViewById(R.id.gitHubUserName);
Logger.addLogAdapter(new AndroidLogAdapter());
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final String user = text.getText().toString();
Intent in=new Intent(MainActivity.this,Tab.class);
in.putExtra("SS",user);
Logger.d(user);
//modification goes here
intent.putString("NAME",user);
startActivity(in);
}
});
}
}
and
public PagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
ProfileActivity profileActivity=new ProfileActivity();
//modifications goes here
Bundle b = new Bundle();
bundle.putString("NAME",user);
profileActivity.setArguments(b);
return profileActivity;
case 1:
FollowersActivity followersActivity=new FollowersActivity();
return followersActivity;
case 2:
PublicRepos publicRepos=new PublicRepos();
return publicRepos;
default:
return null;
}
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Profile Info";
case 1:
return "Followers";
case 2:
return "Public Repos";
}
return null;
}
}
How to exec something when changing tabs ? Like a method or something is there any easy way ? I have searched for it but I have found something called TabHost can anyone explain me that too ?
Here is my tab code :
ublic class Testes extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
private DatePickerDialog.OnDateSetListener hourSetListener;
private DatePickerDialog.OnDateSetListener dateSetListener;
private Button date_button;
private Button hora_picker;
private TextView date_text;
private TextView hora_text;
private Caldroid_fragment cf;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testes);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
cf = new Caldroid_fragment();
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_testes, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case go_to_fazer_media:{
if (item.isChecked())
item.setChecked(false);
else{
Intent i = new Intent(Testes.this,Formas.class);
startActivity(i);
return true;
}
}
case definicoes:{
if (item.isChecked())
item.setChecked(false);
else{
return true;
}
}
}
return super.onOptionsItemSelected(item);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch(position){
case 0:
Tab1tests tab1 = new Tab1tests();
return tab1;
case 1:
Tab2calendar tab2 = new Tab2calendar();
return tab2;
default:
return null;
}
}
#Override
public int getCount() {
// Show 3 total pages.
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Adicionar Testes";
case 1:
return "Mapa de Testes";
}
return null;
}
}
EDIT:
AFTER THE ANSWER:
public class Testes extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
private DatePickerDialog.OnDateSetListener hourSetListener;
private DatePickerDialog.OnDateSetListener dateSetListener;
private Button date_button;
private Button hora_picker;
private TextView date_text;
private TextView hora_text;
private Caldroid_fragment cf;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testes);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
cf = new Caldroid_fragment();
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
#Override
public void onTabSelected(TabLayout.Tab tab){
int position = tab.getPosition();
switch(position){
case 0 :{}
case 1 : {
addToCalendar();
}
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_testes, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case go_to_fazer_media:{
if (item.isChecked())
item.setChecked(false);
else{
Intent i = new Intent(Testes.this,Formas.class);
startActivity(i);
return true;
}
}
case definicoes:{
if (item.isChecked())
item.setChecked(false);
else{
return true;
}
}
}
return super.onOptionsItemSelected(item);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch(position){
case 0:
Tab1tests tab1 = new Tab1tests();
return tab1;
case 1:
Tab2calendar tab2 = new Tab2calendar();
return tab2;
default:
return null;
}
}
#Override
public int getCount() {
// Show 3 total pages.
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Adicionar Testes";
case 1:
return "Mapa de Testes";
}
return null;
}
}
}
If you are using TabLayout, use OnTabSelectedListener.
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
#Override
public void onTabSelected(TabLayout.Tab tab){
int position = tab.getPosition();
// Execute here based on position
switch(position){
case 0 :
break;
case 2 :
addToCalendar();
break;
}
});
Tab position starts at 0 and not 1.
I want startActivity(LoginActivity) when after 3rd fragment.
In position 3rd fragment, I slide from right to left to start the activity.
I found some question but:
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if(position == tutorialViewPager.getAdapter().getCount() - 1){
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
}
}
In that case, when move 2nd fragment to 3rd fragment with slide, 3rd fragment(last fragment) was appeared a little bit and change Activity.
I want to change Activity when I slide last fragment(not from 2nd fragment to 3rd fragment, just slide 3rd fragment).
How I can warp Fragment to specific Activity when slide last fragment?
Please help me.
public class TutorialActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {
int MAX_PAGE = 3;
Fragment cur_fragment = new Fragment();
ViewPager tutorialViewPager;
private int selectedPageIndex = -1;
private boolean exitWhenScrollNextPage = false;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tutorial_page);
tutorialViewPager = (ViewPager) findViewById(R.id.tutorialViewPager);
tutorialViewPager.setAdapter(new adapter(getSupportFragmentManager()));
tutorialViewPager.addOnPageChangeListener(this);
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if(position == tutorialViewPager.getAdapter().getCount() - 1){
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
}
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
private class adapter extends FragmentPagerAdapter{
public adapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
if(position<0 || MAX_PAGE <= position){
return null;
}
switch (position){
case 0:
cur_fragment = new TutorialFragmentA();
break;
case 1:
cur_fragment = new TutorialFragmentB();
break;
case 2:
cur_fragment = new TutorialFragmentC();
break;
}
return cur_fragment;
}
#Override
public int getCount() {
return MAX_PAGE;
}
}
}
You have to modify the implementation of the methods of ViewPager.OnPageChangeListener. Here is what i used and managed to solve this issue.
I put here your transition to (LoginActivity).
mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
boolean onLastPageChanged = false;
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
int lastFrag = mQuestionCollectionPagerAdapter.getCount() -1;
if (onLastPageChanged && position == lastFrag) {
//next
Log.e(TAG, "Transition Happened");
}
}
#Override
public void onPageScrollStateChanged (int state) {
int lastFrag = mQuestionCollectionPagerAdapter.getCount() -1;
int currentFragment = mViewPager.getCurrentItem();
if (currentFragment == lastFrag /*&& lastPos==lastIdx*/ && state == 1) {
onLastPageChanged = true;
Log.e(TAG, "page scroll state >>>>>>");
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
finish();
} else {
onLastPageChanged = false;
}
}
});
I have a tablayout, viewpager with 4 fragments.
I added bottombar in my MainActivity so it was showing all fragments.
I just want bottombar only in first fragment.
How to hide bottombar for other 3 fragments?
This is MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//setHasOptionsMenu(true);
bottombar = BottomBar.attach(MainActivity.this,savedInstanceState);
bottombar.setItemsFromMenu(R.menu.bottom_bar_menu, new OnMenuTabSelectedListener() {
#Override
public void onMenuItemSelected(int itemId) {
switch (itemId) {
case R.id.about_us:
About_Us about_us = new About_Us();
Intent intent = new Intent(MainActivity.this,About_Us.class);
startActivity(intent);
break;
case R.id.contact_us:
SquareFragment squareFragment = new SquareFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.frame,squareFragment).commit();
//squareFragment.updateColor(Color.parseColor(colors[1]));
break;
//Toast.makeText(MainActivity.this,"selected contact_us",Toast.LENGTH_SHORT).show();
case R.id.donate_us:
donate_us_Fragment donate_us_fragment = new donate_us_Fragment();
getSupportFragmentManager().beginTransaction().replace(R.id.frame,donate_us_fragment).commit();
//Toast.makeText(MainActivity.this,"selected donate_us",Toast.LENGTH_SHORT).show();
break;
}
}
});
bottombar.setActiveTabColor("#C2185B");
//Initializing the tablayout
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
//Adding the tabs using addTab() method
tabLayout.addTab(tabLayout.newTab().setText("Home").setIcon(R.drawable.home_selector));
tabLayout.addTab(tabLayout.newTab().setText("News").setIcon(R.drawable.news_selector));
tabLayout.addTab(tabLayout.newTab().setText("Videos").setIcon(R.drawable.video_selector));
tabLayout.addTab(tabLayout.newTab().setText("PodCasts").setIcon(R.drawable.podcast_selector));
tabLayout.addTab(tabLayout.newTab().setText("More").setIcon(R.drawable.more_selector));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
//tabLayout.setupWithViewPager(viewPager);
//Initializing viewPager
viewPager = (ViewPager) findViewById(R.id.pager);
//Creating our pager adapter
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
//Adding adapter to pager
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
#Override
public void onTabSelected(TabLayout.Tab tab) {
super.onTabSelected(tab);
}
}
This is ViewPagerAdapter:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
//integer to count number of tabs
int tabCount;
BottomBar bottombar;
//Constructor to the class
public ViewPagerAdapter(FragmentManager fm, int tabCount) {
super(fm);
//Initializing tab count
this.tabCount = tabCount;
}
//Overriding method getItem
#Override
public Fragment getItem(int position) {
//Returning the current tabs
switch (position) {
case 0:
return new Home_Fragment();
case 1:
News_Fragment news_fragment = new News_Fragment();
bottombar.hide();
return news_fragment;
case 2:
Video_Fragment video_fragment = new Video_Fragment();
return video_fragment;
case 3:
return new PodCasts_Fragment();
case 4:
return new More_Fragment();
}
return null;
}
//Overriden method getCount to get the number of tabs
#Override
public int getCount() {
return tabCount;
}
See this image below:
try this:
public void onTabSelected(TabLayout.Tab tab) {
if(tab.getPosition == 0 )
bottomBar.hide();
super.onTabSelected(tab);
}
And remove the
bottomBar.hide();
from the getItem() in the fragment.
this is correct code
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
#Override
public void onTabSelected(TabLayout.Tab tab) {
if(tab.getPosition() == 0){
bottombar.show();
}else
if(tab.getPosition() == 1){
bottombar.hide();
}else
if(tab.getPosition() == 2){
bottombar.hide();
}else
if(tab.getPosition() == 3){
bottombar.hide();
}
super.onTabSelected(tab);
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
super.onTabUnselected(tab);
//tab.getIcon().setAlpha(127);
}
});
}
}
I created an onTouchListener for my ImageView "car" and want to do the same for another ImageView, however I can't figure out how. So my question is:
How do you use one onTouchListener that detects the MotionEvents of two separate ImageViews and makes something happen accordingly?
#Override
public boolean onTouch(View view, MotionEvent event) {
//int action = event.getAction();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
carcolor.setBackgroundColor(this.getResources().getColor(R.color.colorPrimary));
car.startAnimation(pressdown);
pressdown.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
carback.setScaleX((float) 0.9);
carback.setScaleY((float) 0.9);
}
#Override
public void onAnimationEnd(Animation animation) {
carback.setVisibility(View.VISIBLE);
car.setVisibility(View.INVISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
car.startAnimation(release);
carcolor.setBackgroundColor(this.getResources().getColor(R.color.unpressed));
release.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
carback.setVisibility(View.INVISIBLE);
}
#Override
public void onAnimationEnd(Animation animation) {
car.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
break;
case MotionEvent.ACTION_CANCEL:
break;
}
return true;
}
You should implement the onTouchListener like:
imageView.setOnTouchListener(this);
and init it like :
#Override
public boolean onTouch(View v, MotionEvent event) {
ImageView view = (ImageView) v;
switch (view.getId()){
case R.id.car1: // example id
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_CANCEL:
break;
}
break;
case R.id.car2: // example id
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_CANCEL:
break;
}
break;
}
return true;
}
If your activity is implementing the onTouchListener event (I'm assuming based on the override), on your other ImageView just do the following
imageView.setOnTouchListener(this);
Where this is the current activity.
EDIT: Based on your comment
public class ImageView1Listener implements OnTouchListener
{
**** OVERRIDES****
}
public class ImageView2Listener implements OnTouchListener{
**** OVERRIDS FOR THIS IMAGE****
}
then in your main remove the implements OnTouchListener and use bind it programatically
imageView1.setOnClickListener(new ImageViewListener1());
imageView2.setOnClickListener(new ImageViewListener2());
Attach the listener to the parent of them.
Than, use gesturesDetector as stated here:
https://developer.android.com/training/gestures/detector.html
public class MainActivity extends Activity implements
GestureDetector.OnGestureListener,
GestureDetector.OnDoubleTapListener{
private static final String DEBUG_TAG = "Gestures";
private GestureDetectorCompat mDetector;
// Called when the activity is first created.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Instantiate the gesture detector with the
// application context and an implementation of
// GestureDetector.OnGestureListener
mDetector = new GestureDetectorCompat(this,this);
// Set the gesture detector as the double tap
// listener.
mDetector.setOnDoubleTapListener(this);
}
#Override
public boolean onTouchEvent(MotionEvent event){
this.mDetector.onTouchEvent(event);
// Be sure to call the superclass implementation
return super.onTouchEvent(event);
}
#Override
public boolean onSingleTapConfirmed(MotionEvent event) {
Log.d(DEBUG_TAG, "onSingleTapConfirmed: " + event.toString());
return true;
}
....
}