"We already have an EditText, can only have one"
I've built a Fragment for my app (LoginFragment) that handles both 2 of the main authentication modes; namely logging in and signing up a user. There is a button to allow the user to switch between "login mode" and "sign up" mode. Each "mode" has some additional views that aren't required by the other. Therefore it is necessary to add and remove views as the mode is toggled.
I'm using EditText views within TextInputLayout layouts. My application crashes when I do the following:
Add the EditText programmatically
Remove the EditText programmatically
Add the EditText programmatically -> Crash
This is the error that I get is:
java.lang.IllegalArgumentException: We already have an EditText, can only have one
at android.support.design.widget.TextInputLayout.setEditText(TextInputLayout.java:166)
at android.support.design.widget.TextInputLayout.addView(TextInputLayout.java:155)
at android.view.ViewGroup.addView(ViewGroup.java:3985)
at android.view.ViewGroup.addView(ViewGroup.java:3961)
at com.mydomain.myapp.fragments.LoginFragment.showActivateAccountViews(LoginFragment.java:317)
This comes from android.support.design.widget.TextInputLayout which has an internal private EditText variable that is set when the view is added (source below). It appears that when I try to add the view to the TextInputLayout for the 2nd time that the mEditText variable has already set. The class does not have it's own .removeView() method so I don't know how it should be removed?
I suspect that I am removing the EditText view incorrectly but cannot figure out what I am doing wrong. I've also read some other Stack Overflow posts that deal with removing views but these approaches have not resolved the issue either.
Adding a view - https://stackoverflow.com/a/13889257/1500317
Remove a view - Call removeView() on the child's parent first
Does anyone have any ideas on how I can get this to work?
Below is my own code for reference.
LoginFragment.java
...
import android.support.design.widget.TextInputLayout;
import android.widget.EditText;
public class LoginFragment extends Fragment {
private RelativeLayout mContainer;
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_login, container, false);
mContainer = ((RelativeLayout) view.findViewById(R.id.login_container));
showLoginViews();
LayoutTransition layoutTransition = mContainer.getLayoutTransition();
layoutTransition.enableTransitionType(LayoutTransition.CHANGING);
return view;
}
/**
* Show the view elements for Login mode
*/
private void showLoginViews() {
LayoutInflater li = (LayoutInflater)getActivity().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
// Configure the button for the primary action
Button loginButton = (Button)mContainer.findViewById(R.id.button_login_fragment_primary_action);
...
// Configure the toggle button to navigate to Activate Account mode
TextView toggleButton = (TextView)mContainer.findViewById(R.id.button_toggle_mode);
toggleButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LoginFragment.this.showActivateAccountViews();
}
});
toggleButton.setText(getResources().getString(R.string.action_activate_account));
// Hide the Member ID EditText
((TextInputLayout)mContainer.findViewById(R.id.member_id_inputlayout)).removeView(mContainer.findViewById(R.id.editText_member_id_field));
}
/**
* Show view elements for Activate Account mode
*/
private void showActivateAccountViews() {
LayoutInflater li = (LayoutInflater)getActivity().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
// Configure the primary button for the primary action - Activate Account
Button activateAccountButton = (Button)mContainer.findViewById(R.id.button_login_fragment_primary_action);
...
// Add the Member ID EditText
((TextInputLayout)mContainer.findViewById(R.id.member_id_inputlayout)).addView(li.inflate(R.layout.login_member_id_element_layout, (ViewGroup)mContainer.findViewById(R.id.member_id_inputlayout), false));
// Configure the toggle button to navigate to Login mode
TextView toggleButton = (TextView)mContainer.findViewById(R.id.button_toggle_mode);
toggleButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LoginFragment.this.showLoginViews();
}
});
toggleButton.setText(getResources().getString(R.string.action_login));
}
...
}
login_member_id_element_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/editText_member_id_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/member_id" />
login_fragment.xml
<android.support.design.widget.CoordinatorLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.mydomain.myapp.fragments.LoginFragment">
<RelativeLayout
android:id="#+id/login_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true">
<!--placeholder layout with params for activate account elements-->
<android.support.design.widget.TextInputLayout
android:id="#+id/member_id_inputlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- a view can be added here-->
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/email_inputlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/editText_email_field"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textEmailAddress" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/button_login_fragment_primary_action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/password_inputlayout"
android:text="#string/action_login" />
<!-- Toggle button for Login/Activate Account-->
<TextView
android:id="#+id/button_toggle_mode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/action_activate_account" />
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
android.support.design.widget.TextInputLayout (from the latest 22.2.1 support library)
public class TextInputLayout extends LinearLayout {
private EditText mEditText;
...
public void addView(View child, int index, LayoutParams params) {
if(child instanceof EditText) {
android.widget.LinearLayout.LayoutParams params1 = this.setEditText((EditText)child, params);
super.addView(child, 0, params1);
} else {
super.addView(child, index, params);
}
}
private android.widget.LinearLayout.LayoutParams setEditText(EditText editText, LayoutParams lp) {
if(this.mEditText != null) {
throw new IllegalArgumentException("We already have an EditText, can only have one");
} else {
this.mEditText = editText;
this.mCollapsingTextHelper.setExpandedTextSize(this.mEditText.getTextSize());
this.mEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
TextInputLayout.this.mHandler.sendEmptyMessage(0);
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});
this.mDefaultTextColor = this.mEditText.getHintTextColors().getDefaultColor();
this.mEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View view, boolean focused) {
TextInputLayout.this.mHandler.sendEmptyMessage(0);
}
});
if(TextUtils.isEmpty(this.mHint)) {
this.setHint(this.mEditText.getHint());
this.mEditText.setHint((CharSequence)null);
}
if(this.mErrorView != null) {
ViewCompat.setPaddingRelative(this.mErrorView, ViewCompat.getPaddingStart(this.mEditText), 0, ViewCompat.getPaddingEnd(this.mEditText), this.mEditText.getPaddingBottom());
}
this.updateLabelVisibility(false);
android.widget.LinearLayout.LayoutParams newLp = new android.widget.LinearLayout.LayoutParams(lp);
Paint paint = new Paint();
paint.setTextSize(this.mCollapsingTextHelper.getExpandedTextSize());
newLp.topMargin = (int)(-paint.ascent());
return newLp;
}
}
}
There appears to be a limitation in the com.android.support.design library (v22.2.1). You cannot directly remove and then add an EditText to a TextInputLayout at runtime. You can star this bug here.
I've devised a workaround for the problem. I modified the xml layout so that instead of adding/removing EditText views from the TextInputLayout at runtime (which doesn't work) we add/remove the TextInputLayout itself to a LinearLayout holder. With this solution we never need to actually remove the EditText from the TextInputLayout.
The only thing to note about this solution is that it makes your view hierarchy 1 level deeper than it otherwise need to be. So bear this in mind if you already have UI performance problems. if when you are reading this and version of com.android.support.design v22.2.1 is available it may be worth checking to see if this issue has been resolved.
Otherwise see the sample code below for my implementation of the workaround.
LoginFragment.java
import android.support.design.widget.TextInputLayout;
import android.widget.EditText;
public class LoginFragment extends Fragment {
private RelativeLayout mContainer;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_login, container, false);
mContainer = ((RelativeLayout) view.findViewById(R.id.login_container));
showLoginViews();
LayoutTransition layoutTransition = mContainer.getLayoutTransition();
layoutTransition.enableTransitionType(LayoutTransition.CHANGING);
return view;
}
/**
* Show the view elements for Login mode
*/
private void showLoginViews() {
LayoutInflater li = (LayoutInflater)getActivity().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
// Configure the toggle button to navigate to Activate Account mode
s TextView toggleButton = (TextView)mContainer.findViewById(R.id.button_toggle_mode);
toggleButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LoginFragment.this.showActivateAccountViews();
}
});
toggleButton.setText(getResources().getString(R.string.action_activate_account));
// Hide the Member ID EditText
((LinearLayout)mContainer.findViewById(R.id.member_id_holderlayout)).removeView(mContainer.findViewById(R.id.member_id_inputlayout));
}
/**
* Show view elements for Activate Account mode
*/
private void showActivateAccountViews() {
LayoutInflater li = (LayoutInflater)getActivity().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
// Configure the primary button for the primary action - Activate Account
Button activateAccountButton = (Button)mContainer.findViewById(R.id.button_login_fragment_primary_action);
// Add the Member ID EditText
((LinearLayout)mContainer.findViewById(R.id.member_id_holderlayout)).addView(li.inflate(R.layout.login_member_id_element_layout, (ViewGroup) mContainer.findViewById(R.id.member_id_inputlayout), false));
// Configure the toggle button to navigate to Login mode
TextView toggleButton = (TextView)mContainer.findViewById(R.id.button_toggle_mode);
toggleButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LoginFragment.this.showLoginViews();
}
});
toggleButton.setText(getResources().getString(R.string.action_login));
}
}
login_member_id_element_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.TextInputLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/member_id_inputlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/editText_member_id_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/member_id" />
</android.support.design.widget.TextInputLayout>
login_fragment.xml
<android.support.design.widget.CoordinatorLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<RelativeLayout
android:id="#+id/login_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--placeholder for TextInputLayout to be dynamically added at runtime-->
<LinearLayout
android:id="#+id/member_id_holderlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- a login_member_id_element_layout can be dynamically added/removed here at runtime-->
</LinearLayout>
<!--TextInputLayout for static fields, the EditText is not removed at runtime-->
<android.support.design.widget.TextInputLayout
android:id="#+id/email_inputlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/member_id_holderlayout">
<EditText
android:id="#+id/editText_email_field"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawablePadding="#dimen/edittext_drawable_padding"
android:drawableStart="?emailIcon"
android:focusable="true"
android:hint="Email"
android:inputType="textEmailAddress" />
</android.support.design.widget.TextInputLayout>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
Congratulations, you (maybe?) found a possible bug (or should i say non-expected behaviour on removing TextInputLayout's EditText ?)
You can see that removeView() is a method from ViewGroup. It removes your View from the array of children views of ViewGroup, but not the reference that your InputTextLayout have to the EditText.
What should i do, then?
You gotta extend TextInputLayout and create your own method that sets super.mEditText to null. The problem is that you will still need to call these two methods because just setting a Layout reference to null may leave your old layout laying on memory through your application life cycle.
Related
I am making a window to open a dialog window and write a comment.
When you open the dialog window, there is a layout where you can enter your first comment.
Comment can only be written in one line and the following comment layout is created by pressing EnterKey in the input field.
I originally implemented this as a RecyclerView, but now I am trying other methods as well. (using addView())
But when I used addView() I got the following error.
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
The solution to this is shown in the here. (And I would appreciate it if you could explain why this error occurs.)
There is no error when using this.
However, no more items are added.
There is only the first item that existed when the dialog box was opened, and there is no response when pressing the Enter key.
I want to add items dynamically in real time.
What should I do?
fragment_wc_dialog.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".fragment.WritingCommentDialogFragment">
</LinearLayout>
wc_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".fragment.WritingCommentDialogFragment">
<EditText
android:id="#+id/comment_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:gravity="center_vertical"
android:drawableLeft="#drawable/ic_bullet_point"
android:drawablePadding="5dp"
android:layout_marginHorizontal="10dp"
android:background="#null"
android:textSize="15sp"
android:inputType="text"
android:maxLines="1"
android:maxLength="22"
android:imeOptions="actionNone"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
WCDialogFragment.java
public class WritingCommentDialogFragment extends DialogFragment {
LinearLayout mContainer; // input layout
View inputView;
EditText editText;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_writing_comment_dialog, container, false);
initViews(view);
mContainer.addView(inputView); // The first item to be present when the dialog is opened
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN) {
mContainer.addView(inputView);
}
return true;
}
});
return view;
}
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setCanceledOnTouchOutside(false);
return dialog;
}
#Override
public void onResume() {
super.onResume();
Toast.makeText(getContext(), "onResume()", Toast.LENGTH_SHORT).show();
setDialogSize();
}
private void initViews(View view) {
mContainer = view.findViewById(R.id.container);
inputView = LayoutInflater.from(getContext()).inflate(R.layout.writing_comment_item, null);
editText = inputView.findViewById(R.id.comment_edit);
}
}
ADDED
Your problem and the linked issue are different, as far as I can tell. The link has two views that they want to swap back and forth between, so the answer is to remove one view before adding the second. You want to build up a list of views, so you must use different instances rather than adding the same view over and over.
If you want the newly-added view to also respond to the Enter key, you can set the same listener to the new view too:
if(event.getAction() == KeyEvent.ACTION_DOWN) {
EditText addedView = (EditText) LayoutInflater.from(getContext()).inflate(R.layout.writing_comment_item, mContainer, false);
addedView.setOnEditorActionListener(this);
mContainer.addView(addedView);
}
I have created a fragment and want to start a dialogue on a button click on which i would display n numbers of texts distributed vertically using Linear layout vertical orientation. To begin with i have created a textview which i am populating dynamically. But i get a null pointer exception saying the any of the layout i referenced here points to NULL.
View view,tempView;
TextView myName, myPhNo, myEmail, myDob;
ImageButton selectRingTone;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_profile, container, false);
tempView = inflater.inflate(R.layout.fragment_selectsong, container, false);
myName = (TextView)view.findViewById(R.id.username);
myPhNo = (TextView)view.findViewById(R.id.userPhNo);
myEmail = (TextView)view.findViewById(R.id.useremailId);
myDob = (TextView)view.findViewById(R.id.userdob);
selectRingTone = (ImageButton)view.findViewById(R.id.selectRingTone);
setUserProfileData();
//setUserTextStatus();
//setUserAudioStatus();
selectRingTone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chooseAndSetRingTone();
}
});
return view;
}
private void chooseAndSetRingTone(){
final Dialog fbDialogue = new Dialog(view.getContext(), android.R.style.Theme_Black);
fbDialogue.getWindow().setTitle("Select your audio status song");
fbDialogue.getWindow().setBackgroundDrawable(new ColorDrawable(Color.argb(100, 0, 0, 0)));
fbDialogue.setContentView(R.layout.fragment_selectsong);
getSongsAndFillDialogue();
fbDialogue.setCancelable(true);
fbDialogue.show();
}
private void getSongsAndFillDialogue(){
LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout container = (LinearLayout) tempView.findViewById(R.id.eachRingToneSong);
TextView tv = new TextView(tempView.getContext());
tv.setText("Hi hello");
Button b = new Button(tempView.getContext());
b.setText("abcde");
container.addView(tv);
container.addView(b);
}
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLyt"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:id="#+id/scrollCotainerForRingToneSongs"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/eachRingToneSong"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--<TextView
android:id="#+id/song1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="abcde"
/>-->
</LinearLayout>
</ScrollView>
</RelativeLayout>
you are creating View for your Dialog in onCreateView method:
tempView = inflater.inflate(R.layout.fragment_selectsong, container, false);
and fulfilling it using getSongsAndFillDialogue method, but you are setting for your Dialog another new instance passing only resource id:
fbDialogue.setContentView(R.layout.fragment_selectsong);
when you are passing just id then Dialog is inflating this layout by itself (just like you). So there are two layouts created, one in Activity - fulfilled, and one created "automatically" by setContentView method, but not touched at all by your methods (so it stays empty)
instead of passing resource id pass already prepared tempView, like below:
fbDialogue.setContentView(tempView);
I called setRetainInstance(true) for a fragment. I added an EditText and TextView on the fragment. When rotated, the text in the EditText survived, but the text in the TextView was gone.
I think I can manually restore the text of the TextView, but I wonder why the system automatically restores the text for EditText, and not for TextView. Did I do something wrong?
Steps to reproduce.
Type "android" in the EditText
Press the [Test 1] button. The TextView now displays "android"
Rotate the device.
Result
The EditText has "android", but the TextView is empty.
MyFragment.java
public class MyFragment extends Fragment
{
private static final String TAG = "MyFragment";
EditText etInput;
Button btnTest1;
TextView tvMessage;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState)
{
Log.d(TAG, "onCreateView()");
View v= inflater.inflate(R.layout.my_fragment, container, false);
etInput = (EditText)v.findViewById(R.id.etInput);
btnTest1 = (Button)v.findViewById(R.id.btnTest1);
btnTest1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
tvMessage.setText(etInput.getText().toString());
Log.d(TAG, "btnTest1 was clicked");
}
});
tvMessage = (TextView) v.findViewById(R.id.tvMessage);
return v;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity
{
String TAG = this.getClass().getName();
#Override
protected void onCreate(Bundle savedInstanceState)
{
Log.d(TAG, "onCreate()");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
Fragment mf = fm.findFragmentById(R.id.placeholder);
if(mf == null)
{
Log.d(TAG, "creating new my fragment");
mf = new MyFragment();
mf.setRetainInstance(true);
fm.beginTransaction().add(R.id.placeholder, mf).commit();
}
}
}
my_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/etInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/tvMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/btnTest1"
android:text="Test 1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:text="Fragment Test"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="#+id/placeholder"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
</FrameLayout>
</LinearLayout>
If you want to enable auto restoration of TextView, you need to set freezesText attribute to true in your xml (or call setFreezesText(true)).
For TextView, freezesText default value is false, but in case of EditText, default value is true.
From documentation of freezesText:
If set, the text view will include its current complete text inside of
its frozen icicle in addition to meta-data such as the current cursor
position. By default this is disabled; it can be useful when the
contents of a text view is not stored in a persistent place such as a
content provider. For EditText it is always enabled, regardless of the
value of the attribute.
I have a fragment with his class:
public class FormatShortFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.bag_format_short, container, false);
return rootView;
}
}
And this is his layout:
<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">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
</RelativeLayout>
This fragment is shown by a FragmentPageAdapter.
The layout will grow when an user add an item from server (the origin of the item doesn't matter), and this item will be an horizontal linearlayout, with textview and edittext (item_name, item_value). This value can be modifiable, so must be an edittext, also, this edittext will receive values periodically from server, so can be updated automatically.
After explain this, how can I add this linearLayouts and his subviews to the GUI dynamically? and when should I update the interface?
Thanks a lot, any help is good received, I'm really lost on this on this themes.
EDITED WITH MODIFICATIONS:
I have modified the code in this way:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.bag_format_short, container, false);
return rootView;
}
#Override
public void onResume() {
super.onResume();
LinearLayout parentLinear = (LinearLayout) getActivity().findViewById(R.id.short_container);
for (int i = 1; i<10; i++ ){
LinearLayout innerLinear = (LinearLayout) new LinearLayout(getActivity());
TextView itemName = new TextView(getActivity());
itemName.setText("Nombre");
EditText itemValue = new EditText(getActivity());
itemValue.setText("Valor");
innerLinear.addView(itemName);
innerLinear.addView(itemValue);
parentLinear.addView(innerLinear);
}
}
#Override
public void onStart() {
super.onStart();
}
#Override
public void onStop() {
super.onStop();
}
And the fragment layout has been modified on this way:
<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">
<LinearLayout
android:id="#+id/short_container"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
</RelativeLayout>
Now the layout is growing in each for iteration.
In this point i have another problems, I hope you can help me with this.
- How can I generate a LayouParams object with attributes like gravity or weigth?
- How can I pass data from MainActivity to the Fragment?
- And how can i force the Fragment view to be raloaded with new data?
Also i need to know how bind a clickListener event to programatically linearLayouts added, i need modify some values on server depending on the name and the edited value.
Thanks a lot for this, you are saving my life.
you Can easily Update UI of Fragment on Button click or any event just
Like in Activity. You can get any view by
getActivity().findViewById()
See Example, It will help you to undersatand:-
public class FragmentTab3 extends SherlockFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragmenttab3, container, false);
return rootView;
}
#Override
public void onResume() {
super.onResume();
}
#Override
public void onStart() {
super.onStart();
LinearLayout linear = (LinearLayout) getActivity().findViewById(R.id.linear);
Button button = new Button(getActivity());
button.setText("Click Me");
linear.addView(button);
}
#Override
public void onStop() {
super.onStop();
}
}
If you want to add subview to a layout dynamically, take a look at RecyclerView in android documentation. Sounds like what you're looking for.
I have this app, that I created a custom dialog for. I must of goofed something up cause while the .show call on the dialog does indeed bring it up, it looks like a whole new fragment and it is not floating but instead replacing the ui with its contents. I did see in their help for DialogFragment:
http://hi-android.info/docs/reference/android/app/DialogFragment.html#Lifecycle
that one can embed a dialog as a regular fragment or not. Though I am not doing anything to do this so I cannot figure out why its acting like an embedded fragment and not floating. After thinking on it, is it the way I defined my XML definition? The dialogfragment example above didn't really give a definition for the xml layout, so maybe that is where my issue is? (Even added the gravity to the xml file, still no dice)
My xml definition for this Dialog is here:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textSize="20sp"
android:text = "Location:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"/>
<Spinner
android:id="#+id/location_spinner"
android:layout_width = "450sp"
android:layout_height="wrap_content"/>
<!-- fill out the data on the package total cost etc -->
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button android:id="#+id/location_dlg_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Okay"/>
<Button android:id="#+id/location_dlg_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"/>
<Button android:id="#+id/location_dlg_new"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create new..."/>
</LinearLayout>
</LinearLayout>
Like I said displays just fine, the code for the fragment:
package com.viciousbytes.studiotab.subactivities.dialogfragments;
import ... ...
public class LocationPicker extends DialogFragment {
ArrayList<Location> mLocations;
public static LocationPicker newInstance()
{
LocationPicker loc = new LocationPicker();
return loc;
}
private void setLocations(ArrayList<Location> loc)
{
mLocations=loc;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Pick a style based on the num.
int style = DialogFragment.STYLE_NORMAL, theme = android.R.style.Theme;
setStyle(style, theme);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.location_dialog, container, false);
Spinner spinner = (Spinner)v.findViewById(R.id.location_spinner);
ArrayAdapter<Location> adapter = new ArrayAdapter<Location>(v.getContext(), android.R.layout.simple_spinner_item, mLocations);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
if(mLocations==null)
spinner.setPrompt("No Locations");
else
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new LocationSelectedListener());
// Watch for button clicks.
Button newBtn = (Button)v.findViewById(R.id.location_dlg_new);
newBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// When button is clicked, call up to owning activity.
//create new start that activity...
}
});
// Cancel do nothing dismissthis
Button cancelBtn = (Button)v.findViewById(R.id.location_dlg_cancel);
cancelBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// When button is clicked, call up to owning activity.
//create new start that activity...
}
});
// okay button means set listener with the selected location.
Button okBtn = (Button)v.findViewById(R.id.location_dlg_ok);
okBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// When button is clicked, call up to owning activity.
//create new start that activity...
}
});
return v;
}
}
It is called from a fragment itself? though does that matter? because I am calling a TimePIckerDialog and a DatePickerDialog and those work fine, but my calling code from my other fragment is:
void showLocationDialog() {
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment prev = getFragmentManager().findFragmentByTag("locpicker");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
// Create and show the dialog.
DialogFragment newFragment = LocationPicker.newInstance();
newFragment.show(ft, "locpicker");
}
Your constructors are wrong. Try to have just one static method newInstance to instantiate the fragment for all cases and use a Bundle to store the arguments that you want to use in the fragment. Refer to Basic Dialog section here and extend it to your case.