I'm building an Android application that will be like an eBook. Standard flip through pages, view images, etc. One of the requirements is that in multiple places you will see a preview of what a page will look like. It'll be the exact contents of that page, just in a smaller size.
I can't for the life of me get this preview working the way I need it to. I coded an activity which will display either controls to play audio or video depending on some values passed in the extras. That works fine. The preview should be this activity, but only in a smaller size, and shown in a separate activity. Here's what I've tried to get a smaller version of the activity to show:
First, I stumbled across the poorly documented ActivityGroup object. I figured I could use this to call the activity, and then get the view and throw it into a parent view somehow. Here's what that code looked like:
public class GroupTest extends ActivityGroup {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grouptest);
LocalActivityManager manager = getLocalActivityManager();
Intent intent = new Intent();
intent.setClass(getBaseContext(), ViewElement.class);
intent.putExtra("ElementID", 22);
intent.putExtra("PackID", 10);
Window w = manager.startActivity("videoelement", intent);
View v = w.getDecorView();
// Grab the table and add a view to it.
TableLayout tl = (TableLayout)findViewById(R.id.tlTest);
TableRow tr = new TableRow(this);
tr.addView(v);
tl.addView(tr);
}
}
And here's what the layout looked liked:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
android:id="@+id/tlTest">
</TableLayout>
This sort of worked. The activity ViewElement was indeed displaying in the GroupTest activity. The problem was that whenever I tried to add something else to the table (like a label), the ViewElement activity would disappear. I tinkered with this for a while figuring I was failing to grasp some UI constraint, then realized that the ViewElement activity being displayed had clickable buttons, which isn't what I really want. I just want to see what that page looks like, not be able to interact with it.
So I tried a different approach. Found this page on saving a view to an image: http://jtribe.blogspot.com/2008/12/saving-view-as-image-with-android.html. Sounded more like what I want.
My modified code now looks like:
public class GroupTest extends ActivityGroup {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tocrow);
LocalActivityManager manager = getLocalActivityManager();
Intent intent = new Intent();
intent.setClass(getBaseContext(), ViewElement.class);
intent.putExtra("ElementID", 22);
intent.putExtra("PackID", 10);
Window w = manager.startActivity("videoelement", intent);
View v = w.getDecorView();
Bitmap image = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.RGB_565);
v.draw(new Canvas(image));
ImageView img = (ImageView)findViewById(R.id.ivwElementPreview);
img.setImageBitmap(image);
}
}
And the layout (this is the actual layout I would be putting the previews in if I could get it working):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
android:id="@+id/ivwElementPreview"
android:layout_width="75px"
android:layout_height="75px"
android:layout_marginRight="6dip"
android:src="@drawable/icon" />
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="@+id/txtElementTitle"
android:layout_width="300px"
android:layout_height="0dip"
android:layout_weight="1"
/>
<ImageView
android:id="@+id/ivwPackIcon"
android:layout_width="15px"
android:layout_height="15px"
android:layout_marginRight="6dip"
android:src="@drawable/icon" />
<ImageView
android:id="@+id/ivwPackIcon"
android:layout_width="15px"
android:layout_height="15px"
android:layout_marginRight="6dip"
android:src="@drawable/icon" />
<TextView
android:id="@+id/txtElementComments"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
/>
</LinearLayout>
</LinearLayout>
This just flat out doesn't work at all. As far as I can tell calling w.getDecorView() doesn't return a view that actually has any height, width, or drawn properties on it. If I run that code I get the error "width and height must be > 0" And if I put in values for width and height, nothing shows but a black image. So I then proceeded to create simple things such as a TextView and draw it to the bitmap to display in my LinearLayout, but could never get it working right either. I'm to the point of just being completely fed up with manipulating UI in Android.
So, has anyone else does something similar with previewing what another activity would look like, and how did you accomplish it? Am I just going down the completely wrong path here? I've googled all over the place and part of me feels like someone has to have solved this problem before, but I've lost over a day of work on it and I just can't pour much more time into it right now.
Thanks for any help.