I believe that it takes up the whole screen because you do not have a container like Linear Layout which then contains an ImageView with layout restrictions, so the ImageView expands to fill the available screen. Try the following:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView tv = new TextView(this); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(80, 100); tv.setLayoutParams(layoutParams); tv.setText("testing 1 2 3"); tv.setTextColor(Color.BLACK); tv.setBackgroundColor(Color.TRANSPARENT); Bitmap testB; testB = Bitmap.createBitmap(80, 100, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(testB); tv.layout(0, 0, 80, 100); tv.draw(c); ImageView iv = (ImageView)findViewById(R.id.menuIcon); iv.setLayoutParams(layoutParams); iv.setBackgroundColor(Color.GRAY); iv.setImageBitmap(testB); iv.setMaxHeight(80); iv.setMaxWidth(80); }
And in the main.xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/menuIcon" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
Iβm not sure what you want to achieve, but Iβm sure that there are more effective ways to get it closer.
source share