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);
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 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);
I try to create dynamically linerlayout and i created two buttoms.
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
TextView titleView = new TextView(this);
titleView.setWidth(LayoutParams.WRAP_CONTENT);
titleView.setHeight(LayoutParams.WRAP_CONTENT);
titleView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
titleView.setText("Hallo Welt!");
layout.addView(titleView);
Button btnConnect = new Button(this);
btnConnect.setText("Connect");
layout.addView(btnConnect);
Button btnDisconnect = new Button(this);
btnDisconnect.setText("Disconnect");
layout.addView(btnDisconnect);
I want to put Connect button to left corner and want to put disconnet button to the right corner. How can i do that?
Have you tried to set layout gravity for your buttons?
LayoutParams params;
Button btnConnect = new Button(this);
params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.Left;
btnConnect.setLayoutParams(params);
...
Button btnDisconnect = new Button(this);
params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.Right;
btnConnect.setLayoutParams(params);
...
Layout gravity defines where to place a view in its parent (see Gravity and layout_gravity on Android too)
Cheers
You can use RelativeLayout instead of linear layout.
and add rules to the layout params of the
RelativeLayout.Layoutparams params = RelativeLayout.Layoutparams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
I am having issues in TableLayout, width issues. I tried searching a lot, but all I get is solution using xml. I am doing it programmatically. Here is what I am doing
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TableLayout table = new TableLayout(this);
table.setStretchAllColumns(true);
table.setShrinkAllColumns(true);
TableRow tableTitle = new TableRow(this);
tableTitle.setGravity(Gravity.CENTER_HORIZONTAL);
TableRow name = new TableRow(this);
TableRow password = new TableRow(this);
TableRow button = new TableRow(this);
TextView empty = new TextView(this);
TextView title = new TextView(this);
title.setText("Hello table layout");
title.setGravity(Gravity.CENTER_HORIZONTAL);
TextView txtUname = new TextView(this);
txtUname.setText("Username");
TextView txtPass = new TextView(this);
txtPass.setText("Password");
EditText uname = new EditText(this);
EditText pass = new EditText(this);
Button login = new Button(this);
login.setText("Login");
name.addView(txtUname);
name.addView(uname);
password.addView(txtPass);
password.addView(pass);
button.addView(empty);
button.addView(login);
table.addView(name);
table.addView(password);
table.addView(button);
table.setColumnShrinkable(0, true);
setContentView(table);
}
I want to shrink first column. What should I add in this code?
Anyone help!
You should consider moving your to layout xml file. This would make your life way easier.
To make only first column shrinkable replace setShrinkAllColumns(true)
with setColumnShrinkable(0, true).