[Java for Android App Development] anurag.dev1512:
Have u ever thought to use the activities as a dialog box or window...

if u ever thought so then it would have been difficult for u as u might be thinking how to resize the activity window...

but its possible...

Using activities as a dialog boxes will have following benefits:-

* i have personally faced difficulties using list views in dialog boxes... but this method is super easyt..

* u can theme each dialog boxes separately in the same app...regardless of what themes u have defined in resources..

* u can add custom library views to the dialog boxes...this thing is not possible in traditional dialog boxes that android offers...

* and much things can be implemented depending upon how developer thinks...

JUST SEE HOW I HAVE IMPLEMENTED THIS IN MY OPEN SOURCED APP AT THIS THREAD...

http://xdaforums.com/showthread.php?t=2547427

NOTE:- THIS GUIDE IS FOR ANDROID 4.0+

In this tutorial i will be adding "title page indicator to an activity" and use that activity as a dialog box.....

STEP 1:

Create an empty project and in .java file paste the following codes..

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.FragmentPagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {

/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
* will keep every loaded fragment in memory. If this becomes too memory
* intensive, it may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;

/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());

// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {

public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}

@Override
public int getCount() {
// Show 3 total pages.
return 3;
}

@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase();
case 1:
return getString(R.string.title_section2).toUpperCase();
case 2:
return getString(R.string.title_section3).toUpperCase();
}
return null;
}
}

/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";

public DummySectionFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Create a new TextView and set its text to the fragment's section
// number argument value.
TextView textView = new TextView(getActivity());
textView.setGravity(Gravity.CENTER);
textView.setText(Integer.toString(getArguments().getInt(
ARG_SECTION_NUMBER)));
return textView;
}
}

}

STEP 2:

Now download the "Title Page Indicator Library" and include it in your project

http://viewpagerindicator.com/

if u get any error then delete "android-support-v4.jar" from the sample project that u have created for this guide..

STEP 3:

Now open the only layout file.. and paste the following...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<com.viewpagerindicator.TitlePageIndicator
android:id="@+id/indicator"
android:background="@android:color/darker_gray"
app:footerLineHeight="2dp"
app:footerIndicatorHeight="4dp"
app:footerIndicatorStyle="underline"
app:selectedBold="true"
android:padding="10.0dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:background="#80000000"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="1.0" >
</android.support.v4.view.ViewPager>
</LinearLayout>

STEP 4:

Verify the minimum sdk requirement is 14 or above...

STEP 5:

Now go to .java file

i). Create a variable of type "TitlePageIndicator" and it should look like

TitlePageIndicator indicator;

ii).Get id for indicator ...

indicator = (TitlePageIndicator)findViewById(R.id.indicator);

iii).Add view to indicator

indicator.setViewPager(mViewPager);

iv).Till now we have created the simple activity including title page indicator ..now comes trickiest part..

go to line below the setContentView(R.layout.activity_main);

Type the following codes...

WindowManager.LayoutParams params = this.getWindow().getAttributes();
params.width = 400;// setting the width of the dialog box
params.height = 500;// setting the height of the dialog box...
//params.(......) much more explorations can be done...

STEP 6: (OPTIONAL)

If u want to do theaming and all to the dialog box read my thread about adding different transparency level to activity that can be applied to this dialog boxes also..

http://xdaforums.com/showthread.php?p=46964005#post46964005

NOW DONE AND FINALLY COMPILE IT AND RUN IT...

For better understanding i m attaching the same sample project...

i hope it helped u and dont forget u see my app(open source app) that uses dialog boxes created using this method...