Android make whole tablerow clickable - java

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.

Related

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>

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

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);
}

Moving imageview in XML causes crash

I am making some changes to an XML layout side of my application. There is a ImageView I am trying to bring to the front yet when I do the app crashes and I can seem to tell why.
Working XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/place_distance"
android:layout_width="wrap_content"
android:layout_height="14dip"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:layout_margin="5dip"
android:gravity="right"
android:text="28-12-1920 22:22"
android:textColor="#color/white"
android:textSize="12sp" />
<ImageView
android:id="#+id/place_img"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:scaleType="centerCrop"
android:src="#drawable/icon_4860_1" />
<LinearLayout
android:id="#+id/wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/place_img"
android:layout_alignRight="#+id/place_distance"
android:layout_below="#+id/place_distance"
android:layout_marginTop="5dp"
android:background="#drawable/chatbox" >
<TextView
android:id="#+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="43dp"
android:padding="9dp"
android:paddingLeft="10dip"
android:text="Hello bubbles!"
android:textColor="#android:color/white"
android:textColorLink="#android:color/holo_blue_dark"
android:textIsSelectable="true" />
</LinearLayout>
<TextView
android:id="#+id/place_name"
android:textIsSelectable="true"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/place_distance"
android:layout_toLeftOf="#+id/place_distance"
android:layout_toRightOf="#+id/place_img"
android:text="Tweeked"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/gray"
android:textSize="11dp"
android:textStyle="bold" />
<ImageView
android:id="#+id/pe_profile_pic"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_margin="2dp"
android:layout_alignBottom="#+id/place_img"
android:layout_alignRight="#+id/place_img"
android:src="#drawable/offline" />
XML that crashes:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/place_distance"
android:layout_width="wrap_content"
android:layout_height="14dip"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:layout_margin="5dip"
android:gravity="right"
android:text="28-12-1920 22:22"
android:textColor="#color/white"
android:textSize="12sp" />
<LinearLayout
android:id="#+id/wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/place_img"
android:layout_alignRight="#+id/place_distance"
android:layout_below="#+id/place_distance"
android:layout_marginTop="5dp"
android:background="#drawable/chatbox" >
<TextView
android:id="#+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="43dp"
android:padding="9dp"
android:paddingLeft="10dip"
android:text="Hello bubbles!"
android:textColor="#android:color/white"
android:textColorLink="#android:color/holo_blue_dark"
android:textIsSelectable="true" />
<!-- android:autoLink="web|all" -->
</LinearLayout>
<ImageView
android:id="#+id/place_img"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:scaleType="centerCrop"
android:src="#drawable/icon_4860_1" />
<TextView
android:id="#+id/place_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/place_distance"
android:layout_toLeftOf="#+id/place_distance"
android:text="Tweeked"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/gray"
android:textIsSelectable="true"
android:textSize="11dp"
android:textStyle="bold" />
<ImageView
android:id="#+id/pe_profile_pic"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignBottom="#+id/place_img"
android:layout_alignRight="#+id/place_img"
android:layout_margin="2dp"
android:src="#drawable/offline" />
Im just trying to bring the #place_img to the front of the #wrapper.
java.lang.NullPointerException
at com.peekatucorp.peekatu.DiscussArrayAdapter.getView(DiscussArrayAdapter.java:172)
DiscussArrayAdapter.java:
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(type==1 || type==3)
row = inflater.inflate(R.layout.listitem_discuss, parent, false);
else if(type==4 || type==5)
row = inflater.inflate(R.layout.listitem_users, parent, false);
else
row = inflater.inflate(R.layout.listitem_messages, parent, false);
}
//
final OneComment coment = getItem(position);
userComment = (TextView) row.findViewById(R.id.comment);
userImage = (ImageView) row.findViewById(R.id.place_img);
userName = (TextView) row.findViewById(R.id.place_name);
userOnlineImage = (ImageView) row.findViewById(R.id.pe_profile_pic);
commentDate = (TextView) row.findViewById(R.id.place_distance);
userComment.setText(coment.comment);
ImageLoader imageLoader = ImageLoader.getInstance();
// imageLoader = ImageLoader.getInstance();
// imageLoader.init(ImageLoaderConfiguration.createDefault(convertView.getContext()));
imageLoader.displayImage(coment.image, userImage);
userImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// it was the 1st button
final TabInfo tab = navact.getCurrentTabInfo();
final ProfileFragment fragment = new ProfileFragment().setUser(coment.userid).setNAV(navact);
// fragment.setText(characters[position]);
// second, you push the fragment. It becomes visible and the up button is
// shown
navact.pushFragment(tab, fragment);
/*
Intent i = new Intent(context, ProfileActivity.class);
i.putExtra("userID", coment.userid);
// Create the view using FirstGroup's LocalActivityManager
View view = ChatTabGroup.group.getLocalActivityManager()
.startActivity("show profile", i
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
// Again, replace the view
ChatTabGroup.group.replaceView(view);*/
}
});
if(coment.online.equalsIgnoreCase("1"))
userOnlineImage.setImageResource(R.drawable.online);
else
userOnlineImage.setImageResource(R.drawable.offline);
if(coment.gender.equalsIgnoreCase("M"))
userName.setTextColor(Color.parseColor("#878ff4"));
else if(coment.gender.equalsIgnoreCase("F"))
userName.setTextColor(Color.parseColor("#f487d6"));
else
userName.setTextColor(Color.parseColor("#969696"));
userName.setText(coment.username);
commentDate.setText(coment.time);
if(type==4){
commentDate = (TextView) row.findViewById(R.id.textView1);
SharedPreferences preferences = getContext().getSharedPreferences("MyPreferences", getContext().MODE_PRIVATE);
double distance = distFrom(Double.parseDouble(getItem(position).time.split(",")[0]),Double.parseDouble(getItem(position).time.split(",")[1]),
Double.parseDouble(preferences.getString("user_lat", "0.0")),Double.parseDouble(preferences.getString("user_lng", "0.0"))
);
commentDate.setText(""+String.format("%.2f", (distance*0.62))+"miles");
}
if(type==1 || type==3){
wrapper = (LinearLayout) row.findViewById(R.id.wrapper);
wrapper.setGravity(coment.left ? Gravity.LEFT : Gravity.RIGHT);
}else{
}
//
return row;
}
Thank you.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/place_distance"
android:layout_width="wrap_content"
android:layout_height="14dip"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:layout_margin="5dip"
android:gravity="right"
android:text="28-12-1920 22:22"
android:textColor="#color/white"
android:textSize="12sp" />
<LinearLayout
android:id="#+id/wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/place_img"
android:layout_alignRight="#+id/place_distance"
android:layout_below="#+id/place_distance"
android:layout_marginTop="5dp"
android:background="#drawable/chatbox" >
<TextView
android:id="#+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="43dp"
android:padding="9dp"
android:paddingLeft="10dip"
android:text="Hello bubbles!"
android:textColor="#android:color/white"
android:textColorLink="#android:color/holo_blue_dark"
android:textIsSelectable="true"
android:typeface="serif"
android:autoLink="web|all"
/>
<!-- android:autoLink="web|all" -->
</LinearLayout>
<ImageView
android:id="#+id/place_img"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:scaleType="centerCrop"
android:src="#drawable/icon_4860_1" />
<ImageView
android:id="#+id/pe_profile_pic"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignBottom="#+id/place_img"
android:layout_alignRight="#+id/place_img"
android:layout_margin="2dp"
android:src="#drawable/offline" />
<TextView
android:id="#+id/place_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/wrapper"
android:layout_toRightOf="#+id/place_img"
android:text="Tweeked"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/gray"
android:textIsSelectable="true"
android:textSize="14sp"
android:textStyle="bold"
android:typeface="normal" />

Returning values from a custom dialog view

I'm trying to make a custom dialog for my android application. I get null pointer exceptions when I attempt to access the fields from the 'ok' button method. It seems like my fields fade from existence as soon as the onclick listener fires. Is there a better way to do this?
addchars.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="#+id/scrollBox">
<RelativeLayout
android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="#+id/addChars">
<AutoCompleteTextView android:layout_height="wrap_content" android:id="#+id/inCharName" android:hint="#string/txtCharacterNameBox" android:layout_below="#+id/textView1" android:layout_centerHorizontal="true" android:layout_width="fill_parent">
<requestFocus></requestFocus>
</AutoCompleteTextView>
<TextView android:id="#+id/textView2" android:layout_height="wrap_content" android:gravity="center" android:layout_width="wrap_content" android:text="#string/txtHP" android:layout_below="#+id/inCharName" android:layout_alignParentLeft="true" android:layout_marginLeft="22dp"></TextView>
<TextView android:id="#+id/textView3" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="#string/txtTempHP" android:layout_below="#+id/inCharName" android:layout_alignParentRight="true" android:layout_marginRight="57dp"></TextView>
<EditText android:id="#+id/inHP" android:digits="-0123456789" android:minEms="3" android:gravity="center" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="3" android:layout_width="wrap_content" android:layout_below="#+id/textView2" android:layout_alignLeft="#+id/textView2">
</EditText>
<EditText android:id="#+id/inTempHP" android:digits="0123456789" android:minEms="3" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="3" android:layout_width="wrap_content" android:layout_alignBaseline="#+id/inHP" android:layout_alignBottom="#+id/inHP" android:layout_alignLeft="#+id/textView3"></EditText>
<TextView android:id="#+id/TextView01" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="#+id/inHP" android:layout_alignLeft="#+id/inHP" android:text="#string/txtInitRoll"></TextView>
<TextView android:id="#+id/TextView02" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="#+id/inTempHP" android:layout_alignLeft="#+id/inTempHP" android:text="#string/txtInitBonus"></TextView>
<EditText android:digits="0123456789" android:minEms="3" android:layout_height="wrap_content" android:gravity="center" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_below="#+id/TextView01" android:layout_alignLeft="#+id/TextView01" android:id="#+id/inInitRoll"></EditText>
<EditText android:digits="0123456789" android:minEms="3" android:layout_height="wrap_content" android:gravity="center" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_below="#+id/TextView02" android:layout_alignLeft="#+id/TextView02" android:id="#+id/inInitBonus"></EditText>
<TextView android:id="#+id/TextView03" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="#string/txtAC" android:layout_below="#+id/inInitRoll" android:layout_alignRight="#+id/textView2"></TextView>
<EditText android:id="#+id/inAC" android:digits="0123456789" android:gravity="center" android:minEms="3" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_below="#+id/TextView03" android:layout_alignLeft="#+id/TextView03"></EditText>
<TextView android:id="#+id/TextView04" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_above="#+id/inAC" android:layout_alignLeft="#+id/inInitBonus" android:text="#string/txtFortitude"></TextView>
<EditText android:digits="0123456789" android:gravity="center" android:minEms="3" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_below="#+id/TextView04" android:layout_alignLeft="#+id/TextView04" android:id="#+id/inFort"></EditText>
<TextView android:id="#+id/TextView06" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="#+id/inFort" android:layout_alignLeft="#+id/inFort" android:text="#string/txtWill"></TextView>
<TextView android:id="#+id/TextView05" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="#+id/inAC" android:layout_alignLeft="#+id/inAC" android:text="#string/txtRef"></TextView>
<EditText android:digits="0123456789" android:gravity="center" android:minEms="3" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_below="#+id/TextView05" android:layout_alignLeft="#+id/inInitRoll" android:id="#+id/inReflex"></EditText>
<EditText android:id="#+id/inWill" android:digits="0123456789" android:gravity="center" android:minEms="3" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_above="#+id/chkRoll" android:layout_alignLeft="#+id/TextView06"></EditText>
<CheckBox android:text="#string/txtRollBox" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/chkRoll" android:layout_below="#+id/inReflex" android:layout_alignLeft="#+id/TextView05" android:layout_alignParentRight="true"></CheckBox>
</RelativeLayout>
</ScrollView>
And this is my alertdialog:
addBtn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
//this block is to prep the layout for the alert dialog.
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = inflater.inflate(R.layout.addchars, (ViewGroup) findViewById(R.id.scrollBox));
//taking this part out to try a better way.
new AlertDialog.Builder(initiative.this)
.setTitle("Add a Character")
.setView(addView)
.setPositiveButton("Ok", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton)
{
//ui controls
AutoCompleteTextView characterName = (AutoCompleteTextView) findViewById(R.id.inCharName);
EditText hp = (EditText) findViewById(R.id.inHP);
EditText tempHp = (EditText) findViewById(R.id.inTempHP);
EditText initRoll = (EditText) findViewById(R.id.inInitRoll);
EditText initBonus = (EditText) findViewById(R.id.inInitBonus);
EditText ac = (EditText) findViewById(R.id.inAC);
EditText fort = (EditText) findViewById(R.id.inFort);
EditText reflex = (EditText) findViewById(R.id.inReflex);
EditText will = (EditText) findViewById(R.id.inWill);
CheckBox rollInit = (CheckBox) findViewById(R.id.chkRoll);
//variables for various checks
entriesOk=false;
needToRoll=false;
if(characterName.getText().length() > 0)
{
holdCharacterName = characterName.getText().toString();
if(hp.getText().length() > 0)
{
holdHp = hp.getText().toString();
if(initBonus.getText().length() > 0)
{
holdInitBonus = initBonus.getText().toString();
if(ac.getText().length() > 0)
{
holdAc = ac.getText().toString();
if(fort.getText().length() > 0)
{
holdFort = fort.getText().toString();
if(reflex.getText().length() > 0)
{
holdReflex = reflex.getText().toString();
if(will.getText().length() > 0)
{
holdWill = will.getText().toString();
if(tempHp.getText().length() == 0)
{
holdTempHp = "0";
tempHp.setText("0");
}
else
{
holdTempHp = tempHp.getText().toString();
}
if(rollInit.isChecked())
{
entriesOk = true;
needToRoll = true;
}
else
{
if(initRoll.getText().length() > 0)
{
holdInitRoll = initRoll.getText().toString();
entriesOk = true;
}
}
}
}
}
}
}
}
}
btnLogic();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton)
{
//do nothing
}
}).show();
addView.findViewById(R.id.inHP) instead of findViewById(R.id.inHP)
The problem is that you are calling addView.findViewById inside your onClick event, the onclick doesn't really happen until somebody clicks on it, I'll suggest that you make addView class member variable so that it can persist in memory. That'll solve your problem.

Categories

Resources