How to start new activity from tab control? - android

I have home activity which have tab control and on first tab the main activity is shown. On main tab i have buttons which will redirect to another activity.
and cannot redirect to another activity.
public class Main1 extends Fragment
{
ImageView saving, loan;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
saving = (ImageView) container.findViewById(R.id.imgSavingCollection);
loan = (ImageView) container.findViewById(R.id.imgLoanCollection);
saving.setOnClickListener((View.OnClickListener) container);
loan.setOnClickListener((View.OnClickListener) container);
View v = inflater.inflate(R.layout.activity_main1, container, false);
return v;
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.imgSavingCollection:
Intent savingactivity = new Intent(v.getContext(), SavingCollection.class);
startActivity(savingactivity);
break;
}
}
}

instead of
Intent savingactivity = new Intent(v.getContext(), SavingCollection.class);
startActivity(savingactivity);
add this
Intent savingactivity = new Intent(getActivity(), SavingCollection.class);
startActivity(savingactivity );

Try to change your code like,
public class Main1 extends Fragment
{
ImageView saving, loan;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_main1, container, false);
saving = (ImageView) rootView.findViewById(R.id.imgSavingCollection);
loan = (ImageView) rootView.findViewById(R.id.imgLoanCollection);
saving.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent savingactivity = new Intent(getActivity(), SavingCollection.class);
startActivity(savingactivity);
}
});
return rootView;
}
}
also check your manifest.xml
<activity android:name=".SavingCollection"/>
this may helps you

problem may be with v.getContext(), so just replace it by getActivity() when you declaring intent.

There is issue with your onCreateView, replace below code with your onCreateView
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_main1, container, false);
saving = (ImageView) v.findViewById(R.id.imgSavingCollection);
loan = (ImageView) v.findViewById(R.id.imgLoanCollection);
saving.setOnClickListener((View.OnClickListener) container);
loan.setOnClickListener((View.OnClickListener) container);
return v;
}

Use this
Intent savingactivity = new Intent(getActivity(), SavingCollection.class);
startActivity(savingactivity);
Replace v.getContext() to getActivity().
and Define your layout line upper side
View v = inflater.inflate(R.layout.activity_main1, container, false);
Full code :
View v = inflater.inflate(R.layout.activity_main1, container, false);
saving = (ImageView) container.findViewById(R.id.imgSavingCollection);
loan = (ImageView) container.findViewById(R.id.imgLoanCollection);
saving.setOnClickListener((View.OnClickListener) container);
loan.setOnClickListener((View.OnClickListener) container);
return v;

Related

I can't make a new intent / activity from fragment that has 2 buttons

I'm trying to make a new activity from a fragment that has 2 buttons. The button showed up on the emulator, but when I clicked it it doesn't do anything. I've followed many tutorials online but most of them do onClickListener on a single button. Sorry I'm new to android development.
public class UserFragment extends Fragment implements View.OnClickListener {
Activity context = getActivity();
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_user, container, false);
Button register = v.findViewById(R.id.btnAkunRegister);
Button login = v.findViewById(R.id.btnAkunLogin);
return v;
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btnAkunLogin:
Intent moveIntent = new Intent(context, LoginActivity.class);
startActivity(moveIntent);
break;
case R.id.btnAkunRegister:
moveIntent = new Intent(context, RegisterActivity.class);
startActivity(moveIntent);
break;
}
}
}
update: I've tried following the answer from stackoverflow, but it doesn't solve the problem.
You should set onClickListener for your buttons. You implement this interface in you
Frgamnet, so you should set this as listener in your onCreateView() method.
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_user, container, false);
Button register = v.findViewById(R.id.btnAkunRegister);
Button login = v.findViewById(R.id.btnAkunLogin);
//set listener
register.setOnClickListener(this);
login.setOnClickListener(this);
return v;
}

Intent from Fragment to a MapsActivity from onCreateView

this is my code of my third tab in which i have put a button to intent from this current fragment to my MapsActivity, but as soon as i click my button, the app stops running.
This is my code:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_tab3, container, false);
mapbtn = (Button) v.findViewById(R.id.mapButton);
mapbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), MapsActivity.class);
startActivity(intent);
}
});
return v;
}
Please someone help!!!

How to open Activity from Fragment using intent?

When i use onClickListener on a Button inside a Fragment and use intent to open an Activity, it gives NullPointerException.
This is my code
public class TabFragment extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tabs, container, false);
Button tab1=(Button) getView().findViewById(R.id.tab1upgradeCost);
tab1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getActivity(), MainActivity.class);
getActivity().startActivity(i);
}
});
return rootView;
}
}
Your getting NLP because tab1 is not initialized with right view
Don't use
Button tab1=(Button) getView().findViewById(R.id.tab1upgradeCost);
In onCreateView() getView() will retrun null because still fragment is not initlized.
Instead use
Button tab1=(Button) rooView.findViewById(R.id.tab1upgradeCost);
I think problem is with your view that getView may return null.
replace your code with this.
I didn't test your code but may be it help.
public class TabFragment extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tabs, container, false);
Button tab1=(Button) rootView.findViewById(R.id.tab1upgradeCost);
tab1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getActivity(), MainActivity.class);
getActivity().startActivity(i);
}
});
return rootView;
}
}
Change
Button tab1=(Button) getView().findViewById(R.id.tab1upgradeCost);
to
Button tab1=(Button) rootView.findViewById(R.id.tab1upgradeCost);
startActivity(new Intent(getActivity(),Actvty_Ground.class));

Android ImageButton worked well in Activity. It doesn't work in Fragment

I'm trying to convert a layout so that it will include fragments.
One of the views is an ImageButton that has a listener.
The code worked fine as an Activity but makes trouble as a Fragment...
First problem was that I couldn't use findViewById,
but I was able to find the answer for that here and fixed it with getView().
I can't find an answer to the second problem...
after I declared:
ImageButton camBt = (ImageButton)getView().findViewById(R.id.button1);
I want to set an on click listener:
camBt.setOnClickListener(listener);
But it keeps acting like the ImageButton (camBt) doesn't exist...
I don't even know what's the problem... it worked fine in the Activity...
Here's the full code.
Many thanks!
public class CameraFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.camera_fragment, container, false);
return V;
}
ImageButton camBt = (ImageButton)getView().findViewById(R.id.button1);
camBt.setOnClickListener(listener);
ImageButton.OnClickListener listener = new ImageButton.OnClickListener()
{
#Override
public void onClick(View arg0)
{
Camera.open().getParameters();
}
};
}
Move
ImageButton camBt = (ImageButton)getView().findViewById(R.id.button1);
camBt.setOnClickListener(listener);
inside onCreateView().
Like
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.camera_fragment, container, false);
// Here it is
ImageButton camBt = (ImageButton)v.findViewById(R.id.button1);
camBt.setOnClickListener(listener);
return v;
}
And one more thing you need to understand that variable name should starts with small letter. So you should use View v instead of View V.
You should add
ImageButton camBt = (ImageButton)getView().findViewById(R.id.button1);
camBt.setOnClickListener(listener);
inside onCreateView() like:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.camera_fragment, container, false);
ImageButton camBt = (ImageButton)v.findViewById(R.id.button1);
camBt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
return V;
}
public class CameraFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.camera_fragment, container, false);
ImageButton camBt = (ImageButton)V.findViewById(R.id.button1);
ImageButton.OnClickListener listener = new ImageButton.OnClickListener()
{
#Override
public void onClick(View arg0)
{
Camera.open().getParameters();
}
};
camBt.setOnClickListener(listener);
return V;
}
}

How to get an id of a View inside a Fragment

Hi I am trying to get an id of some View in a Fragment from a string, however it doesn't seem to work... Is there a special case for fragments?
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View singleplayerView = inflater.inflate(R.layout.singleplayer_tab, container, false);
setupMap();
return singleplayerView;
}
private void setupRank() {
TextView view = (TextView) getByStringId("rank0");
view.setText("hello");
}
private final View getByStringId(final String id) {
return findViewById(getResources().getIdentifier(id, "id", getActivi
ty().getPackageName()));
}
So I am trying to set the textView field rank0 in the layout that is used for my fragment... but right now it gets a NUllPointer because it couldnt find the id
Thank you
you can define your Layout In your onCreateView() inside Fragments like this:
View V = new View(getActivity());
V = inflater.inflate(R.layout.fragment_layout,container, false);
Then
TextView SomeField = (TextView)V.findViewById(R.id.TxtSomeField);
Inside fragment, add getActivity before findViewById:
getActivity().findViewById(...);
Try this,
String id;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extra = getArguments();
id = extra.getString("id", "");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_calendar_details, container, false);
Button b = (Button) view.findViewById(id);
return view;
}

Categories

Resources