android


Programatically give space between buttons in table layout


I have a activity like this.
![Image][1]
Here start ,complete and delivered are buttons.how to give space between buttons.?
Below is my code.
i have a table layout designed in .xml and since its data is dynamic i am adding rows programatically.
/*** xml code ****/
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFF"
android:orientation="vertical" >
<ImageView android:id="#+id/title_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/timg"/>
<TextView android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="63dp"
android:text="Alpha Restaurant"
android:gravity="center_horizontal"
android:layout_toRightOf="#+id/title_image"
android:background="#ED4040"
android:typeface="serif"
android:textColor="#FFF"
android:textStyle="bold"
android:textSize="35sp"/>
<TextView android:id="#+id/tbno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:text="Kitchen Dashboard :"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:typeface="serif"
android:textColor="#05539C"
android:textStyle="bold"
android:textSize="18sp"/>
<TextView android:id="#+id/tvtbno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:layout_toRightOf="#+id/tbno"
android:text="KIT01"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:typeface="serif"
android:textColor="#05539C"
android:textStyle="bold"
android:textSize="18sp"/>
<TextView android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:text="Time :"
android:layout_toRightOf="#+id/tvtbno"
android:layout_marginLeft="60dp"
android:layout_marginTop="20dp"
android:typeface="serif"
android:textColor="#05539C"
android:textStyle="bold"
android:textSize="18sp"/>
<TextView android:id="#+id/tvtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:layout_toRightOf="#+id/time"
android:text="KIT01"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:typeface="serif"
android:textColor="#05539C"
android:textStyle="bold"
android:textSize="18sp"/>
<TextView android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:text="Date :"
android:layout_toRightOf="#+id/tvtime"
android:layout_marginLeft="60dp"
android:layout_marginTop="20dp"
android:typeface="serif"
android:textColor="#05539C"
android:textStyle="bold"
android:textSize="18sp"/>
<TextView android:id="#+id/tvdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:layout_toRightOf="#+id/date"
android:text="KIT01"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:typeface="serif"
android:textColor="#05539C"
android:textStyle="bold"
android:textSize="18sp"/>
<ScrollView android:layout_width="fill_parent"
android:layout_height="370dp"
android:id="#+id/sc" android:layout_below="#+id/tvdate">
<TableLayout android:id="#+id/tableLayout1"
android:layout_below="#+id/tbno"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="550dp"
android:shrinkColumns="*"
android:stretchColumns="*">
<TableRow
android:id="#+id/tableRow1"
android:background="#A9D0F5"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:id="#+id/tv1"
android:text="Lot No"
android:layout_marginLeft="15dp"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#190710"
android:typeface="serif"/>
<TextView
android:id="#+id/tv2"
android:text="Item"
android:layout_marginLeft="40dp"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#190710"
android:typeface="serif"/>
<TextView
android:id="#+id/tv3"
android:text="Quantity"
android:layout_marginLeft="40dp"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#190710"
android:typeface="serif"/>
<TextView
android:id="#+id/tv4"
android:text="Order"
android:layout_marginLeft="40dp"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#190710"
android:typeface="serif"/>
<TextView
android:id="#+id/tv5"
android:text="Notes"
android:layout_marginLeft="40dp"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#190710"
android:typeface="serif"/>
<TextView
android:id="#+id/tv6"
android:text="Start"
android:layout_marginLeft="40dp"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#190710"
android:typeface="serif"/>
<TextView
android:id="#+id/tv7"
android:text="Completed"
android:layout_marginLeft="40dp"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#190710"
android:typeface="serif"/>
<TextView
android:id="#+id/tv8"
android:text="Delivered"
android:layout_marginLeft="40dp"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#190710"
android:typeface="serif"/>
</TableRow>
</TableLayout>
</ScrollView>
<!-- <TextView android:id="#+id/total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total : "
android:textSize="22sp"
android:typeface="serif"
android:textStyle="bold"
android:layout_marginLeft="230dp"
android:layout_below="#+id/tableLayout1"/>
<TextView android:id="#+id/tvtotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="500 Rs"
android:textSize="22sp"
android:typeface="serif"
android:textStyle="bold"
android:layout_toRightOf="#+id/total"
android:layout_below="#+id/tableLayout1"/>-->
<Button android:id="#+id/refresh"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Refresh"
android:textStyle="bold"
android:layout_marginTop="20dp"
android:layout_marginLeft="340dp"
android:layout_below="#+id/sc"/>
<Button android:id="#+id/home"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Home"
android:layout_marginLeft="20dp"
android:textStyle="bold"
android:layout_marginTop="20dp"
android:layout_toRightOf="#+id/refresh"
android:layout_below="#+id/sc"/>
</RelativeLayout>
/**** java code ****/
#Override
protected void onPostExecute(Boolean a) {
// TODO Auto-generated method stub
super.onPostExecute(a);
progressDialog.dismiss();
int count= 0;
try{
allarray = new JSONArray(result);
System.out.println("array is " +allarray.length());
JSONObject json_data=null;
for(int j=0;j<allarray.length();j++){
json_data = allarray.getJSONObject(j);
json_data.getString("PreparationLot");
json_data.getString("Prep_MenuItem");
json_data.getString("Prep_Quantity");
json_data.getString("Prep_Order_Number");
json_data.getString("Prep_Notes");
TableLayout tl = (TableLayout) findViewById(R.id.tableLayout1);
TableRow tr = new TableRow(DashBoard.this);
if(count%2!=0) {
tr.setBackgroundColor(Color.rgb(222, 219, 219));
}else{
tr.setBackgroundColor(Color.rgb(255, 255, 255));
}
tr.setId(20);
tr.setLayoutParams(new LayoutParams());
final TextView lotno = new TextView(DashBoard.this);
lotno.setId(200+count);// define id that must be unique
lotno.setText(""+json_data.getString("PreparationLot")); // set the text for the header
lotno.setTextColor(Color.BLACK); // set the color
lotno.setPadding(10, 10, 0, 10); // set the padding (if required)
lotno.setTextSize(14);
tr.addView(lotno);
final TextView itemname = new TextView(DashBoard.this);
itemname.setId(200+count);// define id that must be unique
itemname.setText(""+json_data.getString("Prep_MenuItem")); // set the text for the header
itemname.setTextColor(Color.BLACK); // set the color
itemname.setMaxLines(3);
itemname.setPadding(10, 10, 0, 10); // set the padding (if required)
itemname.setTextSize(14);
tr.addView(itemname);
TextView qty = new TextView(DashBoard.this);
qty.setId(200+count);// define id that must be unique
qty.setText(""+json_data.getString("Prep_Quantity")); // set the text for the header
qty.setTextColor(Color.BLACK); // set the color
qty.setPadding(50, 10, 0, 10); // set the padding (if required)
qty.setTextSize(14);
tr.addView(qty);
TextView order = new TextView(DashBoard.this);
order.setId(200+count);// define id that must be unique
order.setText(""+json_data.getString("Prep_Order_Number")); // set the text for the header
order.setTextColor(Color.BLACK); // set the color
order.setPadding(40, 10, 0, 10); // set the padding (if required)
order.setTextSize(14);
tr.addView(order);
TextView notes = new TextView(DashBoard.this);
notes.setId(200+count);// define id that must be unique
notes.setText(""+json_data.getString("Prep_Notes")); // set the text for the header
notes.setTextColor(Color.BLACK); // set the color
notes.setPadding(40, 10, 0, 10); // set the padding (if required)
notes.setTextSize(14);
tr.addView(notes);
final Button start = new Button(DashBoard.this);
start.setText("Start");
start.setMaxWidth(10);
start.setTextSize(16);
start.setTypeface(null, Typeface.BOLD);
start.setBackgroundColor(Color.RED);
tr.addView(start);
final Button complete = new Button(DashBoard.this);
complete.setText("Complete");
complete.setMaxWidth(10);
complete.setTextSize(16);
complete.setTypeface(null, Typeface.BOLD);
complete.setBackgroundColor(Color.BLUE);
tr.addView(complete);
final Button delivered = new Button(DashBoard.this);
delivered.setText("Delivered");
delivered.setMaxWidth(10);
delivered.setTextSize(16);
delivered.setTypeface(null, Typeface.BOLD);
delivered.setBackgroundColor(Color.BLUE);
tr.addView(delivered);
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
count++;
}
add your buttons using following code,
and if you want to increase the space then increase the leftMargin from 10 to 15 or 20.
final Button start = new Button(DashBoard.this);
ViewGroup.MarginLayoutParams lpButton = (ViewGroup.MarginLayoutParams) start.getLayoutParams();
lpButton.setMargins(10, 0, 0, 0);
start.setText("Start");
start.setMaxWidth(10);
start.setTextSize(16);
start.setTypeface(null, Typeface.BOLD);
start.setBackgroundColor(Color.RED);
start.setLayoutParams(lpButton);
tr.addView(start);
final Button complete = new Button(DashBoard.this);
complete.setText("Complete");
complete.setMaxWidth(10);
complete.setTextSize(16);
complete.setTypeface(null, Typeface.BOLD);
complete.setBackgroundColor(Color.BLUE);
complete.setLayoutParams(lpButton);
tr.addView(complete);
final Button delivered = new Button(DashBoard.this);
delivered.setText("Delivered");
delivered.setMaxWidth(10);
delivered.setTextSize(16);
delivered.setTypeface(null, Typeface.BOLD);
delivered.setBackgroundColor(Color.BLUE);
delivered.setLayoutParams(lpButton);
tr.addView(delivered);
try to giving paddingLeft or paddingRight to the buttons. Besides you can give padding to a layout that wraps them.

Related Links

Passing Intent can't get extra();
AddRequest Is Not Respected in HttpURLConnection
How to zoom in out with the Google cardboard
accessing a view in a SimpleCursorAdapter from the ListFragment
Set looping in ringtone in android
Android app data recovery
Resize/Scaling down a YouTubePlayerFragment while video is playing
Null Pointer Exception in ExpandableListView in .put()
Implement Navigation Drawer Activity to existing app
HttpURLConnect - Http PUT with JSON using GSON streaming not sending content
Android Studio - Devices Logcat showing two process
Android: Trouble with FragmentManager not replacing FrameLayout
WindowManager.removeView() gives IllegalStateException
IOS App Development in Windows System using Titanium studio
GUI Development (in Android)
where to implement network operation in android service or in background thread?

Categories

HOME
google-cloud-platform
angular2-material
youtube-data-api-v3
package
sublimetext
carousel
aws-cli
flume
accessibility
syntaxnet
startup
project-intu
ycsb
3nf
scrolltop
capistrano3
scsi
mathprog
dreamweaver
easyphp
html5-appcache
sencha-touch
oledb
ag
imagemap
postscript
hta
pylons
android-maps
wcag
comparable
realm-mobile-platform
finagle
record
quickfixj
dynamic-jasper
imx6
lubridate
navigator
video-capture
wurfl
openalpr
railstutorial.org
opensmpp
odoo-website
parser-generator
softmax
outputcache
lampp
page-replacement
headless
flyout
mathcad
django-1.10
pure-css
stax
mediametadataretriever
ikiwiki
createprocess
html-to-pdf
user-management
grgit
rate-limiting
eoferror
wso2greg
typeconverter
bufferedinputstream
zxspectrum
adehabitathr
packet-sniffers
crowd
punycode
tinyxml
dct
vsx
embedded-fonts
rolify
mvc-editor-templates
cmmi
flex++
facebook-timeline
abstract-data-type
pclzip
android-icons
midlet
qtconcurrent
boost-msm
subgraph
inkcanvas
kqueue
drawtobitmap
w3c-geolocation
maven-1
object-database
scrubyt
application-planning
firefox-3
lemmatization
appliance

Resources

Encrypt Message