Im creating several buttons by using this function:
private void setlayout(Integer numbers_of_buttons){
// creating LinearLayout
LinearLayout linLayout = new LinearLayout(this);
// creating LayoutParams
LinearLayout.LayoutParams linLayoutParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
// set LinearLayout as a root element of the screen
setContentView(linLayout, linLayoutParam);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
Button[] butArray = new Button[numbers_of_buttons];
for (int i = 0; i < numbers_of_buttons; i++)
{
butArray[i] = new Button(this);
butArray[i].setLayoutParams(params);
RelativeLayout.LayoutParams Btnparams = (RelativeLayout.LayoutParams) butArray[i].getLayoutParams();
butArray[i].setText("i ... " + i);
butArray[i].setId(i); // Setting the ids
butArray[i].setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.ic_launcher, 0, 0);
butArray[i].setBackgroundColor(Color.TRANSPARENT);
if (i != 0) {
Btnparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, butArray[i -1].getId());
}
butArray[i].setOnClickListener(listener);
linLayout.addView(butArray[i], Btnparams);
}
}
Which is called when I click something in my NavigationDrawer and the parameter: numbers_of_buttons is retrieved by some query done in the sqlite database.
My only issues is that if numbers_of_buttons is > 7 i.e. (25) i can see only 7 of them.
This is because my scrren width is 720pixel (Nexus7).
So how am I suppose to handle this ?
My idea is
- calculate the widthness of the screen
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
System.out.println("DisplayWidth -> " + metrics.widthPixels);
if getLeft() of last button create is > screenwidth than go to next row.
Can't find a way to do the second pseudocode.
EDIT - 1
private void setlayout(Integer numbers_of_buttons){
FlowLayout flowLayout = new FlowLayout(this);
setContentView(flowLayout);
Button[] butArray = new Button[numbers_of_buttons];
for (int i = 0; i < numbers_of_buttons; i++)
{
butArray[i] = new Button(this);
butArray[i].setText("i ... " + i);
butArray[i].setId(i);
butArray[i].setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.ic_launcher, 0, 0);
butArray[i].setBackgroundColor(Color.TRANSPARENT);
butArray[i].setOnClickListener(listener);
flowLayout.addView(butArray[i]);
}
Related
I add two Checkboxes dynamically to a Linearlayout. Then those Linearlayouts are added one after another in a Relativelayout. The weights of the checkboxes are set so that each take 50% of the Linearlayout width. Now, if their heights do not match, the bottom of the checkbox with bigger height disappears. How to solve this? Here's a screenshot:
And the code:
LinearLayout ll;
LinearLayout.LayoutParams lp;
CheckBox ch;
int id = 1200, i, j;
for (i = 0, j = 0; i < selections.size() - 1; i += 2, j += 2) {
ll = new LinearLayout(NotificationSettings.this);
lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ch = new CheckBox(NotificationSettings.this);
lp.weight = 1.0f;
ch.setLayoutParams(lp);
ch.setText(selections.get(i));
ch.setChecked(isSelected);
ch.setTextColor(color);
ch.setId(j);
ll.addView(ch);
ch = new CheckBox(NotificationSettings.this);
ch.setLayoutParams(lp);
ch.setText(selections.get(i + 1));
ch.setChecked(isSelected);
ch.setTextColor(color);
ch.setId(j + 1);
ll.addView(ch);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
if (id == 1200)
p.addRule(RelativeLayout.BELOW, addBelow);
else
p.addRule(RelativeLayout.BELOW, id);
ll.setLayoutParams(p);
ll.setId(++id);
rl.addView(ll);
}
Edit:
When both checkboxes have multiple lines:
Can you make sure that the Linear Layout's height below it is not too large that it covers the above Linear Layout?
Or try changing your Relative Layout Params' height to WRAP_CONTENT
Change
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
To
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
The title says it all.
I am making an application in which i have to add dynamic buttons in a linear layout i have tried:
testButtons = new Button[caseDetails.length()];
for (int i = 0; i < caseDetails.length(); i++) {
temp = caseDetails.getJSONObject(i);
Log.e("TEMP " + i, temp.toString());
testButtons[i] = new Button(this) ;
testButtons[i].setText("Hello Hi");
testButtons[i].setHeight(LayoutParams.WRAP_CONTENT);
testButtons[i].setWidth(LayoutParams.WRAP_CONTENT);
testButtons[i].setPadding(20, 20, 20, 20);
testLayout.addView(testButtons[i]);
}
All i can see on emulator is two buttons with no text. Why is this happening?
had the same problem.. try this..
testButtons = new Button[caseDetails.length()];
for (int i = 0; i < caseDetails.length(); i++) {
temp = caseDetails.getJSONObject(i);
testButtons[i] = new Button(this) ;
testButtons[i].setText("Hello Hi");
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
testButtons[i].setPadding(20, 20, 20, 20);
testLayout.addView(testButtons[i], lp);
}
also make sure your linearLayout's orientation is vertical. Good Luck! :)
I have a little complicated layout I want to make in my ListView adpater.
Each item in the list is combined of 4 TextView
The first three TextView are basically one sentence, but I want the second TextView not to be ellipsized, and the first and the third I do want to be ellipsized.
The last TextView is need to be positioned to the right side.
so if the all the first three TextView can't be shown completely, the layout should be like this:
`TextView1...` `TextView2` `TextView3...` `TextView4`
My code is working in this case, but in simple case when no ellipsize is necessary, it doesn't and it looks like this:
`TextView1` `TextView2` `TextView3` `TextView4`
Here's my code:
LinearLayout layout = new LinearLayout(m_context);
layout.setGravity(Gravity.CENTER_VERTICAL);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setLayoutParams(new ListView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
LinearLayout textLayout = new LinearLayout(m_context);
textLayout.setOrientation(LinearLayout.HORIZONTAL);
textLayout.setGravity(Gravity.CENTER_VERTICAL);
view.m_text1 = new TQTextView(m_context);
view.m_text1.setSingleLine();
view.m_text1.setEllipsize(TruncateAt.END);
view.m_text2 = new TQTextView(m_context);
view.m_text3 = new TQTextView(m_context);
view.m_text3.setSingleLine();
view.m_text3.setEllipsize(TruncateAt.END);
view.m_text1 = new TQTextView(m_context);
textLayout.setWeightSum(2f);
textLayout.addView(view.m_text1, new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f));
textLayout.addView(view.m_text2, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0f));
textLayout.addView(view.m_text3, new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f));
layout.addView(textLayout, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1f));
view.m_text4 = new TQTextView(m_context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0f);
int margin = 10;
params.leftMargin = margin;
params.gravity = Gravity.CENTER_VERTICAL|Gravity.RIGHT;
layout.addView(view.m_text4, params);
I found a solution:
I add the three TextView without special LayoutParams like this:
textLayout.addView(view.m_attackeeName);
textLayout.addView(view.m_text);
textLayout.addView(view.m_attackerName);
and I call the following function in the end of getView function (after setting the texts)
private void updateTexts(View parent, ViewHolder view, Data data){
view.m_text1.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
view.m_text3.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
view.m_text2.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
view.m_text4.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
int totalWidth = parent.getWidth() - parent.getPaddingLeft() - parent.getPaddingRight();
totalWidth = totalWidth - view.m_text4.getMeasuredWidth() - view.m_text2.getMeasuredWidth() - m_margin;
int attackerTextWidth = view.m_text1.getMeasuredWidth();
int attackeeTextWidth = view.m_text3.getMeasuredWidth();
if( attackerTextWidth + attackeeTextWidth > totalWidth )
{
int spareWidth = totalWidth / 2 - attackerTextWidth;
if( spareWidth > 0 )
{
attackeeTextWidth = totalWidth / 2 + spareWidth;
}
else
{
spareWidth = totalWidth / 2 - attackeeTextWidth;
if( spareWidth > 0 )
{
attackerTextWidth = totalWidth / 2 + spareWidth;
}
else
{
attackerTextWidth = totalWidth / 2;
attackeeTextWidth = totalWidth / 2;
}
}
}
String text = TextUtils.ellipsize(data.GetText1(), view.m_text1.getPaint(),
attackerTextWidth, TextUtils.TruncateAt.END).toString();
view.m_text1.setText(text);
text = TextUtils.ellipsize(data.GetText3(), view.m_text3.getPaint(),
attackeeTextWidth, TextUtils.TruncateAt.END).toString();
view.m_text3.setText(text);
}
I use this code to dynamically print the vaules from my database with a buttonClick-event.
The buttonClick-event to delete the database entry is present inside the a loop.
Here my code:
RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeLayout3);
final DatabaseHandler dbpin = new DatabaseHandler(this);
// Log.d("Reading: ", "Reading all tasks..");
List<Detail> detail1 = dbpin.getAllDetail();
Button[] button=new Button[1000];
for (Detail cn : detail1) {
String log = cn.getTitle();
final int i = cn.getID();
button[i] = new Button(this);
button[i].setText("Delete");
button[i].setTextSize(10);
button[i].setId(2000 + i);
int width = 80;
int height = 60;
TextView textview = new TextView(this);
textview.setText(log);
textview.setWidth(200);
textview.setTextSize(20);
textview.setPadding( 0, 10, 0, 0);
textview.setId(2000 + i);
if (i == 0) {
RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp2.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rlp2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
textview.setLayoutParams(rlp2);
rl.addView(textview);
RelativeLayout.LayoutParams rlp1 = new RelativeLayout.LayoutParams(
width, height);
rlp1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rlp1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
button[i].setLayoutParams(rlp1);
rl.addView(button[i]);
} else {
RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
rlp2.addRule(RelativeLayout.BELOW, button[i].getId() - 1);
textview.setLayoutParams(rlp2);
rl.addView(textview);
RelativeLayout.LayoutParams rlp1 = new RelativeLayout.LayoutParams(
width, height);
rlp1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rlp1.addRule(RelativeLayout.BELOW, button[i].getId() - 1);
button[i].setLayoutParams(rlp1);
rl.addView(button[i]);
}
button[i].setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), details.class);
Detail detail = new Detail();
detail.setID(i);
dbpin.deleteDetail(detail);
startActivityForResult(myIntent, 1);
}
});
}
Following the database handler code is, to retrieve all details from database using a loop:
// Getting All detail
public List<Detail> getAllDetail() {
List<Detail> detailList = new ArrayList<Detail>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_DETAIL;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Detail detail = new Detail();
detail.setID(Integer.parseInt(cursor.getString(0)));
detail.setTitle(cursor.getString(1));
detail.setDetail(cursor.getString(2));
// Adding contact to list
detailList.add(detail);
} while (cursor.moveToNext());
}
// return contact list
return detailList;
}
// Deleting single detail
public void deleteDetail(Detail detail) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_DETAIL, KEY_DETID + " = ?",
new String[] { String.valueOf(detail.getID()) });
db.close();
}
At first the layout is normal. Deleting first or the last data row doesn't cause any change, but if a row in the middle is deleted, then the layout overlaps each other.
Please give me suggestions to clear this logical error.
Ok I have understand your problem. Problem is that you are using relative layout as your parent layout in which you are adding all your child relative layouts. Now if you delete your first relative layout then it automatically align with its parent so there will be no problem.
If you delete last relative layout then also not problem occurs.
Now you have align all your relative layout form their above layout so if you delete above one it automatically aligns to its parent.
Solution is simple. Use your parent layout as linear layout so that you dont need to align relative layouts with their above layout. It will automatically arrange in a linear way....
RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeLayout3); convert this layout in linearlayout in your xml file.
This the code which can help you:
LinearLayout lp = (LinearLayout) findViewById(R.id.LinearLayout1);
// for loop start from here
RelativeLayout rl = new RelativeLayout(getApplicationContext());
RelativeLayout.LayoutParams lp_btn = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
lp_btn.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
Button temp_button = new Button(getApplicationContext());
temp_button.setText("button");
rl.addView(temp_button, lp_btn);
TextView tv = new TextView(getApplicationContext());
tv.setText("bharat");
tv.setBackgroundColor(Color.GREEN);
RelativeLayout.LayoutParams lp_tv = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
lp_tv.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rl.addView(tv, lp_tv);
lp.addView(rl);
// for loop will end here
I think you should use listview for your purpose it will be better. Anyway this will also work you have to manage relativelayout array and button array for your purpose.
I want to create 4 RelativeLayout dynamically so that each subsequent layout is placed below the previous one. I'm trying to do this whit this piece of code:
RelativeLayout layoutParent = (RelativeLayout)findViewById(R.id.layoutParent);
int layouts = 4;
int dp15 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, getResources().getDisplayMetrics());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.setMargins(dp15, dp15, dp15, dp15);
for (int l = 0; l <= layouts; l++)
{
RelativeLayout queueLayout = new RelativeLayout(getApplicationContext());
TextView one = new TextView(getApplicationContext());
one.setText(String.valueOf(l));
queueLayout.setId(2000 + l);
if (l != 0) params.addRule(RelativeLayout.BELOW, queueLayout.getId() - 1);
queueLayout.addView(one, params);
layoutParent.addView(queueLayout);
}
But I can't get the desired position of each layout. Can someone tell me how can I do what I wanna do?
Thank you in advance!
You set the BELLOW rule but never use it when you add the child RelativeLayout to the parent layout(as MisterSquonk said). Also use another set of LayoutParams for the child RelativeLayout:
for (int l = 0; l <= layouts; l++) {
RelativeLayout queueLayout = new RelativeLayout(getApplicationContext());
TextView one = new TextView(getApplicationContext());
one.setText(String.valueOf(l));
queueLayout.setId(2000 + l);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
if (l != 0) lp.addRule(RelativeLayout.BELOW, queueLayout.getId() - 1);
queueLayout.addView(one, params);
layoutParent.addView(queueLayout, lp);
}