I have a button which adds a textview to my fragment. When I close the activity the textview disappears. The only thing I like to know is how to save and retrieve the created textview.
public class trainingsplan extends Fragment {
ImageButton Btn;
LinearLayout Layout;
private ArrayList<View> mViews= new ArrayList<View>();
int i;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.trainingsplan, container, false);
setHasOptionsMenu(true);
Btn = (ImageButton) rootView.findViewById(R.id.imageButton3);
Layout = (LinearLayout) rootView.findViewById(R.id.linearLayout);
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
final TextView Test = new TextView(getActivity());
Test.setLayoutParams(lparams);
Test.setText("Hallo wie gehts dir ?");
Btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Layout.addView(Test);
mViews.add(Test);
}
});
return rootView;
}
Related
It has been awhile so I am struggling with fragments a bit. I am working on demos right now to get familiar again. What I am doing is in the main activity, I have a button to click which opens a fragment to enter information like name, address, etc. For some reason, the main activity doesn't go away and shows over the fragment. What am I doing wrong???
Note: I have not wired the cancel button yet to go back to Main Activity without doing anything.
Main Activity:
public class MainActivity extends AppCompatActivity {
public static User mUser = new User();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button requestServiceButton = findViewById(R.id.servicesButton);
requestServiceButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction().setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
NewUser fragment = new NewUser();
transaction.replace(R.id.fragmentHolder, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
NewUser Class:
public class NewUser extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.dialog_new_user, container, false);
final EditText fName = v.findViewById(R.id.firstNameEditText);
final EditText lName = v.findViewById(R.id.lastNameEditText);
final EditText address = v.findViewById(R.id.editTextAddress);
final EditText city = v.findViewById(R.id.cityEditText);
final EditText email = v.findViewById(R.id.editTextEmailAddress);
final EditText password = v.findViewById(R.id.editTextPassword);
final EditText phoneNumber = v.findViewById(R.id.editTextPhone);
final EditText username = v.findViewById(R.id.usernameEditText);
final EditText zipCode = v.findViewById(R.id.zipEditText);
Button okButton = v.findViewById(R.id.btnOK);
okButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MainActivity.mUser.setFirstName(fName.getText().toString());
MainActivity.mUser.setLastName(lName.getText().toString());
MainActivity.mUser.setAddress(address.getText().toString());
MainActivity.mUser.setCity(city.getText().toString());
MainActivity.mUser.setEmailAddress(email.getText().toString());
MainActivity.mUser.setPassword(password.getText().toString());
MainActivity.mUser.setPhoneNumber(phoneNumber.getText().toString());
MainActivity.mUser.setUsername(username.getText().toString());
MainActivity.mUser.setZipCode(zipCode.getText().toString());
}
});
Button cancelButton = v.findViewById(R.id.btnCancel);
cancelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
return v;
}
the code works fine at the first time but when i rotate the screen to landscape it is disabling my fragment button even it is no longer usable after i change the orientation back to normal(portrait). why this is happening??
Main Activity
public class MainActivity extends AppCompatActivity {
Button button;
EditText editText;
TextView textView;
fragment_one fragment_one;
FragmentManager fragmentManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragment_one = new fragment_one();
fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().add(R.id.container,fragment_one).commit();
button = (Button) findViewById(R.id.mybutton_main);
textView = (TextView) findViewById(R.id.mytextView_main);
editText = (EditText) findViewById(R.id.myedittext_main);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText("button Clicked");
editText.setText("button Clicked");
}
});
}
}
Fragment
public class fragment_one extends Fragment implements View.OnClickListener {
Button button;
TextView textView;
public fragment_one() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment_one, container, false);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
button = (Button) getActivity().findViewById(R.id.mybutton_frag);
textView = (TextView) getActivity().findViewById(R.id.mytextView_frag);
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
textView.setText("clicked");
}
}
Try this:
if (savedInstanceState == null) {
fragment_one = new fragment_one();
fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.add(R.id.container,fragment_one)
.commit();
}
heres what happen when i used the code below public class MenFragment extends Fragment {
ImageButton imageButtonCald;
ImageButton imageButtonCk;
ImageButton imageButtonCr;
ImageButton imageButtonTLt;
ImageButton imageButtonSttic;
ImageButton imageButtonSh;
View rootView;
public MenFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragment_men, container, false);
imageButtonCald = (ImageButton) rootView.findViewById(R.id.cald);
imageButtonCk = (ImageButton) rootView.findViewById(R.id.ck);
imageButtonCr = (ImageButton) rootView.findViewById(R.id.cr);
imageButtonTLt = (ImageButton) rootView.findViewById(R.id.tlt);
imageButtonSttic = (ImageButton) rootView.findViewById(R.id.sttc);
imageButtonSch = (ImageButton) rootView.findViewById(R.id.sch);
/*here i add a setOnClickListener method the my first button , how to add to the one */
imageButtonCald.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent;
intent = new Intent(getActivity(), CaldActivity.class);
startActivity(intent);
}
});
return rootView;
}
}
i already add a 'setOnClickListener' to the first button , how can i add a 'setOnClickListener' method on the other one.
You need to implement onClicklistener in you Fragment.
public static class PlaceholderFragment extends Fragment implements OnClickListener {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
Button button1 = (Button) rootView.findViewById(R.id.try_button1);
Button button2 = (Button) rootView.findViewById(R.id.try_button2);
Button button3 = (Button) rootView.findViewById(R.id.try_button3);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int viedId = v.getId();
switch(viedId ){
case R.id.try_button1:
//your implementation here
break;
case R.id.try_button2:
//your implementation here
break;
case R.id.try_button3:
//your implementation here
break;
}
}
}
}
So I got 2 fragments and an activity. Those 2 fragments are communicating each other via an interface.
Problem is, I can't change the TextView in the second fragment from Activity wih some text from fragment1.
There is the same problem( How to change fragment's textView's text from activity ) but there is no real solution so i thought i should open this.
Fragment 1:
public class test_layout extends Fragment {
DearListener activityCommander;
TextView custom_question_bar, list_number;
String selectedCustomQuestion;
boolean buttonClicked;
Button yiyecekButton;
Context c;
public interface DearListener {
public void getMyText(String theQuestion);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
activityCommander = (DearListener) activity;
Log.i("MYLOG", "onAttach");
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString());
}
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.new_test_layout, container, false);
//custom_question_bar= (TextView) v.findViewById(R.id.custom_question_bar);
//list_number= (TextView) v.findViewById(R.id.list_number);
yiyecekButton = (Button) v.findViewById(R.id.yiyecekButton);
Log.i("MYLOG", "Button referenced");
yiyecekButton.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View v){
Log.i("MYLOG", "onCLickListener Called");
onClickYiyecek(v);
}
}
);
return v;
}
public void onClickYiyecek(View v){
Log.i("MYLOG", "OnClickYiyecek");
activityCommander.getMyText("En sevdiğim yiyecek ne?");
}
public void onClickQuestionReady(View v){
if( custom_question_bar == null)
{
Toast.makeText(c, "Boşluğu doldurmadınız", Toast.LENGTH_SHORT).show();
}
activityCommander.getMyText(custom_question_bar.getText().toString());
}
}
And the Activity:
public class NewTest extends Activity implements test_layout.DearListener {
FragmentManager manager;
test_layout test_layout_frag;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_holder);
manager=getFragmentManager();
test_layout_frag = new test_layout();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.the_frame_layout, test_layout_frag);
transaction.commit();
}
#Override
public void getMyText(String theQuestion) {
manager=getFragmentManager();
answer_layout answer_layout_frag = new answer_layout();
answer_layout answer_layout_obj = (answer_layout)getFragmentManager().findFragmentById(R.id.fragment2);
answer_layout_obj.changeText(theQuestion);
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.the_frame_layout, answer_layout_frag);
transaction.commit();
Log.i("MYLOG", "Transaction Called");
//transaction.addToBackStack(null);
}
}
And the second fragment:
public class answer_layout extends Fragment {
TextView yourSelectedQuestion;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.new_answer_layout, container, false);
yourSelectedQuestion= (TextView) v.findViewById(R.id.yourSelectedQuestion);
return v;
}
public void changeText(String a) {
Log.i("MYLOG", "changeText");
Log.i("MYLOG", a);
yourSelectedQuestion.setText(a);
}
}
No exception is given but the TextView in second fragment isn't changing.
Since i didn't write the xml here, I can if its needed. Using FrameLayout and it contains 2 fragments inside it.
In myFragment class write a static method:
public static myFragment newInstance(String txt) {
myFragment myfragment = new myFragment();
Bundle args = new Bundle();
args.putString("TEXT", txt);
myfragment.setArguments(args);
return myfragment;
}
after that in OnCreate method of myFragment class inflate the textView tv and
tv.setText(getArguments().getString("TEXT"));
In main FragmentActivity class do:
myFragment fragment = myFragment.newInstance(textToBeSent);
before replace,
fragmentTransaction.replace(R.id.fr_dynamic, fragment);
fragmentTransaction.commit();
I have a ParseQueryAdapter that is populating a listview inside of a fragment:
QueryAdapter:
public class CustomAdapter extends ParseQueryAdapter implements AdapterView.OnItemClickListener {
public CustomAdapter(Context context) {
super(context, new ParseQueryAdapter.QueryFactory<ParseObject>() {
public ParseQuery create() {
ParseQuery query = new ParseQuery("Invoice");
query.whereEqualTo("CustomerID", ParseUser.getCurrentUser());
return query;
}
});
}
#Override
public View getItemView(ParseObject object, View cellview, ViewGroup parent) {
if (cellview == null) {
cellview = View.inflate(getContext(), R.layout.cell_layout, null);
}
TextView cellDate = (TextView) cellview.findViewById(R.id.dateTextView);
TextView invoice = (TextView) cellview.findViewById(R.id.invoiceNoTextView);
cellDate.setText(object.getString("Date"));
invoice.setText(object.getString("InvoiceNumber"));
return cellview;
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
}
Fragment Containing Listview:
public class InvoiceHistory extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final FrameLayout layout = (FrameLayout) inflater.inflate(R.layout.fragment_invoice_history, container, false);
ListView invoicesListView = (ListView) layout.findViewById(R.id.invoiceList);
ParseQueryAdapter adapter = new CustomAdapter(getActivity());
invoicesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Fragment detailView = new InvoicesFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.placeholder, detailView);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
invoicesListView.setAdapter(adapter);
adapter.loadObjects();
return layout;
}
}
This populates the ListView with the proper fields from the query, but when I click on one of the cells the fragment transaction takes place, but i can't figure out how to set textfields on that new fragment with ParseObject Keys from the Query.
This is the fragment that gets Presented that I want to set the Textfields in:
public class InvoicesFragment extends Fragment {
public InvoicesFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.fragment_invoices, container, false);
return layout;
}
Android is super new to me so maybe this is a wrong way to approach it. But all i want to do here is have that detail fragment show up with populated TextViews. Im not really sure what I'm doing.
You could approach this by setting up a constructor in your DetailsFragment that you can pass in the textview info you want to show. In the example below, the data and invoice number are passed into the constructor. When the fragment is set up, this fragment is set up with the Parse object's data:
invoicesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
ParseObject object = adapter.getItem(i);
Fragment detailView = DetailsFragment.newInstance(object.getString("Date"),
object.getString("InvoiceNumber"));
// ...
}
});
Here's a portion of the DetailsFragment showing the new constructor as well as how you could use them in setting up your view:
// InvoicesFragment.java
public static class InvoicesFragment extends Fragment {
// ...
public static InvoicesFragment newInstance(String invoiceDate, String invoiceNumber) {
InvoicesFragment f = new InvoicesFragment();
Bundle args = new Bundle();
args.putString("invoiceDate", InvoiceDate);
args.putString("invoiceNumber", InvoiceNumber);
f.setArguments(args);
return f;
}
public String getInvoiceDate() {
return getArguments().getString("invoiceDate", "");
}
public String getInvoiceNumber() {
return getArguments().getString("invoiceNumber", "");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.fragment_invoices, container, false);
// Assign some TextView, ex: dateTextView
dateTextView.setText(getInvoiceDate());
return layout;
}
}