Im working in a fragment where I want the user to input an answer and then when clicking a button, a textview should show Correct.
This is my code
public class FragmentOne extends Fragment {
private EditText userAnswer;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_fragment_one, null);
userAnswer = (EditText)v.findViewById(R.id.editText);
final String theAnswer = (userAnswer.getText().toString());
Button submit = (Button)v.findViewById(R.id.submitBtn1);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (theAnswer.equalsIgnoreCase("Germany")) {
TextView tv = (TextView)v.findViewById(R.id.showCorrect);
tv.setText("Correct!");
}
}
});
return v;
}
}
However, when Germany is typed, the textView is still just showing 'x' (which I set it to show originally)
Relevant XML's are
<Button
android:text="Submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:id="#+id/submitBtn1"
android:layout_below="#+id/editText"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="220dp"
android:layout_height="70dp"
android:inputType="textPersonName"
android:hint="Write answer and press enter"
android:ems="10"
android:id="#+id/editText"
android:textSize="16sp"
android:layout_below="#+id/imageView"
android:layout_centerHorizontal="true" />
<TextView
android:text="x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/showCorrect"
android:layout_below="#+id/skipBtn"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp" />
What has gone wrong?
You have to change this line :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_fragment_one, null);
userAnswer = (EditText)v.findViewById(R.id.editText);
TextView tv = (TextView)v.findViewById(R.id.showCorrect);
Button submit = (Button)v.findViewById(R.id.submitBtn1);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String theAnswer = (userAnswer.getText().toString());
if (theAnswer.equalsIgnoreCase("Germany")) {
tv.setText("Correct!");
}
}
});
return v;
}
i.e : in your code userAnswer value is always the same (not checked on Button click)
Code needed to be changed to:
public class FragmentOne extends Fragment {
private EditText userAnswer;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_fragment_one, null);
userAnswer = (EditText)v.findViewById(R.id.editText);
final TextView tv = (TextView)v.findViewById(R.id.showCorrect); //changed
Button submit = (Button)v.findViewById(R.id.submitBtn1);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String theAnswer = (userAnswer.getText().toString());
if (theAnswer.equalsIgnoreCase("Germany")) {
//TextView tv = (TextView)v.findViewById(R.id.showCorrect);
tv.setText("Correct!");
}
// updateScore();
}
});
return v;
}
Related
I created a Fragment and called it Kabar and I can see that the Button and the TextView appears as expected.
Here is my Fragment:
public class Kabar extends Fragment {
private Button button;
private TextView text;
public Kabar() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_kabar , container, false);
return v;
}
}
This is my fragment_kabar layout:
<FrameLayout
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"
tools:context="com.examplee.my.Kabar">
<!-- TODO: Update blank fragment layout -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="mohsen kabar" />
<Button
android:id="#+id/tt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="113dp"
android:text="Button"
android:layout_below="#+id/mohsenkabar"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="93dp"
android:layout_marginEnd="93dp" />
</RelativeLayout>
I need to use the Button and the TextView.
Where can I place this code?
text=(TextView) text.findViewById(R.id.tt1);
button=(Button) button.findViewById(R.id.tt2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tt1.setText("kkk mmm bbb ddd");
}
});
There's two options onCreateView and onViewCreated (preferred) methods.
void onViewCreated (View view,
Bundle savedInstanceState)
Called immediately after onCreateView(LayoutInflater, ViewGroup,
Bundle) has returned, but before any saved state has been restored in
to the view. This gives subclasses a chance to initialize themselves
once they know their view hierarchy has been completely created. The
fragment's view hierarchy is not however attached to its parent at
this point.
try this add this code in your kabar fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_kabar , container, false);
TextView text=(TextView) v.findViewById(R.id.tt1);
TextView button=(TextView) v.findViewById(R.id.tt2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text.setText("kkk mmm bbb ddd");
}
});
return v;
}
You should Add your code in the OnCreateView in your Kabar Fragment:
public class Kabar extends Fragment {
private Button button;
private TextView text;
public Kabar() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_kabar , container, false);
TextView text = (TextView) v.findViewById(R.id.tt1);
Button button = (Button) v.findViewById(R.id.tt2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text.setText("kkk mmm bbb ddd");
}
});
return v;
}
}
Replace your code with folowing in onCreateView
text =(TextView) v.findViewById(R.id.tt1);
button=(Button) v.findViewById(R.id.tt2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text.setText("kkk mmmm bbbb ddd");
}
});
make this change in your code
text=(TextView) v.findViewById(R.id.tt1);
button=(Button) v.findViewById(R.id.tt2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text.setText("kkk 'mmm' 'bbb' 'ddd'");
}
});
I want to change a text of TextView on clicking a button. I have 2 buttons in my fragment and both have an onClick method for same TextView. Here is in detail what's happen in my fragment.
I set a default text to TextView means 1st text.
When 1st button clicked the 2nd text will appear on the TextView.
When 2nd button clicked again 1st text will appear on the TextView.
Here is my Fragment code
#Override
public void onCreate(Bundle savedInstanceState) {
Log.d("Rahul", "On Ganpati 1 fragment On Create ");
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("Rahul", "On Ganpati 1 fragment On Create View ");
// Inflate the layout for this fragment
final TextView textView = (TextView)getView().findViewById(R.id.g1TextView);
textView.setText(getString(R.string.Ganpati_1));
Button marathi = (Button)getView().findViewById(R.id.g1Marathi);
marathi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText(getString(R.string.Ganpati_1));
}
});
Button english = (Button)getView().findViewById(R.id.g1English);
english.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText(getString(R.string.GanpatiE));
}
});
return inflater.inflate(R.layout.fragment_ganpati_1, container, false);
}
Here is the XML file
<FrameLayout 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"
tools:context="com.example.lenovo.shopping.Shankar">
<Button
android:layout_width="168dp"
android:layout_height="50dp"
android:text="Left"
android:id="#+id/g1Marathi"
android:textAlignment="center"
android:layout_gravity="left|top" />
<Button
android:layout_width="168dp"
android:layout_height="50dp"
android:text="Right"
android:id="#+id/g1English"
android:textAlignment="center"
android:layout_gravity="right|top" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:paddingBottom="50dp"
android:id="#+id/scrollView"
android:layout_gravity="end|fill_vertical">
<TextView
android:id="#+id/g1TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/TextView" />
</ScrollView>
</FrameLayout>
When use Fragment, you need to override onCreateView and return View like this :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_ganpati_1, null);
Log.d("Rahul", "On Ganpati 1 fragment On Create View ");
// Inflate the layout for this fragment
final TextView textView = (TextView)view.findViewById(R.id.g1TextView);
textView.setText(getString(R.string.Ganpati_1));
Button marathi = (Button)view.findViewById(R.id.g1Marathi);
marathi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText(getString(R.string.Ganpati_1));
}
});
Button english = (Button)view.findViewById(R.id.g1English);
english.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText(getString(R.string.GanpatiE));
}
});
return view;
}
I have three buttons in a custom list view, and list view is in a fragment not in an activity, but when I set onclick listner . it throws exception null object reference passed.
custome_list_view.xml
<Button
android:text="#string/pickUpButtonText"
android:textColor="#FFFFFF"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="#dimen/standard_hight"
android:background="#drawable/buttonshape"
android:shadowColor="#5BA84F"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:id="#+id/button_pickup_list"
/>
customeListView.Java
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_jobs, container, false);
ArrayList<JobListDataProvider> listJobs = GetRequests();
ListView lv = (ListView)rootView.findViewById(R.id.listViewJobs);
lv.setAdapter(new JobsListAdapter(getActivity(), listJobs));
//.....bindind on click....
Button pickup = (Button) rootView.findViewById(R.id.button_pickup_list);
pickup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//......................
}
});
return rootView;
}
you have to put this code in JobsListAdapter in public View onCreateView
Button pickup = (Button) rootView.findViewById(R.id.button_pickup_list);
pickup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//......................
}
});
I want to call the LocationActivity when the user click on the button and get the location information and set it into my button.
xml file
<Button
android:id="#+id/locationTxt"
android:text="#string/setLocation"
android:textStyle="bold"
android:layout_weight="0.5"
android:layout_width="8dp"
android:layout_height="40dp"
android:layout_marginTop="4dp"
android:layout_marginLeft="18dp"
android:layout_marginRight="18dp"
android:editable="false"
android:gravity="center"
android:background="#drawable/btn_shape" />
Java file
public class AddTask extends android.app.Fragment {
Button btnLocation;
public AddTask()
{
// Constructor
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.add_task, container, false);
btnLocation = (Button) v.findViewById(R.id.locationTxt);
btnLocation.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
The click listener in my fragment isn't working properly. I'm not getting any debug messages in my logcat whatsoever from my requestData method, nor my onClick method. I was thinking it was probably due to trying to use activity methods from the fragment, but I don't see any problems with doing that; it also wouldn't affect my onClick debug messages not showing up.
Activity:
public class MainActivity extends AppCompatActivity {
public void requestData(String first, String second) {
Log.d(“Success!”, first + “ “ + second);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
}
Fragment:
public class WelcomeFragment extends Fragment implements View.OnClickListener{
private Button mButton;
private EditText mFirstEditText;
private EditText mSecondEditText;
private View v;
public WelcomeFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_main, container, false);
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mCalculateButton = (Button) v.findViewById(R.id.calculate_button);
mCalculateButton.setOnClickListener(this);
}
#Override
public void onClick(View view) {
mFirstEditText = (EditText) v.findViewById(R.id.first_editText);
String first = mFirstEditText.getText().toString();
Log.d("Button clicked", "mFirstEditText set " + first);
mSecondEditText = (EditText) v.findViewById(R.id.second_editText);
String second = mSecondEditText.getText().toString();
Log.d("Button clicked", "mSecondEditText set " + second);
v.requestData(first, second);
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<fragment
android:id="#+id/welcome_fragment"
android:name="com.example.kent.AnApplication.WelcomeFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
welcome_fragment.xml
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivityFragment">
<TextView
android:text="#string/welcome "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/welcome_textView"
android:textSize="20sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="68dp"/>
<EditText
android:layout_width="280dp"
android:layout_height="wrap_content"
android:inputType="text"
android:ems="10"
android:id="#+id/first_editText"
android:layout_marginTop="60dp"
android:layout_below="#+id/welcome_textView"
android:layout_centerHorizontal="true"/>
<EditText
android:layout_width="280dp"
android:layout_height="wrap_content"
android:inputType="text"
android:ems="10"
android:id="#+id/second_editText"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/calculate_button"
android:layout_marginTop="43dp"
android:layout_below="#+id/second_editText"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
You didn't set clicked listener. Try below code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_main, container, false);
mButton = (Button) v.findViewById(R.id.your_button_id);
mButton.setOnClickListener(this);
return v;
}
If you have some "clicked" events, you have to use if/else or switch on your onClick(); method.
Change your Fragment code to following:
public class WelcomeFragment extends Fragment implements View.OnClickListener{
private Button mButton;
private EditText mFirstEditText;
private EditText mSecondEditText;
private View v;
public WelcomeFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_main, container, false);
return v;
}
#Override
public void onViewCreated (View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mCalculateButton = (Button) view.findViewById(R.id.calculate_button);
mCalculateButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick (View v) {
mFirstEditText = (EditText) v.findViewById(R.id.first_editText);
String first = mFirstEditText.getText().toString();
Log.d("Button clicked", "mFirstEditText set " + first);
mSecondEditText = (EditText) v.findViewById(R.id.second_editText);
String second = mSecondEditText.getText().toString();
Log.d("Button clicked", "mSecondEditText set " + second);
v.requestData(first, second);
}
});
}
}
The problem is in your onclick method, you haven't find the id of that Button on which click has occurred, change your onclick() method code as:
#Override
public void onClick(View view) {
if(view.getId() == R.id.calculate_button) {
mFirstEditText = (EditText) v.findViewById(R.id.first_editText);
String first = mFirstEditText.getText().toString();
Log.d("Button clicked", "mFirstEditText set " + first);
mSecondEditText = (EditText) v.findViewById(R.id.second_editText);
String second = mSecondEditText.getText().toString();
Log.d("Button clicked", "mSecondEditText set " + second);
v.requestData(first, second);
}
}
I hope it'll work for you