How to set left margin for RelativeLayout Programmatically Android - java

I am trying to set the left margin for first RelativeLayout, and
I have this xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/market_layout"
android:orientation="horizontal">
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/horizontalScrollView"
android:layout_gravity="center_horizontal"
android:layout_weight="1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/market_linear_layout_in_hsv">
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
In this java code insert in LinearLayout with id = market_linear_layout_in_hsv inserting RelativeLayout including some view (Button, TextView, ImageView):
market_ll = (LinearLayout)findViewById(R.id.market_linear_layout_in_hsv);
RelativeLayout.LayoutParams TVParams = new RelativeLayout.LayoutParams(RLwrapContent, RLwrapContent );
RelativeLayout.LayoutParams IVParams = new RelativeLayout.LayoutParams(250, 400);
RelativeLayout.LayoutParams BParams = new RelativeLayout.LayoutParams(RLwrapContent, RLwrapContent);
RelativeLayout.LayoutParams RLParams = new RelativeLayout.LayoutParams(RLMatchParent, RLMatchParent);
TVParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
TVParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
IVParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
IVParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
BParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
BParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
if (folder.exists()) {
String[] files = folder.list();
relationsIdMarket = new ArrayList<RelationsIdMarket>(files.length);
for (int i = 0; i < files.length; i++){
RLParams.setMargins(i == 0?150:0, 0, 0, 0);//here i set on first iteration left margin 150
RelationsIdMarket rim = new RelationsIdMarket();
rim.idProduct = i;
rim.idTextView = ViewIdGenerator.generateViewId();
rim.idImageView = ViewIdGenerator.generateViewId();
rim.idButton = ViewIdGenerator.generateViewId();
TextView tv = new TextView(this);
tv.setText("name");
tv.setId(rim.idTextView);
ImageView iv = new ImageView(this);
if(GeneralData.images.size() == i)
GeneralData.images.add(BitmapFactory.decodeFile(pathToImg+"/"+files[i]));
iv.setImageBitmap(GeneralData.images.get(i));
iv.setId(rim.idImageView);
iv.setImageBitmap(rp.bmp);
Button btn = new Button(this);
btn.setText("open");
btn.setOnClickListener(this);
btn.setId(rim.idButton);
RelativeLayout rl = new RelativeLayout(this);
rl.addView(tv, TVParams);
rl.addView(iv, IVParams);
rl.addView(btn, BParams);
rl.setPadding(50, 0, 50, 0);
market_ll.addView(rl, RLParams);
relationsIdMarket.add(rim);
}
}
But I haven't been able to get this working. Any help is appreciated.
I am corrected my code as follows:
ViewGroup.MarginLayoutParams mlp = new ViewGroup.MarginLayoutParams(RLMatchParent, RLMatchParent);
if (folder.exists()) {
String[] files = folder.list();
relationsIdMarket = new ArrayList<RelationsIdMarket>(files.length);
for (int i = 0; i < files.length; i++){
mlp.leftMargin = i==0?100:0;
RelationsIdMarket rim = new RelationsIdMarket();
rim.idProduct = i;
rim.idTextView = ViewIdGenerator.generateViewId();
rim.idImageView = ViewIdGenerator.generateViewId();
rim.idButton = ViewIdGenerator.generateViewId();
TextView tv = new TextView(this);
tv.setText("Название");
tv.setId(rim.idTextView);
ImageView iv = new ImageView(this);
if(GeneralData.images.size() == i)
GeneralData.images.add(BitmapFactory.decodeFile(pathToImg+"/"+files[i]));
iv.setImageBitmap(GeneralData.images.get(i));
iv.setId(rim.idImageView);
iv.setImageBitmap(rp.bmp);
Button btn = new Button(this);
btn.setText("Открыть");
btn.setOnClickListener(this);
btn.setId(rim.idButton);
RelativeLayout rl = new RelativeLayout(this);
rl.addView(tv, TVParams);
rl.addView(iv, IVParams);
rl.addView(btn, BParams);
rl.setPadding(50, 0, 50, 0);
market_ll.addView(rl, mlp);
relationsIdMarket.add(rim);
}
}
But The problem is not solved.

Use android:layout_marginLeft within your first linear layout.
Android RelativeLayout inherits from android.view.ViewGroup.MarginLayoutParams which provides methods to set margins for layouts.
Ref Source

Related

How to set one view in vertical linearlayout in top and another in bottom programmatically?

I'm trying to create programmatically parent LinearLayout
and 3 TextViews.
One TextView must be aligned in top left, the second one in the center of parent
and the third one to the right and bottom of my screen. Everything must be done in code.
I'm almost done, but there is still some problem.
All my 3 views are on the top
My code:
public class ActivityFour extends AppCompatActivity {
private LinearLayout mLinearLayout;
private TextView tv1;
private TextView tv2;
private TextView tv3;
private static final int TV_ID1 = 101;
private static final int TV_ID2 = 102;
private static final int TV_ID3 = 103;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLinearLayout = new LinearLayout(this);
mLinearLayout.setOrientation(LinearLayout.VERTICAL);
ViewGroup.LayoutParams llParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
setContentView(mLinearLayout, llParams);
LinearLayout.LayoutParams linlayout_params1 =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams linlayout_params2 =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams linlayout_params3 =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
linlayout_params1.setMargins(16,16,16,16);
tv1 = new TextView(this);
tv1.setId(TV_ID1);
linlayout_params1.gravity = Gravity.START;
linlayout_params1.gravity = Gravity.TOP;
mLinearLayout.addView(tv1, linlayout_params1);
tv1.setText("TextView number ONE");
linlayout_params2.setMargins(16,16,16,16);
tv2 = new TextView(this);
tv2.setId(TV_ID2);
linlayout_params2.gravity = Gravity.CENTER;
mLinearLayout.addView(tv2, linlayout_params2);
tv2.setText("TextView number TWO");
linlayout_params3.setMargins(16,16,16,16);
tv3 = new TextView(this);
tv3.setId(TV_ID3);
linlayout_params3.gravity = Gravity.END;
mLinearLayout.addView(tv3, linlayout_params3);
tv3.setText("TextView number THREE");
}
}
After adding the weight property to all view.params it looks like this
In order to make the LinearLayout fill all the available space, you need to set the android:layout_weight attribute on the middle child View. You can do it programmatically by using a Constructor which takes the weight as third parameter:
LinearLayout.LayoutParams linlayout_params2 =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1);
First of all no need to create liner layout for all textview you can do like this:
in xml file :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/test"
tools:context=".activity.TestActivity"/>
and in activity file :
LinearLayout layout = findViewById(R.id.test);
TextView product_1 = new TextView(this);
product_1.setText("Product 1");
product_1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0,1f));
layout.addView(product_1);
TextView product_2 = new TextView(this);
product_2.setText("Product 2");
product_2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0,1f));
product_2.setGravity(Gravity.CENTER);
layout.addView(product_2);
TextView product_3 = new TextView(this);
product_3.setText("Product 3");
product_3.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0,1f));
product_3.setGravity(Gravity.END|Gravity.BOTTOM);
layout.addView(product_3);

Android RelativeLayout below another RelativeLayout dynamically

I'm trying to dynamically build relative layouts consisting of several images.
Theses relative layouts will be display below/rigth of previous relative layouts.
I'm starting with two relative layouts (rl100 and rl200). rl200 is below rl100.
But rl200 isn't below rl100, is "stacked" over rl100.
Can you help me ?
My code :
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rLayout = (RelativeLayout) findViewById(R.id.rlayout);
RelativeLayout rL100 = rlAdd(rLayout,100,-1,-1);
ImageButton imgBtn101 = imgBtnAdd(rL100,101,R.drawable.apricot,-1,-1);
ImageButton imgBtn102 = imgBtnAdd(rL100,102,R.drawable.banana,-1,101);
ImageButton imgBtn103 = imgBtnAdd(rL100,103,R.drawable.cherry,101,-1);
ImageButton imgBtn104 = imgBtnAdd(rL100,104,R.drawable.strawberry,102,103);
RelativeLayout rL200 = rlAdd(rLayout,200,100,-1);
ImageButton imgBtn201 = imgBtnAdd(rL100,201,R.drawable.pineapple,-1,-1);
ImageButton imgBtn202 = imgBtnAdd(rL100,202,R.drawable.pineapple,-1,201);
ImageButton imgBtn203 = imgBtnAdd(rL100,203,R.drawable.pineapple,201,-1);
ImageButton imgBtn204 = imgBtnAdd(rL100,204,R.drawable.pineapple,202,203);
}
private RelativeLayout rlAdd(RelativeLayout parentContainer, int nId, int nIdBelow,
int nIdRightOf) {
RelativeLayout rLayout = new RelativeLayout(this);
rLayout.setId(nId);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
if (nIdBelow != -1) {
rlp.addRule(RelativeLayout.BELOW, nIdBelow);
}
if (nIdRightOf != -1) {
rlp.addRule(RelativeLayout.RIGHT_OF, nIdRightOf);
}
rLayout.setLayoutParams(rlp);
parentContainer.addView(rLayout);
return rLayout;
}
private ImageButton imgBtnAdd(RelativeLayout ParentContainer, int ImgId, int ResDrawable,
int imgIdBelow, int imgIdRightOf) {
ImageButton imgBtn = new ImageButton(this);
imgBtn.setId(ImgId);
imgBtn.setImageResource(ResDrawable);
ParentContainer.addView(imgBtn);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) imgBtn.getLayoutParams();
if (imgIdBelow != -1) {
lp.addRule(RelativeLayout.BELOW, imgIdBelow);
}
if (imgIdRightOf != -1) {
lp.addRule(RelativeLayout.RIGHT_OF, imgIdRightOf);
}
imgBtn.setLayoutParams(lp);
return imgBtn;
}
}
activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >
<RelativeLayout android:id="#+id/rlayout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal">
</RelativeLayout>
</ScrollView>
You are doing wrong over here
RelativeLayout rL200 = rlAdd(rLayout,100,-1,-1);
// From method
if (nIdBelow != -1) {
rlp.addRule(RelativeLayout.BELOW, nIdBelow);
}
As you are not passing id in nIdBelow field how it is supposed to add layout below. pass above image id or use Linear Layout with vertical orientation.
You should use this line of code to add the relative layout with ID 200 below the relative layout with id 100, am I right?
RelativeLayout rL200 = rlAdd(rLayout, 200, 100, -1);
Thanks for your help.
I make differents errors :
RelativeLayout rL200...
RelativeLayout rL200 = rlAdd(rLayout, 200, 100, -1);
Correct version :
RelativeLayout rL200 = rlAdd(rLayout,200,100,-1);
ImageButton imgBtn20x
ImageButton imgBtn201 = imgBtnAdd(rL100,201,R.drawable.pineapple,-1,-1);
Correct version :
ImageButton imgBtn201 = imgBtnAdd(rL200,201,R.drawable.pineapple,-1,-1);
Same error for imgBtn202 ... imgBtn204
activity_main.xml
Errors 1 and 2 are stunning errors.
Third error in RelativeLayout android:id="#+id/rlayout" ...
Correct version :
<RelativeLayout android:id="#+id/rlayout" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="match_parent">
</RelativeLayout>

Dynamic TableLayout with TextView is empty even with values in ArrayList

I'm trying to create a dynamic TableLayout with data from database. I store the data in an ArrayList and use it to setText for the TextView. I used debug to check and the ArrayList had the correct number of values as in the database. It's not empty. But still the screen is blank. I tried to setTextColor for the TextView but it still was empty. The first 5 TextViews are for the heading. Later I use for loop for setting the text. I'm trying to do this in a Fragment btw.
Edit: I even checked the value of the bookingHistoryList.get(0). The value is correct. I can't display it!
Here's the code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
sessionManager = new SessionManager(getActivity());
databaseHelper = new DatabaseHelper(getActivity());
bookingHistoryList = new ArrayList();
view = inflater.inflate(R.layout.fragment_preferences, container, false);
tableLayout = (TableLayout)view.findViewById(R.id.hTableLayout);
TableRow pickDate = new TableRow(getActivity());
TextView pickDateTxt = new TextView(getActivity());
pickDateTxt.setText("Pick up Date");
pickDate.addView(pickDateTxt);
TableRow pickTime = new TableRow(getActivity());
TextView pickTimeTxt = new TextView(getActivity());
pickTimeTxt.setText("Pick up Time");
pickTime.addView(pickTimeTxt);
TableRow pickUpLocation = new TableRow(getActivity());
TextView pickUpLocationTxt = new TextView(getActivity());
pickUpLocationTxt.setText("Pick up Location");
pickUpLocation.addView(pickUpLocationTxt);
TableRow destination = new TableRow(getActivity());
TextView destinationTxt = new TextView(getActivity());
destinationTxt.setText("Destination");
destination.addView(destinationTxt);
TableRow vehicleType = new TableRow(getActivity());
TextView vehicleTypeTxt = new TextView(getActivity());
vehicleTypeTxt.setText("Vehicle Type");
vehicleType.addView(vehicleTypeTxt);
// databaseHelper.delete();
String getUserEmail = sessionManager.getUserEmail();
bookingHistoryList = databaseHelper.getBookingHistory(sessionManager.getUserEmail());
rowCount = DatabaseUtils.queryNumEntries(databaseHelper.getReadableDatabase(),"bookingDetails");
n = (int) rowCount*5;
for(int i = 0; i<n; i++) {
TableRow tableRow = new TableRow(getActivity());
TextView textView = new TextView(getActivity());
textView.setText(bookingHistoryList.get(i).toString());
textView.setTextColor(Color.BLACK);
tableRow.addView(textView);
tableLayout.addView(tableRow,new TableLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
TableRow tableRow1 = new TableRow(getActivity());
TextView textView1 = new TextView(getActivity());
textView1.setText(bookingHistoryList.get(i+1).toString());
tableRow1.addView(textView1);
tableLayout.addView(tableRow,new TableLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
TableRow tableRow2 = new TableRow(getActivity());
TextView textView2 = new TextView(getActivity());
textView2.setText(bookingHistoryList.get(i+2).toString());
tableRow2.addView(textView2);
tableLayout.addView(tableRow,new TableLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
TableRow tableRow3 = new TableRow(getActivity());
TextView textView3 = new TextView(getActivity());
textView2.setText(bookingHistoryList.get(i+3).toString());
tableRow2.addView(textView3);
tableLayout.addView(tableRow,new TableLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
TableRow tableRow4 = new TableRow(getActivity());
TextView textView4 = new TextView(getActivity());
textView2.setText(bookingHistoryList.get(i+4).toString());
tableRow2.addView(textView4);
tableLayout.addView(tableRow,new TableLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
i += 4;
}
return view;
}
Here's the code to get from database. Its return value is not empty.
public ArrayList getBookingHistory(String email) {
ArrayList bookingHistoryList = new ArrayList();
String e = email;
Cursor c = db.rawQuery("select * from bookingDetails where email = '"+ email + "'", null);
int cou = c.getCount();
c.moveToFirst();
do {
bookingHistoryList.add(c.getString(c.getColumnIndex("date")));
bookingHistoryList.add(c.getString(c.getColumnIndex("time")));
bookingHistoryList.add(c.getString(c.getColumnIndex("fromLocation")));
bookingHistoryList.add(c.getString(c.getColumnIndex("destination")));
bookingHistoryList.add(c.getString(c.getColumnIndex("vehicle")));
} while (c.moveToNext());
return bookingHistoryList;
}
Stacktrace:
java.lang.NullPointerException
at com.prematixsofs.taxiapp.PreferencesFragment.onCreateView(PreferencesFragment.java:75)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:45)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
Layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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.looper.loop.PreferencesFragment">
<LinearLayout
android:id="#+id/fragmentPreferencesLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableLayout
android:id="#+id/hBookingHistoryTable"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TableLayout>
</ScrollView>
</LinearLayout>
</FrameLayout>
Two things :
One:
Your TableLayout id is hBookingHistoryTable. But in code, you are using a different id.
tableLayout = (TableLayout)view.findViewById(R.id.hTableLayout);
Change it to,
tableLayout = (TableLayout)view.findViewById(R.id.hBookingHistoryTable);
Two:
You are creating TableRows but not adding them to TableLayout. Add this line to all TableRows,
tableLayout.addView(<table_row_name>, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
Correct steps for dynamically add rows to a TableView :
TableLayout table = (TableLayout) findViewById(R.id.main_table);
TableRow row = new TableRow(this);
TextView item = new TextView(this);
item.setId(200+count);
item.setText(date);
row.addView(item);
table.addView(row, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

Incremental Opacity, wanting constant opacity Image View with Drawable

I am programmatically creating textviews with horizontal lines between each view. Using a drawable created programmatically.
The problem is, the opacity starts off light and gradually increases for each line.
I've logged the opacity (getAlpha()) of the drawable, paint, image view and linear layout at all the points in the two methods provided and from the drawables it's always 255 and the views 1.0. I don't understand why it's not behaving as though this is true. I've also tried setting Alpha, it makes no difference.
Why is it doing this and how do I fix it?
xml:
<LinearLayout android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" .../>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="PaintDashedLines"
android:text="Press Me"/>
</LinearLayout>
java:
static int tvCount = 0;
public void PaintDashedLines(View v) {
LinearLayout ll = (LinearLayout) findViewById(R.id.main);
TextView tv = new TextView(MainActivity.this);
tv.setGravity(Gravity.CENTER);
tv.setTextSize(25);
tv.setPadding(0, 5, 0, 5);
ll.addView(tv);
tv.setText("TextView " + tvCount);
ImageView divider = new ImageView(MainActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
ll.getWidth(), 2);
lp.setMargins(0, 5, 0, 5);
divider.setLayoutParams(lp);
divider.setBackground(CreateDashedLined());
ll.addView(divider);
tvCount++;
}
public static Drawable CreateDashedLined() {
ShapeDrawable sd = new ShapeDrawable(new RectShape());
Paint fgPaintSel = sd.getPaint();
fgPaintSel.setColor(Color.BLACK);
fgPaintSel.setStyle(Paint.Style.STROKE);
fgPaintSel.setPathEffect(new DashPathEffect(new float[]{5, 10}, 0));
return sd;
}
To me looks like more some issue with the emulator. You could also try disable the hardware acceleration with
ll.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
Please, keep also in mind that you don't need to instantiate a new ImageView every time, just to draw a divider between the TextViews. You could use setDivider. E.g.
In your onCreate
ShapeDrawable sd = new ShapeDrawable(new RectShape());
sd.setIntrinsicHeight(1);
Paint fgPaintSel = sd.getPaint();
fgPaintSel.setARGB(255, 0, 0, 0);
fgPaintSel.setStyle(Paint.Style.STROKE);
fgPaintSel.setPathEffect(new DashPathEffect(new float[]{5, 10}, 0));
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.main);
linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE | LinearLayout.SHOW_DIVIDER_END);
linearLayout.setDividerDrawable(sd);
and when you press the button
public void pressMe(View view) {
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.main);
TextView tv = new TextView(MainActivity.this);
tv.setGravity(Gravity.CENTER);
tv.setTextSize(25);
tv.setPadding(0, 5, 0, 5);
tv.setText("TextView " + linearLayout.getChildCount());
linearLayout.addView(tv);
}
the result is

AddView error, Just show the first element

Let's say I have a LinearLayout, and I want to add two View to it. The first one contain editText, and the other one contain listview. I have been try code in java follows:
EditText inputViaText;
ListView historyInput;
protected static LinearLayout askTextLayout = null;
askTextLayout = new LinearLayout(this);
askTextLayout.setVisibility(LinearLayout.VISIBLE);
askTextLayout.setOrientation(LinearLayout.HORIZONTAL);
inputViaText = new EditText(this);
LinearLayout.LayoutParams askTextParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
historyInput = new ListView(this);
LinearLayout.LayoutParams historyInputParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,70);
askTextLayout.addView(historyInput,historyInputParams);
askTextLayout.addView(inputViaText,askTextParams);
FrameLayout.LayoutParams frameAskTextParams = new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
setContentView(R.layout.activity_main);
addContentView(askTextLayout, frameAskTextParams);
But, It just show the first one that i add. So when i code like follows:
askTextLayout.addView(historyInput,historyInputParams);
askTextLayout.addView(inputViaText,askTextParams);
It just show listView. When i code like follows:
askTextLayout.addView(inputViaText,askTextParams);
askTextLayout.addView(historyInput,historyInputParams);
It just show edittext. Anyone can help me?
Try this code in oncreate Method of your activity
context = this;
setContentView(R.layout.activity_main);
container = (LinearLayout) findViewById(R.id.Linear);
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setWeightSum(100);
ListView v1 = new ListView(context);
v1.setBackgroundColor(Color.CYAN);
LinearLayout.LayoutParams p1 = new LinearLayout.LayoutParams(0,
50);
p1.weight = 90;
v1.setLayoutParams(p1);
EditText v2 = new EditText(context);
v2.setText("Hello");
v2.setBackgroundColor(Color.WHITE);
LinearLayout.LayoutParams p2 = new LinearLayout.LayoutParams(0,
50);
p2.weight = 10;
v2.setLayoutParams(p2);
linearLayout.addView(v1, p1);
linearLayout.addView(v2, p2);
View view = new View(MainActivity.this);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
1);
view.setLayoutParams(lp);
view.setBackgroundColor(Color.BLACK);
container.addView(linearLayout);
container.addView(view);
Write this in activity_main:
<LinearLayout
android:id="#+id/Linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</LinearLayout>

Categories