How to show an image on a table in Android -
i'm trying show image (of snake) on android application, can appear on board table (which tablelayout
). how can rather image being shown above table?
package com.example.test; import android.app.activity; import android.graphics.color; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.imageview; import android.widget.tablelayout; import android.widget.tablerow; import android.widget.toast; public class mainactivity extends activity { tablelayout table; imageview image; private static final int table_width = 12; private static final int table_height = 10; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); table = (tablelayout) findviewbyid(r.id.view_root); image = (imageview) findviewbyid(r.id.imageview1); // populate table stuff (int y = 0; y < table_height; y++) { final int row = y; tablerow r = new tablerow(this); table.addview(r); (int x = 0; x < table_width; x++) { final int col = x; button b = new button(this); b.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // toast t = toast.maketext(getapplicationcontext(), "you clicked (" + row + "," + col + ")", toast.length_short); toast t = toast.maketext(getapplicationcontext(), "tooth cavity in 6 month\n (previously in 2 years)", toast.length_short); t.getview().setbackgroundcolor(color.red); t.show(); if(row==0 && col==0){ image.setimageresource(r.drawable.snake); } else{ image.setvisibility(view.gone); } } }); r.addview(b); } } } }
and layout:
<tablelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/view_root" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:shrinkcolumns="*" tools:context=".mainactivity" > <imageview android:id="@+id/imageview1" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </tablelayout>
you create framelayout
tablelayout
, imageview
in it. due framelayouts have ability of overlapping things, making background of imageview transparent , drawing snake on work.
Comments
Post a Comment