When declaring buttons different fragments does not recognize - android

I have a problem when declaring a button.
I will try to explain as specific as possible.
In my main Layout I have a Fragment containing a secondary Layout. In which I have several buttons.
My intention is that my main fichero.java to declare the buttons within the fragment.
Here we put the main Layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false">
<fragment
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:name="josejuansosarodriguez.radioecca.conocecanarias.TrueoFalseFragment"
android:id="#+id/fragmentTrueFalse"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Grp1"
android:id="#+id/textGrp1"
android:textSize="25sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"/>
<fragment
android:layout_width="fill_parent"
android:layout_height="450dp"
android:name="josejuansosarodriguez.radioecca.conocecanarias.Grp1FragmentP1"
android:id="#+id/fragmetaskGRP1"
android:layout_below="#+id/textGrp1"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp" />
</RelativeLayout>
Here we put the secondary Layout that has the buttons:
<?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:id="#+id/fragment_TrueorFalse">
<Button
android:layout_width="120sp"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="#string/buttontrue"
android:id="#+id/buttontrue"
android:layout_marginLeft="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" />
<Button
android:layout_width="120sp"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="#string/buttonfalse"
android:id="#+id/buttonfalse"
android:layout_marginRight="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true" />
</RelativeLayout>
And here I leave the my main activity:
public class Grp1Fragment extends Fragment {
private Button buttonTrue;
private Button buttonFalse;
private Button buttonNextAsk;
private View view;
public Grp1Fragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
buttonTrue = (Button) view.findViewById(R.id.buttontrue);
buttonTrue.setOnClickListener(this);
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_grp1, container, false);
return view;
}
My problem I find in the following line:
buttonTrue.setOnClickListener (this);
The message reads: View can not be in Applied to josejuansosarodriguez.radioecca.conocecanarias.Grp1Fragment
I hope I have spread far.
Thank you very much for everything, this forum is amazing.

Try this:
Note that you have to FIRST inflate the layout to make accesible the widgets of the layout.
Then you can "bind" the widgets, and finallly in this case, set the listeners to the buttons.
I set you a Log, if you need change it to Toast, or code
public class Grp1Fragment extends Fragment {
private Button buttonTrue;
private Button buttonFalse;
private Button buttonNextAsk;
private View view;
public Grp1Fragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_grp1, container, false);
//Declare your widgets (Buttons)
buttonTrue = (Button) view.findViewById(R.id.buttontrue);
buttonFalse = (Button) view.findViewById(R.id.buttonfalse);
//Set the Listeners
buttonTrue.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Toast or Log, or whateber you need
Log.d("Fragment1" , "Button: True");
}
});
buttonFalse.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Toast or Log, or whateber you need
Log.d("Fragment1" , "Button: FAlse");
}
});
//return the view
return view;
}

void setOnClickListener(View.OnClickListener l)
requires a OnClickListener, which Grp1Fragment is not.
Use something like
buttonTrue.setOnClickListener(new View.OnClickListener() {
void onClick(View v) {
... // reaction on the button
}
});

Related

Button click event not fired in Fragment

I have a problem when I insert this code in the fragment
public class CameraConnectionFragment extends Fragment{
boolean clicked = false;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.camera_connection_fragment, container, false);
Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
clicked = true;
}
});
return view;
}
}
The problem is essentialy that the variable clicked doesn't change when I press the button when the application is running. I've put an if statement which evaluate the clicked variable but it's always set to false even if I keep pressing the button.
The code where the button is located (camera_connection_fragment) is the following:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<org.tensorflow.demo.AutoFitTextureView
android:id="#+id/texture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
<org.tensorflow.demo.RecognitionScoreView
android:id="#+id/results"
android:layout_width="match_parent"
android:layout_height="112dp"
android:layout_alignParentTop="true" />
<org.tensorflow.demo.OverlayView
android:id="#+id/debug_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="25dp"
android:layout_marginEnd="135dp"
android:text="Button"
android:onClick="getMe"/>
</RelativeLayout>
Do you have any idea why this is happening?
Where do you put the if statement that checks the clicked variable? Maybe, your code already passes the if statement before your click listener is triggered. Look at the following code:
boolean clicked = false;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.camera_connection_fragment, container, false);
Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Put your logic in the if(clicked) control.
clicked = true // you can delete this line
(your logic)
}
});
return view;
}
#Override
protected void onStart() {
super.onStart();
//You can delete the below if check and move your logic to onClick method
if(clicked) {
(your logic)
}
}
In the above code, clicked variable check in onStart always false, since it is already executed before your click listener is triggered. If it is possible you can check clicked variable in click listener's onClick method. If you share the place where you put your if logic we can provide more proper answer.

OnClick Listener is not triggered inside a fragment with View Pager

I'd like to be able to set a View.OnclickListener to an ImageView inside a page (fragment) of my ViewPager, I don't want to click on the whole page of my ViewPager, I just want to be able to click on a specifig ImageView. I followed these questions (onClick not triggered on LinearLayout with child, How to set OnClickListener in ViewPager
) but they did not help me. This is my actual code:
MyPageFragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="240dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:focusableInTouchMode="true"
android:id="#+id/container_linear"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop" />
</LinearLayout>
</LinearLayout>
MyActivity.xml
<android.support.v4.view.ViewPager
android:id="#+id/product_view_pager"
android:layout_width="match_parent"
android:layout_height="240dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:gravity="center"
android:overScrollMode="never" />
MyFragment.class (where I'm binding the views using Butterknife)
#BindView(R.id.container_linear)
LinearLayout mContainer;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View mRootView = inflater.inflate(R.layout.my_layout, container, false);
ButterKnife.bind(this, mRootView);
mContainer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("Test", "Clicked!");
}
});
}
Thanks for your support.
One thing to consider would be moving your OnClickListener to the parent LinearLayout you're setting as clickable and seeing if that helps.
Another thing is adding android:focusableInTouchMode="true" to the view you're attaching your OnClickListener to.
Other than that, you might want to share the code for registering the OnClickListener so we can investigate other possible errors.
If I understand correctly you want to click on the image view insideMyPageFragment.xml. This is simple but you need to give ID to the ImageView and refer it in your onCreateView method like you do for LinearLayout and then use onClickListener on that image.
So with recent conversation it seems that in linear layout you need to remove line clickable="false"
Basically that has blocked all the clicks on its children, thus that linear layout as well as the imageview clicks are not working for you. See this code I removed that line. Hope this works
Follow this code.
MyPageFragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:duplicateParentState="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="240dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:focusableInTouchMode="true"
android:id="#+id/container_linear"
android:gravity="center"
android:orientation="vertical">
<--you need to give ID to the view to be able to
perform events on them specifically.-->
<ImageView
android:id="#+id/my_awesome_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop" />
</LinearLayout>
Now in your MyFragment.class
#BindView(R.id.container_linear)
LinearLayout mContainer;
//declare with butterknife
#BindView(R.id.my_awesome_image)
ImageView myAwesomeImage;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View mRootView = inflater.inflate(R.layout.my_layout, container, false);
ButterKnife.bind(this, mRootView);
//you set click listener previously on entire linear layout instead of imageview
//that's why it was not reflecting on imageview click.
myAwesomeImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG , "Awesome Imageview clicked!");
}
});
}
I solved this issue using the following chunk of code:
#Override
public void onViewCreated(View view, Bundle savedInstanceState){
view.setOnClickListener(new OnClickListener(){
public void onClick(View v){
/* Do as you please here. */
}
});
}
The reference for this answer can be viewed here: How to detect click on ImageView in ViewPager's PagerAdapter (android)?

Android OnClick not working on android 4.3

I have a fragment inside the main activity.The fragment contains an image view.My aim is to divide the image view into 3 different buttons.So ,i set up a linear layout which overlaps the image view and it contains 3 clickable views with equal gravity.
In the fragment i implements OnClickListener ,as this-
public class FragmentHomeMenu extends Fragment implements OnClickListener {
View view;
Context activity_context;
View button_search_1;
View button_search_2;
View button_search_3;
...plenty of other stuff
public FragmentHomeMenu() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_home_menu, container, false);
activity_context = getActivity().getApplicationContext();
button_search_1 = (View) view
.findViewById(R.id.button_search_1);
button_search_1.setOnClickListener(this);
button_search_2 = (View) view.findViewById(R.id.button_search_2);
button_search_2.setOnClickListener(this);
button_search_3 = (View) view.findViewById(R.id.button_search_3);
button_search_3.setOnClickListener(this);
....plenty of other stuff
return view;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
activity_context = activity.getApplicationContext();
}
#Override
public void onClick(View v) {
Log.w("", "some thing was clicked");
switch (v.getId()) {
case R.id.button_search_2:
dostuff1();//stuff1 is not happening in android 4.3,but works on 4.0
break;
case R.id.button_search_1:
dostuff2();//stuff2 is not happening in android 4.3,but works on 4.0
break;
......plenty of other stuff
}
I checked for the problem on plenty of places but could not find solution.My code works perfect on
devices with android 4.0 but in 4.3 nothing happens when i click on the image.
here is the xml-
.......
<ImageView
android:id="#+id/imageview_searchbar"
android:layout_width="370dp"
android:layout_height="wrap_content"
android:layout_below="#+id/rltv1"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:adjustViewBounds="true"
android:clickable="true"
android:src="#drawable/search_bar" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageview_searchbar"
android:layout_alignLeft="#+id/imageview_searchbar"
android:layout_alignRight="#id/imageview_searchbar"
android:layout_alignTop="#+id/imageview_searchbar"
android:orientation="horizontal"
android:weightSum="21" >
<View
android:id="#+id/button_search_1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="7"
android:clickable="true" />
<View
android:id="#+id/button_search_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="7"
android:clickable="true" />
<View
android:id="#+id/button_search_3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="7"
android:clickable="true" />
</LinearLayout>
.......lots of other stuff
I figured it out-I just had to bring the linear layout(containing buttons) to the front.
It got hidden under the image when using android 4.3 so i did this-
LinearLayout search_bar_buttons = (LinearLayout) view.findViewById(R.id.ll_search_bar_buttons);
search_bar_buttons.bringToFront();
Thank you guys for helping

Fragment is not fully covering parent activity

There is Button in Activity when we click on Button fragment's view overlaps Activity's view .
When Button is clicked it is forwarded to Fragment. Problem is it overlaps with Button.
In MainActivity i have created Button and set listener on that when user click on Button it is forwarded to fragment file which contain only textview.
MainActivity.java:
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
fragment fr = new fragment();
getSupportFragmentManager().beginTransaction().add(R.id.parent_frame, fr).commit();
}
});
}
}
fragment.java
public class fragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
return view;
}
}
fragment.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">
<TextView
android:id="#+id/detailsText"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:text="Default Text ggggggg"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30dip" />
</RelativeLayout>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press to update"
/>
<fragment
class="com.example.demo3.fragment"
android:id="#+id/parent_frame"
android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
Why aren't you using the Activity merely as a container, and use Fragments for ALL ui elements, including the button you are clicking to change between them?
Something like this: Programatically switching between Fragments
this is unique thought and i appreciate it.if you want to do this just follow below steps:
1) in your activity main take framelayout with height width system fit(matchparent) and id is parent_frame
2) take button inside it and when you click on it set button visible to gone
3) then you can add fragment to this framlayout
Make sure in your activity_main must contain framlayout with matchparent

add android button's on click event in a separate class

I have a button in android which i have defined in fragment_main.xml as -
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/love_button_text"
android:onClick="onLoveButtonClicked"
/>
the main_activity xml file is as follows -
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.newa.newapp2.MainActivity"
tools:ignore="MergeRootFrame" >
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100sp"
android:text="#string/a_button"
android:onClick="onLoveButtonClicked1"
/>
</FrameLayout>
Now as there is a onClick event defined i wanted to define in a separate class file as follows -
public class AndroidLove extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onLoveButtonClicked1(View view) {
TextView tv = (TextView) findViewById(R.id.anotherText);
tv.setVisibility(View.VISIBLE);
}
}
This compiles fine but at run time throws the error
"could not find the event listener named onLoveButtonClicked"
This however is working fine if i write the function in my MainActivity.java
Any ideas?
Thanks
Use an anonymous inner class in your fragment class.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
Button button = (Button)view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//your code here
}
}
}
Lastly remove this from your xml:
android:onClick="onLoveButtonClicked"
Hello the Button is in your fragment_main.xml file
and you are setting content view in activity_main to AndroidLove activity.
i think you need to write onLoveButtonClicked() method inside Fragment not in activity.
Method name from xml and java file must same.
activity/fragment that set that xml file as content view it must be have same name method with view as params.
Thanx.

Categories

Resources