I wanted to create a app with Tabview so i started a app with Tabbed Activity with Action Bar tabs(with viewPager).Then i created fragment named tab1,tab2,tab3. You can find the tutorial that i followed to do above tabview in the following link and source code too
https://www.simplifiedcoding.net/android-tablayout-example-using-viewpager-fragments/
from here my question starts in the above three tabs i created image view and a button up on clicking that button i wanted to open new activity.I am new to programming hope i will get answers. And i will be very grateful for the help.
Here is the code of the tab from here i wanted to call new activity on clicking a button.
tab1.xml
:
<?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">
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#ca064006"
android:id="@+id/imageView" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView"
android:layout_centerHorizontal="true"
android:layout_marginTop="88dp"
android:id="@+id/button" />
</RelativeLayout>
Tab1.java
import android.content.Intent;
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;
public class Tab1 extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.messages, container, false);
}
}
On clicking button in above code i want to call new activity can you please edit the code.
First you have to bind this button in you code, then use Intent
to open another activity on the onClick event of that button
in the Fragment do the following:
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.messages, container, false);
Button button = (Button) rootView.findViewById(R.id.YOUR_BUTTON_ID_IN_XML); //Binding the button view
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do something in response to button click
Intent intent = new Intent (Tab1.this.getActivity(), OTHER-ACTIVITY-CLASS-NAME.class);
Tab1.this.getActivity().startActivity(intent);
}
});
return rootView;
}
I already posted an answer on a similar question, So to save time here was my answer:
I was facing the same issue and i easily solved it by calling my MainActivity viewpager from the firstFragment class, and then easily setting the viewpager's current item to the index of the tab you want.
here's the code which i placed in the onClick method of the first tab fragment for me to go the third tab fragment;
ViewPager viewPager = getActivity().findViewById(R.id.simpleViewPager);
viewPager.setCurrentItem(2);
My answer might be late, but i hope it helps anyone else who might meet this challenge.
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tab, container, false);
Button button = (Button) rootView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent (Tab1.this.getActivity(), //your_activity.class);
Tab1.this.getActivity().startActivity(intent);
}
});
return rootView;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.