[Java for Android App Development] DHD22800:
Fragment

Hi guys :D

I've made a simple "FrameWarp", it can help you showing a fragment, with only one activity:

public class FrameWarp extends Activity {

private SlidingMenu mSlidingMenu;
private ActionBarExt actionBarExt=null;
private ActionBarText actionBarText=null;
private ActionBarContainer actionBarContainer=null;
private FrameWarp context;
private ActionBarItemView mActionMenuButton;
private ActionBarItemView mActionOpenSlide;
private String primaryText, secondaryText;
private int BackVisibility, SlidingLayout;
private Boolean Enabled = false;
private static FrameLayout mGlossaryContainer;
private String FtToStart;

public void readFrameWarp() {
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
this.primaryText = bundle.getString("primary_text");
this.secondaryText = bundle.getString("secondary_text");
this.Enabled = bundle.getBoolean("sliding_enabled");
this.BackVisibility = bundle.getInt("back_visibility");
this.SlidingLayout = bundle.getInt("sliding_layout");
this.FtToStart = getIntent().getStringExtra("fragment");
}

public static int getHtcThemeID(Context context, int i)
{
return HtcWrapConfiguration.getHtcThemeId(context, i);

}

private void SetupActionBar(String primaryText, String secondaryText, int BackVisibility) {
actionBarExt=new ActionBarExt(this,getActionBar());
actionBarContainer=actionBarExt.getCustomContainer();
actionBarText=new ActionBarText(this);
actionBarText.setPrimaryText(primaryText);
actionBarText.setSecondaryText(secondaryText);
if (!secondaryText=="0")
actionBarContainer.addCenterView(actionBarText);
mActionMenuButton = new ActionBarItemView(this);
mActionMenuButton.setVisibility(BackVisibility);
mActionMenuButton.setIcon(getResources().getIdentifier("icon_btn_previous_dark", "drawable", "com.htc"));
mActionMenuButton.setOnClickListener(new android.view.View.OnClickListener() {

public void onClick(View view)
{
finish();
}
});
actionBarContainer.addLeftView(mActionMenuButton);
}

@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
super.onCreate(savedInstanceState);

context = this;
setTheme(getHtcThemeID(context, 2));
//Here you can set your statusbar traslucent how do you prefer
final int rootId = 1;
FrameLayout rootFrame = new FrameLayout(this);
int bkgResId = getResources().getIdentifier("common_app_bkg", "drawable", "com.htc");
rootFrame.setId(2);
setContentView(R.layout.frame_warp);
readFrameWarp();
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment frag;
try
{
frag = (Fragment)Class.forName(getIntent().getStringExtra("fragment")).newInstance();
}
catch (Exception exception)
{
Log.e(Constants.TAG, (new StringBuilder("Cant get fragment class: ")).append(exception.toString()).toString());
finish();
return;
}
if (frag == null || !(frag instanceof Fragment))
{
Log.e(Constants.TAG, "Not defined fragment to run!");
finish();
return;
}
getFragmentManager().beginTransaction().replace(R.id.container, frag).commit();
getWindow().setFormat(1);
context = this;
int multiply_color_id = getResources().getIdentifier("multiply_color", "attr", "com.htc");
TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(multiply_color_id, typedValue, true);
int currentThemeColor = typedValue.data;
rootFrame.setBackgroundResource(bkgResId);
SetupActionBar(primaryText, BackVisibility);
if (this.Enabled)
initSlidingMenu(Enabled, SlidingLayout);

String deviceVersion= Build.VERSION.RELEASE;
String remoteFile = null;
}

private void initSlidingMenu(Boolean Enabled, int SlidingLayout)
{
mSlidingMenu = new SlidingMenu(this);
mSlidingMenu.setBehindWidth(750); //any width as you want
mSlidingMenu.setFadeDegree(0.6F);
mSlidingMenu.setFadeEnabled(true);
mSlidingMenu.setEnabled(this.Enabled);
mSlidingMenu.setMode(0);
mSlidingMenu.setAboveOffset(200);
mSlidingMenu.setBehindOffset(200);
mSlidingMenu.setTouchModeAbove(1);
mSlidingMenu.attachToActivity(this, SlidingMenu.SLIDING_WINDOW);
mSlidingMenu.setShadowWidth(100);
mSlidingMenu.setShadowDrawable(R.drawable.sliding_menu_shadow);
if (Enabled==true)
mSlidingMenu.setMenu(SlidingLayout);
mSlidingMenu.setOnOpenedListener(new com.htc.widget.SlidingMenu.OnOpenedListener() {

public void onOpened()
{
mSlidingMenu.setShadowWidth(100);
mSlidingMenu.setShadowDrawable(R.drawable.sliding_menu_shadow);
}
});
}
}

For execute this FraweWarp:

Intent i = new Intent(getActivity(), FrameWarp.class); //Import FrameWarp class
i.putExtra("primary_text", getString(R.string.app_name)); //set primary text
i.putExtra("secondary_text", 0); //set secondary text
i.putExtra("sliding_enabled", false); //set sliding menu enabled
i.putExtra("back_visibility", View.VISIBLE); //set back visibility in action bar
i.putExtra("sliding_layout", 0); //set sliding menu layout
i.putExtra("fragment", "example.fragment.First"); //set fragment to replace, note that you must write full package name
startActivity(i); //finally start intent

In your mainfest, declare it with this option:

<activity android:name=".widget.FrameWarp" <!-- declaring -->
android:configChanges="keyboardHidden|orientation|screenSize" <!-- this parameters will NOT recreate activity on screen rotate -->
android:label="@string/app_name" > <!-- label -->
</activity>

Use this simple layout ( frame_warp.xml in code) :

<?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"
android:fitsSystemWindows="true"
android:id="@+id/container">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_container"/>
</FrameLayout>

Here I thank [MENTION=5386198]Mikanoshi[/MENTION] for Manifest hint :P and fitsSystemWindows ;)