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;
}
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'");
}
});
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;
}
I am adding a fragment to an activity and it's layout is very simple:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:padding="16dp">
<Button
android:id="#+id/fragment_pay_request_pbutton"
style="#style/ButtonsStyle"
android:text="Pay" />
<Button
android:layout_marginTop="16dp"
android:id="#+id/fragment_pay_request_rbutton"
style="#style/ButtonsStyle"
android:layout_below="#id/fragment_pay_request_pbutton"
android:text="Request" />
<Button
android:layout_marginTop="16dp"
android:id="#+id/fragment_pay_request_bbutton"
style="#style/ButtonsStyle"
android:layout_below="#id/fragment_pay_request_rbutton"
android:text="Bills" />
</RelativeLayout>
And all is ok on the preview:
But on device:
I'm using Lolipop 5.1 and overlay mode for testing layouts.
Has someone an idea why it doesn't work and what I could do to solve this kind of problem.
BTW: The fragment is very simple:
/**
* Created by laurentmeyer on 15/03/15.
*/
public class PayOrRequestFragment extends BaseFragment {
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View toReturn = inflater.inflate(R.layout.fragment_pay_request, container, false);
Button pay = (Button) toReturn.findViewById(R.id.fragment_pay_request_pbutton);
Button request = (Button) toReturn.findViewById(R.id.fragment_pay_request_rbutton);
configureButton(request, pay);
return toReturn;
}
private void configureButton(Button request, Button pay) {
request.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String[] parameters = {"com.payment.laurentmeyer.mobilepayment.fragments.ReceiveFragment", "com.payment.laurentmeyer.mobilepayment.fragments.RequestMethodsFragment"};
mCallbacks.createSliderWithParameters(parameters);
}
});
pay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String[] parameters = {"com.payment.laurentmeyer.mobilepayment.fragments.SendFragment", "com.payment.laurentmeyer.mobilepayment.fragments.VerificationFragment"};
mCallbacks.createSliderWithParameters(parameters);
}
});
}
}
You must be putting the fragment into a view container in the Activity... You didn't post the layout for your Activity. Does your view container have android:layout_height="wrap_content"?
If I try to inflate a view within a fragment I am getting NULL.. For example:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Here I will inflate my view using the layout ID & return.
return view;
}
Whenever a button is clicked I need to create a dynamic view e.g.: button & add to the LinearLayout. I would like to perform this operation inside my fragment class like this:
public void addPlaces() {
Button button = new Button(null);
button.setText("button name");
// e.g. like adding button to enter code here linear layout
linearLayout.addView(button);
}
So, if I get inflate LinearLayout inside onCreateView and use it in add class, I'm getting NULL. How to achieve?
Declare the variable as a instance variable and then initialize Linear Layout
LinearLayout linearLayout;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment1, container, false);
linearLayout = (LinearLayout) rootView.findViewById(R.id.linearlayout);
return rootView;
}
Then
public void addPlaces() {
Button button = new Button(getActivity());
// needs activity context
// fragment hosted by a activity. use getActivity() to get the context of the hosting activity.
button.setText("button name");
linearlayout.addView(button);
}
Example: Modify the below according to your requirement.
fragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Button" />
<LinearLayout
android:layout_width="fill_parent"
android:id="#+id/linearlayout"
android:layout_height="fill_parent"
android:layout_above="#+id/button1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
Myfragment.java
public class Myfragment extends Fragment {
LinearLayout linearLayout;
View rootView;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Button b = (Button) rootView.findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
addPlaces();
}
});
linearLayout = (LinearLayout) rootView.findViewById(R.id.linearlayout);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment1, container, false);
return rootView;
}
public void addPlaces() {
Button button = new Button(getActivity()); // needs activity context
button.setText("button name");
linearLayout.addView(button);
}
}
Snap shot of my emulator
Edit :
activity-main.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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<fragment android:name="com.example.fragments.Myfragment"
android:id="#+id/frag"
android:layout_above="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Button" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends FragmentActivity {
Button b;
Myfragment fragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragment = new Myfragment();
fragmentTransaction.add(R.id.frag, fragment);
fragmentTransaction.commit();
b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
fragment.addPlaces();
}
});
}
}
Myfragment.java
public class Myfragment extends Fragment {
LinearLayout linearLayout;
View rootView;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
linearLayout = (LinearLayout) rootView.findViewById(R.id.linearlayout);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment1, container, false);
return rootView;
}
public void addPlaces() {
Button button = new Button(getActivity()); // needs activity context
button.setText("button name");
linearLayout.addView(button);
}
}
I have an app with fragments working ok,
I have a view with buttons, when a button gets tapped, a popup view is showed,
but I need to set different text on the pop up view for each button that is pressed.
Im new to android and java and just realised I don't understand how to send the data to the pop up view, to set the text on the xml for the pop up view,
public class Tab2HeadH1 extends Fragment implements OnClickListener{
//testeo popero
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_2_head_buttons, container,false);
//Buttons
final Button buttonNose = (Button) view.findViewById(R.id.button_pop_nose);
buttonNose.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
//aqui tus tareas,,
LayoutInflater layoutInflater = (LayoutInflater)getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
btnDismiss.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}});
popupWindow.showAsDropDown(buttonNose, 50, 30);
}
});
Button buttonEye = (Button) view.findViewById(R.id.button_pop_eye);
buttonEye.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
// onLoginClicked(v);
Toast.makeText(getActivity(), "ss9 eye",
Toast.LENGTH_SHORT).show();
}
});
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
((TabActivity)getActivity()).setHeader("TAPING APPLICATION");
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
}
}
}
and the xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#android:color/background_light">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="1dp"
android:background="#android:color/darker_gray">
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="20dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="It's a PopupWindow" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<Button
android:id="#+id/dismiss"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Dismiss" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
So how can I set the text of the textView,
Thanks a lot!
you need to find your textView in popup layout and set popup text
TextView text = popupView.findViewById(R.id.popup_text);
text.setText(your text);