android


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

Related Links

How to send image through bluetooth in android?
Change layout without calling onCreate
Android SeekBar with Thumb BELOW the bar
apply PageTransformer to PagerView as soon as possible
Android VPNService - how do I bring up the 'system-managed dialog' from my activity?
ViewPage solution
android app runs perfectly on device but always force close on emulator [closed]
Can i NOT use an array (just plain old array not an array list) with the base adapter for a list view? [closed]
Blank screen is shown when I launch the Activity that plays video from an RTSP URL using VideoView?
Android intent override all textview
AQuery shouldDelay and scroll listener
Can ImageView in android.widget.Gallery be framed with a View that includes picture title below?
How to close/finish call ended screen in android program
HTML5 audio/video loading issue in Android browsers
How do I import ViewPagerIndicator library after I imported ActionBarSherlock?
ImageView is not showing the youtube thumbnail

Categories

HOME
azure-stream-analytics
cheerio
string
fpga
phpmyadmin
interface
interpolation
settings
data-visualization
vert.x
joomla3.0
rcloud
alamofire
aws-sdk
red5
google-content-api
bourbon
visual-studio-extensions
moses
mat
datastax-enterprise-graph
scrollview
left-join
jmockit
simplecv
contacts
installscript
hivemq
parse-tree
nohup
loading
mongoose-schema
news
trigonometry
skylink
dbnull
android-checkbox
rfc3339
monkey-testing
puredata
dynamic-linq
proget
tinymce-3
java-bytecode-asm
truezip
quicktime
rocks
cronexpression
meteor-packages
segment-io
file-uri
uicolor
mapinfo
cfchart
upx
fuzzywuzzy
cycle2
crouton-os
relativelayout
atan2
concurrentmodification
morton-number
poppler
coda
google-refine
nutiteq
uvc
mmwormhole
apache-spark-1.3
sonarqube-5.0
psr-4
with-statement
template-deduction
dct
extjs2
twitter-bootstrap-rails
artemis
sphinxql
mvc-editor-templates
ui-select2
stacky
iboutlet
ilasm
automationelement
qglwidget
objective-j
request-headers
data-mapping
asynchronous-wcf-call
localizable.strings
fault-tolerance
application-planning
business-model

Resources

Encrypt Message