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);
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);
I am using this code to get the contents from a source and hence using dynamic initialisation.
The problem is that the views made by the createcomment(), overlap each other.
What can be done so that overlapping does not occur??
createdynamic
void createdymanic()
{
rl= new RelativeLayout(getActivity());
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams ivprofile = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams textname = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams textdate = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams textbody = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams imagelike = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams imagecomment = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams imageshare = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams editcomment = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
// set picture of the person
ivprofilepic= new ImageView(getActivity());
ivprofile.addRule(RelativeLayout.ALIGN_PARENT_TOP);
ivprofile.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
ivprofile.setMargins(5,5,0,0);
ivprofilepic.setId(1);
ivprofilepic.setImageResource(R.drawable.ic_launcher);
tvname= new TextView(getActivity());
textname.addRule(RelativeLayout.ALIGN_PARENT_TOP);
textname.addRule(RelativeLayout.RIGHT_OF, ivprofilepic.getId());
textname.setMargins(8, 5, 0, 0);
tvname.setId(2);
tvname.setText("Name");
tvname.setTextColor(getResources().getColor(R.color.textcolor));
tvname.setTextSize(18);
tvdate= new TextView(getActivity());
textdate.addRule(RelativeLayout.ALIGN_BOTTOM, ivprofilepic.getId());
textdate.addRule(RelativeLayout.ALIGN_LEFT, tvname.getId());
textdate.addRule(RelativeLayout.BELOW, tvname.getId());
tvdate.setId(3);
tvname.setText("Date");
tvcontent= new TextView(getActivity());
textbody.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
textbody.addRule(RelativeLayout.BELOW, ivprofilepic.getId());
textbody.setMargins(5,0,0,0);
tvcontent.setId(4);
tvcontent.setText("Content");
iblike= new Button(getActivity());
imagelike.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
imagelike.addRule(RelativeLayout.BELOW, tvcontent.getId());
imagelike.setMargins(5, 7, 0, 0);
iblike.setId(5);
iblike.setText("like");
ibcomment= new Button(getActivity());
imagecomment.addRule(RelativeLayout.ALIGN_TOP, iblike.getId());
imagecomment.addRule(RelativeLayout.CENTER_HORIZONTAL);
ibcomment.setId(6);
ibcomment.setText("comment");
ibshare= new Button(getActivity());
imageshare.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
imageshare.addRule(RelativeLayout.ALIGN_TOP, iblike.getId());
imageshare.setMargins(0,0,0,5);
ibshare.setId(7);
ibshare.setText("share");
etcommentbody= new EditText(getActivity());
editcomment.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
editcomment.addRule(RelativeLayout.BELOW, iblike.getId());
editcomment.setMargins(5,8,0,0);
etcommentbody.setId(8);
etcommentbody.setHint("Add a comment...");
rl.addView(ivprofilepic, ivprofile);
rl.addView(tvname, textname);
rl.addView(tvdate ,textdate);
rl.addView(tvcontent, textbody);
rl.addView(iblike, imagelike);
rl.addView(ibcomment, imagecomment);
rl.addView(ibshare, imageshare);
rl.addView(etcommentbody, editcomment);
mainlayout.addView(rl, 0, rlp);
tvname.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//profile pe le jaana h yaha se
//profile class banegi
}
});
iblike.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
new Caller().execute("3", api, id);
}
});
ibcomment.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String a=etcommentbody.getText().toString();
new Caller().execute("4", api, id,a);
}
});
ibshare.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// h.share(user_id, post_id);
}
});
}
createcomments
void createcomment()
{
RelativeLayout clayout= new RelativeLayout(getActivity());
RelativeLayout.LayoutParams clp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
//clp.addRule(RelativeLayout.BELOW, etcommentbody.getId());
clp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
RelativeLayout.LayoutParams ivcommentprofile = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams textcommentname = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams textcommentdate = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams textcommentbody = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
ivcommentprofilepic= new ImageView(getActivity());
ivcommentprofile.addRule(RelativeLayout.ALIGN_PARENT_TOP);
ivcommentprofile.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
ivcommentprofile.setMargins(5,5,0,0);
ivcommentprofilepic.setId(101);
ivcommentprofilepic.setImageResource(R.drawable.ic_launcher);
tvcommentname= new TextView(getActivity());
textcommentname.addRule(RelativeLayout.ALIGN_PARENT_TOP);
textcommentname.addRule(RelativeLayout.RIGHT_OF, ivcommentprofilepic.getId());
textcommentname.setMargins(5,5,0,0);
tvcommentname.setId(102);
tvcommentname.setText("Name");
tvcommentname.setTextColor(getResources().getColor(R.color.textcolor));
tvcommentname.setTextSize(18);
tvcommentdate= new TextView(getActivity());
textcommentdate.addRule(RelativeLayout.ALIGN_BOTTOM, ivcommentprofilepic.getId());
textcommentdate.addRule(RelativeLayout.RIGHT_OF, ivcommentprofilepic.getId());
textcommentdate.addRule(RelativeLayout.BELOW, tvcommentname.getId());
textcommentdate.setMargins(5, 3, 0, 0);
tvcommentdate.setId(3);
tvcommentdate.setText("Date");
tvcommentcontent= new TextView(getActivity());
textcommentbody.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
textcommentbody.addRule(RelativeLayout.BELOW, ivcommentprofilepic.getId());
textcommentbody.setMargins(5,0,0,0);
tvcommentcontent.setId(4);
tvcommentcontent.setText("Content");
clayout.addView(ivcommentprofilepic, ivcommentprofile);
clayout.addView(tvcommentname, textcommentname);
clayout.addView(tvcommentdate ,textcommentdate);
clayout.addView(tvcommentcontent, textcommentbody);
rl.addView(clayout, clp);
}
I use the following statements
for(ka=0;ka<jsarray.length();ka++){
createdymanic(ka);
// setting on click listener and all
for(int i=0;i<comment_array.length();i++){
createcomment(i);
// detting onclicklistener and all
}
}
It looks like your last text view you're adding (tvcommentcontent) doesn't have a right bound, so your third (tvcommentdate) and your 4th view (tvcommentcontent) you're adding are overlapping. Try adding
textcommentbody.addRule(RelativeLayout.LEFT_OF, tvcommentdate.getId());
right above your
textcommentbody.setMargins(5,0,0,0);
line. This should make sure the last view stayes on the left of the third view. (I made a sweet diagram with MS paint to show this better but apparently new users can't add images :) )
I hope this helps!
Sorry for creating such a lame question.
The problem was that the default value of commentlayout's orientation is horizontal by default.
The following code solved the problem :
commentlayout= new LinearLayout(getActivity());
commentlayout.setOrientation(LinearLayout.VERTICAL);
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);
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!!!