two onClick view in fragment for different purpose - android

My project needs two onClick event. One is for image browse and another is to submit. When I add two onClick event error is generated 'onClick(View)' is already defined in 'com.' When I search solution of this problem, It is suggested to combine two method with same parameters, but how ?
public class News_Yeb extends Fragment implements OnClickListener {
Button bt_register;
TextInputLayout til_name, til_username;
ImageView iv_profile;
String name, username, password, email, mobile, profile, confirm;
RequestQueue requestQueue;
boolean IMAGE_STATUS = false;
Bitmap profilePicture;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View RootView = inflater.inflate(R.layout.news_yeb, container, false);
til_name = RootView.findViewById(R.id.til_name_reg);
til_username = RootView.findViewById(R.id.til_username_reg);
bt_register = RootView.findViewById(R.id.bt_register);
iv_profile = RootView.findViewById(R.id.im_profile);
bt_register.setOnClickListener(this);
iv_profile.setOnClickListener(this);
return RootView;
}
public void onClick(View iv_profile) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, 1000);
}
#Override
public void onClick (View bt_register){
name = til_name.getEditText().getText().toString();
username = til_username.getEditText().getText().toString();
password = til_password.getEditText().getText().toString();
if (
validateName(name) &&
) .....

Dont use seperate onClick methods. Use this approach :
1) Implement class by View.OnClickListener
2) apply listener to yourButton.setOnClickListener(this) OR yourImage.setOnClickListener(this);
3) implement onClick method:
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.yourButton1:
//do your stuff
break;
case R.id.yourImage:
//do your stuff
break;
}
}

In one of the activities you might have
android:onClick = "onClick"
Replace one of these by, for instance,
android:onClick = "onClick2"
And rename the corresponding onClick method to onClick2:
public void onClick(View iv_profile){
...
}
public void onClick2 (View bt_register){
...
}

Related

How to get the text entered in an edittext?

I want to get the data entered in an edittext of android fragment in that same fragment of android.
I want that input details and send to a ble device.
Code is here:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
setCancelable(false);
View view = inflater.inflate(R.layout.verification, null);
verify=(EditText)view.findViewById(R.id.verify);
Next =(Button) view.findViewById(R.id.next);
Cancel=(Button) view.findViewById(R.id.cancel);
Next.setOnClickListener(this);
Cancel.setOnClickListener(this);
appContext = AppContext.getContext();
mContext=getActivity();
appContext = AppContext.getContext();
mOnDataSendListener = appContext.getOnDataSendListener();
timer();
Time=(TextView) view.findViewById(R.id.time);
return view;
}
#Override
public void onClick(View view) {
if(view.getId()==R.id.next){
factoryReset();
}
else {
dismiss();
}
}
Get verify data when you call method factoryReset() & send data as a parameter. Use below line to getText from editText:
verify.getText().toString()
Try with below code :
#Override
public void onClick(View view) {
if(view.getId()==R.id.next){
factoryReset(verify.getText().toString());
}
else {
dismiss();
}
public void factoryReset(String str1){
Log.e("VERIFY_DATA",""+str1);
}

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

Exception when showing a dialog after orientation change

I have an activity and a fragment inside it.inside fragment, there is a button, and on click of button a dialog shows.
Everything works, until user do a orientation change and click button after it.
IllegalStateException(cannot perform this action after onsaveinstancestate) occurs when user clicks button after orientation change. I'm using android support framework.
Anybody have any idea regarfing this?
Activity Code
public void openMoreDialog(String shareData, String link) {
DialogFragment dialog = new MoreDialog(shareData, link);
dialog.show(getSupportFragmentManager(), "MoreDialog");
}
Fragment Code
public void onAttach(Activity activity) {
super.onAttach(activity);
mControl = (ActivityControl)activity;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
ImageButton moreButton = (ImageButton)v.findViewById(R.id.moreButton);
moreButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mControl.openMoreDialog(shareData, link);
}
});
return rootView;
}
FragmentDialog code
public class MoreDialog extends DialogFragment {
private String mShareData;
private String mLink;
public MoreDialog(String shareData, String link){
mShareData = shareData;
mLink = link;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.more_dialog, null);
Button openBtn = (Button)dialogView.findViewById(R.id.openBtn);
openBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
openLink(mLink);
}
});
Button shareBtn = (Button)dialogView.findViewById(R.id.shareBtn);
shareBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
shareNews(mShareData);
}
});
builder.setView(dialogView);
return builder.create();
}
private void openLink(String link){
}
private void shareNews(String data){
}
}
Helpful link & solution how to:
https://stackoverflow.com/a/17413324/619673 and btw, constructor in fragment must be empty! Documentation:
http://developer.android.com/reference/android/app/Fragment.html
public Fragment ()
Added in API level 11
Default constructor.
Every fragment must have an empty constructor, so
it can be instantiated when restoring its activity's state. It is
strongly recommended that subclasses do not have other constructors
with parameters, since these constructors will not be called when the
fragment is re-instantiated; instead, arguments can be supplied by the
caller with setArguments(Bundle) and later retrieved by the Fragment
with getArguments().
Applications should generally not implement a constructor. The first
place application code an run where the fragment is ready to be used
is in onAttach(Activity), the point where the fragment is actually
associated with its activity. Some applications may also want to
implement onInflate(Activity, AttributeSet, Bundle) to retrieve
attributes from a layout resource, though should take care here
because this happens for the fragment is attached to its activity.

noob: android button

I have two button b1 and b2 .If button b1 is pressed then perform a query q1 else if button b2 is pressed then perform an another query q2.
if(b1_click)
{
mCursor=//query
}
else if(b2_click)
{
mCursor=//query
}
Please tell me how can i implement this.How to implement b1_click method or any inbuilt method which tell that button is pressed.I tried
Cursor c;
c=//querys
if(b1.isPressed())
{
next.setOnClickListener
(
new View.OnClickListener()
{
#Override public void onClick(View v) {
c=db.getData1(); (getData1 method return cursor)
}
}
);
}
tv.append(c.getString(column_number) (tv=TextView)
"Same as above for b2"
It is saying that cursor (c) should be final
Help?
First of all I would recommend going through some tutorials Hello Android and you could also follow the code and samples in Common Tasks and How to do them in Android.
If you would read this carefully you would know that is very simple, something like this:
public class MyActiviy extends Activity implements OnClickListener{
protected void onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
setContentView(R.layout.myLayout);
findViewById(R.id.Button1).setOnClickListener(this);
findViewById(R.id.Button2).setOnClickListener(this);
//more code...
}
public void onClick(View v){
switch(v.getId()){
case R.id.Button1:
//Button1 pressedd...do stuff
break;
case R.id.Button2:
//Button2 pressed...do some other stuff
break;
default:
break;
}
}
}
Try creating your OnClickListeners as private classes:
private class Button1ClickedListener implements OnClickListener {
public void onClick( View v ) {
//Do what needs to be done when button 1 is clicked.
}
}
private class Button2ClickedListener implements OnClickListener {
public void onClick( View v ) {
//Do what needs to be done when button 2 is clicked.
}
}
Then you set the onClick-listeners:
b1.setOnClickListener( new Button1ClickedListener() );
b2.setOnClickListener( new Button1ClickedListener() );
If you need the OnClickListeners to be able to use the cursor, you just need to declare it as a private field in the parent class of the OnClickListeners.

Categories

Resources