How to replcae AbsoluteLayout with RelativeLayout - android

I have a Class that used in it below code
this.guageBack= (AbsoluteLayout) findViewById(R.id.gaugeFrame);
AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(this.needleWidth,this.needleHeight,this.needleX,this.needleY);
gaugeNeedle.setLayoutParams(params);
AbsoluteLayout deprecated so When I want to use RelativeLayout I have not replcement for
AbsoluteLayout.LayoutParams(this.needleWidth,this.needleHeight,this.needleX,this.needleY);
Because it accept two args like below
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(needleWidth, needleHeight);
What should I do?

Just add this code instead of yours
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(needleWidth, needleHeight);
params.topMargin = needleY;
params.leftMargin = needleX;

Do it like this
public class CodeLayout extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Creating a new RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(this);
// Defining the RelativeLayout layout parameters.
// In this case I want to fill its parent
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
// Creating a new TextView
TextView tv = new TextView(this);
tv.setText("Test");
// Defining the layout parameters of the TextView
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
// Setting the parameters on the TextView
tv.setLayoutParams(lp);
// Adding the TextView to the RelativeLayout as a child
relativeLayout.addView(tv);
// Setting the RelativeLayout as our content view
setContentView(relativeLayout, rlp);
}
}

try this code,
AbsoluteLayout absoluteLayout = //get absolute layout
Button button = new Button();
AbsoluteLayout.LayoutParms params = absoluteLayout.generateDefaultLayoutParams();
params.x = 100;
params.y = 100;
absoluteLayout.addView(button, params);

Related

Set LayoutParams.BELOW programmatically

I'm trying to set LayoutParams.BELOW programmatically. Here my code:
RelativeLayout layout = new RelativeLayout(this.getActivity());
layout.setLayoutParams(new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
layout.setBackgroundColor(Color.parseColor("#CCCDCDCD"));
ImageView imageView = new ImageView(this.getActivity());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
imageView.setLayoutParams(params);
imageView.setBackgroundResource(R.drawable.create_template);
AnimationDrawable frameAnimation = (AnimationDrawable) imageView.getBackground();
if (frameAnimation != null) {
frameAnimation.start();
}
TextView textView = new TextView(this.getActivity());
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, imageView.getId());
textView.setLayoutParams(params);
textView.setText("Generating information...");
layout.addView(imageView);
layout.addView(textView);
return layout;
But it's not being correctly positioned. Any idea on why it is not well positioned?
The ImageView doesn't have an ID. You need to give it one. Either define some constant integer value, or use View.generateViewId(), and call setId() before using it in the layout params:
ImageView imageView = new ImageView(this.getActivity());
imageView.setId(View.generateViewId());
...
params.addRule(RelativeLayout.BELOW, imageView.getId());

setMargins(left, top, right, bottom); Android doesn't work

I have this code, but the textview are all without Margin right and i don't know why.
I add some lines of onCreate for declaration.
And the functions about the creation of the textview.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLayout = (LinearLayout) findViewById(R.id.linearlayout);
mEditText = (EditText) findViewById(R.id.input_materia);
mButton = (Button) findViewById(R.id.add_materia);
mButton.setOnClickListener(onClick());
TextView textView = new TextView(this);
textView.setText("New text");
/*
* add textview from editext
*/
private OnClickListener onClick() {
return new OnClickListener() {
#Override
public void onClick(View v) {
mLayout.addView(createNewTextView(mEditText.getText().toString()));
}
};
}
private TextView createNewTextView(String text) {
final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lparams.setMargins(0, 0, 30, 0);
final TextView textView = new TextView(this);
textView.setLayoutParams(lparams);
textView.setText(text);
textView.setGravity(Gravity.CENTER);
return textView;
}
some helps?
Have you tried referencing Relative Layout Params or Linear Layout Params depending on what is set in your XML?
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);

how to set child view at LAYOUT_LEFT OF="+/id" by programically in relative layout

as shown in image i need to set text view LEFT OF RADIO GROUP and i did it using java code....so i don't know to set parameter for LAYOUT LEFT OF="" in java code.....how to solve it ..please guide me this way...
this is class file......
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.rate_me_up);
textViewShowTime = (TextView) findViewById(R.id.tvTimeCount);
layout = (RelativeLayout) findViewById(R.id.linear);
Button btnp = (Button) findViewById(R.id.buttonp);
btnp.setOnClickListener(this);
}
#Override
public void onClick(View v) {
RadioGroup radioGroup = new RadioGroup(Rate_me_up.this);
radioGroup.setOrientation(0);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
if (mLastRadioGroup != null)
p.addRule(RelativeLayout.BELOW, mLastRadioGroup.getId());
radioGroup.setId(mRadioGroupId++);
mLastRadioGroup = radioGroup;
layout.addView(radioGroup, p);
RadioButton radioButtonView = new RadioButton(Rate_me_up.this);
radioButtonView.setText("radio1");
radioButtonView.setId(id++);
radioButtonView.setButtonDrawable(R.drawable.e404);
radioButtonView.setChecked(false);
radioGroup.addView(radioButtonView, p);
RadioButton radioButtonView2 = new RadioButton(Rate_me_up.this);
radioButtonView2.setText("radio2");
radioButtonView2.setId(id++);
radioButtonView2.setChecked(false);
radioGroup.addView(radioButtonView2, p);
RadioButton radioButtonView3 = new RadioButton(Rate_me_up.this);
radioButtonView3.setText("radio3");
radioButtonView3.setId(id++);
radioGroup.addView(radioButtonView3, p);
radioButtonView3.setChecked(false);
TextView txt = new TextView(Rate_me_up.this);
txt.setText("FOOD QUALITY!");
layout.addView(txt, p);
radioGroup.setOnCheckedChangeListener(this);
}
Alright you can do it by this way:
TextView txt = new TextView(Rate_me_up.this);
txt.setText("FOOD QUALITY!");
RelativeLayout.LayoutParams params=
new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.LEFT_OF,radiofroup.getId());
txt.setLayoutParams(params);
layout.addView(txt);
Hope I answered your question. :)
addRule Do something like this..
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);
button.setLayoutParams(params);

Can't attach custom layout to Activity dynamically

When I create Activity and set new RelativeLayout right in onCreate(), all works fine.
But I think it's more convenient to create a class MView that extends RelativeLayout and do all the settings there. And if I do this, Activity stays empty. What should I change in MView (or shall I need to use RelativeLayout without extension)?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout mLayout = new RelativeLayout(this);//Change this to MView mLayout=new MView(this);
//I've been trying to move text from here...
TextView tv = new TextView(this);
tv.setText("TestText");
tv.setTextAppearance(this, android.R.attr.textAppearanceSmall);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
tv.setLayoutParams(lp);
mLayout.addView(tv);
//... to here to MView class that extends RelativeLayout
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
setContentView(mLayout, rlp);
}
So, code for MView constructor is:
public MView(Context context) {
super(context);
TextView tv = new TextView(context);
tv.setText("TestText");
tv.setTextAppearance(context, android.R.attr.textAppearanceLarge);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
tv.setLayoutParams(lp);
this.addView(tv);
}
And onCreate becomes this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MView mLayout = new MView(this);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
setContentView(mLayout, rlp);
}
It was VERY simple. When I created MView, my implementation of onLayout was empty. Obviously, I had to write
super.onLayout(arg0, arg1, arg2, arg3, arg4);
And it worked fine. Thanks for helping :)
It is better to set layout parmas to direct view rather than when you add another layout.
In your case, set wrong add rules for text now.
Corrected code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout mLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
mLayout.setLayoutParams(rlp);
TextView tv = new TextView(this);
tv.setText("TestText");
tv.setTextAppearance(this, android.R.attr.textAppearanceSmall);
RelativeLayout.LayoutParams rlt = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rlt.addRule(RelativeLayout.CENTER_IN_PARENT);
tv.setLayoutParams(rlt);
mLayout.addView(tv);
setContentView(mLayout);
}

RelativeLayout parameters programmatically align error

I am trying programmatically align some elements in RelativeLayout but I am getting some problem. All my TextView elements aligned to the top left corner even when I set them deferentially. here is my screen shot:
Here is my code (this is the RelativeLayout):
title = new TextView(context);
date = new TextView(context);
rating = new RatingBar(context);
saleImage = new ImageView(context);
arrowImage = new ImageView(context);
RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
this.setBackgroundResource(R.drawable.list_selector);
this.setLayoutParams(relativeLayoutParams);
this.setClickable(true);
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
saleImage.setLayoutParams(relativeLayoutParams);
saleImage.setImageResource(R.drawable.rihanna);
this.addView(saleImage);
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
relativeLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
arrowImage.setImageResource(R.drawable.arrow);
arrowImage.setLayoutParams(relativeLayoutParams);
this.addView(arrowImage);
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
relativeLayoutParams.addRule(RelativeLayout.RIGHT_OF, saleImage.getId());
title.setLayoutParams(relativeLayoutParams);
title.setTextAppearance(context, android.R.style.TextAppearance_Medium);
title.setText("Sale title");
this.addView(title);
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
relativeLayoutParams.setMargins(0, 0, 26, 0);
relativeLayoutParams.addRule(RelativeLayout.LEFT_OF, arrowImage.getId());
date.setTextAppearance(context, android.R.style.TextAppearance_Small);
date.setText("14.01.13 22:00");
this.addView(date);
It seems that the problem is with the RelativeLayout.RIGHT_OF and RelativeLayout.LEFT_OF attributes.
here is the Activity code:
public class MainPage extends Activity {
private LinearLayout test;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_sales);
test = (LinearLayout)findViewById(R.id.salesConteiner);
SaleRow row = new SaleRow(this);
test.addView(row);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_page, menu);
return true;
}
}
Is there something I have missed? Thanks!!!
For the view.getId() function to work you first need to se an id: view.setId(1).
Thanks for the help!!!

Categories

Resources