TextViews aren't appearing when created programmatically - java

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

Related

Layout in a loop Android Studio,

Hello i am doing a little web scraping project to learn in android studio.
What my app does Right now is that it ask you to fill a field with a query then when you push a button it takes your query and it changes to a new view and searches in that page all possible instances of it and changes to a new view and shows you the information, right until this point i got it, now the problem is to show the results and how to do it because with just 1 result it shows the info with 0 problems but i want to show more than 1 results. so it shows
This image but one bellow the other until there are no more results
Here is the layout code that i want to put in a for loop
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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=".HomeActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/imgViewEbay"
android:scaleType="fitCenter"
android:layout_width="match_parent"
android:layout_height="200dp"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textStyle="bold"
android:layout_weight="3"
android:text="Nombre"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_weight="1"
android:id="#+id/txtTitleEbay"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textStyle="bold"
android:layout_weight="3"
android:text="Precio"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_weight="1"
android:textColor="#color/design_default_color_on_primary"
android:id="#+id/txtPrecioEbay"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textStyle="bold"
android:layout_weight="3"
android:text="Localizacion"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_weight="1"
android:textColor="#color/design_default_color_on_primary"
android:id="#+id/txtlocaEbay"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textStyle="bold"
android:layout_weight="3"
android:text="Tipos de pago"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_weight="1"
android:textColor="#color/design_default_color_on_primary"
android:id="#+id/txtpagoebay"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textStyle="bold"
android:layout_weight="3"
android:text="Condition"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_weight="1"
android:id="#+id/txtDescripcionebay"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textStyle="bold"
android:layout_weight="3"
android:text="Tiempo"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_weight="1"
android:textColor="#color/design_default_color_on_primary"
android:id="#+id/txttiempoEbay"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/txtRatingebay"
android:gravity="center"
android:textStyle="bold"
android:layout_weight="3"
android:text="4.5"
android:textSize="28sp"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RatingBar
style="#style/Widget.AppCompat.RatingBar.Indicator"
android:id="#+id/rating_barebay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/btnbackebay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="Volver" />
<Button
android:id="#+id/btnLinkebay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="Link" />
</LinearLayout>
</LinearLayout>
</ScrollView>
And here is my attempt to do it :
```
Intent intent = getIntent();
String busqueda = intent.getStringExtra("busqueda");
final TextView txtitle = findViewById(R.id.txtTitleEbay);
final TextView txtprice = findViewById(R.id.txtPrecioEbay);
final ImageView img = findViewById(R.id.imgViewEbay);
final TextView txtlocation = findViewById(R.id.txtlocaEbay);
final TextView txtPayments = findViewById(R.id.txtpagoebay);
final RatingBar ratingBar = findViewById(R.id.rating_barebay);
final TextView txtRating = findViewById(R.id.txtRatingebay);
final TextView txtCondtion = findViewById(R.id.txtDescripcionebay);
final TextView txttime = findViewById(R.id.txttiempoEbay);
try {
for (int i = 0; i < buscadorebay(busqueda).length; i++) {
String url= buscadorebay(busqueda)[i];
new Thread(new Runnable()
{
final EbayProduct product = new EbayProduct();
#Override
public void run()
{
try
{
Document doc = Jsoup.connect(url)
.timeout(6000).get();
Elements image = doc.select("#icImg");
Elements title = doc.select("#vi-lkhdr-itmTitl");
Elements price = doc.select("#vi-mskumap-none #prcIsum");
Elements location = doc.select("#SRPSection [class='ux-textspans ux-textspans--SECONDARY']");
Elements payments = doc.select("#SRPSection [role='img']");
ArrayList<String> paymentMethods = new ArrayList<>();
for (Element payment : payments) {
paymentMethods.add(payment.attr("aria-label"));
}
Elements ratingEle = doc.select("#LeftSummaryPanel [itemprop='ratingValue']");
String ele = ratingEle.attr("content");
Elements condition1 = doc.select("#viTabs_0_is .ux-layout-section__row .ux-labels-values__values .ux-labels-values__values-content .ux-textspans");
Elements condtion2 = doc.select("#vi-desc-maincntr");
Elements Time = doc.select("#vi-cdown_timeLeft");
product.setTitle(title.text());
product.setImagUrl(image.attr("src"));
product.setPrice(price.text());
product.setLocation(location.text().replace("See detailsfor shipping Located in:", "").replace("See details- for more information about returns Learn moreLearn more about eBay global shipping program", ""));
product.setPayment(paymentMethods.toString());
product.setRating(Float.parseFloat(ratingEle.text()));
product.setCondition(condition1.text().replace(" Read moreabout the condition", "").replaceAll("[()]", "").replaceAll("[.]", ""));
product.setTime(Time.text());
}catch (Exception ex){}
runOnUiThread(new Runnable()
{
#Override
public void run()
{
txtitle.setText(product.getTitle());
Picasso.get()
.load(product.getImagUrl())
.into(img);
txtprice.setText(product.getPrice());
txtlocation.setText(product.getLocation());
txtPayments.setText(product.getPayment());
txtCondtion.setText(product.getCondition());
ratingBar.setRating(product.getRating());
txtRating.setText(product.getRating()+"");
txttime.setText(product.getTime());
}
});
}
}).start();
}} catch (IOException e) {
e.printStackTrace();
}
As you Can see i try to enter the view in a loop but i guess it is wrong since without the for loop and with just the normal url it goes without a problem. How then to make it so that it takes the info and then puts the views one below the other?
If you guys could help i would be thankful.

Relative Layout adding views programmatically one below other not aligning correctly

I am trying to add Text views programmatically to Relative layout parent one below other. However, the first view is not aligning correctly while the rest of the view got aligned correctly. See attached image.
My code:
private void setupQuestionChoices(int position) {
question.setText(questionList.get(position).question());
choicesTextViewsList.clear();
for (int i = 0; i < questionList.get(position).choices().size(); i++) {
TextView textView = new TextView(getActivity());
textView.setBackgroundResource(R.drawable.qa_button_background);
textView.setMinWidth(300);
textView.setGravity(Gravity.CENTER);
textView.setText(questionList.get(position).choices().get(i));
textView.setId(i);
RelativeLayout.LayoutParams layoutParams =
new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
if (i == 0) {
layoutParams.addRule(RelativeLayout.BELOW, question.getId());
} else {
layoutParams.addRule(RelativeLayout.BELOW, choicesTextViewsList.get(i - 1).getId());
}
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
layoutParams.setMargins(20, 20, 20, 20);
relativeLayout.addView(textView, layoutParams);
choicesTextViewsList.add(textView);
choicesTextViewsList.get(i).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
answerSelected = true;
}
});
}
}
Relative layout xml where I add my views to:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:id="#+id/relative_parent" //here is where I add views to
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="40dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal"
android:textColor="#color/colorPrimary"
android:textSize="30sp"
android:textStyle="bold" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:layout_marginBottom="30dp"
android:gravity="center">
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/button_back"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="30dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="30dp"
android:background="#drawable/rectangle_border"
android:text="Back"
android:textColor="#android:color/darker_gray" />
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/button_next"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="30dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="30dp"
android:background="#drawable/rectangle_border"
android:text="Next"
android:textColor="#android:color/darker_gray" />
</RelativeLayout>
</RelativeLayout>
</ScrollView>
I read some posts related to this issue and followed the guidelines from them but that did not help.
How to lay out Views in RelativeLayout programmatically?
Create a new TextView programmatically then display it below another TextView
I was using the index of the for loop as id for the textViews which I changed to make Android generate Id for it as below and it is working fine now as expected.
I changed the line
textView.setId(i)
to
textView.setId(ViewGroup.generateViewId())
Thanks.

OnClickListener only one function is fired up

This is my view where I have Monday-Sunday tables with textviews where I have dates
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#drawable/background"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingTop="10dp">
<TextView
android:id="#+id/monday"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="15dp"
android:layout_weight="1"
android:gravity="center|top"
android:text="#string/monday"
android:textAlignment="center"
android:textColor="#color/textColor"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/mondayTimesList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/orangeFlat"
android:padding="5dp"
android:text="Period 1"
android:textColor="#color/primary" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:hint="From"
android:padding="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text=":" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:padding="10dp"
android:hint="To" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/orangeFlat"
android:padding="5dp"
android:text="Period 2"
android:textColor="#color/primary" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:padding="10dp"
android:hint="From" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text=":" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:padding="10dp"
android:hint="To" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/orangeFlat"
android:padding="5dp"
android:text="Period 3"
android:textColor="#color/primary" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:padding="10dp"
android:hint="From" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text=":" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:padding="10dp"
android:hint="To" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/orangeFlat"
android:padding="5dp"
android:text="Period 4"
android:textColor="#color/primary" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:padding="10dp"
android:hint="From" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text=":" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:padding="10dp"
android:hint="To" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/orangeFlat"
android:padding="5dp"
android:text="Period 5"
android:textColor="#color/primary" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:padding="10dp"
android:hint="From" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text=":" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:padding="10dp"
android:hint="To" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/primary"
>
<LinearLayout
android:id="#+id/back"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="#drawable/arrow"
android:rotation="180"
android:layout_weight="1"
android:onClick="backToHomeScreen"
android:background="#color/orangeFlat"
android:paddingVertical="10dp"/>
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="#drawable/save"
android:layout_weight="1"
android:onClick="saveAsTemplate"
android:background="#color/orangeFlat"
android:paddingVertical="10dp"/>
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="#drawable/update"
android:layout_weight="1"
android:onClick="updateTimming"
android:background="#color/orangeFlat"
android:paddingVertical="10dp"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
Here is my activity for that view
public class TimesheetActivity extends AppCompatActivity {
TextView selectedTextView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timming_list);
Intent data = getIntent();
String times = data.getStringExtra("Times");
int counter = 1;
counter = fillFields(R.id.sundayTimesList, times, counter);
counter = fillFields(R.id.mondayTimesList, times, counter);
counter = fillFields(R.id.tuesdayTimesList, times, counter);
counter = fillFields(R.id.wednesdayTimesList, times, counter);
counter = fillFields(R.id.thursdayTimesList, times, counter);
counter = fillFields(R.id.fridayTimesList, times, counter);
fillFields(R.id.saturdayTimesList, times, counter);
}
private int fillFields(int id, String data, int counter){
ViewGroup timeList = findViewById(id);
for(int i = 0; i < 5; i++) {
ViewGroup firstModayTimeListChild = (ViewGroup) timeList.getChildAt(i);
ViewGroup period = (ViewGroup) firstModayTimeListChild.getChildAt(1);
TextView periodFromText = (TextView) period.getChildAt(0);
TextView periodToText = (TextView) period.getChildAt(2);
periodFromText.setClickable(true);
periodFromText.setFocusableInTouchMode(false);
periodFromText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
periodToText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
periodFromText.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
Toast.makeText(getApplicationContext(), "WORKS!", Toast.LENGTH_SHORT).show();
setDate(view);
}
});
periodToText.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
setDate(view);
}
});
// Log.i("FOCUSABLE ", "" + periodFromText.isFocusable());
// Log.i("FOCUSABLE TOUCH MODE ", "" + periodFromText.isFocusableInTouchMode());
// Log.i("IS CLICKABLE ", "" + periodFromText.isClickable());
periodFromText.setText(
String.format(
data.charAt(++counter) + "" +
data.charAt(++counter) + ":" +
data.charAt(++counter) + "" +
data.charAt(++counter)
));
periodToText.setText(
String.format(
data.charAt(++counter) + "" +
data.charAt(++counter) + ":" +
data.charAt(++counter) + "" +
data.charAt(++counter)
));
}
return counter;
}
public void setDate(View view){
selectedTextView = (TextView) view;
view.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
showDialog(0);
}
});
}
protected Dialog onCreateDialog(int id){
Calendar now = Calendar.getInstance();
TimePickerDialog timePickerDialog = new TimePickerDialog(this, TimePickerDialog.THEME_HOLO_DARK, timePickerListener, now.get(Calendar.HOUR_OF_DAY), now.get(Calendar.MINUTE), true);
return timePickerDialog;
}
private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker timePicker, int hour, int minute) {
selectedTextView.setText(String.format("%02d:%02d", hour, minute));
}
};
}
So my problem is when I touch/click textview first time "toast" fired up datepicker is not showing up, but after that every time when I touch/click I can see datepicker? Why datepicker is not showing up when I press textview first time? How to make it to works?
vikas kumar thanks that help
The reason is for the first time when you click it sets up click listener as you have set in setDate(View view); after that, it listens for subsequent calls. so you need to change the method a bit.
public void setDate(View view){
selectedTextView = (TextView) view;
showDialog(0);
}
or you can just do this at the site of onClick also for date pickers.
periodFromText.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
Toast.makeText(getApplicationContext(), "WORKS!", Toast.LENGTH_SHORT).show();
selectedTextView = (TextView) view;
showDialog(0);
}
});
this doesn't require you to set two onClickListeners for the same TextView.
Modify your setDate() method to below
public void setDate(View view){
selectedTextView = (TextView) view;
showDialog(0);
}

implement setOnFocusChangeListener for multiple CustomAutoCompleteTextView in Android [duplicate]

And I set adapter to each CustomAutocompleteTextView. User select Entry type from the list of items. But if user enter that is not present in the list, I wanted to make that field empty.
For that I wrote code like this. It's Working fine. When I focus on next field this method is calling.
actvEntryCategory.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
String str = actvEntryCategory.getText().toString();
for (int i=0;i<EntryTypeAdapter.getCount();i++)
{
String temp = EntryTypeAdapter.getItem(i).toString();
if (str.compareTo(temp) == 0){
return;
}
}
actvEntryCategory.setText("");
// actvEntryCategory.setError("Please select valid Entry Type");
}
});
Now, user have to select Station Name from the list of Items, if not I wanted to make this field null.
For that I have written code like this. But this method is not calling when I focus on Next field.
actvDivName.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
public void onFocusChange(View view, boolean b) {
try{
String str = actvDivName.getText().toString();
for (int i=0;i<QRDivArrayAdapter.getCount();i++)
{
String temp = QRDivArrayAdapter.getItem(i).toString();
if (str.compareTo(temp) == 0){
return;
}
}
actvDivName.setText("");
// actvEntryCategory.setError("Please select valid Entry Type");
}catch(Exception focusfordivision)
{
Log.d("eEmp/Resume", "Error raised due to " + focusfordivision.toString());
}
}
});
This is my XML code.
<LinearLayout
android:id="#+id/LLPlaceDetails"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="vertical"
android:weightSum="1">
<android.support.v7.widget.CardView
android:id="#+id/zone_card_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="#dimen/activity_horizontal_margin"
android:layout_marginLeft="#dimen/activity_custom_margin"
android:layout_marginRight="#dimen/activity_custom_margin"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:layout_weight="0.54"
card_view:cardElevation="#dimen/cardview_default_elevation"
card_view:contentPadding="10dp">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusableInTouchMode="false">
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_zone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_weight="1">
<com.efftronics.android.eEmployee.Common.UI.CustomAutoCompleteTextView
android:id="#+id/actventry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="11"
android:hint="#string/Entry_Type"
android:inputType="textCapWords|textMultiLine"
android:maxLength="250"
android:theme="#style/AutocompleteTheme" />
</android.support.design.widget.TextInputLayout>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_Division"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_weight="1">
<com.efftronics.android.eEmployee.Common.UI.CustomAutoCompleteTextView
android:id="#+id/actvDivName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/station"
android:inputType="textCapWords|textMultiLine"
android:maxLength="250"
app:hintTextAppearance="#style/AutocompleteTheme"
/>
</android.support.design.widget.TextInputLayout>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_ProductType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_weight="1">
<com.efftronics.android.eEmployee.Common.UI.CustomAutoCompleteTextView
android:id="#+id/actvProductType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/product_type"
android:inputType="textCapWords|textMultiLine"
android:maxLength="250"
app:hintTextAppearance="#style/AutocompleteTheme" />
</android.support.design.widget.TextInputLayout>
</TableRow>
Why it is not calling?
Any help would be appreciated.

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