Scroll horizontally in programmatically created ScrollView - android

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

Related

Dynamically add imageview to Relativelayout (Alignment Issue)

I am trying to achieve a layout dynamically which is given below:
I can manage to done all things dynamically. But I can't align item name (Chicken Masala) to right of the ImageView . I am reached at this position as following.
RelativeLayout primary_layout = new RelativeLayout(this);
LayoutParams layoutParam = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
primary_layout.setLayoutParams(layoutParam);
// primary_layout.setOrientation(LinearLayout.HORIZONTAL);
// primary_layout.setBackgroundColor(0xff99ccff);
//String cross = " � ";
String makeString = aOrder.getQuantity() + " "
+ aOrder.getFoodName();
ImageView imageView_remove = createAImageview(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, RelativeLayout.CENTER_VERTICAL,
10, 20);
TextView item_name = createATextViewWithParam(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, RelativeLayout.ALIGN_TOP, imageView_remove.getId(),
makeString, 20, 10, 20);
TextView txt_item_price = createATextView(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, RelativeLayout.ALIGN_PARENT_RIGHT,
"" + item_price, 20, 10, 20);
primary_layout.addView(imageView_remove);
primary_layout.addView(item_name);
primary_layout.addView(txt_item_price);
I am share Two methods createAImageview() & createATextViewWithParam() which is necessary for this Layout.
public ImageView createAImageview(int layout_width, int layout_height, int align,
int margin, int padding) {
ImageView imageView = new ImageView(this);
RelativeLayout.LayoutParams _params = new RelativeLayout.LayoutParams(
layout_width, layout_height);
_params.setMargins(margin, margin, margin, margin);
_params.addRule(align);
imageView.setLayoutParams(_params);
imageView.setPadding(padding, padding, padding, padding);
imageView.setImageResource(R.mipmap.remove);
return imageView;
}
public TextView createATextViewWithParam(int layout_widh, int layout_height, int align, int align_id,
String text, int fontSize, int margin, int padding) {
TextView textView_item_name = new TextView(this);
// LayoutParams layoutParams = new LayoutParams(
// LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// layoutParams.gravity = Gravity.LEFT;
RelativeLayout.LayoutParams _params = new RelativeLayout.LayoutParams(
layout_widh, layout_height);
_params.setMargins(margin, margin, margin, margin);
_params.addRule(align, align_id);
textView_item_name.setLayoutParams(_params);
textView_item_name.setText(text);
textView_item_name.setTextSize(TypedValue.COMPLEX_UNIT_SP, fontSize);
textView_item_name.setTextColor(Color.parseColor("#000000"));
// textView1.setBackgroundColor(0xff66ff66); // hex color 0xAARRGGBB
textView_item_name.setPadding(padding, padding, padding, padding);
return textView_item_name;
}
You need to add rule for LEFT_OF/RIGHT_OF :
_params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
_params.addRule(RelativeLayout.LEFT_OF, R.id.id_of_textview);
imageView.setLayoutParams(_params);
Try this,
activity_main.xml
<RelativeLayout 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"
tools:context="com.example.dynamicviewdemo.MainActivity" >
<RelativeLayout
android:id="#+id/llAddMember"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical" />
</RelativeLayout>
MainActivity.java
package com.example.dynamicviewdemo;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
public class MainActivity extends Activity {
private RelativeLayout llAddMember;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
llAddMember = (RelativeLayout) findViewById(R.id.llAddMember);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
final RelativeLayout linearLayout = new RelativeLayout(getApplicationContext());
linearLayout.setLayoutParams(layoutParams);
LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ImageView imgView = new ImageView(MainActivity.this);
imgView.setImageResource(R.drawable.ic_launcher);
imgView.setLayoutParams(lparams);
linearLayout.addView(imgView);
lparams.setMargins(0, 15, 0, 0);
LayoutParams lparams1 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView txtResGender = new TextView(MainActivity.this);
txtResGender.setLayoutParams(lparams1);
txtResGender.setText("Hello World");
txtResGender.setTextSize(14);
txtResGender.setTextColor(Color.parseColor("#9C9C9C"));
linearLayout.addView(txtResGender);
lparams1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
lparams1.setMargins(0, 30, 15, 0);
llAddMember.addView(linearLayout);
}
}

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

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.

Add Relative Layout below current Relative Layout programmatically in 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.

Adding TextView to RelativeLayout inside AsyncTask does not work

I am adding few TextViews to a RelativeLayout. I have extended RelativeLayout. I could add the TextView in the constructor definition. I could see the item on the viw. But the same code, when I use in onPostExecute of AyncTask or use post() method, TextView is not seen added to the layout. Same coe is executed both place. I just changed the top margin.
Here is my code
TextView tv = new TextView(context);
addView(tv);
tv.setHeight(49);
tv.setWidth(100);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.leftMargin = (int) 0;
params.topMargin = (int) 600;
tv.setLayoutParams(params);
================ Implementation ================================
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
DateDetailsScrollView sc = new DateDetailsScrollView(container.getContext());
new SyncInThread().execute(sc);
return sc;
}
================= AsyncTask ===================================
private static class SyncInThread extends AsyncTask<Object, Void, String> {
private ArrayList<EVT> _mEvents = new ArrayList<EVT>();
DateDetailsScrollView _lv = null;
#Override
protected String doInBackground(Object... cal) {
_lv = (DateDetailsScrollView)cal[0];
//The SQL cannot be shared here... Please forgive me.
boolean f = cursor.moveToFirst();
while(f) {
EVT evt = new EVT();
evt.loadFromCursor(cursor);
f = cursor.moveToNext();
_mEvents.add(evt);
cc++;
}
return "";
}
#Override
protected void onPostExecute(String result) {
for(EVT ev : _EVT) {
_lv.addEvent(ev);
}
}
}
public class DateDetailsScrollView extends ScrollView {
private EventRelativeLayout _mEventLayout = null;
public DateDetailsScrollView(Context context) {
super(context);
_mEventLayout = new EventRelativeLayout(context);
ll.addView(_mEventLayout);
LinearLayout ll = new LinearLayout(context);
ll.setOrientation(LinearLayout.HORIZONTAL);
addView(ll);
}
public void addEvent(final VEVENT evt) {
_mEventLayout.addEvent(evt);
invalidate();
this.refreshDrawableState();
}
}
public class EventRelativeLayout extends RelativeLayout {
public EventRelativeLayout(Context context) {
//This works fine.
super(context);
TextView tv = new TextView(context);
addView(tv);
int[] colors = { 0xFF052d42, 0xFF096da0 };
GradientDrawable drawable = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM, colors);
tv.setBackgroundColor(0xFF290202);
tv.setBackgroundDrawable(drawable);
tv.setHeight(49);
tv.setWidth(100);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.leftMargin = (int) 0;
params.topMargin = (int) 100;
tv.setLayoutParams(params);
}
public void addEvent(EVT evt) {
TextView tv = new TextView(getContext());
addView(tv);
int[] colors = { 0xFF052d42, 0xFF096da0 };
GradientDrawable drawable = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM, colors);
tv.setBackgroundColor(0xFF290202);
tv.setBackgroundDrawable(drawable);
tv.setHeight(100);
int dm = em -eh;
int dh = eh - sh;
if( dm < 0) {
dm += 60;
dh--;
}
tv.setWidth(100);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.leftMargin = 0;
params.topMargin = 600;
tv.setHeight(49);
tv.setLayoutParams(params);
}
}
I got the issue. It was a programmatic mistake and due to a wrong calculation, the veiw was added to a different page.

Categories

Resources