help me solve my problem. There is a main layout RelativeLayout it is Linearlayout where I want to programmatically add view. Here is my code.
public class GenBarCodeActivity extends Activity {
BarCodeView Barview = null;
LinearLayout.LayoutParams layoutParams;
LinearLayout ll;
RelativeLayout rl;
Button getBarCode;
private static final int ID = 34646456;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_barcode);
getBarCode = (Button)findViewById(R.id.btnGetBarCode);
getBarCode.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ll = (LinearLayout)findViewById(R.id.linearCodeView);
EANTEXT = edBarText.getText().toString();
ImageView old = (ImageView) ll.findViewById(ID);
if (old != null) {
((LinearLayout) old.getParent()).removeViewInLayout(old);
}
layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ll.addView(Barview, layoutParams);
rl.invalidate();
}
});
fillLinear();
setBarCode();
}
private void fillLinear(){
rl = (RelativeLayout)findViewById(R.id.layout_barcode);
ll = (LinearLayout)findViewById(R.id.linearCodeView);
layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ImageView imBar = new ImageView(this);
Bitmap imbitmap = BitmapFactory.decodeResource(getResources(), R.drawable.barcode);
imBar.setLayoutParams(layoutParams);
imBar.setImageBitmap(imbitmap);
imBar.setId(ID);
ll.setBackgroundColor(Color.TRANSPARENT);
ll.addView(imBar);
}
private void setBarCode(){
Barview = new BarCodeView(GenBarCodeActivity.this);
Barview.setBarCodeNum(0);
}
}
This is my View
public class BarCodeView extends View {
private static int numcode = 0;
private static String codedata = null;
public BarCodeView(Context context) {
super(context);
}
public void setBarCodeText(String text){
this.codedata = text;
}
public String getBarCodeText(){
return this.codedata;
}
public void setBarCodeNum(int num){
this.numcode = num;
}
public int getBarCodeNum(){
return this.numcode;
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
switch (numcode) {
// ean 13
case 0:
try{
showEAN13(canvas);
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
private static void showEAN13(Canvas canvas) throws Exception {
EAN13 barcode = new EAN13();
/*
EAN 13 Valid data char set:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9 (Digits)
EAN 13 Valid data length: 12 digits only, excluding the last checksum digit
*/
barcode.setData(codedata);
// for EAN13 with supplement data (2 or 5 digits)
/*
barcode.setSupData("12");
// supplement bar height vs bar height ratio
barcode.setSupHeight(0.8f);
// space between barcode and supplement barcode (in pixel)
barcode.setSupSpace(15);
*/
// Unit of Measure, pixel, cm, or inch
barcode.setUom(IBarcode.UOM_PIXEL);
// barcode bar module width (X) in pixel
barcode.setX(2f);
// barcode bar module height (Y) in pixel
barcode.setY(90f);
// barcode image margins
barcode.setLeftMargin(10f);
barcode.setRightMargin(10f);
barcode.setTopMargin(10f);
barcode.setBottomMargin(10f);
// barcode image resolution in dpi
barcode.setResolution(72);
// disply barcode encoding data below the barcode
barcode.setShowText(true);
// barcode encoding data font style
barcode.setTextFont(new AndroidFont("Arial", Typeface.NORMAL, 10));
// space between barcode and barcode encoding data
barcode.setTextMargin(6);
barcode.setTextColor(AndroidColor.black);
// barcode bar color and background color in Android device
barcode.setForeColor(AndroidColor.black);
barcode.setBackColor(AndroidColor.white);
/*
specify your barcode drawing area
*/
RectF bounds = new RectF(120, 120, 0, 0);
barcode.drawBarcode(canvas, bounds);
}
}
here is my xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_barcode"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0e0a2"
android:orientation="vertical" >
<ImageView
android:id="#+id/closeBarCode"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/txtInfo"
android:layout_marginRight="3dp"
android:src="#drawable/delete" />
<TextView
android:id="#+id/txtInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="3dp"
android:layout_marginTop="3dp"
android:text="#string/barcode_title"
android:textColor="#000"
android:textSize="20dip" />
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/closeBarCode" >
<TextView
android:id="#+id/txtEAN"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/barcode_ean"
android:textColor="#000"
android:textSize="20dip" />
<Spinner
android:id="#+id/spEAN"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtInfo"
android:layout_below="#+id/linearLayout1" >
<TextView
android:id="#+id/txtCodeColor"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/barcode_color"
android:textColor="#000"
android:textSize="20dip" />
<Spinner
android:id="#+id/spCodeColor"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearLayout2"
android:layout_below="#+id/linearLayout2" >
<TextView
android:id="#+id/txtCodeBackColor"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/barcode_back_color"
android:textColor="#000"
android:textSize="20dip" />
<Spinner
android:id="#+id/spCodeBackColor"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout3" >
<TextView
android:id="#+id/txtCodePosition"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/barcode_position"
android:textColor="#000"
android:textSize="20dip" />
<Spinner
android:id="#+id/spCodePosition"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearCodeView"
android:layout_width="300dip"
android:layout_height="200dip"
android:layout_below="#+id/edBarCode"
android:layout_marginRight="45dp"
android:layout_marginTop="25dip"
android:layout_toLeftOf="#+id/closeBarCode"
android:orientation="vertical" >
</LinearLayout>
<Button
android:id="#+id/btnSetBarCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btnGetBarCode"
android:layout_alignBottom="#+id/btnGetBarCode"
android:layout_toLeftOf="#+id/closeBarCode"
android:text="#string/set_code" />
<Button
android:id="#+id/btnGetBarCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="31dp"
android:text="#string/get_code" />
<EditText
android:id="#+id/edBarCode"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearCodeView"
android:layout_alignRight="#+id/linearCodeView"
android:layout_below="#+id/txtCodeText"
android:layout_marginTop="23dp"
android:ems="10" />
<TextView
android:id="#+id/txtCodeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/edBarCode"
android:layout_below="#+id/txtInfo"
android:layout_marginRight="86dp"
android:text="#string/code_text"
android:textColor="#000"
android:textSize="20dip" />
</RelativeLayout>
I need to Linearlayout "#+id/linearCodeView" insert my BarCodeView.
View is not visible after adding LinearLayout.
RelativeLayout.LayoutParams paramsBottom = new RelativeLayout.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
rl.addView(ll, paramsBottom); //add linearlayout in relativelayout
Edit:-
LinearLayout.LayoutParams paramsBottom2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
ll.addView(Barview, paramsBottom2);//add Barview in linearlayout
OR
RelativeLayout.LayoutParams paramsBottom2 = new RelativeLayout.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
rl.addView(Barview, paramsBottom2);//add Barview in relativelayout
Related
I am currently creating android app. I have a login activity in which on top there is LinearLayout with ImageViewand below it there is GirdLayout where there are few EditTexts. I would like to move my LinearLayout to given height when any of the EditTexts has focus. I was trying the solution from here:
Android translate animation - permanently move View to new position using AnimationListener and put AnimationListener inside of onFocusChange, but in my case it appears to do nothing. Could You help me?
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="6"
android:background="#74B897"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/imageLayout"
>
<ImageView
android:layout_width="247dp"
android:layout_height="290dp"
android:id="#+id/logo_imageView"
android:layout_alignParentTop="true"
android:layout_gravity="center_horizontal"
android:layout_row="1"
android:src="#drawable/white_logo" />
</LinearLayout>
<GridLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#drawable/layout_shape"
android:layout_weight="2"
android:layout_row="7"
android:layout_column="1"
android:weightSum="5"
android:layout_below="#+id/logo_imageView"
android:layout_marginLeft = "15dp"
android:layout_marginRight = "15dp"
android:layout_marginBottom = "30dp"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/fname_editText"
android:hint="First Name"
android:layout_rowWeight="0"
android:layout_marginTop = "15dp"
android:layout_row="1"
android:layout_column="0"
android:textColor="#FFFFFF"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/sname_editText"
android:layout_below="#+id/fname_editText"
android:hint="Second Name"
android:layout_rowWeight="0"
android:layout_row="2"
android:layout_column="0"
android:textColor="#FFFFFF"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/password_editText"
android:inputType="textPassword"
android:ems="10"
android:hint="Password"
android:layout_rowWeight="0"
android:layout_row="3"
android:layout_column="0"
android:textColor="#FFFFFF"/>
<Button
android:layout_width="141dp"
android:text="Log In"
android:id="#+id/log_button"
android:background="#drawable/button_shape"
android:layout_marginTop="10dp"
android:layout_marginBottom="40dp"
android:layout_below="#+id/id_editText"
android:layout_centerInParent="true"
android:layout_gravity="center_horizontal"
android:layout_rowWeight="1"
android:textSize="20dp"
android:textStyle="bold"
android:layout_row="5"
android:layout_column="0"
android:layout_height="29dp"
android:textColor="#636363" />
</GridLayout>
</LinearLayout>
Activity:
public class LoginActivity extends Activity implements View.OnFocusChangeListener{
EditText sname;
EditText password;
TextView info;
ImageView img;
EditText fname;
LinearLayout imageLayout;
int oldX, oldY, newX, newY;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_layout);
fname = (EditText) findViewById(R.id.fname_editText);
sname = (EditText)findViewById(R.id.sname_editText);
password = (EditText)findViewById(R.id.password_editText);
img= (ImageView) findViewById(R.id.logo_imageView);
imageLayout = (LinearLayout) findViewById(R.id.imageLayout);
oldX = imageLayout.getWidth();
oldY = imageLayout.getHeight();
img.setImageResource(R.drawable.white_logo);
Button log_Button = (Button) findViewById(R.id.log_button);
fname.setFocusable(true);
sname.setFocusable(true);
password.setFocusable(true);
}
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(v.getId() == R.id.fname_editText || v.getId() == R.id.sname_editText || v.getId() == R.id.password_editText)
{
final ObjectAnimator moveDownAnim = ObjectAnimator.ofFloat(imageLayout, "translationY", 0.F, -300);
final ObjectAnimator moveUpAnim = ObjectAnimator.ofFloat(imageLayout, "translationY", 300, 0.F);
if(hasFocus){
//first option
moveDownAnim.start();
/*
second option
TranslateAnimation anim = new TranslateAnimation(0, 0, 0, -300);
anim.setFillAfter(false);
anim.setDuration(1000);
anim.setAnimationListener(new TranslateAnimation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
imageLayout.clearAnimation();
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) imageLayout.getLayoutParams();
params.topMargin += -300;
imageLayout.setLayoutParams(params);
animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, -300);
animation.setDuration(1);
imageLayout.startAnimation(animation);
}
});
imageLayout.startAnimation(anim);*/
}
}
}
}
I don't see you setting the OnFocusChangeListener for the EditTexts in your code.
fname.setOnFocusChangeListener(this);
sname.setOnFocusChangeListener(this);
I want to display the video thumbnails in listview, I use images instead of thumbnails and add ImageView (image play button) on the front but failed my xml code like this
<?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="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/txttgl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:paddingLeft="10dip"
android:gravity="center_horizontal"/>
<TextView
android:id="#+id/tv_batas_unread"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:paddingLeft="12dip"
android:background="#drawable/counter_shape_birudonker"
android:visibility="gone"
android:gravity="center_horizontal"/>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/ll_chat_item"
android:orientation="horizontal" >
<com.ltvie.chatkrawala.ImageViewRounded
android:id="#+id/img_photo_pp_chatbox"
android:layout_width="50dp"
android:layout_height="50dp"
android:paddingBottom="0dp"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:adjustViewBounds="true"
android:paddingTop="0dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="3dp"
android:src="#drawable/gada_photo"
android:scaleType="fitXY" />
<LinearLayout
android:id="#+id/wrapper"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="vertical"
android:layout_marginBottom="12dp"
android:layout_weight="1"
android:cacheColorHint="#android:color/transparent"
>
<TextView
android:id="#+id/txtKodeGbr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<TextView
android:id="#+id/txtPesan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<TextView
android:id="#+id/comment"
android:layout_marginTop="9dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buble_kiri"
android:text="Bismillahirohmanirrohim"
android:textColor="#000"
android:textSize="15sp" />
<LinearLayout
android:id="#+id/rowFile"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="wrap_content"
>
<ImageView
android:id="#+id/img_dilvChat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:contentDescription="#string/descGambar"
android:background="#drawable/buble_kanan"
android:src="#drawable/gada_photo"
/>
<ImageView
android:id="#+id/img_btn_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/descGambar"
android:layout_centerInParent="true"
android:src="#drawable/play_icon"
/>
<ProgressBar
android:id="#+id/chatBox_progress_img"
style="?android:attr/progressBarStyleSmallInverse"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
<LinearLayout
android:id="#+id/rowFileDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_gravity="bottom"
android:orientation="vertical">
<TextView
android:id="#+id/txtFileName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dip"
android:gravity="left"
android:text="Nama File : test.zip"
android:textSize="12sp" />
<TextView
android:id="#+id/txtFileSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dip"
android:gravity="left"
android:text="Ukuran : 2324342 bytes"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/img_statusBaca"
android:layout_marginTop="2dip"
android:layout_width="15dip"
android:layout_height="15dip"
android:layout_marginRight="1dip"
android:background="#drawable/indicator_sending"
android:contentDescription="#string/descGambar"
/>
<TextView
android:id="#+id/txtjamchatMasuk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dip"
android:gravity="left"
android:text="setatus baca"
android:textSize="12sp" />
<TextView
android:id="#+id/txtKiriKanan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:text="right"
android:visibility="gone"
/>
</LinearLayout>
</LinearLayout>
<ImageView
android:id="#+id/imgselected"
android:layout_width="20dp"
android:layout_height="20dp"
android:contentDescription="#string/kosongan"
android:src="#drawable/ico_member" />
</LinearLayout>
</LinearLayout>
my java code
public class ChatboxArrayAdapter extends ArrayAdapter<OneComment> implements SectionIndexer{
private TextView countryName;
private LinearLayout wrapper,rowFileDesc,ll_chat_item;
private TextView TanggalMasuk,txtKodeGbr,txtPesan,txtJam,txtKiriKanan,txtUkuranFile;
private ImageView img_upload,img_statusbaca,imgPp_round;
ImageView imgPhoto,imgCheck,imgBtnPlay;
String namafileGambar,strPath,varStsBaca,tampilTgl;
public CacheImageLoader imageLoader;
Context ctx;
Options opts = new BitmapFactory.Options();
String TAG="chatboxArrayAdapter";
File dir = new File(Environment.getExternalStorageDirectory()+"");
File dirImage=new File(dir+"/a");
File dirVideo=new File(dir+"/a");
List<OneComment> countries;
#Override
public void add(OneComment object) {
countries.add(object);
super.add(object);
}
public void hapus_semua(){
countries.clear();
}
public void hapus_item(int nomerx){
countries.remove(nomerx);
notifyDataSetChanged();
}
public void refresh_lv(){
notifyDataSetChanged();
Log.d("notify", "datachange");
}
public ChatboxArrayAdapter(Context context, int textViewResourceId, List<OneComment> datanya) {
super(context, textViewResourceId);
this.countries=datanya;
this.ctx=context;
imageLoader=new CacheImageLoader(context.getApplicationContext());
}
public int getCount() {
return this.countries.size();
}
#Override
public OneComment getItem(int index) {
return this.countries.get(index);
}
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
try{
if (row == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.chatbox_detail, parent, false);
}
OneComment isiOneComen = getItem(position);
wrapper = (LinearLayout) row.findViewById(R.id.wrapper);
rowFileDesc=(LinearLayout) row.findViewById(R.id.rowFileDesc);
ll_chat_item=(LinearLayout) row.findViewById(R.id.ll_chat_item);
imgPhoto = (ImageView) row.findViewById(R.id.img_dilvChat);
countryName = (TextView) row.findViewById(R.id.comment);
txtKiriKanan = (TextView) row.findViewById(R.id.txtKiriKanan); //isi dari coment untuk menentukan letak buble di kiri ato dikanan,hanya untuk buble file untuk menentukan filenya masih di server apa sudah di downloa
TanggalMasuk= (TextView) row.findViewById(R.id.txttgl);
img_upload=(ImageView) row.findViewById(R.id.img_dilvChat);
txtKodeGbr = (TextView) row.findViewById(R.id.txtKodeGbr);
txtPesan=(TextView) row.findViewById(R.id.txtPesan);
txtJam=(TextView) row.findViewById(R.id.txtjamchatMasuk);
txtUkuranFile=(TextView) row.findViewById(R.id.txtFileSize);
imgCheck=(ImageView) row.findViewById(R.id.imgselected);
img_statusbaca=(ImageView) row.findViewById(R.id.img_statusBaca);
imgPp_round=(ImageView) row.findViewById(R.id.img_photo_pp_chatbox);
imgBtnPlay=(ImageView) row.findViewById(R.id.img_btn_play);
txtPesan.setVisibility(View.GONE);
txtKodeGbr.setVisibility(View.GONE);
img_upload.setVisibility(View.GONE);
rowFileDesc.setVisibility(View.GONE);
if(isiOneComen.comment.trim().equalsIgnoreCase("video")){
//error goes here
countryName.setVisibility(View.GONE);
Log.d("fileDesc", isiOneComen.Filedesc);
//strPath = namafileGambar;
strPath = moduleGlobal.dirImageSent+"/"+isiOneComen.Filedesc;
imgPhoto.setImageBitmap(changeSize(strPath));
scaleImage(imgPhoto, ((int) isiOneComen.lebarScreen)-((int) isiOneComen.lebarScreen/4));
imgBtnPlay.setVisibility(View.VISIBLE);
}else{ //run work well
countryName.setVisibility(View.VISIBLE);
try{
countryName.setText(getSmiledText(getContext(),isiOneComen.comment.toString()));
}catch (Exception e) {
// TODO: handle exception
Log.e("addsmiley", e.toString());
}
countryName.setBackgroundResource(isiOneComen.left ? R.drawable.buble_kiri : R.drawable.buble_kanan);
}
}catch (Exception ez) {
ll_chat_item.setVisibility(View.GONE);
Log.e(TAG, ez.toString());
}
return row;
}
private Bitmap changeSize(String path){
opts.inSampleSize = 1;
Bitmap bm = BitmapFactory.decodeFile(path);
if (bm.getHeight() > 150 || bm.getWidth() > 150) {
final int halfHeight = bm.getHeight() / 2;
final int halfWidth = bm.getWidth() / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / opts.inSampleSize) > 150
&& (halfWidth / opts.inSampleSize) > 150) {
opts.inSampleSize *= 2;
}
}
bm = BitmapFactory.decodeFile(path,opts); // this bitmap will be 1/8 the size of the original
return bm;
}
private void scaleImage(ImageView view, int boundBoxInDp)
{
// Get the ImageView and its bitmap
Drawable drawing = view.getDrawable();
Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();
// Get current dimensions
int width = bitmap.getWidth();
int height = bitmap.getHeight();
// Determine how much to scale: the dimension requiring less scaling is
// closer to the its side. This way the image always stays inside your
// bounding box AND either x/y axis touches it.
float xScale = ((float) boundBoxInDp) / width;
float yScale = ((float) boundBoxInDp) / height;
float scale = (xScale <= yScale) ? xScale : yScale;
// Create a matrix for the scaling and add the scaling data
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
// Create a new bitmap and convert it to a format understood by the ImageView
Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
#SuppressWarnings("deprecation")
BitmapDrawable result = new BitmapDrawable(scaledBitmap);
width = scaledBitmap.getWidth();
height = scaledBitmap.getHeight();
// Apply the scaled bitmap
view.setImageDrawable(result);
// Now change ImageView's dimensions to match the scaled image
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
params.width = width;
params.height = height;
view.setLayoutParams(params);
}
public Bitmap decodeToBitmap(byte[] decodedByte) {
return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}
/**tambah smiley
*
*/
private static final HashMap<String, Integer> emoticons = new HashMap<String, Integer>();
static {
emoticons.put(":)", R.drawable.s1);
emoticons.put(":D", R.drawable.s2);
emoticons.put(":(", R.drawable.s3);
emoticons.put("8o|", R.drawable.s16);
emoticons.put(":/", R.drawable.s17);
}
// Get image for each text smiles
public static Spannable getSmiledText(Context context, String text) {
SpannableStringBuilder builder = new SpannableStringBuilder(text);
int index;
for (index = 0; index < builder.length(); index++) {
for (Entry<String, Integer> entry : emoticons.entrySet()) {
int length = entry.getKey().length();
if (index + length > builder.length())
continue;
if (builder.subSequence(index, index + length).toString().equals(entry.getKey())) {
builder.setSpan(new ImageSpan(context, entry.getValue()), index, index + length,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
index += length - 1;
break;
}
}
}
return builder;
}
#Override
public int getPositionForSection(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public int getSectionForPosition(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public Object[] getSections() {
// TODO Auto-generated method stub
return null;
}
}
everything work well before i add relative layout,I want to add image view center with other imageview but I get error log
java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams
Problem with
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
This line Change this line to
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();
Because you forgot to Add
</LinearLayout>
</LinearLayout>
at the end of your xml.
Use this
<?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="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:gravity="center_horizontal"
android:paddingLeft="10dip" />
<LinearLayout
android:id="#+id/ll_chat_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
<LinearLayout
android:id="#+id/wrapper"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:layout_weight="1"
android:cacheColorHint="#android:color/transparent"
android:gravity="left"
android:orientation="vertical" >
<TextView
android:id="#+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
<LinearLayout
android:id="#+id/rowFile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/img_dilvChat"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_margin="3dp"
android:background="#drawable/buble_kanan"
android:contentDescription="#string/descGambar" />
<ImageView
android:id="#+id/img_btn_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription="#string/descGambar"
android:src="#drawable/play_icon" />
</RelativeLayout>
<TextView
android:id="#+id/tv4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:gravity="center_horizontal"
android:paddingLeft="10dip" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
My code is as follows:
activity_for_me.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="#+id/recco_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
<ImageView
android:id="#+id/checkin"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:onClick="ClickCheckIn"
android:src="#drawable/checkin" />
</RelativeLayout>
recco_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#layout/nine_patch" >
<ImageView
android:id="#+id/outlet_icon"
android:layout_marginLeft="15dp"
android:layout_height="55dp"
android:layout_width="55dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="#+id/distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="15dp"
android:hint="Distance"
/>
<TextView
android:id="#+id/outlet_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/outlet_icon"
android:layout_toLeftOf="#id/distance"
android:layout_marginLeft = "15dip"
android:hint="outlet"
/>
<TextView
android:id="#+id/reward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/outlet_name"
android:layout_toRightOf="#id/outlet_icon"
android:layout_toLeftOf="#id/distance"
android:layout_marginLeft = "15dip"
android:layout_marginTop="5dp"
android:textColor="#color/abc_search_url_text_holo"
android:hint="Reward"
/>
<View
android:id="#+id/rule"
android:layout_below="#id/reward"
android:layout_toRightOf="#id/outlet_icon"
android:layout_toLeftOf="#id/distance"
android:layout_width="fill_parent"
android:layout_marginLeft = "15dip"
android:layout_height="1dp"
android:background="#c0c0c0"/>
<ImageView
android:id="#+id/favourite_icon"
android:layout_below="#id/rule"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:layout_height="15dp"
android:layout_width="15dp"
android:src="#drawable/heart_empty"/>
<ImageView
android:id="#+id/loc_icon"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_below="#id/rule"
android:layout_marginLeft = "15dip"
android:layout_marginTop="5dp"
android:layout_toRightOf="#id/outlet_icon"
android:src="#drawable/map_pointer"/>
<TextView
android:id="#+id/locality"
android:layout_toRightOf="#id/loc_icon"
android:layout_toLeftOf="#id/distance"
android:layout_below="#id/rule"
android:layout_marginRight="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft = "5dip"
android:layout_marginTop="5dp"
android:hint="Locality"
/>
</RelativeLayout>
Activity.java
public class ForMeActivity extends Fragment {
#SuppressLint("ClickableViewAccessibility")
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
RelativeLayout rootView = (RelativeLayout) inflater.inflate(
R.layout.activity_for_me, container, false);
ImageView favourite_heart = (ImageView) rootView
.findViewById(R.id.favourite_icon);
favourite_heart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(YourActivityName.this,
// "The favorite list would appear on clicking this icon",
// Toast.LENGTH_LONG).show();
System.out.println("Favourite Icon clicked");
}
});
list = (ListView) rootView.findViewById(R.id.recco_list);
ImageView check_in_img = (ImageView) rootView
.findViewById(R.id.checkin);
ImageView outlet_logo = (ImageView) rootView
.findViewById(R.id.outlet_icon);
final String data = getArguments().getString("data");
ForMeFile = new File(getActivity().getExternalFilesDir(filepath),
filename);
final String cookie = getArguments().getString("cookie");
System.out.print(cookie);
check_in_img.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in = new Intent(getActivity(), CheckInView.class);
startActivity(in);
System.out.println("Checkin Icon clicked");
}
});
}
I want to change image for favourite_icon ImageView when I click on it. But as soon as I click on the icon I get NullPointerException. What is the reason that click of check_in_img is working but click of favourite_icon is not working even though the current layout is the one which contains favourite_icon? Other sources of SO says to check the same.
favourite_icon is a part of recco_list.xml and you are trying to find it in activity_for_me.xml. The view does not exist there and hence, the exception.
R.id.favourite_icon is not there in activity_for_me.xml.
And if you want to access the items in the listview, I suggest you read about ListView and getView().
I have made an application in which I will be dynamically adding image and text to the application for this i am using
1.relative layout (main layout)
1.(a) linear layout ( a linear layout containing all the 1.(b) values)
1.(b) linear layout ( d layout which contains a image and a text) , each (1.(b)) layout contain one image and one text
now as I am running this app on set top box (with remote) with android, which is connected to smart tv,,,,, so i have to control my app with a remote control provided with the set top box.
so what I want is when the application loads , the focus will be on the first ((1.(b) linear layout)) i.e. on the first pair of image and text.
already done:- I have used requestFocus() but of no use
any help will be greatly appreciated thanks in advance cheers !!!!!!!
the code snippet is as follows:-
activity
LinearLayout lm = (LinearLayout) findViewById(R.id.linearMain1);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
75, 98);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
params.setMargins(0, 25, 0, 0);
RelativeLayout.LayoutParams paramrelative = new RelativeLayout.LayoutParams(
170, 158);
paramrelative.setMargins(2, 0, 0, 0);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(180,
ViewGroup.LayoutParams.WRAP_CONTENT);
p.setMargins(2,0, 0, 0);
//Create four
for(int j= 0;j<=count-1;j++)
{
// Create LinearLayout
ScrollView sv = new ScrollView(this);
final RelativeLayout ll = new RelativeLayout(this);
ll.setBackgroundResource(R.drawable.app_bg);
ll.setLayoutParams(paramrelative);
ll.setFocusableInTouchMode(true);
ll.setClickable(true);
// final String url= separated[j+1];
// Create Button
final Button img = new Button(this);
final TextView tv = new TextView(this);
// Give button an ID
img.setId(j+1);
int s=img.getId();
p.addRule(RelativeLayout.BELOW, s);
tv.setLayoutParams(p);
tv.setGravity(Gravity.CENTER);
//File sdCardRoot = Environment.getExternalStorageDirectory();
// File sdcardPath = new File(Environment.getExternalStorageDirectory()
// .getPath() + "/CATEGORIES");
String filename=finalname[j];
File myDir = new File(sdcardPath,filename);
// File myDir = new File(sdcardPath, "CATEGORIES_hotelinfo.png");
String m1 = myDir.getPath();
Resources res = getResources();
Bitmap bitmap = BitmapFactory.decodeFile(m1);
BitmapDrawable bd = new BitmapDrawable(res, bitmap);
img.setBackgroundDrawable(Drawable.createFromPath(m1.toString()));
//2ndaugust2014 tv.setText(finalname[j]);
int s1= finalname[j].indexOf(".");
final String finalname1=finalname[j].substring(0,s1);
tv.setText(finalname1);
img.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String newPath=newPath1+finalname1;
File sdcardPath = new File(Environment.getExternalStorageDirectory()
.getPath() +newPath);
new CountDownTimer(2000,1000) {
public void onTick(long millisUntilFinished) {
ll.setBackgroundResource(R.drawable.hover_btn_d); //This is when you click on each tick it came here after 1000 millisecond
}
public void onFinish() {
ll.setBackgroundResource(R.drawable.app_bg);
}
}.start();
// if the directory does not exist, create it
if (!sdcardPath.exists())
{
Toast.makeText(getApplicationContext(),
"redirecting to www."+finalname1+".com",
Toast.LENGTH_SHORT).show();
}
else
{ Intent intent = new Intent(Second60grid.this,Second60grid.class);
// startActivity(intent);
// i.setClassName(packageName, activitylast);
Toast.makeText(getApplicationContext(),
"name" + finalname1,
Toast.LENGTH_SHORT).show();
intent.putExtra("nameoffile",newPath+"/");
startActivity(intent);
}
}
});
// set the layoutParams on the button
img.setLayoutParams(params);
//Add button to LinearLayout
// setContentView(sv);
ll.addView(img);
ll.addView(tv);
if(j==0)
{
ll.requestFocus();
}
//Add button to LinearLayout defined in XML
lm.addView(ll);
}
and the xml file is as follows:-----
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="200dip"
android:id="#+id/rl1"
>
<TextView
android:id="#+id/TEXT0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FF0000"
android:text="CATEGORIES" />
<LinearLayout
android:id="#+id/linearMain1"
android:layout_below="#+id/TEXT0"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#80000000">
</LinearLayout>
<LinearLayout
android:id="#+id/linearMain2"
android:layout_below="#+id/linearMain1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#80000000">
</LinearLayout>
<LinearLayout
android:layout_below="#+id/linearMain2"
android:orientation="horizontal"
android:id="#+id/linearMain3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#80000000"
>
</LinearLayout>
<LinearLayout
android:layout_below="#+id/linearMain3"
android:orientation="horizontal"
android:id="#+id/linearMain4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#80000000"
> </LinearLayout>
<LinearLayout
android:layout_below="#+id/linearMain4"
android:orientation="horizontal"
android:id="#+id/linearMain5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#80000000"
>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="1400dp"
android:layout_height="70dp"
android:background="#80ffffff"
android:layout_alignParentBottom="true"
android:layout_below="#+id/rl1"
android:id="#+id/rl2"
>
<ImageView
android:id="#+id/remote_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/remote_btn"
android:layout_alignParentBottom="true" />
<TextView
android:id="#+id/TEXT1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/remote_btn2"
android:layout_toRightOf="#+id/remote_btn"
android:text="MOVE" />
<ImageView
android:id="#+id/remote_btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="42dp"
android:layout_toRightOf="#+id/TEXT1"
android:adjustViewBounds="true"
android:src="#drawable/remote_btn2" />
<TextView
android:id="#+id/TEXT2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/TEXT1"
android:layout_toRightOf="#+id/remote_btn2"
android:text="SELECT" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/rl3"
android:layout_width="wrap_content"
android:layout_height="800dip"
android:background="#10ffffff"
android:layout_marginLeft="10dip"
android:layout_marginBottom = "10dp"
android:layout_above="#+id/rl2"
>
<Button
android:id="#+id/buttoniptv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="42dp"
android:text="iptv"
android:background="#FF0000"/>
<Button
android:id="#+id/buttontvchannels"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/buttoniptv"
android:background="#FF0000"
android:text="tvchannels" />
<Button
android:id="#+id/buttoninternet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/buttontvchannels"
android:background="#FF0000"
android:text="internet" />
</RelativeLayout>
</RelativeLayout>
You are currently adding view in ascending order
parentView.addView(child, 0);
Add it in Descending Order:
parentView.addView(child, parentView.getChildCount() - 1);
We are writing a "dashboard" for a car. The code for calculating/drawing the speedometer was done separate from the rest of the code, and now I am trying to combine the two.
They both work separately, and with the speedometer view code in, the main portion of the UI is still functioning, I just can't see the speedometer.
I have it compiling, and there are no obvious errors. However, when the app is run, I don't see the speedometer showing up in the main view, even though I have added it using the addView method.
For Reference:
My code to create and add the view:
setContentView(R.layout.main);
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.RelativeLayout1);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.CENTER_VERTICAL);
myView v = new myView(this);
v.setLayoutParams(lp);
relativeLayout.addView(v, lp);
The code for the myView class:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.view.View;
public class myView extends View {
private Paint mPaints;
private Paint textpaint;
private boolean mUseCenters;
private RectF mBigOval;
private float mStart;
private float mSweep;
public float SPEED_raw;
public int SPEED = 0; //initialized to 0 just for loop. Remove initialization when IF statement below is deleted
public static Bitmap gaugefront;
public static Bitmap gaugeback;
public myView(Context context) {
super(context);
mPaints = new Paint();
mPaints.setAntiAlias(true);
mPaints.setColor(Color.YELLOW);
textpaint = new Paint();
textpaint.setAntiAlias(true);
textpaint.setColor(Color.WHITE);
textpaint.setTextSize(150);
textpaint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
gaugefront = BitmapFactory.decodeResource(context.getResources(),R.drawable.gaugefront); //Load gaugefront.png
gaugeback = BitmapFactory.decodeResource(context.getResources(),R.drawable.gaugeback); //Load gaugeback.png
mUseCenters = true;
mBigOval = new RectF(400, 10, 880, 490); //left[x-coordinate],top[y-coordinate],right[x],bottom[y]
}
private void drawArcs(Canvas canvas, RectF oval, boolean useCenter, Paint paint) {
canvas.drawArc(oval, mStart, mSweep, useCenter, paint);
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(myView.gaugeback, 400, 10, null); //draw gauge background, X coordinate:400, Y coordinate: 10
drawArcs(canvas, mBigOval, mUseCenters, mPaints);
//SPEED_raw = 70; //Raw float data from CCS. Uncomment when the IF statement below is deleted.
SPEED = Math.round(SPEED_raw); //SPEED integer. Units km/h - Top speed of 135. Used for digital readout
mStart = 90; //Start drawing from -90 degrees (Counter clockwise)
mSweep = SPEED_raw*2; //Draw stop point
if(SPEED >= 135){ //JUST FOR SHOW. Delete this for actual speedo
SPEED_raw = 0; // "
} // "
else // "
SPEED_raw += 0.5; // "
canvas.drawBitmap(myView.gaugefront, 400, 10, null); //draw gauge foreground, X coordinate:400, Y coordinate: 10
String speed_string = String.valueOf(SPEED); //Convert int SPEED to String for display
// while (speed_string.endsWith(".0") || speed_string.endsWith(".")){ //Erase trailing ".0" for float types. Don't need if SPEED is an int
// speed_string = (speed_string.substring(0, speed_string.length() - 1));
// }
canvas.drawText(speed_string, 640, 420, textpaint); //arguments: (string, x-coordinate, y-coordinate, paint)
invalidate();
}
}
Here's my XML File:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:keepScreenOn="true"
android:orientation="vertical" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/solarbg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/solarbg" />
</LinearLayout>
<ImageView
android:id="#+id/l_lamp"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_gravity="left"
android:src="#drawable/l_arrow_dim" />
<DigitalClock
android:id="#+id/digitalClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="50dp" />
<ImageView
android:id="#+id/r_lamp"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/r_arrow_dim" />
<TextView
android:id="#+id/setAmps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="left"
android:text="#string/setAmps"
android:textColor="#ffffff"
android:textSize="30dp" />
<TextView
android:id="#+id/setAmpsVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_below = "#+id/setAmps"
android:layout_alignParentLeft="true"
android:maxEms = "5"
android:textColor="#ffffff"
android:textSize="30dp" />
<TextView
android:id="#+id/setVelocity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/setAmps"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="right"
android:text="#string/setVelocity"
android:textColor="#ffffff"
android:textSize="30dp" />
<TextView
android:id="#+id/setVelocityVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_below = "#+id/setVelocity"
android:layout_alignParentRight="true"
android:maxEms = "5"
android:textColor="#ffffff"
android:textSize="30dp" />
<TextView
android:id="#+id/actVelocity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/setVelocityVal"
android:gravity="right"
android:text="#string/actVelocity"
android:textColor="#ffffff"
android:textSize="30dp" />
<TextView
android:id="#+id/actVelocityVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below = "#+id/actVelocity"
android:layout_alignParentRight="true"
android:gravity="right"
android:maxEms = "5"
android:textColor="#ffffff"
android:textSize="30dp" />
<TextView
android:id="#+id/busAmps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/setAmpsVal"
android:gravity="left"
android:text="#string/busAmps"
android:textColor="#ffffff"
android:textSize="30dp" /><TextView
android:id="#+id/busAmpsVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_below = "#+id/busAmps"
android:maxEms = "5"
android:textColor="#ffffff"
android:textSize="30dp" />
<TextView
android:id="#+id/powerVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom = "true"
android:gravity="right"
android:textColor="#ffffff"
android:textSize="30dp" />
<TextView
android:id="#+id/power"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_above = "#+id/powerVal"
android:gravity="left"
android:text="#string/power"
android:textColor="#ffffff"
android:textSize="30dp" />
<TableLayout
android:id="#+id/msgCenterLayout"
android:layout_width="600dp"
android:layout_height="200dp"
android:layout_alignParentBottom = "true"
android:layout_centerHorizontal = "true"
android:orientation = "vertical"
android:visibility = "invisible">
<ScrollView
android:id="#+id/messageCenterText"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="left"
android:text="#string/busAmps"
android:textColor="#ffffff"
android:textSize="30dp" />
</TableLayout>
</RelativeLayout>
edit:
I'm using an XML file for the main layout, and trying to add this new view to a relativelayout inside that XML file.
Try writing
LayoutParams p = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT);
v.setLayoutParams(lp);
relativeLayout.addView(v);