Android : android make button square

on Sunday, August 31, 2014


I have several Buttons inside a linear layout One of the buttons (sett) needs to be square however the height changes depending on the screen size and resolution


How to i make the width the same as the height?


the button in question is "@+id/sett"


My code is below


activity_activity1.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/background2"
android:background="#EEEDEB"
android:layout_width="match_parent"
android:layout_height="match_parent"

android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/sodexotop" />



<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:background="#ffffff"
android:gravity="center_horizontal"
android:orientation="vertical" >

<Button
android:id="@+id/Button01"
android:layout_width="match_parent"
android:layout_marginLeft="50sp"
android:layout_marginRight="50sp"
android:onClick="clk_fault"
android:text="Enter Fault"
android:textColor="#000000"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="10sp"
android:background="@drawable/butt1"
android:textSize="30sp" />

<Button
android:id="@+id/Button03"
android:layout_width="match_parent"
android:layout_marginLeft="50sp"
android:layout_marginRight="50sp"
android:onClick="clk_send"
android:text="Send Data"
android:textColor="#000000"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="10sp"
android:background="@drawable/butt1"
android:textSize="30sp" />

<Button
android:id="@+id/Button04"
android:layout_width="match_parent"
android:layout_marginLeft="50sp"
android:layout_marginRight="50sp"
android:onClick="clk_clear"
android:text="Clear Data"
android:textColor="#000000"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="10sp"
android:background="@drawable/butt1"
android:textSize="30sp" />



<Button
android:id="@+id/sett"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10sp"
android:paddingTop="10sp"
android:paddingRight="10sp"
android:paddingBottom="10sp"
android:layout_marginTop="10sp"

android:background="@drawable/butt1"
android:onClick="clk_sett"
android:text="S"
android:textColor="#000000"
android:textSize="50sp" />




<Button
android:id="@+id/exitbtn2"
android:text="O"
android:textColor="#000000"
android:textSize="30sp"
android:layout_width="60sp"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="10sp"
android:background="@drawable/butt1"
android:onClick="clk_exit" />
<TextView
android:id="@+id/label_xxx3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text=""
android:layout_marginTop="10sp"
android:textSize="10sp" />
</LinearLayout>

<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/sodexobottom" />
</LinearLayout>


Activity1.java



public class Activity1 extends Activity {
String BACK="";
public static Drawable myDrawable;
/** Called when the activity is first created. */
DBAdapter db;
final int ACTIVITY_CHOOSE_FILE = 1;
@Override
public void onStart()
{
// RUN SUPER | REGISTER ACTIVITY AS INSTANTIATED IN APP CLASS
super.onStart();
App.Activity1 = this;
}
@Override
public void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.RGBA_8888);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DITHER);
setContentView(R.layout.activity_activity1);

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int h = metrics.heightPixels;
int w = metrics.widthPixels;
int w1 = (w/2);



String testStringw = Integer.toString(w);
String testStringh = Integer.toString(h);
Button txt1 = (Button) findViewById(R.id.Button01);
Typeface font1 = Typeface.createFromAsset(getAssets(), "fonts/DistProTh.otf");
txt1.setTypeface(font1);
Button txt2 = (Button) findViewById(R.id.Button03);
Typeface font2 = Typeface.createFromAsset(getAssets(), "fonts/DistProTh.otf");
txt2.setTypeface(font2);
Button txt3 = (Button) findViewById(R.id.Button04);
Typeface font3 = Typeface.createFromAsset(getAssets(), "fonts/DistProTh.otf");
txt3.setTypeface(font3);
Button txt4 = (Button) findViewById(R.id.sett);
Typeface font4 = Typeface.createFromAsset(getAssets(), "fonts/WebSymbols-Regular.otf");
txt4.setTypeface(font4);


txt4.setLayoutParams(new LayoutParams(60, 60));




Button txt5 = (Button) findViewById(R.id.exitbtn2);
Typeface font5 = Typeface.createFromAsset(getAssets(), "fonts/heydings_controls.ttf");
txt5.setTypeface(font5);



}


Any help is appreciated


Mark


0 comments:

Post a Comment