Can anyone see what is wrong with the below:
if (mViewPager.getCurrentItem() == 3){
TableLayout tbl = (TableLayout)findViewById(R.id.tblHistory);
if (tbl.getChildCount() != mExceptions.size()){
for (int i = tbl.getChildCount(); i < mExceptions.size(); i++){
GPSException e = mExceptions.get(i);
//TableRow
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
tr.setBackgroundColor(Color.BLACK);
//DateTime
TextView tx = new TextView(this);
tx.setLayoutParams(new android.view.ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tx.setText(e.DateTime);
tx.setTextAppearance(getApplicationContext(), android.R.attr.textAppearanceMedium);
tx.setTextColor(Color.BLACK);
tx.setBackgroundColor(Color.WHITE);
tr.addView(tx);
//Speed
tx = new TextView(this);
tx.setLayoutParams(new android.view.ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tx.setText(Float.toString(e.Speed));
tx.setTextAppearance(getApplicationContext(), android.R.attr.textAppearanceMedium);
tx.setTextColor(Color.BLACK);
tx.setBackgroundColor(Color.WHITE);
tr.addView(tx);
//Bearing
tx = new TextView(this);
tx.setLayoutParams(new android.view.ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tx.setText(Double.toString(e.Degrees));
tx.setTextAppearance(getApplicationContext(), android.R.attr.textAppearanceMedium);
tx.setTextColor(Color.BLACK);
tx.setBackgroundColor(Color.WHITE);
tr.addView(tx);
//Exceptions
tx = new TextView(this);
tx.setLayoutParams(new android.view.ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tx.setText(e.ExceptionString());
tx.setTextAppearance(getApplicationContext(), android.R.attr.textAppearanceMedium);
tx.setTextColor(Color.BLACK);
tx.setBackgroundColor(Color.WHITE);
tr.addView(tx);
tbl.addView(tr, 0);
}
tbl.invalidate();
tbl.refreshDrawableState();
}
}
My class implements LocationListener and the above code is fired in the onLocationChanged event. The aim is to add a TableRow to a TableLayout for each queued "Exception". Each row is inserted at position 0.
I've tried everything apart from the correct approach it would appear!
My layout is as follows (resources not used for the time being, but will be once I've sorted out this issue - for all you eagle eyed viewers out there!):
<ScrollView 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<TableRow android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Dt/Tm"></TextView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Spd"></TextView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Brg"></TextView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Ex"></TextView>
</TableRow>
</TableLayout>
There are no exceptions thrown, and if I toast a short message in the for loop, it does appear.
If you need more info, not a problem.
Thanks.
tbl.addView(tr, 0);
I think you need to give that layout params, not 0. Perhaps try this?
tbl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
After all of this, removing the android.view.ViewGroup.LayoutParams qualifier (meaning the layout params for each TextView are now TableRow.LayoutParams) has fixed the solution. Why? I do not know......
Maybe the layout parameters of the TextView have to pertain to the TableRow???! Scraping here. Anyway, problem solved :-(
Related
I have been looking these past two days on questions regarding setting the weight of a layout or a group of layouts programmatically.
all the answers i found are almost the same so that means i know what code no to use but i do not seem to understand how to assign the float attribute. in the following code.
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT,
1.0f
);
YOUR_VIEW.setLayoutParams(param);
would someone please give me an example of how to assign the weights of two TextView with a weight sum of 3???
It depends on your view if you want to split the view horizontally between them you can use something like this.
TextView secondTV = findViewById(R.id.secondTextView);
TextView firstTV = findViewById(R.id.firstTextView);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 3);
LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1);
firstTV.setLayoutParams(layoutParams);
secondTV.setLayoutParams(layoutParams1);
And your layout looks like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/firstTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello" />
<TextView
android:id="#+id/secondTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/icon"
android:text="Hello" />
</LinearLayout>
but if you want to split the view vertically you can use something like this.
TextView secondTV = findViewById(R.id.secondTextView);
TextView firstTV = findViewById(R.id.firstTextView);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 3);
LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1);
firstTV.setLayoutParams(layoutParams);
secondTV.setLayoutParams(layoutParams1);
And your layout looks like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/firstTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello" />
<TextView
android:id="#+id/secondTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/icon"
android:text="Hello" />
</LinearLayout>
I want to set the buttons one after another vertically. I was trying something like below, but it doesn't work. Need help please.
RelativeLayout body=(RelativeLayout) findViewById(R.id.body);
RelativeLayout.LayoutParams buttonParams =
new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Button btn;
List<Button> allEds = new ArrayList<Button>();
for(int i=0;i<totalSID;i++){
btn = new Button(FaqList.this);
btn.setId(i);
allEds.add(btn);
if(i==0){}
else
buttonParams.addRule(RelativeLayout.BELOW,allEds.get(i-1).getId());
body.addView(btn,buttonParams);
}
Adding the xml file. For reference you can go through it.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:scrollbars="vertical"
>
<HorizontalScrollView
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:fillViewport="true"
>
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginBottom="20dp"
>
<LinearLayout
android:id="#+id/body"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:orientation="horizontal"
android:animateLayoutChanges="true">
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
</ScrollView>
</LinearLayout
I am not sure where is the problem actually
If you want to add buttons vertically then just use LinearLayout instead of RelativeLayout in your body.
int iNumberOfButtons = productTypeList.size();
Button[] dynamicButtons = new Button[iNumberOfButtons];
LinearLayout.LayoutParams paramsButton = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for (int i = 0; i < iNumberOfButtons; i++) {
ProductType productType = productTypeList.get(i);
dynamicButtons[i] = new Button(getActivity());
dynamicButtons[i].setText(productType.getTitleString());
dynamicButtons[i].setId(i);
dynamicButtons[i].setTextSize(15.0f);
dynamicButtons[i].setOnClickListener(this);
dynamicButtons[i].setLayoutParams(paramsButton);
dynamicButtonsLinearLayout.addView(dynamicButtons[i]); // dynamicButtonsLinearLayout is the container of the buttons
}
Use linear layout instead of Relative layout. If you need to add multiple button dynamically use RecyclerView based on Item position you can do whatever you need
LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button bt=new Button(this);
bt.setLayoutParams(lparams);
this.m_vwJokeLayout.addView(bt);
you used relative layout . with that bottons cover eachother . use linearlayout as parent container . but if you want use relative layout you should store previous buttons id and set new buttons below property like this :
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
relativeParams.addRule(RelativeLayout.BELOW, idOfTheViewBelow);
I am new to Android.
I am drawing Textfield using drag-drop from Eclipse.
Also adding a Table programatically.
But I can see only table which is added programatically.
How to keep both components from XML and those added programatically.
Sorry if it is a really basic and stupid question.
Here is my code
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:shrinkColumns="*"
android:stretchColumns="*">
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<ZoomButton
android:id="#+id/zoomButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/btn_plus" />
<ZoomControls
android:id="#+id/zoomControls1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TwoLineListItem
android:id="#+id/twoLineListItem1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<AbsoluteLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</AbsoluteLayout>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword" />
<AnalogClock
android:id="#+id/analogClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<CalendarView
android:id="#+id/calendarView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</TableLayout>
and
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatabaseHandler db = new DatabaseHandler(this);
//To insert
// db.addContact(new Contact("Ninad", "9893353432"));
TableLayout table = new TableLayout(this);
table.setStretchAllColumns(true);
table.setShrinkAllColumns(true);
TableRow rowTitle = new TableRow(this);
rowTitle.setGravity(Gravity.CENTER_HORIZONTAL);
TableRow rowDayLabels = new TableRow(this);
// title column/row
TextView title = new TextView(this);
title.setText("Contacts");
title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
title.setGravity(Gravity.CENTER);
title.setTypeface(Typeface.SERIF, Typeface.BOLD);
TableRow.LayoutParams params = new TableRow.LayoutParams();
params.span = 3;
rowTitle.addView(title, params);
// Header 1
TextView header1 = new TextView(this);
header1.setText("ID");
header1.setTypeface(Typeface.SERIF, Typeface.BOLD);
rowDayLabels.addView(header1);
// Header 2
TextView header2 = new TextView(this);
header2.setText("NAME");
header2.setTypeface(Typeface.SERIF, Typeface.BOLD);
rowDayLabels.addView(header2);
// Header 3
TextView header3 = new TextView(this);
header3.setText("PHONE");
header3.setTypeface(Typeface.SERIF, Typeface.BOLD);
rowDayLabels.addView(header3);
// Add to Table
table.addView(rowTitle);
table.addView(rowDayLabels);
List<Contact> contacts = db.getAllContacts();
TableRow rowHighs = null;
for (Contact cn : contacts) {
rowHighs = new TableRow(this);
// Data
TextView day1High = new TextView(this);
day1High.setText(String.valueOf(cn.getID()));
rowHighs.addView(day1High);
TextView day2High = new TextView(this);
day2High.setText(cn.getName());
rowHighs.addView(day2High);
TextView day3High = new TextView(this);
day3High.setText(cn.getPhoneNumber());
rowHighs.addView(day3High);
table.addView(rowHighs);
}
setContentView(table);
}
My Table in the OnCreate method is populated properly, but components from XML are lost.
Update :
What I found is, I am setting it two times and later one is overwritting it
setContentView(R.layout.activity_main);
setContentView(table);
But how can I merge it ?
Type in your activity below oncreate
TableLayout tbl_lay=(TableLayout)findViewById(R.id.tablelayout1);
.....
.....
.....
Your Code.....
TableLayout tb=new TableLayout(this);
.....
.....
atlast add these line
tbl_lay.addview(tb);
it will help you
Your layout name is "tableLayout1" in xml, you should implement a TableLayout object from it by using findViewById, and add your programmatically created view to it. Be careful about positioning, you may put it on your drag-drop views!
TableLayout myLayout = (TableLayout) findViewById(R.id.tableLayout1);
//your elements created
myLayout.addView(yourCreatedViewElement);
i know its easy question but i am newbie .....i want to add button for every row e.g for row1 there should be button1 and for row2 there should be button2 , buttons insertion and text are controlled using for loop
first.xml
<LinearLayout
android:id="#+id/summaryHeaderLinear_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableLayout
android:id="#+id/summaryTable_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="*">
</TableLayout>
firstrow.xml
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/summary_tablerow"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/summaryHeader_linearlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/summaryHeader_firstlinearlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:id="#+id/summaryHeader_secondlinearlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:id="#+id/summaryHeader_thirdlinearlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
</TableRow>
firstRow.java:
setContentView(R.layout.first);
summaryTableLayout = (TableLayout)findViewById(R.id.summaryTable_layout);
summaryInflator = getLayoutInflater();
for(int i =0;i<2;i++)
{ final TableRow summaryTableRow = (TableRow)summaryInflator.inflate(R.layout.firstrow,null);
LinearLayout summaryHeaderLinearLayout = (LinearLayout)summaryTableRow.findViewById(R.id.summaryHeader_linearlayout);
summaryTableRow.setId(i);
Button summaryBtn = new Button(this);
summaryBtn.setId(i);
summaryBtn.setText("sample button:" +i);
summaryBtn.setBackgroundColor(Color.MAGENTA);
summaryTableRow.addView(summaryBtn);
for(int k=0;k<5;k++)
{
LinearLayout summaryFirstHorizontalLinearLayout = (LinearLayout)summaryTableRow.findViewById(R.id.summaryHeader_firstlinearlayout);
TextView newText = new TextView(this);
newText.setId(k);
newText.setText(topRowArray[k]);
newText.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT,1f));
newText.setBackgroundColor(Color.BLACK);
newText.setTextColor(Color.WHITE);
summaryFirstHorizontalLinearLayout.addView(newText);
}
summaryTableLayout.addView(summaryTableRow);
}
the output of code is:
while i want to create this layout:
Button for row1
name qty cost sell margin
Button for row2
name qty cost sell margin
but its not displaying like this i already have defined separate xml i dnt want to create dynamic Row.I want to use defined firstrow.xml please help me what should i need to do changes in my existing code
use to a dynamically add textview and button on linearlayout
LinearLayout ll = (LinearLayout)findViewById(R.id.layout);
for(int y =0;y<5;y++){
TextView textv= new TextView(this);
final Button myButton = new Button(this);
textv.setText("record");
myButton.setText("button"+y);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ll.addView(textv,lp);
ll.addView(myButton,lp);
}
i think this are usefull your problem solution
I have an Android app with this portion of main activity:
<LinearLayout
android:id="#+id/vistaAnni"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/TextView02"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".1"
android:text="1998" />
<CheckBox
android:id="#+id/CheckBox02"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".1" />
<CheckBox
android:id="#+id/CheckBox07"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".1" />
On the main activity class, I need another linear layout with other checks box.
LinearLayout myView = (LinearLayout) findViewById(R.id.vistaAnni);
Log.v("blah", "Crea tabella dinamica");
for(int i = 0; i < anni.size();i++){
Log.v("blah", "Creo colonna: "+anni.get(i));
LinearLayout newLine = new LinearLayout(getApplicationContext());
newLine.setOrientation(1);
newLine.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
TextView testo = new TextView(getApplicationContext());
testo.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, .1f));
testo.setText(anni.get(i));
newLine.addView(testo);
CheckBox check1c = new CheckBox(getApplicationContext());
check1c.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, .1f));
newLine.addView(check1c);
CheckBox check2c = new CheckBox(getApplicationContext());
check2c.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, .1f));
newLine.addView(check2c);
myView.addView(newLine);
The system creates the box and the text fine, but with different colors and hardly visibility.
But I haven't customized it over activity.xml it or over activity.java. Why this change?
This is a sreen of app:
I have change all:
CheckBox check1c = new CheckBox(getApplicationContext());
with:
CheckBox check1c = new CheckBox(this);
as suggested by Luksprog and works.