Add Relative Layout below current Relative Layout programmatically in Android - android

I am trying to create a method where each time I click a button, I want the screen to display a Relative Layout box below one that is currently there or create one at the top if there isn't one there and I want it to keep creating them below from each button click. Currently it just displays the Relative Layout box at the top of the screen as it would expect to do.
Here is the code that I have so far and can someone please help me see what I am doing wrong and what I can do to fix this issue:
onCreate code:
Resources r = this.getResources();
dpMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, r.getDisplayMetrics()); //Changes pixels to dp of margins
dpContainerHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80, r.getDisplayMetrics()); //Changes pixels to dp of container height
dpContainerPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, r.getDisplayMetrics()); //Changes pixels to dp of container padding
layout = new RelativeLayout(this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
RelativeLayout.LayoutParams textParams1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams textParams2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams textParams3 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams textParams4 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams progressParams1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams imageButtonParams1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams newLayoutButtonParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams containerParams1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, dpContainerHeight);
RelativeLayout.LayoutParams containerParams2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, dpContainerHeight);
layout.setPadding(dpMargin, dpMargin, dpMargin, dpMargin);
//Declaring new views
tv1 = new TextView(this);
tv2 = new TextView(this);
containerLayout1 = new RelativeLayout(this);
containerLayout2 = new RelativeLayout(this);
tv3 = new TextView(this);
tv4 = new TextView(this);
pb1 = new ProgressBar(this);
ib1 = new ImageButton(this);
newLayoutButton = new Button(this);
//Declaring views ID's
tv1.setId(1);
tv2.setId(2);
containerLayout1.setId(3);
containerLayout2.setId(4);
tv3.setId(5);
tv4.setId(6);
pb1.setId(7);
ib1.setId(8);
newLayoutButton.setId(9);
//Text view 1
textParams1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
textParams1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
tv1.setText("TextView1");
//Text view 2
textParams2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
textParams2.addRule(RelativeLayout.BELOW, tv1.getId());
textParams2.setMargins(0, 0, 0, dpMargin);
tv2.setText("TextView2");
//Container 1
containerParams1.addRule(RelativeLayout.ALIGN_LEFT, tv2.getId());
containerParams1.addRule(RelativeLayout.ALIGN_RIGHT, tv1.getId());
containerParams1.addRule(RelativeLayout.BELOW, tv2.getId());
containerParams1.setMargins(0, 0, 0, dpMargin);
containerLayout1.setPadding(dpContainerPadding, 0, dpContainerPadding, dpContainerPadding);
containerLayout1.setBackgroundResource(R.color.display_panels);
//Container 2
containerParams2.addRule(RelativeLayout.ALIGN_LEFT, containerLayout1.getId());
containerParams2.addRule(RelativeLayout.ALIGN_RIGHT, containerLayout1.getId());
containerParams2.addRule(RelativeLayout.BELOW, containerLayout1.getId());
containerParams2.setMargins(0, 0, 0, dpMargin);
containerLayout2.setBackgroundResource(R.color.display_panels);
//Text view 3
textParams3.addRule(RelativeLayout.ABOVE, tv4.getId());
textParams3.addRule(RelativeLayout.ALIGN_LEFT, pb1.getId());
tv3.setText("TextView3");
tv3.setTextAppearance(this, android.R.style.TextAppearance_Small); //Need to change text colour to white
tv3.setTextColor(getResources().getColor(R.color.text_colour));
//Text view 4
textParams4.addRule(RelativeLayout.ABOVE, pb1.getId());
textParams4.addRule(RelativeLayout.CENTER_HORIZONTAL);
tv4.setText("TextView4");
tv4.setTextAppearance(this, android.R.style.TextAppearance_Small); //Need to change text colour to white
tv4.setTextColor(getResources().getColor(R.color.text_colour));
//Progress bar 1
progressParams1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
progressParams1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
progressParams1.addRule(RelativeLayout.LEFT_OF, ib1.getId());
pb1.setProgress(40);
pb1.setMax(100);
//Image button 1
imageButtonParams1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
imageButtonParams1.addRule(RelativeLayout.BELOW, tv4.getId());
ib1.setBackgroundResource(R.color.display_panels);
ib1.setImageResource(R.drawable.ic_green_ok);
newLayoutButtonParams.addRule(RelativeLayout.ABOVE, tv2.getId());
newLayoutButton.setText("New Layout");
newLayoutButton.setOnClickListener(this);
layout.addView(tv1, textParams1);
layout.addView(tv2, textParams2);
layout.addView(newLayoutButton, newLayoutButtonParams);
containerLayout1.addView(tv3, textParams3);
containerLayout1.addView(tv4, textParams4);
containerLayout1.addView(pb1, progressParams1);
containerLayout1.addView(ib1, imageButtonParams1);
layout.addView(containerLayout1, containerParams1);
layout.addView(containerLayout2, containerParams2);
setContentView(layout, layoutParams);
createNewLayout method code:
public RelativeLayout createNewLayout(RelativeLayout newlayout){
newLayoutContainer = new RelativeLayout(this);
final RelativeLayout.LayoutParams newLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, dpContainerHeight);
newLayoutContainer.setLayoutParams(newLayoutParams);
newLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
newLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
newLayoutParams.addRule(RelativeLayout.BELOW);
newLayoutParams.setMargins(0, 0, 0, dpMargin);
newLayoutContainer.setBackgroundResource(R.color.display_panels);
return newLayoutContainer;
}
onClick code:
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==newLayoutButton){
layout.addView(createNewLayout(newLayoutContainer), 0);
}
}
EDIT
new method code:
public void createNewLayout(){
int currentId = 1;
for(int i = 1; i <= numberOfLayouts; i++){
newLayoutContainer = new RelativeLayout(this);
newLayoutContainer.setId(currentId);
if(currentId == 1){
newLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, dpContainerHeight);
newLayoutParams.addRule(RelativeLayout.ALIGN_LEFT, containerLayout2.getId());
newLayoutParams.addRule(RelativeLayout.ALIGN_RIGHT, containerLayout2.getId());
newLayoutParams.addRule(RelativeLayout.BELOW, containerLayout2.getId());
newLayoutParams.setMargins(0, 0, 0, dpMargin);
newLayoutContainer.setBackgroundResource(R.color.display_panels);
}
else{
newLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, dpContainerHeight);
newLayoutParams.addRule(RelativeLayout.ALIGN_LEFT, newLayoutContainer.getId());
newLayoutParams.addRule(RelativeLayout.ALIGN_RIGHT, newLayoutContainer.getId());
newLayoutParams.addRule(RelativeLayout.BELOW, currentId);
newLayoutParams.setMargins(0, 0, 0, dpMargin);
newLayoutContainer.setBackgroundResource(R.color.display_panels);
}
newLayoutContainer.setLayoutParams(newLayoutParams);
layout.addView(newLayoutContainer);
currentId++;
}
}

You could use a global variable to store currentId assigned to layouts, and update it every time u add a layout.
int currentId = 10;
button click function
public void onClick(View v) {
if(v==newLayoutButton){
layout.addView(createNewLayout(newLayoutContainer,currentId), 0);
currentId++;
}
}
createNewLayout function will have one more attribute as currentId.
public RelativeLayout createNewLayout(RelativeLayout newlayout, int currentId){
newLayoutContainer = new RelativeLayout(this);
final RelativeLayout.LayoutParams newLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, dpContainerHeight);
newLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
newLayoutParams.addRule(RelativeLayout.BELOW, currentId);
newLayoutParams.setMargins(0, 0, 0, dpMargin);
newLayoutContainer.setLayoutParams(newLayoutParams);
newLayoutContainer.setBackgroundResource(R.color.display_panels);
return newLayoutContainer;
}
Try this working code:
int currentId = 5;
#Override
public void onClick(View v) {
if(v.getId()==9){
containerLayout2.addView(createNewLayout(currentId), 0);
currentId +=10;
}
}
createNewLayoutFunction
public RelativeLayout createNewLayout(int currentId){
RelativeLayout newLayoutContainer = new RelativeLayout(this);
final RelativeLayout.LayoutParams newLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
newLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
newLayoutParams.addRule(RelativeLayout.BELOW, currentId);
newLayoutParams.setMargins(0, 0, 0, dpMargin);
newLayoutContainer.setLayoutParams(newLayoutParams);
RelativeLayout.LayoutParams image = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
ImageView x = new ImageView(this);
x.setImageResource(android.R.drawable.ic_media_play);
newLayoutContainer.addView(x, image);
newLayoutContainer.setId(currentId+10);
newLayoutContainer.setBackgroundResource(R.color.display_panels);
return newLayoutContainer;
}
also change this line in containerParams2
RelativeLayout.LayoutParams containerParams2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, dpContainerHeight+330);
this should work fine. working in my code.

Related

Scroll horizontally in programmatically created ScrollView

I'm creating a ScrollView, nesting in a RelativeLayout (which contains tables). Everything is properly added to the layout. The problem is: vertically scrolling works fine, but horizontal scrolling does NOT work at all. I already tested various combinations of MATCH_PARENT, WRAP_CONTENT and FILL_PARENT. No try even worked close. So my question is: What am I missing here?
Here is the code:
public class PlayerView extends View {
private RelativeLayout relativeLayout;
private TableLayout tableLayout;
private int player_loop;
private int player_count;
public PlayerView(Context context, int player_count){
super(context);
setPlayer_count(player_count);
}
public ScrollView create_scrollView(){
ScrollView scrollView = new ScrollView(getContext());
ScrollView.LayoutParams scroll_params = new ScrollView.LayoutParams(
ScrollView.LayoutParams.MATCH_PARENT,
ScrollView.LayoutParams.MATCH_PARENT
);
scrollView.setLayoutParams(scroll_params);
scrollView.addView(create_relativeLayout());
return scrollView;
}
public RelativeLayout create_relativeLayout(){
relativeLayout = new RelativeLayout(getContext());
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT
);
relativeLayout.setLayoutParams(relativeParams);
create_Player_Table_Layout();
return relativeLayout;
}
public void create_Player_Table_Layout(){
for(player_loop = 1; player_loop <= player_count; player_loop++){
relativeLayout.addView(player_table(getResources().getString(R.string.dummy_player_name) + player_loop, player_loop));
}
}
public TableLayout player_table(String playername, int playernumber){
tableLayout = new TableLayout(getContext());
tableLayout.setId(playernumber * 1000);
if (playernumber > 1) {
//TABLE PLACEMENT
RelativeLayout.LayoutParams tbl_params_New = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
tbl_params_New.addRule(RelativeLayout.RIGHT_OF, (playernumber - 1) * 1000);
tableLayout.setLayoutParams(tbl_params_New);
}
//Add Playername
TableRow row_playername = new TableRow(getContext());
TextView view_name = new TextView(getContext());
TableRow.LayoutParams view_name_params = new TableRow.LayoutParams();
view_name_params.setMargins(20,20,20,20);
view_name.setLayoutParams(view_name_params);
view_name.setGravity(Gravity.CENTER);
view_name.setText(playername);
view_name.setTextSize(20);
view_name.setId(playernumber * 100);
row_playername.addView(view_name);
tableLayout.addView(row_playername);
//Add Lifepoints
TableRow row_lifepoints = new TableRow(getContext());
TextView view_lifepoints = new TextView(getContext());
TableRow.LayoutParams view_lifepoints_params = new TableRow.LayoutParams();
view_lifepoints_params.setMargins(20, 0, 20, 20);
view_lifepoints.setText("40");
view_lifepoints.setTextSize(40);
view_lifepoints.setGravity(Gravity.CENTER);
view_lifepoints.setId(playernumber * 100 + 10);
view_lifepoints.setLayoutParams(view_lifepoints_params);
row_lifepoints.addView(view_lifepoints);
tableLayout.addView(row_lifepoints);
Log.d("Test", "Player count:" + player_count);
for(int opponent_loop = 1; opponent_loop <= player_count; opponent_loop++){
tableLayout.addView(commander_damage_from_player(player_loop, opponent_loop));
}
return tableLayout;
}
private TableRow commander_damage_from_player(int player_loop, int opponent_loop){
TableRow row = new TableRow(getContext());
TextView textView = new TextView(getContext());
TableRow.LayoutParams layoutParams = new TableRow.LayoutParams();
layoutParams.setMargins(20, 0, 40, 20);
textView.setLayoutParams(layoutParams);
textView.setText("0 | " + getResources().getString(R.string.dummy_player_name)+ " " + opponent_loop);
textView.setTextSize(20);
textView.setId(player_loop + 100 + opponent_loop);
row.addView(textView);
return row;
}
private void setPlayer_count(int player_count){
this.player_count = player_count;
}
}
And for documentation the calling class:
public class Home extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PlayerView playerView = new PlayerView(this, 8);
setContentView(playerView.create_scrollView());
}
}
The only way you can do this is by creating a horizontalscrollview to be the child of the scrollview. If you make the minor modification to your create_relativeLayout() to create a horizontalscroll view then it will work correctly. I have seen this happen in other examples.
public HorizontalScrollView create_relativeLayout(){
HorizontalScrollView horizontalScrollView = new HorizontalScrollView(getContext());
relativeLayout = new RelativeLayout(getContext());
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT
);
relativeLayout.setLayoutParams(relativeParams);
create_Player_Table_Layout();
horizontalScrollView.addView(relativeLayout);
return horizontalScrollView;
}

How to remove relatively aligned view and keep other views intact?

I have a problem with removing views from relative view. The code below programatically adds phone fields and then appends switches below. When adding fields it's all good, but I can't find the way to easily remove them so i.e. there are three fields and I want to delete second or third the ones below are left without view they anchored to.
Code:
private int lastCreatedView;
private int tokensIteration;
private int addButtonId, switch1Id, switch2Id;
private ArrayList<Integer> editTextsIds = new ArrayList<>();
private Contact contact;
private void setPhoneField(String number){
EditText editText = new EditText(new ContextThemeWrapper(this, R.style.ContactDetailsEditText));
editText.setId(View.generateViewId());
editText.setText(number);
editText.setHint(R.string.contact_details_activity_hint_phone_number);
editText.setTextColor(getResources().getColor(android.R.color.white));
editText.setEms(10);
editText.setInputType(InputType.TYPE_CLASS_PHONE);
editText.setImeOptions(tokenizer != null && tokenizer.hasMoreTokens()
? EditorInfo.IME_ACTION_NEXT : EditorInfo.IME_ACTION_DONE);
LinearLayout.LayoutParams editTextParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
editText.setLayoutParams(editTextParams);
editTextsIds.add(editText.getId());
TextInputLayout til = new TextInputLayout(new ContextThemeWrapper(this, R.style.ContactDetailsEditText));
til.setId(View.generateViewId());
RelativeLayout.LayoutParams textInputLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
int viewToAttach = lastCreatedView == 0 ? R.id.input_layout_company : lastCreatedView;
textInputLayoutParams.addRule(RelativeLayout.BELOW, viewToAttach);
textInputLayoutParams.addRule(RelativeLayout.END_OF, R.id.contact_photo);
til.setLayoutParams(textInputLayoutParams);
til.setTag(editTextsIds.size());
lastCreatedView = til.getId();
ImageView phoneButton = new ImageView(this);
phoneButton.setImageResource(R.drawable.ic_phone_black_48dp);
phoneButton.setPadding(5,5,5,5);
phoneButton.setScaleType(ImageView.ScaleType.FIT_END);
phoneButton.setColorFilter(Color.argb(255, 255, 255, 255));
RelativeLayout.LayoutParams phoneButtonParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
phoneButtonParams.addRule(RelativeLayout.START_OF, til.getId());
phoneButtonParams.addRule(RelativeLayout.ALIGN_TOP, til.getId());
phoneButtonParams.addRule(RelativeLayout.ALIGN_BOTTOM, til.getId());
phoneButton.setLayoutParams(phoneButtonParams);
phoneButton.setTag(editTextsIds.size());
phoneButton.setOnClickListener(view -> {
Intent call = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + number));
startActivity(call);
});
til.addView(editText);
contactContainer.addView(til);
contactContainer.addView(phoneButton);
ImageView removeNumber = new ImageView(this);
removeNumber.setId(View.generateViewId());
removeNumber.setVisibility(tokensIteration > 0 ? View.VISIBLE : View.GONE);
removeNumber.setImageResource(R.drawable.minus);
RelativeLayout.LayoutParams removeNumberParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
removeNumberParams.addRule(RelativeLayout.END_OF, til.getId());
removeNumberParams.addRule(RelativeLayout.ALIGN_TOP, til.getId());
removeNumberParams.addRule(RelativeLayout.ALIGN_BOTTOM, til.getId());
removeNumber.setLayoutParams(removeNumberParams);
removeNumber.setTag(editTextsIds.size());
removeNumber.setOnClickListener(view -> {
for(View v : getViewsByTag(contactContainer, view.getTag()))
contactContainer.removeView(v);
});
if(tokensIteration > 0) contactContainer.removeView(findViewById(addButtonId));
ImageView addNumber = new ImageView(this);
addNumber.setId(View.generateViewId());
addNumber.setImageResource(R.drawable.plus);
addNumber.setPadding(tokensIteration > 0 ? 5 : 0, 0, 0, 0);
RelativeLayout.LayoutParams addNumberParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
addNumberParams.addRule(RelativeLayout.END_OF, removeNumber.getId());
addNumberParams.addRule(RelativeLayout.ALIGN_TOP, removeNumber.getId());
addNumberParams.addRule(RelativeLayout.ALIGN_BOTTOM, removeNumber.getId());
addNumber.setLayoutParams(addNumberParams);
addNumber.setTag(editTextsIds.size());
addNumber.setOnClickListener(view -> {
//Toast.makeText(this, "Function not available", Toast.LENGTH_SHORT).show();
setPhoneField("");
setSwitches();
}
);
addButtonId = addNumber.getId();
contactContainer.addView(removeNumber);
contactContainer.addView(addNumber);
setSwitches();
}
void setSwitches(){
RelativeLayout.LayoutParams recordCallsParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
recordCallsParams.addRule(RelativeLayout.BELOW, lastCreatedView);
recordCallsParams.addRule(RelativeLayout.ALIGN_START, lastCreatedView);
recordCallsParams.addRule(RelativeLayout.ALIGN_END, lastCreatedView);
if(switch1Id != 0 && switch2Id != 0){
contactContainer.updateViewLayout(findViewById(switch1Id), recordCallsParams);
return;
}
Switch recordCalls = new Switch(this);
recordCalls.setText(R.string.contact_details_activity_record_calls);
recordCalls.setTextColor(getResources().getColor(R.color.white));
recordCalls.setSwitchPadding(5);
recordCalls.setId(View.generateViewId());
recordCalls.setLayoutParams(recordCallsParams);
recordCalls.setChecked(contact != null && contact.getIsRecordCalls());
switch1Id = recordCalls.getId();
Switch switchPrivate = new Switch(this);
switchPrivate.setText(R.string.contact_details_activity_private_contact);
switchPrivate.setTextColor(getResources().getColor(R.color.white));
switchPrivate.setSwitchPadding(5);
switchPrivate.setId(View.generateViewId());
RelativeLayout.LayoutParams switchPrivateParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
switchPrivateParams.addRule(RelativeLayout.BELOW, recordCalls.getId());
switchPrivateParams.addRule(RelativeLayout.ALIGN_START, recordCalls.getId());
switchPrivateParams.addRule(RelativeLayout.ALIGN_END, recordCalls.getId());
switchPrivate.setLayoutParams(switchPrivateParams);
switchPrivate.setChecked(contact != null && contact.getIsPrivateContact());
switch2Id = switchPrivate.getId();
contactContainer.addView(recordCalls);
contactContainer.addView(switchPrivate);
}
private static ArrayList<View> getViewsByTag(ViewGroup root, Object tag){
ArrayList<View> views = new ArrayList<>();
final int childCount = root.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = root.getChildAt(i);
if (child instanceof ViewGroup) {
views.addAll(getViewsByTag((ViewGroup) child, tag));
}
final Object tagObj = child.getTag();
if (tagObj != null && tagObj.equals(tag)) {
views.add(child);
}
}
return views;
Do you really need to completely remove the view? If you just want to hide some view without messing with the layout you can set it to INVISIBLE:
phoneButton.setVisibility(View.INVISIBLE);
BTW: I'd recommend you use layout resource files. It is much easier to work with especially when your layouts get more complex and you get the idea to make your app compatible with multiple screen sizes, screen orientations etc.

Scroll Height for ScrollView does not work

i'm writing progrmicaly adding Linearlayout and Textview into ScrollView. but my ScrollView could not scroll to height.
My programicall Code:
if (cursor.moveToFirst()) {
do {
String last_ID = cursor.getString(cursor.getColumnIndex("lastId"));
String smsBody = cursor.getString(cursor.getColumnIndex("smsBody"));
String senderName = cursor.getString(cursor.getColumnIndex("senderName"));
String date[] = cursor.getString(cursor.getColumnIndex("receiveDate")).split("/");
CalendarTool ct =
new CalendarTool(
Integer.valueOf(date[0]),
Integer.valueOf(date[1]),
Integer.valueOf(date[2])
);
String IranianDate = ct.getIranianDate();
ScrollView SV =new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setBackgroundColor(Color.parseColor("#f7cbad"));
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
params.setMargins(0, 0, 0, 10);
ll.setLayoutParams(params);
TextView TV_IranianDate = new TextView(this);
TV_IranianDate.setText(IranianDate);
TV_IranianDate.setTextColor(Color.parseColor("#ffffff"));
TV_IranianDate.setLayoutParams(
new ViewGroup.LayoutParams
(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT)
);
ll.addView(TV_IranianDate);
TextView TV_smsBody = new TextView(this);
TV_smsBody.setText(smsBody);
TV_smsBody.setGravity(Gravity.RIGHT);
TV_smsBody.setLayoutParams(
new ViewGroup.LayoutParams
(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT)
);
TV_smsBody.setPadding(5, 5, 5, 5);
ll.addView(TV_smsBody);
TextView TV_spacer2 = new TextView(this);
TV_spacer2.setBackgroundColor(Color.parseColor("#000000"));
TV_spacer2.setLayoutParams(
new ViewGroup.LayoutParams
(ViewGroup.LayoutParams.FILL_PARENT,
1)
);
ll.addView(TV_spacer2);
SV.addView(ll);
((LinearLayout) linearLayout).addView(SV);
} while (cursor.moveToNext());
}
I guess the problem is the ScrollView object. You should use HorizontalScrollView instead, like this for example:
HorizontalScrollView SV = new HorizontalScrollView(this);
SV.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
like described in the API:
ScrollView only supports vertical scrolling. For horizontal scrolling, use HorizontalScrollView.

Android : Childs of a relative layout either overlap each other or only the last or the first one is visible

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);

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