Advertisement
Guest User

Untitled

a guest
Mar 9th, 2013
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. MyActivity.java
  2. package com.example.HelloAndroid;
  3.  
  4. import android.os.Bundle;
  5. import android.support.v4.app.Fragment;
  6. import android.support.v4.app.FragmentActivity;
  7. import android.support.v4.app.FragmentManager;
  8. import android.support.v4.app.FragmentTransaction;
  9. import android.view.LayoutInflater;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.widget.TextView;
  13.  
  14. import java.security.SecureRandom;
  15.  
  16. public class MyActivity extends FragmentActivity {
  17. private int counter = 0;
  18.  
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.main);
  23.  
  24. findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
  25. @Override
  26. public void onClick(View view) {
  27. final FragmentManager manager = getSupportFragmentManager();
  28. final FragmentTransaction transaction = manager.beginTransaction();
  29. final Fragment fragment = TextFragment.newInstance(String.valueOf(counter++));
  30.  
  31. final int enter = R.anim.slide_in_from_bottom;
  32. final int exit = R.anim.slide_out_to_bottom;
  33. final int popEnter = R.anim.slide_in_from_bottom;
  34. final int popExit = R.anim.slide_out_to_bottom;
  35.  
  36. transaction.setCustomAnimations(enter, exit, popEnter, popExit);
  37. transaction.add(R.id.container, fragment);
  38. transaction.addToBackStack(fragment.getClass().getSimpleName());
  39. transaction.commit();
  40. }
  41. });
  42. }
  43.  
  44. public static class TextFragment extends Fragment {
  45. public static TextFragment newInstance(String text) {
  46. final TextFragment fragment = new TextFragment();
  47. final Bundle bundle = new Bundle();
  48.  
  49. bundle.putString("text", text);
  50. fragment.setArguments(bundle);
  51.  
  52. return fragment;
  53. }
  54.  
  55. @Override
  56. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  57. final View view = inflater.inflate(R.layout.fragment, container, false);
  58. final TextView text = (TextView) view.findViewById(R.id.text);
  59.  
  60. final String value = getArguments().getString("text");
  61. final int color = new SecureRandom().nextInt();
  62.  
  63. view.setBackgroundColor(color);
  64. text.setText(value);
  65.  
  66. return view;
  67. }
  68. }
  69. }
  70.  
  71. main.xml
  72. <?xml version="1.0" encoding="utf-8"?>
  73. <LinearLayout
  74. xmlns:android="http://schemas.android.com/apk/res/android"
  75. android:layout_width="match_parent"
  76. android:layout_height="match_parent"
  77. android:orientation="vertical">
  78.  
  79. <FrameLayout
  80. android:id="@+id/container"
  81. android:layout_width="match_parent"
  82. android:layout_height="0dip"
  83. android:layout_weight="1"/>
  84.  
  85. <Button
  86. android:id="@+id/button"
  87. android:layout_width="match_parent"
  88. android:layout_height="wrap_content"
  89. android:singleLine="true"
  90. android:text="Add fragment"/>
  91.  
  92. </LinearLayout>
  93.  
  94. fragment.xml
  95. <?xml version="1.0" encoding="utf-8"?>
  96. <FrameLayout
  97. xmlns:android="http://schemas.android.com/apk/res/android"
  98. android:layout_width="match_parent"
  99. android:layout_height="match_parent">
  100.  
  101. <TextView
  102. android:id="@+id/text"
  103. android:layout_width="wrap_content"
  104. android:layout_height="wrap_content"
  105. android:layout_gravity="center"
  106. android:singleLine="true"
  107. android:textSize="50dip"/>
  108.  
  109. </FrameLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement