Monday 31 October 2011

MainScreen.java =>dynamically adjusting height,width of widget


package com.OfficeDepot;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;

public class MainScreen extends TabActivity implements OnClickListener {

protected boolean _active = true;
protected int _splashTime = 3000;
static TabHost tabHost;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash2);

RelativeLayout main = (RelativeLayout) findViewById(R.id.mainscreen);
WindowManager w = getWindowManager();
Display d = w.getDefaultDisplay();
int width = d.getWidth();
int height = d.getHeight();
LayoutParams l = main.getLayoutParams();

l.width = width;
if (height == 320) {
l.height = (int) (height * 0.75);
} else {
l.height = (int) (height * 0.80);
}
main.setLayoutParams(l);

main.setOnClickListener(this);
RelativeLayout.LayoutParams layout1 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
if (height == 800) {
layout1.height = 110;
layout1.width = 100;
layout1.setMargins(205, 95, 0, 0);

} else if (height == 854) {
layout1.height = 125;
layout1.width = 110;
layout1.setMargins(205, 95, 0, 0);

} else if (height == 432) {
layout1.width = 53;
layout1.height = 65;
layout1.setMargins(100, 45, 0, 0);
} else if (height == 400) {
layout1.width = 53;
layout1.height = 65;
layout1.setMargins(100, 40, 0, 0);
}

else {
layout1.width = 50;
layout1.height = 50;
layout1.setMargins(100, 30, 0, 0);

}
RelativeLayout.LayoutParams layout2 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
if (height == 800) {
layout2.height = 110;
layout2.width = 100;
layout2.setMargins(30, 200, 0, 0);

} else if (height == 854) {
layout2.height = 125;
layout2.width = 110;
layout2.setMargins(30, 210, 0, 0);

}

else if (height == 432) {
layout2.width = 53;
layout2.height = 65;
layout2.setMargins(15, 106, 0, 0);
} else if (height == 400) {
layout2.width = 53;
layout2.height = 65;
layout2.setMargins(15, 100, 0, 0);
} else {
layout2.width = 50;
layout2.height = 50;
layout2.setMargins(15, 65, 0, 0);

}
RelativeLayout.LayoutParams layout3 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
if (height == 800) {
layout3.height = 110;
layout3.width = 100;
layout3.setMargins(350, 210, 0, 0);

} else if (height == 854) {
layout3.height = 125;
layout3.width = 110;
layout3.setMargins(350, 220, 0, 0);

} else if (height == 432) {
layout3.width = 53;
layout3.height = 65;
layout3.setMargins(170, 113, 0, 0);
} else if (height == 400) {
layout3.width = 53;
layout3.height = 65;
layout3.setMargins(170, 107, 0, 0);
} else {
layout3.width = 50;
layout3.height = 50;
layout3.setMargins(170, 80, 0, 0);

}
RelativeLayout.LayoutParams layout4 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
if (height == 800) {
layout4.height = 110;
layout4.width = 100;
layout4.setMargins(65, 425, 0, 0);

} else if (height == 854) {
layout4.height = 125;
layout4.width = 110;
layout4.setMargins(65, 450, 0, 0);

} else if (height == 432) {
layout4.width = 53;
layout4.height = 63;
layout4.setMargins(35, 226, 0, 0);
} else if (height == 400) {
layout4.width = 53;
layout4.height = 63;
layout4.setMargins(35, 210, 0, 0);
} else {
layout4.width = 50;
layout4.height = 50;
layout4.setMargins(35, 160, 0, 0);

}
RelativeLayout.LayoutParams layout5 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
if (height == 800) {
layout5.height = 110;
layout5.width = 100;
layout5.setMargins(300, 435, 0, 0);

} else if (height == 854) {
layout5.height = 125;
layout5.width = 110;
layout5.setMargins(300, 460, 0, 0);

} else if (height == 432) {
layout5.width = 53;
layout5.height = 63;
layout5.setMargins(151, 232, 0, 0);

} else if (height == 400) {
layout5.width = 53;
layout5.height = 63;
layout5.setMargins(151, 218, 0, 0);

} else {
layout5.width = 50;
layout5.height = 50;
layout5.setMargins(155, 160, 0, 0);

}

final Button shop = (Button) this.findViewById(R.id.b1);
final Button specials = (Button) this.findViewById(R.id.b2);
final Button printers = (Button) this.findViewById(R.id.b3);
final Button copiers = (Button) this.findViewById(R.id.b4);
final Button cashregister = (Button) this.findViewById(R.id.b5);
final RelativeLayout mainscreen = (RelativeLayout) this
.findViewById(R.id.mainscreen);
mainscreen.removeAllViews();
mainscreen.addView(shop, layout1);
mainscreen.addView(specials, layout2);
mainscreen.addView(printers, layout3);
mainscreen.addView(copiers, layout4);
mainscreen.addView(cashregister, layout5);

shop.setOnClickListener(this);
specials.setOnClickListener(this);
printers.setOnClickListener(this);
copiers.setOnClickListener(this);
cashregister.setOnClickListener(this);
tabHost = getTabHost();

LayoutInflater.from(this).inflate(R.layout.tabs1,
tabHost.getTabContentView(), true);

tabHost.addTab(tabHost
.newTabSpec("tab1")
.setIndicator("tab1",
getResources().getDrawable(R.drawable.settings))
.setContent(R.id.view1));
tabHost.addTab(tabHost
.newTabSpec("tab3")
.setIndicator("tab2",
getResources().getDrawable(R.drawable.mainimage))
.setContent(R.id.view2));
tabHost.addTab(tabHost
.newTabSpec("tab3")
.setIndicator("tab3",
getResources().getDrawable(R.drawable.wishlist))
.setContent(R.id.view3));
tabHost.addTab(tabHost.newTabSpec("tab4").setIndicator("tab4")
.setContent(R.id.view3));
tabHost.getTabWidget().getChildAt(4).setVisibility(4);

getTabWidget().getChildAt(1).setOnClickListener(
new View.OnClickListener() {

@Override
public void onClick(View v) {
// do whatever you need
Intent intent;
if (flag.counter_islogin == 0) {
intent = new Intent(MainScreen.this,
LoginActivity.class);
} else {
intent = new Intent(MainScreen.this,
LogoutActivity.class);
}
flag.count_login = 0;

flag.flag_mainscreen = true;
MainScreen.this.finish();
startActivity(intent);
}
});
getTabWidget().getChildAt(3).setOnClickListener(
new View.OnClickListener() {

@Override
public void onClick(View v) {
// do whatever you need
Intent intent;
if ((flag.flag_wishlistdelete == false)
|| (LoginActivity.flag_login == false)
|| (flag.counter_islogin == 0)) {
intent = new Intent(MainScreen.this,
wishlistActivity.class);
} else {
intent = new Intent(MainScreen.this,
wishlistdeleteActivity.class);
}
flag.count_wishlist = 0;
flag.flag_mainscreen_wish = true;
MainScreen.this.finish();
startActivity(intent);
}
});

}

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (arg0.getId() == R.id.b1) {
Intent intent = new Intent(MainScreen.this,
MainCategoryActivity.class);
flag.maincatfromwebservice = 1;
startActivity(intent);
} else if (arg0.getId() == R.id.b2) {
Intent m = new Intent(MainScreen.this, SubCategoriesActivity.class);
flag.subcatfromwebservice = 1;
m.putExtra("CategoryID", "1424");
m.putExtra("Name", "Specials");
m.putExtra("MappedParentCategoryID", "0");
startActivity(m);
} else if (arg0.getId() == R.id.b3) {
Intent m = new Intent(MainScreen.this, SubCategoriesActivity.class);
flag.subcatfromwebservice = 1;
m.putExtra("CategoryID", "1420");
m.putExtra("Name", "Printers");
m.putExtra("MappedParentCategoryID", "0");
startActivity(m);
} else if (arg0.getId() == R.id.b4) {
Intent m = new Intent(MainScreen.this, SubCategoriesActivity.class);
flag.subcatfromwebservice = 1;
m.putExtra("CategoryID", "1406");
m.putExtra("Name", "Copiers");
m.putExtra("MappedParentCategoryID", "0");
startActivity(m);
} else if (arg0.getId() == R.id.b5) {
Intent m = new Intent(MainScreen.this, SubCategoriesActivity.class);
flag.subcatfromwebservice = 1;
m.putExtra("CategoryID", "1403");
m.putExtra("Name", "Cash Registers");
m.putExtra("MappedParentCategoryID", "0");
startActivity(m);
}

}

}

No comments:

Post a Comment