Change Background / Gravity in ImageButton

I have the following view:

enter image description here

And I want it to look like this:

enter image description here

In the first, the background of the green ImageButton is in the upper left corner. And in the second background, the green ImageButton is in the center. (The background image is the size of a green square)

Here is my XML code:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageButton android:id="@+id/dialer" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/btncall" android:scaleType="centerInside"/> <Button android:id="@+id/sendSMS" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send SMS"/> <Button android:id="@+id/four" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/shutDownDevice" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Turn Off"/> </RelativeLayout> 

Where

 android:background="@drawable/btncall" 

refers to btncall.xml, which is as follows:

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/phone_down" android:state_pressed="true" /> <item android:drawable="@drawable/phone_down" android:state_focused="true" /> <item android:drawable="@drawable/phone_up" /> </selector> 

I am starting with Android, so if you see a better way to create this wiew, plz tell me.

10x a lot, Ryan.


In Addtion, this is part of the code in the main Java file:

 ... //Get width and Height of screen Display display = getWindowManager().getDefaultDisplay(); int halfStageWidth = display.getWidth()/2; int halfStageHeight = display.getHeight()/2; //Get an instance of the Dialer button ImageButton btnDialer = (ImageButton) findViewById(R.id.dialer); Button btnSendSMS = (Button) findViewById(R.id.sendSMS); btnSendSMS.setWidth(halfStageWidth); btnSendSMS.setHeight(halfStageHeight); Button btnShutDownDevice = (Button) findViewById(R.id.shutDownDevice); btnShutDownDevice.setWidth(halfStageWidth); btnShutDownDevice.setHeight(halfStageHeight); Button B4 = (Button) findViewById(R.id.four); B4.setWidth(halfStageWidth); B4.setHeight(halfStageHeight); ... 
+4
source share
2 answers

You do this in a too complicated way, it can only be done using XML without the need for java code.

Your XML should be:

 <?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="fill_parent" android:orientation="vertical" > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="horizontal" > <ImageButton android:id="@+id/dialer" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/btncall" android:background="@android:color/transparent" android:scaleType="centerInside" android:layout_weight="1"/> <Button android:id="@+id/sendSMS" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="Send SMS" android:layout_weight="1" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="horizontal" > <Button android:id="@+id/four" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"/> <Button android:id="@+id/shutDownDevice" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="Turn Off" android:layout_weight="1"/> </LinearLayout> 

+3
source

You have not set the size of the image button so that it wraps around the image. You must set the size and it must be inside the button.

Update:

You can set the layout size as follows:

 RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width,height); // Add relativeLayout rules here btnDialer.setLayoutParams(params); 
0
source

All Articles