onClick at button in Fragment desn't work - android

In my app I have tabbed activity with two fragments. At the fragment I want to use button to open dialog alert. But for five hours now I'm struggling with actual clicking on button. I tried ways with making onClick with xml, Implement OnClickListener But nothing worked. The button is just doing nothing. I tried to debug it and it seems that onCreateView method is not called at all. Any tips?
public class PlayersFragment extends Fragment {
public PlayersFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_players, container, false);
Button addButton = (Button) view.findViewById(R.id.add_button);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "YES is clicked!",
Toast.LENGTH_LONG).show();
}
});
return view;
}

Override the onViewCreated method in your fragment class. Then put the onClickListener on your Button with the method.

Related

How to block the click event to the background of DialogFragment around Samsung S9 Edge button

I have a UI requirement showing a blocking DialogFragment, which can not be dismissed by clicking on the background.
Also when the DialogFragment is showing, any view under it cannot be clicked.
Things are working well except on the Samsung S9 device.
On Samsung S9, which has an edge button, when clicking around the edge button's edge, the view under the DialogFragment got clicked and the event performed, which is not as expected.
Here is a sample code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MyDialogFragment myDialogFragment = new MyDialogFragment();
myDialogFragment.show(getSupportFragmentManager(), MyDialogFragment.TAG);
}
});
findViewById(R.id.background).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e("wrong", "clicked!!!!---------------");
Toast.makeText(MainActivity.this, "Click!", Toast.LENGTH_LONG).show();
}
});
}
}
The layout is quite simple, just a LinearLayout with a button inside it.
Clicking button will show the blocking dialog.
The LinearLayout id is background, which should not be clicked after the dialog has shown.
The dialog code:
public class MyDialogFragment extends DialogFragment {
public static final String TAG = "Dialog";
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setCancelable(false);
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_dialog_layout, container, false);
getDialog().setCanceledOnTouchOutside(false);
return view;
}
#Override
public void onResume() {
Window window = getDialog().getWindow();
Point size = new Point();
Display display = window.getWindowManager().getDefaultDisplay();
display.getSize(size);
window.setLayout((int) (size.x * 0.8), WindowManager.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);
super.onResume();
}
}
But now when the dialog is showing, click the area on the edge button, the background click listener can be entered, I can get the log and toast.
How to prevent this?
In the event that is firing I'd do
Fragment currentFrag = getSupportFragmentManager().findFragmentByTag(MyDialogFragment_TAG);
if (!(currentFrag instanceof MyDialogFragment))
{
// your code for when you're dialog fragment is not visible
}
setcancelable(false); can be used to block clicking on background till task completes
Give the root node an id
<androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/constraintlayoutRoot"
Add an empty OnClickListener
public View onCreateView(...) {
View view = _inflater.inflate(R.layout.fragment_xyz, _container, false);
view.findViewById(R.id.constraintlayoutRoot).setOnClickListener(view1 -> {
});

Android: "Method is never used" warning for onClick method

This is a part of my calc_fragment.xml file ,
<Button
android:id="#+id/b7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="2dp"
android:background="#drawable/roundedbutton"
android:fontFamily="sans-serif-thin"
android:text="Add"
android:textColor="#color/light_grey"
android:textSize="45sp"
android:onClick="clicked"/>
that is linked with the following fragment:
public class CalcFragment extends Fragment {
private TextView textView;
private String text;
private Vibrator vibe;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.calc_fragment, container, false);
return rootView;
}
public void clicked (View v){ <--- "Method clicked is never used"
...content...
}
I get a warning "Method clicked is never used" but I my button is linked with this method
A Fragment is tightly linked with its activity. So, the above method declaration in xml will basically look for that method in the Activity class. One of the ways is that you can explicitly link your method and the button:
public class CalcFragment extends Fragment {
private TextView textView;
private String text;
private Vibrator vibe;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.calc_fragment, container, false);
View myButton = rootView.findViewById(R.id.b7);
rootView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
clicked(v);
}
});
return rootView;
}
public void clicked (View v){ // Your method
...content...
}
Get rid of the 'android:onClick="clicked"' property in your xml.
This is because the XML code for handling clicks does not work for the Fragment class, it currently works only with the Activity class. As such, this method will never be called. See this question for more details.
In the onCreateView method of the CalcFragment, you need to declare and initialize the button after the initializing the rootView:
Button b7 = (Button) rootView.findViewById(R.id.b7);
You then need an onClickListener instead of defining your own clicked method. There are two methods to listen to a click of a button.
The first method:
You can set the onClickListener within the onCreatView as follows:
b7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Do something
}
});
The second method:
You can define an onClickListener as follows:
final View.OnClickListener b7OnClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
// Do something
}
};
When using the second method, you will need to set the onClickListener like you did in the first method however in this case you will pass the name of the onClickListener object you created.
Example:
b7.setOnClickListener(b7OnClickListener);

How to get values of edittext which is in fragment on button click which is in activity?

signup screen with of two different modules driver and customer
Hi everyone I am working in android from past one year but now I am creating an app in which am using fragments instead of activity even after serching from so many sites and blogs am unable to implement what I want.
My requirement is as you see on the image two radio button one for customer and one for driver so when user click on any one of options then signup screen appears which is on fragment and one button is there at bottom which is on activity so I want how to get all edittext value on button click so that i can send these values to the server. My main problem is getting the values of edittext fields on button click.
So any help will be appriciated.
thanks in advance
Try like this
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.your_fragment, container, false);
this.initViews(rootView);
selectProfessionsTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
whta_you_want_on_button_click();
}
});
}
private void initViews(View view) {
//define your edittext like FirstNameEditText = (EditText) view.findViewById(R.id.FirstNameEditText);
}
now define the whta_you_want_on_button_click() method
in your activity, put below code in your button's click event.
try {
Fragment rg = getSupportFragmentManager().findFragmentByTag("YourFragmentTag");
if (rg != null) {
EditText et = (EditText) ((YourFragment) rg).getView().findViewById(R.id.edittext);
String etValue = et.getText().toString();
}
} catch (Exception e) {
}
Your context will change from getApplicationContext() to getActivity.getApplicationContext() when you are referencing a fragment instead of an activity
You have to create your edittext in this fashion
While assigning the layout you need to assign in this way.
View view = inflater.inflate(R.layout.fragmentlayout,container,false);
And while assigning edittext you need to take it as below
EditText edt = (EditText) view.findViewById(R.id.yourid);
String str = edt.getText().toString();
So on button click you can get the edittext string easily from onClick method with above code.
You can use static keyword make all edittext static so you will get all edittext values in activity on button click.
in your fragment suppose MyFragment.java
public class MyFragment extends Fragment{
View rootView;
public static EditText edt;
public MyFragment() {
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_main, container, false);
init();
return rootView;
}
protected void init(){
edt = (EditText) rootView.findViewById(R.id.edt);
}
}
in your activity suppose MainActivity.java
public class MainActivity extends Activity{
Button start;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewByid(R.id.button);
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try{
String value = MyFragment.edt.getText().toString().trim();
Toast.makeText(getApplicationContext(), value, Toast.LENGTH_SHORT).show();
}catch(Exception e){}
}
});
}
}

Android: Button OnClick not working in SherlockFragment

I've tab activity in which i want to use a button, but when i click on button, it force closes the app. Can you please tell me what is going on, i am new to Android.
public class HomeActivity extends SherlockFragment {
private Button bt;
private Context con;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.home_layout, container, false);
bt = (Button)rootView.findViewById(R.id.btn);
bt.setOnClickListener(new View.OnClickListener(){
#Override public void onClick(View v)
{
Toast.makeText(con, "hello", Toast.LENGTH_LONG).show();
}
});
return rootView;
}
}
when i click on button, it force closes the app
Because con object of Context is null. which is used for showing Toast message in Button onClick method.
Do it as:
Toast.makeText(v.getContext(), "hello", Toast.LENGTH_LONG).show();
And initialize con object by calling getActivity() in onCreateView as:
con=getActivity();
If you can avoid that, don't keep a reference to the context; instead of the
private Context con;
get the current Activity in the code, so use
Toast.makeText(getActivity(), "hello", Toast.LENGTH_LONG).show();
Btw, this seems to be the cause of your crash, as Context is null.

performClick() doesn't activate selector

I created a layout with a radiogroup cotaining two buttons. Then i got a drawable selector.
I applied the selector with android:background and withandroid:button to remove the little circles.
In the onCreate method i press one button via button.performClick() but the layout of the button doesn't change. Even if i call button.setChecked(true) it won't change.
Actually it works when the fragment is first created. But if i press the second button, load another fragment and come back with the backbutton, the second button is still pressed.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View f = inflater.inflate(R.layout.fragment_season, container,
false);
final RadioButton bt = (RadioButton) f.findViewById(R.id.bu_season_tabelle);
final RadioButton bs = (RadioButton) f.findViewById(R.id.bu_season_spielplan);
bt.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setTabelle(f);
v.setClickable(false);
bs.setClickable(true);
}
});
bs.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setSpielplan(f);
v.setClickable(false);
bt.setClickable(true);
}
});
bt.performClick();
return f;
}
Would be nice if someone can help me.
I found the solution:
Instead of calling button.performClick() in onCreateView() i have to call it in onStart()

Categories

Resources