Reputation: 41
I'm working on a multi fragment App and can't get any good info how to do this: Top of the screen is a tab bar where you can switch to different fragments. Below is the fragment I'm working on. I would like to have drawed content on the lowest layer and a EditText with a button above. The drawed content has to be updateable on button click. My idea in the moment is to add a content element to a frame layout and draw into this. The FrameLayout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/frag_color_bg">
<!-- Content element here -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<EditText
android:id="@+id/frag_text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/frag_color_te_bg"
android:hint="@string/frag_srvhint"
android:padding="2dp"
android:textColor="@color/frag_text1"
android:textSize="12sp">
</EditText>
<Button
android:id="@+id/frag_plotbtn"
android:background="@drawable/frag_blue_btn"
android:text="@string/frag_plotlabel"
style="@style/frag_btn"
android:layout_marginTop="0dp">
</Button>
</LinearLayout>
</FrameLayout>
Is this even possible? Putting a content element in the map.xml (in the place of the comment) and to draw into this with Java? If this is possible, which content element do I have to put there? A view? Do you have any sample code how to do this?
If this is not possible, what is the best way to implement this?
Any idea or code example welcome. I'm new to Android and have been looking for solution for this for days but the samples on the net are not that clear...
Thanks a lot for your time and help!
The class itself:
public MapFragment extends Fragment {
private final static String LOG_TAG = "Map";
private View mContentView;
private EditText mTxt;
private Activity mAct;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
mAct = (Activity) getActivity();
mContentView = (View) inflater.inflate(R.layout.map, container,
false);
try {
mTxt = (EditText) mContentView.findViewById(R.id.frag_text1);
mContentView.findViewById(R.id.frag_plotbtn).setOnClickListener(
myOnClickListener);
} catch (Exception e) {
Log.w(LOG_TAG, e);
}
return mContentView;
}
public OnClickListener myOnClickListener = new OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
// Plot button
case R.id.frag_plotbtn:
// Update drawing here;
break;
default:
break;
}
}
};
Upvotes: 1
Views: 5998
Reputation: 115
I would use a Canvas on top of a View. Check out this article: http://developer.android.com/guide/topics/graphics/2d-graphics.html#draw-with-canvas
If you use that, you will put your own extended View in the place of your comment. You'll create your own version of the onDraw() method and call invalidate() when you want to redraw (ie, when the button is pressed). Add it to your XML like this:
<com.mypackage.MyView
id="@+id/myView"
... />
Hope that helps!
Upvotes: 2