Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MyActivity.java
- package com.example.HelloAndroid;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.support.v4.app.FragmentActivity;
- import android.support.v4.app.FragmentManager;
- import android.support.v4.app.FragmentTransaction;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.TextView;
- import java.security.SecureRandom;
- public class MyActivity extends FragmentActivity {
- private int counter = 0;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- final FragmentManager manager = getSupportFragmentManager();
- final FragmentTransaction transaction = manager.beginTransaction();
- final Fragment fragment = TextFragment.newInstance(String.valueOf(counter++));
- final int enter = R.anim.slide_in_from_bottom;
- final int exit = R.anim.slide_out_to_bottom;
- final int popEnter = R.anim.slide_in_from_bottom;
- final int popExit = R.anim.slide_out_to_bottom;
- transaction.setCustomAnimations(enter, exit, popEnter, popExit);
- transaction.add(R.id.container, fragment);
- transaction.addToBackStack(fragment.getClass().getSimpleName());
- transaction.commit();
- }
- });
- }
- public static class TextFragment extends Fragment {
- public static TextFragment newInstance(String text) {
- final TextFragment fragment = new TextFragment();
- final Bundle bundle = new Bundle();
- bundle.putString("text", text);
- fragment.setArguments(bundle);
- return fragment;
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- final View view = inflater.inflate(R.layout.fragment, container, false);
- final TextView text = (TextView) view.findViewById(R.id.text);
- final String value = getArguments().getString("text");
- final int color = new SecureRandom().nextInt();
- view.setBackgroundColor(color);
- text.setText(value);
- return view;
- }
- }
- }
- 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">
- <FrameLayout
- android:id="@+id/container"
- android:layout_width="match_parent"
- android:layout_height="0dip"
- android:layout_weight="1"/>
- <Button
- android:id="@+id/button"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:singleLine="true"
- android:text="Add fragment"/>
- </LinearLayout>
- fragment.xml
- <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <TextView
- android:id="@+id/text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:singleLine="true"
- android:textSize="50dip"/>
- </FrameLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement