3 Column TableLayout not centering properly in Android App - java

I have been working on this odd issue for about 6 hours now, and I have searched countlessly to no avail. I am now asking for help. I am currently trying to make a TableLayout with three columns to display texts of different lengths.
Here are some images.
Top left corner
Top Right Corner
The TableLayout is centered before I programmatically add rows to it.
public void populate(String name, String description, String grade){
row = new TableRow(this);
t1 = new TextView(this);
t1.setTextColor(getResources().getColor(R.color.colorPrimary));
t2 = new TextView(this);
t2.setTextColor(getResources().getColor(R.color.colorPrimary));
t3 = new TextView(this);
t3.setTextColor(getResources().getColor(R.color.colorPrimary));
t1.setText(name);
t2.setText(description);
t3.setText(grade);
t1.setTypeface(null, 1);
t2.setTypeface(null, 1);
t3.setTypeface(null, 1);
t1.setBackground(getResources() .getDrawable(R.drawable.cell_shape));
t2.setBackground(getResources() .getDrawable(R.drawable.cell_shape));
t3.setBackground(getResources() .getDrawable(R.drawable.cell_shape));
t1.setTextSize(15);
t2.setTextSize(15);
t3.setTextSize(15);
t1.setPadding(20, 0, 0, 0);
t2.setPadding(20, 0, 0, 0);
t3.setPadding(20, 0, 0, 0);
/*
t1.setGravity(1);
t2.setGravity(1);
t3.setGravity(1);*/
row.addView(t1);
row.addView(t2);
row.addView(t3);
table.addView(row, new
TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,
TableLayout.LayoutParams.WRAP_CONTENT));
}
//Code for title columns:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scrollbars="none"
app:layout_constraintDimensionRatio="1:1">
<HorizontalScrollView
android:id="#+id/horizontalView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dip"
android:scrollbars="horizontal|vertical">
<TableLayout
android:id="#+id/table1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:stretchColumns="*"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TableRow>
<TextView
android:width="90dp"
android:background="#drawable/cell_shape"
android:gravity="center_horizontal"
android:padding="3dip"
android:text="Name" />
<TextView
android:width="90dp"
android:background="#drawable/cell_shape"
android:gravity="center_horizontal"
android:padding="3dip"
android:text="Description" />
<TextView
android:width="90dp"
android:background="#drawable/cell_shape"
android:gravity="center_horizontal"
android:padding="3dip"
android:text="Grade" />
</TableRow>
</TableLayout>
</HorizontalScrollView>
</ScrollView>

Related

How can I change the color of my edit text which is entered by user?

I have created a signup page and I select the hint color blue now I want to change the color of text which is entered in the edit text please tell me how I can change the color of text
Here is the code of my XML file
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Signup"
android:background="#color/purple_500">
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="660dp"
android:layout_marginTop="80dp"
android:background="#drawable/linear_round"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="Please Signup..."
android:textColor="#color/white"
android:textStyle="bold"
android:textSize="29sp"
android:gravity="center"
android:layout_marginTop="16dp"
android:id="#+id/textView">
</TextView>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textColor="#color/black"
android:hint="Enter your name"
android:layout_marginTop="250dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:gravity="center"
android:textColorHint="#color/purple_500"
android:id="#+id/name"/>
<EditText
android:id="#+id/pass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:gravity="center"
android:hint="Enter your password"
android:inputType="textPassword"
android:textColor="#color/black"
android:textColorHint="#color/purple_500"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/name" />
<EditText
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:gravity="center"
android:hint="Enter your valid e-mail"
android:inputType="textEmailAddress"
android:textColor="#color/black"
android:textColorHint="#color/purple_500"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/pass" />
<EditText
android:id="#+id/number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:gravity="center"
android:hint="Enter your cell number"
android:inputType="phone"
android:textColor="#color/black"
android:textColorHint="#color/purple_500"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/email" />
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/submit"
android:layout_width="445dp"
android:layout_height="163dp"
android:layout_centerInParent="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/number"
android:layout_marginBottom="100dp"
app:lottie_autoPlay="true"
app:lottie_loop="true"
app:lottie_rawRes="#raw/submit" />
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/trial"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_centerInParent="true"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/submit"
app:lottie_autoPlay="true"
app:lottie_loop="true"
app:lottie_rawRes="#raw/trial" />
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/signup_animation"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
app:layout_constraintBottom_toTopOf="#+id/name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="70dp"
app:lottie_autoPlay="true"
app:lottie_loop="true"
app:lottie_rawRes="#raw/signup" />
</androidx.constraintlayout.widget.ConstraintLayout>
And how can I add the marquee effect on my text view? Can I make text view scrolling from right to left?
If TextView fill whole width you need to add these lines of code
<TextView
.
.
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
/>
also do not forget to set selected for TexrtView as below
textview.setSelected(true);
and if TextView does not fill width the only thing is to call this method and pass TextView to it.
Kotlin
fun addMarquee(textView: TextView) {
textView.viewTreeObserver.addOnGlobalLayoutListener(object :
ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
val pixels = textView.measuredWidth - 1
val params = textView.layoutParams
params.width = pixels
textView.layoutParams = params
textView.isSelected = true
textView.ellipsize = TextUtils.TruncateAt.MARQUEE
textView.isSingleLine = true
textView.marqueeRepeatLimit = -1
textView.viewTreeObserver.removeOnGlobalLayoutListener(this)
}
})
}
Java
public void addMarquee(TextView textView) {
textView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int pixels = textView.getMeasuredWidth() - 1;
ViewGroup.LayoutParams params = textView.getLayoutParams();
params.width = pixels;
textView.setLayoutParams(params);
textView.setSelected(true);
textView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
textView.setSingleLine(true);
textView.setMarqueeRepeatLimit(-1);
textView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
}
NOTE: this method only work when textView width is wrap_content.
textColor attribute changes color of text which user entered in.
android:textColor="#color/black"
HorizontalScrollView has one chid view, and it can be scrolled horizontally.
There's many ways to move view.
Search with keyword like
move view animation android or move view programmatically android

Android popup window - back button

I'm making application where I'm trying to add custom popup window with ability to close it when I click back button. The problem is I don't know how to add this back button. I'm attaching a file which shows what I have now and want I want to achieve.
I'm making application where I'm trying to add custom popup window with ability to close it when I click back button. The problem is I don't know how to add this back button. I'm attaching a file which shows what I have now and want I want to achieve.
image
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(LAYOUT_INFLATER_SERVICE);
popupView = inflater.inflate(R.layout.activity_transaction, null);
final PopupWindow popupWindow = new PopupWindow(popupView, 565, 760, true);
popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
Toolbar toolbar = popupView.findViewById(R.id.tool_bar);
toolbar.setTitleTextColor(Color.WHITE);
toolbar.setTitle("New transaction");
int currentDay = 1;
int currentMonth = Integer.parseInt(actualChosenMonth.getText().toString().substring(0, 2)) - 1;
int currentYear = Integer.parseInt(actualChosenMonth.getText().toString().substring(3, 7));
SimpleDateFormat simpledateformat = new SimpleDateFormat("EE");
Date date = new Date(currentYear, currentMonth, currentDay - 1);
String currentDayOfWeek = simpledateformat.format(date);
String currentDateAsString = convertDateToString(currentDay, currentMonth, currentYear, currentDayOfWeek);
chooseTransactionDate = popupView.findViewById(R.id.chooseTransactionDate);
chooseTransactionDate.setText(currentDateAsString);
chooseTransactionDate.setOnClickListener(this);
chooseTodayTomorrow = popupView.findViewById(R.id.chooseTodayTommorow);
chooseTodayTomorrow.setOnClickListener(this);
chooseTransactionType = popupView.findViewById(R.id.chooseTransactionType);
chooseTransactionType.setOnCheckedChangeListener(this);
chosenTransactionExpense = popupView.findViewById(R.id.chosenTransactionExpense);
chosenTransactionIncome = popupView.findViewById(R.id.chosenTransactionIncome);
transactionCategoryImage = popupView.findViewById(R.id.transactionCategoryImage);
chooseTransactionCategory = popupView.findViewById(R.id.chooseTransactionCategory);
ArrayAdapter<String> chooseCategoryAdapter = new ArrayAdapter<>(this.getActivity(), R.layout.popup_spinner_layout, getResources().getStringArray(R.array.list_of_expenses));
chooseTransactionCategory.setAdapter(chooseCategoryAdapter);
chooseTransactionCategory.setOnItemSelectedListener(this);
chooseTransactionAmount = popupView.findViewById(R.id.chooseTransactionAmount);
chooseTransactionAmount.requestFocus();
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
chooseTransactionName = popupView.findViewById(R.id.chooseTransactionName);
saveTransactionButton = popupView.findViewById(R.id.saveTransactionButton);
saveTransactionButton.setOnClickListener(this);
LAYOUT
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/bacgroundColorPopup"
tools:context=".TransactionActivity">
<Button
android:id="#+id/saveTransactionButton"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_below="#+id/chooseTransactionName"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#drawable/rounded_button"
android:text="#string/save_transaction"
android:textSize="15sp" />
<EditText
android:id="#+id/chooseTransactionAmount"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_below="#id/transactionCategoryImage"
android:layout_alignParentLeft="true"
android:textColor="#color/BrownGrey"
android:paddingLeft="10dp"
android:hint="Amount"
android:maxLength="5"
android:textColorHint="#color/BrownGreyBrighter"
android:inputType="numberDecimal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#drawable/custom_edit_text"
android:drawableRight="#drawable/calculator"
android:textSize="20sp" />
<EditText
android:id="#+id/chooseTransactionName"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_below="#id/chooseTransactionAmount"
android:layout_alignParentLeft="true"
android:textColor="#color/BrownGrey"
android:layout_marginTop="6dp"
android:layout_marginLeft="10dp"
android:paddingLeft="10dp"
android:hint="Notes"
android:textColorHint="#color/BrownGreyBrighter"
android:layout_marginRight="10dp"
android:background="#drawable/custom_edit_text"
android:drawableRight="#drawable/calculator"
android:textSize="20sp" />
<ImageView
android:id="#+id/transactionCategoryImage"
android:layout_margin="6dp"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="#id/chooseTransactionType"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp" />
<LinearLayout
android:id="#+id/dateButtons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<Button
android:id="#+id/chooseTransactionDate"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="62dp"
android:layout_marginRight="2dp"
android:layout_weight="1.6"
android:background="#drawable/rounded_button"
android:elevation="4dp"
android:padding="7dp"
android:text=""
android:textAllCaps="false"
android:textSize="16sp"
android:textStyle="bold" />
<Button
android:id="#+id/chooseTodayTommorow"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="2dp"
android:layout_marginTop="62dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:background="#drawable/rounded_button"
android:elevation="4dp"
android:padding="10dp"
android:text="#string/yesterday"
android:textAllCaps="false"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
<RadioGroup
android:id="#+id/chooseTransactionType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/dateButtons"
android:orientation="horizontal">
<RadioButton
android:id="#+id/chosenTransactionExpense"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="#drawable/radio_selector"
android:button="#android:color/transparent"
android:checked="true"
android:elevation="4dp"
android:gravity="center"
android:padding="7dp"
android:text="#string/expense"
android:textColor="#drawable/text_color"
android:textSize="16sp" />
<RadioButton
android:id="#+id/chosenTransactionIncome"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginLeft="1dp"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:background="#drawable/radio_selector"
android:button="#android:color/transparent"
android:elevation="4dp"
android:gravity="center"
android:padding="7dp"
android:text="#string/income"
android:textColor="#drawable/text_color"
android:textSize="16sp" />
</RadioGroup>
<Spinner
android:id="#+id/chooseTransactionCategory"
android:popupBackground="#color/bacgroundColorPopup"
android:layout_margin="6dp"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_below="#+id/chooseTransactionType"
android:layout_alignParentRight="true"
android:textAlignment="textEnd" />
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar" />
</RelativeLayout>

Fill the screen with 2 RadioButtons?

How to fill a ConstraintLayout with two radioButtons inside of a RadioGroup? I can't find a way to do it. I post here two images, one is how I want to make it look, and the other is how it looks right now with my code.
This is how it looks right now
This is how I want
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/constraintLayout6">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="#+id/radioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/radio_selector"
android:button="#color/transparent"
android:elevation="4dp"
android:padding="16dp"
android:text="Paypal"
android:textAlignment="center" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/radio_selector"
android:button="#color/transparent"
android:elevation="4dp"
android:padding="16dp"
android:text="Contrarreembolso"
android:textAlignment="center" />
</RadioGroup>
</androidx.constraintlayout.widget.ConstraintLayout>
The best thing that I could do after reading some answers on StackOverflow and trying many things is to get to this point:
I have taken this layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RadioGroup
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="#+id/radioButton"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:background="#193312"
android:elevation="4dp"
android:text="Paypal"
android:textAlignment="center" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#122B33"
android:elevation="4dp"
android:gravity="center"
android:text="Contrarreembolso"
android:textAlignment="center" />
</RadioGroup>
</androidx.constraintlayout.widget.ConstraintLayout>
And used this code to get the height of the screen and put half of it on my views, inside your activity:
#Override
protected void onStart() {
super.onStart();
//Get the height is the screen
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int height = size.y;
DisplayMetrics displayMetrics = new DisplayMetrics();
RadioButton radioButton1 = findViewById(R.id.radioButton);
RadioButton radioButton2 = findViewById(R.id.radioButton2);
//Give view half of the screen height
ViewGroup.LayoutParams params = radioButton1.getLayoutParams();
params.height = (height)/2;
ViewGroup.LayoutParams params2 = radioButton2.getLayoutParams();
params2.height = (height)/2;
}
Notice that this layout is not perfect looking, your radio buttons do spread across your device in height but they are not equal, the one thing left to do if you want your button to be equal in height is to calculate the action bar height and take it into account.

TextViews aren't appearing when created programmatically

Programmatically created TextViews aren't appearing when I run my app. I am trying to download messages and the person that sent them from Firebase and display them in separate TextViews with the sender TextView on top.
Sorry for the lack of information, but I don't know what I've done wrong.
db.collection("messagesIT")
.orderBy("Timesent", Query.Direction.DESCENDING)
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot>task) {
if (task.isSuccessful()) {
ArrayList<MessageObj> messages = new ArrayList<>();
for (QueryDocumentSnapshot document : task.getResult())
String message = document.getString("Message");
String sender = document.getString("Sender");
Timestamp timesent = document.getTimestamp("Timesent");
MessageObj mess = new MessageObj(message, sender, timesent);
messages.add(mess);
Log.d(TAG, "m: " + mess.getMessage());
Log.d(TAG, "s: " + mess.getSender());
Log.d(TAG, "t:" + mess.getTimesent());
showToast("b");
}
TableLayout tl = findViewById(R.id.TableLayoutSubjectGroupPage);
//reading and displaying messages
for (int i = 0; i < messages.size(); i++) {
TableRow trMessage = new TableRow(SubjectGroupPage.this);
TableRow trSender = new TableRow(SubjectGroupPage.this);
TextView txtSender = new TextView(SubjectGroupPage.this);
TextView txtMessage = new TextView(SubjectGroupPage.this);
Log.d(TAG, "uv8iygv iyrtgcvfi8ytgvcrcccccccccccccccccccccccccccc");
LinearLayout.LayoutParams Weight = new LinearLayout.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 1);
txtSender.setText(messages.get(i).getSender());
txtSender.setLayoutParams(Weight);
txtMessage.setText(messages.get(i).getMessage());
txtMessage.setLayoutParams(Weight);
txtMessage.setTextSize(20);
txtMessage.setTextColor(Color.parseColor("#000000"));
if (messages.get(i).getSender().equals(currentUser.getUid())) {
txtMessage.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END);
txtSender.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END);
} else {
txtMessage.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
txtSender.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
}
//creating message display
trSender.addView(txtSender);
trMessage.addView(txtMessage);
tl.addView(trSender);
tl.addView(trMessage);
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
showToast("There has been an error, please try again later.");
Log.d(TAG, "Error: " + e);
}
});
Layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/txtUsernameAdminStudentPage"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SubjectGroupPage">
<ScrollView
android:id="#+id/scrollviewMessages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="#+id/linearLayoutMessageInput"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<TableLayout
android:id="#+id/TableLayoutSubjectGroupPage"
android:layout_width="match_parent"
android:layout_height="wrap_content">
//these were tests for how I want the TextViews
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txtExample"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView"
android:textColor="#000000"
android:textSize="20dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView"
android:textAlignment="textEnd" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txtExample2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView"
android:textAlignment="textEnd"
android:textColor="#000000"
android:textSize="20dp"
tools:layout_editor_absoluteY="675dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView"
android:textAlignment="textEnd" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView"
android:textAlignment="textEnd"
android:textColor="#000000"
android:textSize="20dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView"
android:textColor="#000000"
android:textSize="20sp" />
</TableRow>
</TableLayout>
</ScrollView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
What the page looks like when run
What it should look like when run
Database
I think you forget to add layout params for table row.
BTW you should use recycler view with two view holders one for sender and 2nd for the receiver.
checkout if you want to improve your code
How to create RecyclerView with multiple view type?
or
https://medium.com/#droidbyme/android-recyclerview-with-multiple-view-type-multiple-view-holder-af798458763b
You explictly set the width of your TextViews to zero, using:
public LinearLayout.LayoutParams (int width, int height, float weight)
...where (see LinearLayout.LayoutParams):
int: the width, either ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT or a fixed size in pixels
MATCH_PARENT has a value of -1 and WRAP_CONTENT a value of -2. So this code sets a fixed width of 0:
LinearLayout.LayoutParams Weight = new LinearLayout.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 1)
Instead you should use:
TableLayout.LayoutParams tableParams_forRow = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT);
and
TableRow.LayoutParams rowParams_forTextView = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
See this answer, for an example

Android make whole tablerow clickable

I have a litte problem.
I want to create a Table with clickable TableRows.
Most of this works fine, but the OnClickListener does not work for the whole Tablerow.
The TableRow is not clickable where the vertical Scrollview Element is.
Is there a way to set a clicklistener for the whole Row?
Here a code snippet from the activity:
for (int i = 0; i <anzahl; i++) {
TableRow row = (TableRow) LayoutInflater.from(this).inflate(R.layout.pnunterhaltungslist, null);
TextView UnterhaltungsBetreff = (TextView) row.findViewById(R.id.Unterhaltungsbetreff);
TextView UnterhaltungsBeteiligte = (TextView) row.findViewById(R.id.Unterhaltungvon);
TextView UnterhaltungsUhrzeit = (TextView) row.findViewById(R.id.Uhrzeit);
TextView UnterhaltungsDatum = (TextView) row.findViewById(R.id.Datum);
row.setId(1000+i);
row.setClickable(true);
row.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(PNs.this, Main.class);
PNs.this.startActivity(myIntent);
}
});
UnterhaltungsBetreff.setText(Unterhaltungslist.get(i * 4 + 4) + " ");
UnterhaltungsBeteiligte.setText(Unterhaltungslist.get(i*4+2)+" ");
Date Datum = new Date(Integer.parseInt(Unterhaltungslist.get(i*4+3))*1000L);
DateFormat Datumf = new SimpleDateFormat("dd.MM.yy");
DateFormat Zeitf = new SimpleDateFormat("HH:mm");
UnterhaltungsUhrzeit.setText(Zeitf.format(Datum));
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
long todayInMillis = c.getTimeInMillis();
if(todayInMillis - (Integer.parseInt(Unterhaltungslist.get(i*4+3))*1000L)<= 0){
UnterhaltungsDatum.setText("Heute");
}
else if ((Integer.parseInt(Unterhaltungslist.get(i*4+3))*1000L) - (todayInMillis- 86400000) >= 0){
UnterhaltungsDatum.setText("Gestern");
}
else{
UnterhaltungsDatum.setText(Datumf.format(Datum));
}
Unterhalttable.addView(row,i);
}
}
Here the xml:
<?xml version="1.0" encoding="utf-8"?>
<TableRow
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:drawable/list_selector_background"
android:id="#+id/one">
<RelativeLayout
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Unterhaltungrelativlayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="12:00"
android:id="#+id/Uhrzeit"
android:layout_alignTop="#+id/horizontalScrollView3"
android:layout_alignParentRight="true"
android:layout_marginRight="8dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="heute"
android:id="#+id/Datum"
android:layout_below="#+id/Uhrzeit"
android:layout_alignParentRight="true"
android:layout_marginRight="8dp"
android:layout_marginTop="5dp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ff363132"
android:id="#+id/borderline"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="8dp"
android:layout_below="#+id/horizontalScrollView3" />
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/horizontalScrollView3"
android:layout_marginTop="8dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toEndOf="#+id/Uhrzeit"
android:scrollbars="none"
android:layout_marginLeft="8dp"
android:layout_toLeftOf="#+id/Uhrzeit"
android:layout_marginRight="8dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/relativlayout1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Unterhaltung-Beteiligte"
android:id="#+id/Unterhaltungvon"
android:layout_gravity="left|bottom"
android:layout_below="#+id/Unterhaltungsbetreff"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Unterhaltungs-Betref blabasdkasnfdsadhfhsadfsadgsadögh"
android:id="#+id/Unterhaltungsbetreff"
android:layout_gravity="left|top"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</HorizontalScrollView>
<View
android:layout_width="15dp"
android:layout_height="200dp"
android:background="#drawable/verlaufpn"
android:id="#+id/borderline2"
android:layout_toStartOf="#+id/Uhrzeit"
android:paddingTop="-2dp"
android:paddingBottom="-3dp"
android:layout_above="#+id/borderline"
android:layout_alignRight="#+id/horizontalScrollView3"/>
</RelativeLayout>
</TableRow>
Picture : (green clickable / red not clickable)
http://abload.de/img/screenshot_2014-09-17lpu6s.jpg
Try
<TableRow
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:drawable/list_selector_background"
android:clickable="true"
android:onClick="onTableRowClick"
android:id="#+id/one">
Then in your activity, create the function "onTableRowClick" and it will be called when the layout is clicked.

Categories

Resources