37797

How to add view into LinearLayout of Fragment by onClick?

Question:

I have one Fragment,this layout is one Button.

when button is clicked add textview into textview_container.

how to?

myfragment.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:background="#00000000" android:orientation="vertical" > <LinearLayout android:id="@+id/textview_container" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00000000" android:orientation="horizontal" > </LinearLayout> <Button android:id="@+id/postbutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:background="@null" android:text="@string/edit" android:textColor="@color/black" android:textSize="14sp" /> </LinearLayout>

Myfragment.java

public class AlbumListDetailFragment extends Fragment implements OnClickListener { Button postButton public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.myfragment , container, false); } public void onStart() { super.onStart(); initViews(); } private void initViews() { button = (Button) getView().findViewById(R.id.postbutton); button.setOnClickListner{new View.onClickListener(){ public void onClick(View v) { //make and Textview into textview_container } } } }

Answer1:

LinearLayout linearLayout = (LinearLayout)findViewById(R.id.textview_container); TextView valueTV = new TextView(this); linearLayout.addView(valueTV);

Recommend

  • View disappears after setting height of gridLayout to wrap_content
  • Edittext requestFocus() dynamically not working
  • adMob | Extend Banner size?
  • ExtensionDataObject not marked as serializable
  • Set menu items centered and full-width with ActionBarSherlock
  • Google Contacts API get phone number (PHP)
  • Python SOAP request using urllib2
  • Android custom dialog linearlayout size same as dialogs bg image
  • Android ripple for button not working
  • How to convert NAnt function “path::combine(path1, path2)” to MSBuild?
  • Savon soap body problem
  • Android : Strike out Text with bold or thicker line than default STRIKE_THRU_TEXT_FLAG
  • Background image crashes android
  • Float Label hint (TextInputLayout) not works with Android Data Binding
  • Could be Text orientation of JTextArea changed by keyboard shortcut?
  • Android Custom dialog inflating from layout - alignment issue
  • EditText ignores the ImeActionLabel
  • Translate animation to correct position in Android
  • jqPlot date axis - bars drawn on wrong day?
  • Complex Silverlight TreeView, is nested hierarchy possible?
  • Toolbar shifts up, when soft keyboard appears
  • Loop animation drawable
  • Multibinding Multiselection ListView
  • Gray line inside cardview :)?
  • Wrapping text in spinner android
  • Running jasmine tests for a component with NgZone dependency
  • my tic-tac-toe program in matlab does not work [closed]
  • How to resolve permission denied maybe missing internet permission?
  • C# List of Panels
  • Using MouseListener to select a range of cells in a grid
  • WPF Template Binding in ToggleButton UserControl
  • How Lists (specifically, RecyclerView with CardViews) in Android work
  • How can I speed up CURL tasks?
  • Android - Material Design - NavigationView - How to put vertical scroll?
  • Google Custom Search with transparent background
  • Android fill_parent issue
  • Repeat a vertical line on every page in Report Builder / SSRS
  • How to draw moving and Running sine wave chart using JFree chart in java?
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • how to add data labels for bar graph in matlab