Reputation: 7372
I have an activity with a fragment. Upon a click on a fragment element an onclick event is fired and causes the activity to replace fragment.
The wierd thing is that the event is fired on the activity class, but that not is the main problem.
On the second activity which I just created i set onclick event on the xml but it doesnt get fired. I also tried to put the same handler on the activity (as it works for the 1st fragment) but it doesnt help.
To sum it up I have 3 layout: activity, 1st fragment which loads as the activity created, and 2nd fragment which gets replace on a click on the 1st fragment.
2 classes: Activity class (1st fragment has no class for it), 2nd fragment class which contains the handler.
I feel like the construction of the application is messed up so dont spare on the tips...
here is the code:
activity:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/chat_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MergeRootFrame"
/>
public class Chat extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.chat_container, new PlaceholderFragment())
.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.chat, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_chat, container, false);
return rootView;
}
}
public void contactClicked(View v){
Fragment fragment = new ConversationFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.chat_container, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
public void click(View v){
Toast.makeText(this, "click", Toast.LENGTH_LONG);
}
}
1st fragment:
<LinearLayout
android:id="@+id/main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:onClick="contactClicked" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher"
android:contentDescription="contactPicture" />
<TextView
android:layout_width="112dp"
android:layout_height="wrap_content"
android:text="contactName" />
</LinearLayout>
</LinearLayout>
2nd fragment
<LinearLayout
android:id="@+id/conversation"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:text="koko"
android:onClick="click" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="MMMMM" />
</LinearLayout>
public class ConversationFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_conversation, container, false);
}
public void click(View v){
Toast.makeText(getActivity(), "click", Toast.LENGTH_LONG);
}
}
thanks for the effort for reading so far...
Upvotes: 1
Views: 1127
Reputation: 2693
change your method name from click to contactClicked
public void contactClicked(View v){
Toast.makeText(getActivity(), "click", Toast.LENGTH_LONG);
}
Upvotes: 0
Reputation: 128428
I am sure you are saying that the below click event which you have defined in the ConversationFragment
isn't getting executed.
public void click(View v){
Toast.makeText(getActivity(), "click", Toast.LENGTH_LONG);
}
If this the case then you have to declare this method inside the parent Activity from where you are replacing fragments. So over and all, android:onClick
works directly for Activity.
Or If you want to define click listener for views then you have set onClickListener by using setOnClickListener
in the fragment itself.
Upvotes: 1