I've got a Fragment implementing onClickListener. What I have is a custom dialer which must show on the edittext the introduced number. But it doesn't show anything.
This is the Fragment:
PhoneView.java
public class PhoneView extends Fragment implements OnTabChangeListener, OnClickListener {
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mRoot = inflater.inflate(R.layout.phone_view, null);
mTabHost = (TabHost) mRoot.findViewById(android.R.id.tabhost);
setupTabs();
return mRoot;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
...
ImageButton button1 = (ImageButton) getView().findViewById(R.id.one);
EditText numTxt = (EditText) getView().findViewById(R.id.digits);
...
button1.setOnClickListener(this); //UPDATED - ADDED
...
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.one:
numTxt.setText(numTxt.getText()+"1");
break;
...
you have to call View.setOnClickListener(OnClickListener) of the view you want to react on the click event
In your Activity class, you need to define your Phoneview as you ClickListener to the button or another "view"
Suppose in your activity class, u have a button named deleteButton, and if u want to register a listener for this button... This is how you would do it.
private Button deleteButton= null;
deleteButton.setOnClickListener(PhoneView);
Related
Tried to open a fragment from a button in a fragment. Wasnt able to get this to work so decided just to make it an activity. Tried doing this but still am not able to open the activity on the button press.
public class ZonnepaneelLayout extends Fragment{
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.zonnepaneel_layout, container, false);
Button button2 = (Button)view.findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getActivity(), AddZonnepaneel.class);
startActivity(i);
}
});
getActivity().setTitle("Zonnepaneel");
return view;
}
}
Activity
public class AddZonnepaneel extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.add_zonnepaneel_layout);
}
}
Try using the getContext() instead of the getActivity()
Intent intent = new Intent(getContext(), AddZonnepaneel.class);
startActivity(i);
First of all, your variable View can't start with uppercase letter, set it to lowercase :)
Try to use
getActivity().startActivity(i);
getActivity().finish();
I'm quite new working with Android/Java, so please bear with me.
I moved code from LoginActivity > onCreate into a fragment I created FragmentLogin to method onCreate where many classes no longer resolve, such as findViewById. I'm assuming that somewhere I didn't pass the context of the container Activity properly.
Or some other newbie mistake...
Here is LoginActivity.java [relevant parts copied]
public class LoginActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initialize Fragments //
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
setContentView(R.layout.fragment_login);
}
}
and FragmentLogin.java:
public class FragmentLogin extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_login, container, false);
final ValidationManager Check = new ValidationManager();
final SessionManager Session = new SessionManager(this);
final EditText textEmail = (EditText) findViewById(R.id.login_email);
final EditText textPassword = (EditText) findViewById(R.id.login_pass);
final Button buttonLogIn = (Button) findViewById(R.id.button_login);
final Button buttonSignup = (Button) findViewById(R.id.button_signup);
// Listen for FORGOTTEN PASSWORD click event, open ForgottenPassword Fragment //
final Button forgottenPassword = (Button) findViewById(R.id.button_lost_pass);
forgottenPassword.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.fragment_forgotten_password);
}
});
... more code ...
}
}
The variables/methods that were working when the second block of code was residing in the onCreate method of the Activity but no longer resolve after I moved the code to onCreateView of the FragmentLogin fragment class:
findViewById, setContentView
Basically, this is a login form where the default fragment should be login, and a button on that page (Button forgottenPassword) would open another fragment (FragmentForgottenPassword).
Can anyone tell me what I'm doing wrong?
In your activity layout xml file, add something similar to this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment android:name="your.package.FragmentLogin"
android:id="#+id/fragment_login"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
In your LoginActivity: remove the fragment manager stuff. You don't need it yet.
public class LoginActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_login);
}
}
In your FragmentLogin, move the return statement to the end and use the inflated view to find your views by id:
public class FragmentLogin extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final view view = inflater.inflate(R.layout.fragment_login, container, false);
final ValidationManager Check = new ValidationManager();
final SessionManager Session = new SessionManager(this);
final EditText textEmail = (EditText) view.findViewById(R.id.login_email);
final EditText textPassword = (EditText) view.findViewById(R.id.login_pass);
final Button buttonLogIn = (Button) view.findViewById(R.id.button_login);
final Button buttonSignup = (Button) view.findViewById(R.id.button_signup);
// Listen for FORGOTTEN PASSWORD click event, open ForgottenPassword Fragment //
final Button forgottenPassword = (Button) view.findViewById(R.id.button_lost_pass);
forgottenPassword.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.fragment_forgotten_password);
}
});
... more code ...
return view;
}
}
You should get subviews from layout while it's being inflated. The layout usually inflated in OnCreateView method.
View layout = inflater.inflate( R.layout.fragment_login, null );
final EditText textEmail = (EditText) layout.findViewById(R.id.login_email);
final EditText textPassword = (EditText) layout.findViewById(R.id.login_pass);
...
return layout;
I am guessing you want to the fragment to be shown within the activity. Try this:
public class LoginActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//put the setContentView() before the fragment initialization also, I noticed your activity layout is the same as your fragment layout,this should not be so, hence I changed that
setContentView(R.layout.activity_login);
// Initialize Fragments //
//you did not initialize the fragments completely, it should be like
//this
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction().replace(R.id.fragment_container, new FragmentLogin()).commit();
}}
then in the Login fragment, it should be like this:
public class FragmentLogin extends Fragment {#Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_login, container, false);
final ValidationManager Check = new ValidationManager();
final SessionManager Session = new SessionManager(this);
final EditText textEmail = (EditText) view.findViewById(R.id.login_email);
final EditText textPassword = (EditText) view.findViewById(R.id.login_pass);
final Button buttonLogIn = (Button) view.findViewById(R.id.button_login);
final Button buttonSignup = (Button) view.findViewById(R.id.button_signup);
// Listen for FORGOTTEN PASSWORD click event, open ForgottenPassword Fragment //
final Button forgottenPassword = (Button) view.findViewById(R.id.button_lost_pass);
forgottenPassword.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//you cant call setContentView in a fragment
}
});
... more code ...
}}
I am not totally sure I caught all your mistakes but I think you should read these: one and two; they are online tutorials on fragments, you can also browse for more. I think it is a good starting point. All the best
I've just moved my button onCreatView from my activity_main.java file to the correct file - my ConvertFragment.java file.
Now that I've copy and pasted the code, I'm getting a red-x errors on all of my findViewById's... any thoughts?
thanks in advance!
public class ConvertFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_convert, container, false);
final EditText editDecimal = (EditText) findViewById(R.id.editDecimal);
final EditText editBinary = (EditText) findViewById(R.id.editBinary);
Button buttonConvert = (Button) findViewById(R.id.buttonConvert);
buttonConvert.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
int decimal = Integer.valueOf(editDecimal.getText().toString());
editBinary.setText(Integer.toBinaryString(decimal));
}
});
return rootView;
}
}
It's because a fragment doesn't have a method findViewById().
You can either call that method on your activity:
getActivity().findViewById()
Or just use the layout that you've just inflated and find view's inside of it:
rootView.findViewById()
The 2nd one is better (IMO) as it doesn't rely on the activity.
I am trying to add left side slide menu inside my application but i am facing little trouble getting the slide menu to the following child activities of the fragment.
So far i have tried using this sliding menu example and when i got this image now inside the "Find People" fragment class i have static data.
Find People (Parent Activity) on Button Click go to ---> Friends (Child Activity of Find People) on Button Click go to ----> Names (Child Activity of Friends)
I am trying to get the same sliding menu with the Child Activities also that i failed to achieve so far please can any one help me out
public class FindPeopleFragment extends Fragment {
public FindPeopleFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_find_people, container, false);
Button btn = (Button) rootView.findViewById(R.id.butto);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), Friends.class);
startActivity(intent);
}
});
return rootView;
}
}
Friends (Child Activity)
public class Friends extends Activity {
Button btnNext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.friends_xml);
btnNext = (Button)findViewById(R.id.btnNexts);
btnNext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Go to Names Child Activity
}
});
}
}
And Similarly Names (Child Activity)
How do i get the same slide menu in both the child activities (Friends & Names). Any idea ??
Plz help me out in this.
Code Snippet will be much helpful.
Thanks
As #Piyush Gupta said you need to call new Fragment instead of calling activities,
In Place of activity you need to use Fragment and move from one Fragment to next used this code
Instead of using Fragment element use Framelayout id of the fragment Xml
Replace ur code with this
public class FindPeopleFragment extends Fragment {
public FindPeopleFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_find_people, container, false);
Button btn = (Button) rootView.findViewById(R.id.butto);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
FragementDemo fd = new FragementDemo();
android.support.v4.app.FragmentTransaction ft =
getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fd); // content_frame is your FrameLayout container
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
});
return rootView;
}
}
And your next child activity will extend Fragment not Activity like this :
public class Friends extends SherlockFragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragementdemo, container, false);
// use button click code as used in FindPeopleFragment
return rootView;
}
}
Hope it works fine now for you!!
Okay so I've tried two types of code to get this to work and it keeps giving me force closes when I press the button to go into another Activity. I'm using a Fragment and there's a button in that Fragments code but I can't seem to get it to work. I'm not an experienced Android developer but I'm trying my best to learn.
Here's the Java code:
1st Method
public class About extends Fragment {
Intent intent;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.about, container, false);
intent = new Intent(getActivity(), Contact_Developer.class);
final Button button = (Button) rootView.findViewById(R.id.btnContactDev);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(intent);
}
});
return rootView;
}
}
2nd Method
public class About extends Fragment {
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.about, container, false);
Button button = (Button) rootView.findViewById(R.id.btnContactDev);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(getActivity(), Contact_Developer.class);
getActivity().startActivity(intent);
}
});
return rootView;
}
}
I really don't know what's going on and why I'm getting force closes but if anyone could help me and explain a little what I did wrong that'd be more than enough
Do not handle the onClick for the fragment button in the Fragment. Let it go it's parent activity. And start the activity from the parent activity.
To make sure that the button onClick event is sent to the parent activity, make sure, in your about.xml, for the button with id btnContactDev, you have the following parameter:
<Button android:id="#+id/btnContactDev"
android:onClick="buttonClick"
...
/>
and in your parent activity (parent of About fragment), you have:
public void buttonClick(View v) {
switch(v.getId()) {
case R.id.btnContactDev:
Intent myIntent = new Intent();
myIntent.setClassName(your_package_name_string, your_activity_name_string);
// for ex: your package name can be "com.example"
// your activity name will be "com.example.Contact_Developer"
startActivity(myIntent);
break;
}
}
HTH.
PS: This solution is very specific for what your requirement. In general, it's best to handle the onClick events related to the fragment inside the fragment class.
PS: Yes, as the other solution says, make sure you have registered the Contact_Developer Activity in your Manifest file.
Have you declared the proper activity in the AndroidManifest.xml? Each activity other then the first should be declared inside the application tag, like this:
<activity
android:name=".Contact_Developer"
android:label="#string/app_name" >
</activity>
If it's not found, it will give force close.
I think the issue here is that the activity is not ready.
I would not recommend you following harikris's solution even though it works. It's usually bad practice to put code (onClick() which handles the click event) in your XML file. For example, it will be very difficult to hunt that piece of code down to review/ change it in the future yourself, not to mention another developer in your team.
I'd suggest to listen to the event the activity is ready i.e. onActivityCreated() within your Fragment class. Override the implementation of the same method and call something in your activity e.g. onFragmentReady() like in the example below.
Fragment class:
public class About extends Fragment {
Intent intent;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.about, container, false);
intent = new Intent(getActivity(), Contact_Developer.class);
final Button button = (Button) rootView.findViewById(R.id.btnContactDev);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(intent);
}
});
return rootView;
}
#Override
public void onActivityCreated (Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
((MyActivity) this.getActivity()).onFragmentReady();
}
}
Activity class:
public class MyActivity extends FragmentActivity {
private void onFragmentReady() {
Log.i(TAG, "testing");
intent = new Intent(getActivity(), Contact_Developer.class);
final Button button = (Button) rootView.findViewById(R.id.btnContactDev);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(intent);
}
});
}
public class About extends Fragment {
...
}
}
I think you should use getContext() or getApplicationContext() instead of getActivity()
so code will be
Intent intent =new Intent(getContext(),Contact_Developer.class);
startActivity(intent);