performClick() doesn't activate selector - android

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()

Related

onClick at button in Fragment desn't work

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.

Setting OnClickListener in a Fragment

I've looked at other questions on StackOverflow with setting OnClickListener on Fragment and I tried different solutions and it didn't work. All stated I'm supposed to set getActivity(), a fragment activity but it doesn't work. How do I set an OnClickListener in a fragment then?
/*declare variable for the button...
Declare it like this before the onCreateView method;
*/
private Button commandButton;
//Inside the onCreateView method use the below code...
commandButton = (Button) rootView.findViewById(R.id.NEXTPG);
commandButton.setOnCickListener(new OnClickListener(){
#override
public void onClick(View view){
//your commands here
}
}
you can return also layout in OnCreateView method, so use it my code are given below.
final RelativeLayout mRelativeLayout = (RelativeLayout) inflater.inflate(R.layout.activity_live_tabber,container, false);
Button mButton = (Button) mRelativeLayout.findViewById(R.id.NEXTPG);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// here you set what you want to do when user clicks your button,
}
});
return mRelativeLayout;

Android - How to uncheck radio buttons in a radio group inside a fragment?

I'm new in this world of Android and I'm learning making an app, so I got a problem.
Working on
I created a fragment and inside I have a radio group containing 3 radio buttons
Goal
Clear all the radio buttons inside the fragment when the user returns to this screen
Problem
I don't know how to achieve that
Question
How to clear all checks of the radio buttons?
Steps done
I tried the following:
Uncheck all RadioButton in a RadioButtonGroup
But it seems I can't do it
Code
This piece of code doesn't work for me (from the post above)
protected void onResume()
{
RadioGroup rg=(RadioGroup)findViewById(R.id.RG);
rg.clearCheck();
super.onResume();
}
But I have the following:
public class Operations extends Fragment
{
RadioButton surfArea, rad, diam;
RadioGroup radG;
Button openSelect;
public Operations()
{
// Required empty public constructor
}
public static Operations newInstance()
{
return new Operations();
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_operations_sphere, container, false);
surfArea = (RadioButton) rootView.findViewById(R.id.RB_surfArea);
rad = (RadioButton) rootView.findViewById(R.id.RB_Rad);
diam = (RadioButton) rootView.findViewById(R.id.RB_Diam);
openSelect = (Button) rootView.findViewById(R.id.btn_open_select);
//This piece is for testing purposes
openSelect.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if (surfArea.isChecked())
{
Intent sa = new Intent(getContext(), OperSphere.class);
startActivity(sa);
}
}
});
return rootView;
}
//Here is where I'm stuck
#Override
public void onResume()
{
super.onResume();
}
}
I'm interested in place the code inside onResume()
I know there is docs about fragments (https://developer.android.com/guide/components/fragments.html) but those can't answer my questions
Thanks in advance
1) Initialize your RadioGroup in onCreateView method as
private RadioGroup rg;
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_operations_sphere, container, false);
// initialize your radiogroup here
rg = (RadioGroup) rootView.findViewById(R.id.RG);
.....
// Rest of your code
}
2) Now call the following method i.e.,clearSelection() wherever you want to UNCHECK the RadioButtons (but after above code).
private void clearSelection(){
if(rg != null) rg.clearCheck();
}
3) Example: If you want to uncheck the RadioButtons in onResume(), you can call it from there as
#Override
public void onResume(){
super.onResume();
clearSelection();
}
If you want to do it from some other method, it would work even then as
private void myMethod(){
// need to clear radio buttons
clearSelection();
// rest of my code
}
have you tried placing your call super.onResume() before what you want to achieve...
Call clearCheck() method of radioGroup in your onResume callback, something like:
#Override
protected void onResume() {
super.onResume();
rg.clearCheck();
}
For Anybody Who is facing the same problem While navigating between fragments can do this to make it work.
radioButton.setSaveEnabled(false);
Make sure you do that for all the radio buttons in the group and not the radioGroup itself
NOTE: This flag can only disable the saving of this view; any child views may still have their state saved.

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){}
}
});
}
}

Animation on removeAllViews Android

I have a class where I include another layout on a button click. This included layout has some buttons and a code which executes on clicking these buttons. I have used a counter which indicates the number of times the button is clicked. First time clicking on the button includes the layout and the second time clicking removes the views and so on. Here's the code
public class Home extends Fragment implements OnClickListener {
int c = 0;
Button bmain, bnew, bolder;
RelativeLayout r1;
View rootView;
Animation slidedown, slideup;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.home, container, false);
bmain = (Button) rootView.findViewById(R.id.btn2);
bmain.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View arg0) {
ViewGroup con = null;
LayoutInflater layoutInflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
FrameLayout flContainer = (FrameLayout)rootView.findViewById(R.id.flContainer);
//Loading animation
slidedown = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_down);
slideup = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_up);
//The counter indicates the number of clicks.
//Needs to be replaced for a better solution.
//If it's even add view
if(c%2==0)
{
//Adding layout here
flContainer.addView(layoutInflater.inflate(R.layout.test1,con,false ));
//Starting Animation
flContainer.startAnimation(slidedown);
//After adding layout we can find the Id of the included layout and proceed from there
bnew = (Button) rootView.findViewById(R.id.btntest);
bnew.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0) {
Toast.makeText(getActivity(), "You Clicked New", Toast.LENGTH_LONG).show();
}
});
bolder = (Button) rootView.findViewById(R.id.btntest1);
bolder.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0) {
Intent form = new Intent(getActivity(),FeedbackForm.class);
startActivity(form);
}
});
c++;
} //If ends here
//If it's odd remove view
else
{
flContainer.removeAllViews();
flContainer.startAnimation(slideup);
//flContainer.removeView(flContainer);
//flContainer.removeView(layoutInflater.inflate(R.layout.test1, con, false));
c++;
}
}
}
The code at the end
flContainer.removeAllViews();
flContainer.startAnimation(slideup);
removes the view but fails to process the animation. I have tried using removeView but in that case the buttonclicks in the if statement fail to execute the second time. What am I missing here? How can I achieve it?
The answer is pretty simple. You have to remove the view after the animation is finished. This can be achieved pretty simple, first you have to set an animation listener for your animation and in the onAnimationEnd callback - which is called when the animation is finished - you remove the views.
EDIT:
Replace this:
flContainer.removeAllViews();
flContainer.startAnimation(slideup);
With this:
slideup.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
flContainer.removeAllViews();
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
flContainer.startAnimation(slideup);
If there are any further problems let me know.

Categories

Resources