Reputation: 4559
In my sample app, I try to create Button
dynamically using the below code. Everything works fine, but once I set the background image to the button, what I notice is that the image becomes oval in shape. But in actual the image are circle in shape. Why this happens, is anything to do in my code? Help me to solve this.
main.xml file
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/calendar_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical" >
<ViewFlipper
android:id="@+id/flipview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff">
<LinearLayout
android:id="@+id/liVLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical" >
</LinearLayout>
</ViewFlipper>
</RelativeLayout>
Jave code
public class MainActivity extends Activity
{
private Button[][] m_pBtnDay;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m_pBtnDay = new Button[6][7];
initDay();
}
public boolean initDay()
{
LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
LinearLayout rowLayout = null;
LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1);
for (int i = 0; i<6; i++)
{
rowLayout = new LinearLayout(this);
rowLayout.setWeightSum(7);
layoutVertical.addView(rowLayout, param);
for(int j=0; j<7; j++)
{
m_pBtnDay[i][j] = new Button(this);
m_pBtnDay[i][j].setTextSize(15);
m_pBtnDay[i][j].setBackgroundResource(R.drawable.theme1_cal_button_selected);
rowLayout.addView(m_pBtnDay[i][j], param);
}
}
return true;
}
}
Screenshot
Upvotes: 0
Views: 168
Reputation: 26034
Solution for your problem is to use Bitmap
in drawable directory like following.
Create an xml bitamp
file, name it button_background.xml
in drawable directory and put following short of code.
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/ic_launcher"
android:tileMode="disabled" android:gravity="top" >
</bitmap>
Replace ic_launcher
image with your one. Then instead of using your background image, use following code.
<Button
android:id="@+id/buttonok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_background"
android:text="your Text" />
Bingo.. This will not stretch your image.
Upvotes: 1