I have tried everything but my ScrollView doesn't work in my activity. I have activity where after user clicks button opens a table. I can't find a way that my table scrolls bouth ways. How can I make ScrollView programmatically? My code:
TableLayout tableLayout = new TableLayout(getApplicationContext());
tableLayout.setVerticalScrollBarEnabled(true);
tableLayout.setBackgroundColor(Color.WHITE);
TableRow tableRow;
TextView textView,...
tableRow = new TableRow(getApplicationContext());
textView=new TextView(getApplicationContext());
textView.setText("Date");
textView.setTextColor(Color.BLACK);
textView.setTypeface(null, Typeface.BOLD);
textView.setPadding(20, 20, 20, 20);
tableRow.addView(textView);
tableLayout.addView(tableRow);
for (Integer j = 0; j < count; j++)
{
tableRow = new TableRow(getApplicationContext());
textView1 = new TextView(getApplicationContext());
....
tableLayout.addView(tableRow);
c.moveToNext() ;
}
c.close();
setContentView(tableLayout);
database.close();
You can make it this way and in ScrollView there must be only one child:
ScrollView scrollview = new ScrollView(context);
scrollview.setBackgroundColor(android.R.color.transparent);
scrollview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
scrollview.addView(tableLayout);
Related
I'm having a problem with LinearLayout where I want to display a TableLayout after a TextView. I've tried modifying the LayoutParams however I can't seem to make it wrap (which I'm assuming I want it to do).
This is simplified version:
ScrollView sv = (ScrollView) findViewById(R.id.sv);
sv.removeAllViews();
LinearLayout ll = new LinearLayout(this);
sv.addView(ll);
//This is displayed
TextView tv = new TextView(this);
tv.setText("Hello World");
ll.addView(tv);
//This is not displayed - but if I remove the above view it will display...
TableLayout tl = new TableLayout(this);
ll.addView(tl, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
By specifiying Orientation as Vertical it worked for me:
ScrollView sv = (ScrollView) findViewById(R.id.sv);
sv.removeAllViews();
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
TextView tv = new TextView(this);
tv.setText("Hello World");
ll.addView(tv);
TableLayout tl = new TableLayout(this);
ll.addView(tl);
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));
I tried creating a scroll view in my relative layout to make my table layout scrollable but i keep getting this error:
02-02 19:29:10.116: E/AndroidRuntime(9400):
java.lang.RuntimeException: Unable to start activity ComponentInfo{test.com.classmanagertest/test.com.classmanagertest.StudentsMasterList}: java.lang.IllegalStateException:
The specified child already has a parent. You must call removeView() on the child's parent first.
This is my Table Layout that display's fields in a database:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.students_masterlist);
db=openOrCreateDatabase("ClassManager",MODE_WORLD_WRITEABLE, null);
Cursor c=db.rawQuery("SELECT StudPic, StudentID, LastName FROM MasterStudents", null);
int count= c.getCount();
c.moveToFirst();
TableLayout tableLayout = new TableLayout(getApplicationContext());
tableLayout.setVerticalScrollBarEnabled(true);
TableRow tableRow;
TextView textView, textView1;
ImageView StudImageView;
RelativeLayout rl=(RelativeLayout)findViewById(R.id.layout);
ScrollView sv = new ScrollView(this);
sv.addView(tableLayout);
rl.addView(sv);
for (Integer j = 0; j < count; j++)
{
tableRow = new TableRow(getApplicationContext());
StudImageView = new ImageView(getApplicationContext());
StudImageView.setPadding(20, 20, 20, 20);
StudImage=c.getBlob(c.getColumnIndex("StudPic"));
Bitmap b1= BitmapFactory.decodeByteArray(StudImage, 0, StudImage.length);
StudImageView.setImageBitmap(b1);
tableRow.addView(StudImageView);
textView1 = new TextView(getApplicationContext());
textView1.setText(c.getString(c.getColumnIndex("StudentID")));
textView1.setPadding(20, 20, 20, 20);
textView1.setTextColor(getResources().getColor(R.color.blueactionbar));
textView1.setTextSize(TypedValue.COMPLEX_UNIT_DIP,25);
textView1.setTypeface(null, Typeface.BOLD);
tableRow.addView(textView1);
textView = new TextView(getApplicationContext());
textView.setText(c.getString(c.getColumnIndex("LastName")));
textView.setPadding(20, 20, 20, 20);
textView.setTextColor(getResources().getColor(R.color.blueactionbar));
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,25);
textView.setTypeface(null, Typeface.BOLD);
tableRow.addView(textView);
tableLayout.addView(tableRow);
c.moveToNext() ;
}
setContentView(tableLayout);
db.close();
}
This is my xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:layout_gravity="center_horizontal"
android:scrollbars="vertical|horizontal"
android:id="#+id/layout">
</RelativeLayout>
How do I fix this error?
Any help is appreciated! Thank you so much!
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.students_masterlist);
db=openOrCreateDatabase("ClassManager",MODE_WORLD_WRITEABLE, null);
Cursor c=db.rawQuery("SELECT StudPic, StudentID, LastName FROM MasterStudents", null);
int count= c.getCount();
c.moveToFirst();
TableLayout tableLayout = new TableLayout(getApplicationContext());
tableLayout.setVerticalScrollBarEnabled(true);
TableRow tableRow;
TextView textView, textView1;
ImageView StudImageView;
RelativeLayout rl=(RelativeLayout)findViewById(R.id.layout);
ScrollView sv = new ScrollView(this);
sv.addView(tableLayout);
rl.addView(sv);
for (Integer j = 0; j < count; j++)
{
tableRow = new TableRow(getApplicationContext());
StudImageView = new ImageView(getApplicationContext());
StudImageView.setPadding(20, 20, 20, 20);
StudImage=c.getBlob(c.getColumnIndex("StudPic"));
Bitmap b1= BitmapFactory.decodeByteArray(StudImage, 0, StudImage.length);
StudImageView.setImageBitmap(b1);
tableRow.addView(StudImageView);
textView1 = new TextView(getApplicationContext());
textView1.setText(c.getString(c.getColumnIndex("StudentID")));
textView1.setPadding(20, 20, 20, 20);
textView1.setTextColor(getResources().getColor(R.color.blueactionbar));
textView1.setTextSize(TypedValue.COMPLEX_UNIT_DIP,25);
textView1.setTypeface(null, Typeface.BOLD);
tableRow.addView(textView1);
textView = new TextView(getApplicationContext());
textView.setText(c.getString(c.getColumnIndex("LastName")));
textView.setPadding(20, 20, 20, 20);
textView.setTextColor(getResources().getColor(R.color.blueactionbar));
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,25);
textView.setTypeface(null, Typeface.BOLD);
tableRow.addView(textView);
tableLayout.addView(tableRow);
c.moveToNext() ;
}
db.close();
rl.requestLayout();
}
The First setContentView is called with the parenet relative layout which contains the table layout that has your rows.Therefore you are trying to setContentView to a view which is contained in the parent that you have already setContentView to ie.
rl contains sv contains tableLayout.
but you are setting the activity view first to rl then to tableLayout.
Instead you should set the content view to rl.Then fill your tableLayout like you did and refresh the layout.ie call requestLayout() on the parent view.
I have this code which generates a GUI programmatically
TableLayout table;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
table = new TableLayout(this);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
table.setLayoutParams(lp);
table.setStretchAllColumns(true);
TableLayout.LayoutParams rowLp = new TableLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT, 1.0f);
TableRow.LayoutParams cellLp = new TableRow.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT, 1.0f);
int count = 0;
for (int r = 0; r < 3; ++r) {
TableRow row = new TableRow(this);
for (int c = 0; c < 3; ++c) {
count++;
final Button btn = new Button(this);
btn.setText("");
btn.setId(count);
btn.setOnClickListener(this);
row.addView(btn, cellLp);
}
table.addView(row, rowLp);
}
TableRow erow = new TableRow(this);
Button btn = new Button(this);
btn.setText("Reset");
btn.setId(10);
btn.setOnClickListener(this);
erow.addView(btn, cellLp);
table.addView(erow, rowLp);
setContentView(table);
}
In the listener, I can change the text of the button I click this way
#Override
public void onClick(View v) {
Button btn = (Button)v;
btn.setText("text");
}
But, how, can I interact with other buttons? I have tried with something like btn = (Button)findViewById(7); but it doesn't work.
I also tried with an array of buttons but still doesn't work. Any tips?
findViewById() is mainly for views created by inflating XML layout files.
Though you can call setId() for views, in this case you should keep simply references to the programmatically created views, i.e.
private Button button1;
and then just use those fields.
Try to use Tag instead id for your buttons:
for (...) {
count++;
final Button btn = new Button(this);
...
btn.setTag(count);
btn.setOnClickListener(this);
...
}
And in listener call (Integer)v.getTag()
TableRow tr1 = new TableRow(this);
tr1.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
TextView textview = new TextView(this);
textview.setText(text.getText());
// textview.getTextColors(R.color.)
textview.setTextColor(Color.YELLOW);
tr1.addView(textview);
chatbox.addView(tr1, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
How can i print this code when it adds the view to the left of the TableLayout
Set the Textview Gravity to the Right...
textview.setGravity(Gravity.RIGHT);