Android keyboard brings up textview in Dialog how to fix it - java

I have problem with use keyboard and Dialog in android studio (java). if i use edittext, the keyboard brings up texviews from the bottom up. I tried using WindowSoftInput in manifest and in Dialog getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN). How i can fix this problem?
Thanks for help.
w/o keyboard
with keyboard
Problem TextView have name tvTemp.
<EditText
android:id="#+id/etWriteTempR1"
android:layout_width="100dp"
android:layout_height="65dp"
android:layout_marginStart="128dp"
android:layout_marginTop="110dp"
android:ems="2"
android:hint="00"
android:inputType="number"
android:textAlignment="center"
android:textSize="40dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/btnOnOfVent_R1" />
<ImageButton
android:id="#+id/btnOnOff_R1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="52dp"
android:layout_marginTop="144dp"
android:background="#00FFFFFF"
android:scaleType="fitCenter"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/on" />
<ImageButton
android:id="#+id/btnOnOfVent_R1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="52dp"
android:layout_marginTop="144dp"
android:background="#00FFFFFF"
android:scaleType="fitCenter"
app:layout_constraintStart_toEndOf="#id/btnOnOff_R1"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/on" />
<ImageButton
android:id="#+id/btnOnOffKlimat_R1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="52dp"
android:layout_marginTop="144dp"
android:background="#00FFFFFF"
android:scaleType="fitCenter"
app:layout_constraintStart_toEndOf="#id/btnOnOfVent_R1"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/on" />
<TextView
android:id="#+id/tvDialogR1"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="16dp"
android:text="TextView"
android:textAlignment="center"
android:textSize="25sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/btnClose" />
<Button
android:id="#+id/btnClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="272dp"
android:background="#00FFFFFF"
android:text="X"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tvTemp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="80dp"
android:layout_marginBottom="16dp"
android:text="00"
android:textAlignment="center"
android:textSize="175sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
Dialog Code.
private void DialogR1() {
dialogRoom1 = new Dialog(this);
// dialogRoom1.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialogRoom1.setContentView(R.layout.activity_room);
dialogRoom1.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));
//ImageView dialogpictures = (ImageView) dialogRoom1.findViewById(R.id.prewimg);
//dialogpictures.setImageResource(R.drawable.letter_bukvi);
WindowManager.LayoutParams lp = dialogRoom1.getWindow().getAttributes();
lp.width = MATCH_PARENT;
lp.height = MATCH_PARENT;
dialogRoom1.getWindow().setAttributes(lp);
TextView tvDialogR1 = (TextView) dialogRoom1.findViewById(R.id.tvDialogR1);
tvDialogR1.setText("Room 1");
dialogRoom1.setCancelable(false);
TextView tvTemp = dialogRoom1.findViewById(R.id.tvTemp);
tvTemp.setText(temp1);
TextView btnclose = (TextView) dialogRoom1.findViewById(R.id.btnClose);
btnclose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialogRoom1.dismiss();
}
});
EditText etWriteTempR1 = dialogRoom1.findViewById(R.id.etWriteTempR1);
etWriteTempR1.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if(keyEvent.getAction() == KeyEvent.ACTION_DOWN &&
(i == KeyEvent.KEYCODE_ENTER))
{
Log.e("SendText", "temp1 " + etWriteTempR1.getText().toString() + ";");
connectedThread.write("temp1 " + etWriteTempR1.getText().toString() + ";");
etWriteTempR1.setText("");
return true;
}
return false;
}
});
dialogRoom1.show();
}

You should add a bottom constraint to your EditText, for instance:
app:layout_constraintBottom_toTopOf="#+id/tvTemp" />
You might need as well to set the following in your manifest
android:windowSoftInputMode="adjustNothing">

I fixed my problem. So, for fix problem, need delete all bottom bindings.

Related

Sending a notification through a button by accepting data from both tabs in a tabbed activity in Android Studio

I am currently creating my very first application, it's a very dummy coffee shop application that includes multiple activities like a basic activity and a log in activity. However, I have currently created my first tabbed activity where the user can "order" coffee (first tab) and food (second tab) by selecting each CheckBox respectively as you can see in the images below.
TAB 1 - COFFEE
TAB 2 - FOOD
As you can see, there are two buttons on the bottom of the screen, the "GO BACK" button that sends the user back to the main activity and another button next to it called "ORDER NOW" which is where I am stuck. Both buttons are visible in both tabs and I have an onClick method for the first button "sendNotification". When I press that button it checks which check boxes are clicked and stores them accordingly inside the String order and then insert that value inside the notification. Everything works fine when I "order" coffees but no matter what selection I make in the second tab called "FOOD" it always sends an empty message like this "Here is your oder: , it will arrive in about 1 hour, thank you!".
Implementation for sendNotification:
package com.example.myapp;
public class OrderActivity extends AppCompatActivity {
private ActivityOrderBinding binding;
private PageViewModel pageViewModel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityOrderBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
ViewPager viewPager = binding.viewPager;
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = binding.tabs;
tabs.setupWithViewPager(viewPager);
}
public void goBack(View v)
{
finish();
}
public void sendNotification(View v)
{
CheckBox ch1 = findViewById(R.id.cb1);
CheckBox ch2 = findViewById(R.id.cb2);
CheckBox ch3 = findViewById(R.id.cb3);
CheckBox ch4 = findViewById(R.id.cb4);
CheckBox ch5 = findViewById(R.id.cb5);
CheckBox ch6 = findViewById(R.id.cb6);
CheckBox ch7 = findViewById(R.id.cb7);
CheckBox ch8 = findViewById(R.id.cb8);
CheckBox ch9 = findViewById(R.id.cb9);
CheckBox ch10 = findViewById(R.id.cb10);
String order = "";
if (ch1.isChecked()) {
order += ch1.getText().toString() + ", ";
}
if (ch2.isChecked()) {
order += ch2.getText().toString() + ", ";
}
if (ch3.isChecked()) {
order += ch3.getText().toString() + ", ";
}
if (ch4.isChecked()) {
order += ch4.getText().toString() + ", ";
}
if (ch5.isChecked()) {
order += ch5.getText().toString() + ", ";
}
if (ch6.isChecked()) {
order += ch6.getText().toString() + ", ";
}
if (ch7.isChecked()) {
order += ch7.getText().toString() + ", ";
}
if (ch8.isChecked()) {
order += ch8.getText().toString() + ", ";
}
if (ch9.isChecked()) {
order += ch9.getText().toString() + ", ";
}
if (ch10.isChecked()) {
order += ch10.getText().toString() + ", ";
}
NotificationChannel channel;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
channel = new NotificationChannel("1", "channel1", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this, "1")
.setSmallIcon(R.drawable.coffeeicon)
.setContentTitle("Order confirmation")
.setContentText("Here is your order: " + order + " it will arrive in about 1 hour, thank you!")
.setPriority(NotificationCompat.PRIORITY_MAX);
NotificationManagerCompat notifyAdmin = NotificationManagerCompat.from(this);
notifyAdmin.notify(1, notification.build());
}
finish();
Toast.makeText(getApplicationContext(), "Thank you for ordering, you will now receive a confirmation for your order!", Toast.LENGTH_LONG).show();
}
}
My PageViewModel.java
package com.example.myapp.ui.main;
import androidx.arch.core.util.Function;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Transformations;
import androidx.lifecycle.ViewModel;
public class PageViewModel extends ViewModel {
private MutableLiveData<Integer> mIndex = new MutableLiveData<>();
private LiveData<String> mText = Transformations.map(mIndex, new Function<Integer, String>() {
#Override
public String apply(Integer input) {
return "Hello world from section: " + input;
}
});
public void setIndex(int index) {
mIndex.setValue(index);
}
public LiveData<String> getText() {
return mText;
}
}
My PlaceHolderFragment.java
package com.example.myapp_csc392.ui.main;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.AsyncListUtil;
import com.example.myapp_csc392.R;
import com.example.myapp_csc392.databinding.FragmentOrderBinding;
import org.w3c.dom.Text;
/**
* A placeholder fragment containing a simple view.
*/
public class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
private PageViewModel pageViewModel;
private FragmentOrderBinding binding;
public static PlaceholderFragment newInstance(int index) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle bundle = new Bundle();
bundle.putInt(ARG_SECTION_NUMBER, index);
fragment.setArguments(bundle);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pageViewModel = new ViewModelProvider(this).get(PageViewModel.class);
int index = 1;
if (getArguments() != null) {
index = getArguments().getInt(ARG_SECTION_NUMBER);
}
pageViewModel.setIndex(index);
}
#Override
public View onCreateView(
#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
binding = FragmentOrderBinding.inflate(inflater, container, false);
View root = binding.getRoot();
ImageView img1 = root.findViewById(R.id.ivEspresso);
ImageView img2 = root.findViewById(R.id.ivCapuccino);
ImageView img3 = root.findViewById(R.id.ivLatte);
ImageView img4 = root.findViewById(R.id.ivMilkshake);
ImageView img5 = root.findViewById(R.id.ivWater);
ImageView img6 = root.findViewById(R.id.ivHamburger);
ImageView img7 = root.findViewById(R.id.ivSandwich);
ImageView img8 = root.findViewById(R.id.ivSalad);
ImageView img9 = root.findViewById(R.id.ivCake);
ImageView img10 = root.findViewById(R.id.ivChips);
CheckBox c1 = root.findViewById(R.id.cb1);
CheckBox c2 = root.findViewById(R.id.cb2);
CheckBox c3 = root.findViewById(R.id.cb3);
CheckBox c4 = root.findViewById(R.id.cb4);
CheckBox c5 = root.findViewById(R.id.cb5);
CheckBox c6 = root.findViewById(R.id.cb6);
CheckBox c7 = root.findViewById(R.id.cb7);
CheckBox c8 = root.findViewById(R.id.cb8);
CheckBox c9 = root.findViewById(R.id.cb9);
CheckBox c10 = root.findViewById(R.id.cb10);
TextView e1 = root.findViewById(R.id.tv1);
TextView e2 = root.findViewById(R.id.tv2);
TextView e3 = root.findViewById(R.id.tv3);
TextView e4 = root.findViewById(R.id.tv4);
TextView e5 = root.findViewById(R.id.tv5);
if(getArguments().getInt(ARG_SECTION_NUMBER) == 1)
{
img6.setVisibility(View.INVISIBLE);
img7.setVisibility(View.INVISIBLE);
img8.setVisibility(View.INVISIBLE);
img9.setVisibility(View.INVISIBLE);
img10.setVisibility(View.INVISIBLE);
c6.setVisibility(View.INVISIBLE);
c7.setVisibility(View.INVISIBLE);
c8.setVisibility(View.INVISIBLE);
c9.setVisibility(View.INVISIBLE);
c10.setVisibility(View.INVISIBLE);
c1.setText("Espresso");
c2.setText("Capuccino");
c3.setText("Latte");
c4.setText("Milkshake");
c5.setText("Water");
e1.setText("2.50€");
e2.setText("3.50€");
e3.setText("4.50€");
e4.setText("4.00€");
e5.setText("1.00€");
}
else if(getArguments().getInt(ARG_SECTION_NUMBER) == 2)
{
img1.setVisibility(View.INVISIBLE);
img2.setVisibility(View.INVISIBLE);
img3.setVisibility(View.INVISIBLE);
img4.setVisibility(View.INVISIBLE);
img5.setVisibility(View.INVISIBLE);
c1.setVisibility(View.INVISIBLE);
c2.setVisibility(View.INVISIBLE);
c3.setVisibility(View.INVISIBLE);
c4.setVisibility(View.INVISIBLE);
c5.setVisibility(View.INVISIBLE);
c6.setText("Hamburger");
c7.setText("Sandwich");
c8.setText("Salad");
c9.setText("Red Velvet Cake");
c10.setText("Salty Chips");
e1.setText("7.00€");
e2.setText("5.00€");
e3.setText("5.00€");
e4.setText("4.50€");
e5.setText("1.50€");
}
return root;
}
#Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
}
and the fragment_order.xml
<?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/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.main.PlaceholderFragment">
<ImageView
android:id="#+id/ivEspresso"
android:layout_width="53dp"
android:layout_height="59dp"
android:layout_margin="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.044"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.023"
app:srcCompat="#drawable/espresso" />
<ImageView
android:id="#+id/ivHamburger"
android:layout_width="53dp"
android:layout_height="59dp"
android:layout_margin="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.044"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.023"
app:srcCompat="#drawable/hamburger" />
<ImageView
android:id="#+id/ivCapuccino"
android:layout_width="53dp"
android:layout_height="59dp"
android:layout_margin="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.041"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.214"
app:srcCompat="#drawable/capuccino" />
<ImageView
android:id="#+id/ivSandwich"
android:layout_width="53dp"
android:layout_height="59dp"
android:layout_margin="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.041"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.214"
app:srcCompat="#drawable/sandwich" />
<ImageView
android:id="#+id/ivLatte"
android:layout_width="53dp"
android:layout_height="59dp"
android:layout_margin="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.041"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.394"
app:srcCompat="#drawable/latte" />
<ImageView
android:id="#+id/ivSalad"
android:layout_width="53dp"
android:layout_height="59dp"
android:layout_margin="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.041"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.394"
app:srcCompat="#drawable/salad" />
<ImageView
android:id="#+id/ivMilkshake"
android:layout_width="51dp"
android:layout_height="61dp"
android:layout_margin="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.041"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.588"
app:srcCompat="#drawable/milkshake" />
<ImageView
android:id="#+id/ivCake"
android:layout_width="50dp"
android:layout_height="62dp"
android:layout_margin="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.044"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.585"
app:srcCompat="#drawable/cake" />
<ImageView
android:id="#+id/ivWater"
android:layout_width="53dp"
android:layout_height="59dp"
android:layout_margin="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.044"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.775"
app:srcCompat="#drawable/water" />
<ImageView
android:id="#+id/ivChips"
android:layout_width="53dp"
android:layout_height="59dp"
android:layout_margin="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.044"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.775"
app:srcCompat="#drawable/chips" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#FF9800"
android:onClick="goBack"
android:text="GO BACK"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.791"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.959" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#FF9800"
android:onClick="sendNotification"
android:text="ORDER NOW"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.239"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.959" />
<CheckBox
android:id="#+id/cb6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#FF9800"
android:text="CheckBox"
android:textAlignment="textEnd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.369"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.051" />
<CheckBox
android:id="#+id/cb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#FF9800"
android:text="CheckBox"
android:textAlignment="textEnd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.347"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.409" />
<CheckBox
android:id="#+id/cb8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#FF9800"
android:text="CheckBox"
android:textAlignment="textEnd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.347"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.409"
tools:ignore="DuplicateClickableBoundsCheck" />
<CheckBox
android:id="#+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#FF9800"
android:text="CheckBox"
android:textAlignment="textEnd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.369"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.051" />
<CheckBox
android:id="#+id/cb10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#FF9800"
android:text="CheckBox"
android:textAlignment="textEnd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.347"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.774" />
<CheckBox
android:id="#+id/cb5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#FF9800"
android:text="CheckBox"
android:textAlignment="textEnd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.347"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.774" />
<CheckBox
android:id="#+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#FF9800"
android:text="CheckBox"
android:textAlignment="textEnd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.369"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.238" />
<CheckBox
android:id="#+id/cb7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#FF9800"
android:text="CheckBox"
android:textAlignment="textEnd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.369"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.238" />
<CheckBox
android:id="#+id/cb9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#FF9800"
android:text="CheckBox"
android:textAlignment="textEnd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.369"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.594" />
<CheckBox
android:id="#+id/cb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#FF9800"
android:text="CheckBox"
android:textAlignment="textEnd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.369"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.594" />
<TextView
android:id="#+id/tv5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.093"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.824" />
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.071"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.144" />
<TextView
android:id="#+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.068"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.48" />
<TextView
android:id="#+id/tv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.071"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.66" />
<TextView
android:id="#+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.068"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.313" />
</androidx.constraintlayout.widget.ConstraintLayout>
If anyone could help out I would really appreciate it and please let me know if you need me to show more code.

How to assign an integer value to an imageView in Android Studio?

I am creating a feedback app where a user has to click one of the 5 imageViews (1-5 rating) based on his/her experience. My primary aim is to extract the integer value of this rating from the imageView click and push it to a SQLite database.
I am trying to use setTag() and getTag() but to no avail. Any help would be much appreciated. Thanks in advance.
activity_main.xml -
<?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">
<TextView
android:id="#+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="99dp"
android:layout_marginLeft="99dp"
android:layout_marginTop="64dp"
android:layout_marginEnd="172dp"
android:layout_marginRight="172dp"
android:layout_marginBottom="151dp"
android:text="Name"
android:textSize="22sp"
app:layout_constraintBottom_toTopOf="#+id/imageView1"
app:layout_constraintEnd_toStartOf="#+id/editTextPersonName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editTextPersonName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="172dp"
android:layout_marginLeft="172dp"
android:layout_marginTop="54dp"
android:ems="10"
android:hint="Full Name"
android:inputType="textPersonName"
app:layout_constraintStart_toEndOf="#+id/textViewName"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="92dp"
android:layout_height="103dp"
android:layout_marginStart="35dp"
android:layout_marginLeft="35dp"
android:layout_marginTop="151dp"
android:layout_marginEnd="47dp"
android:layout_marginRight="47dp"
android:tag="1"
app:layout_constraintEnd_toStartOf="#+id/imageView2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textViewName"
tools:srcCompat="#tools:sample/avatars" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="92dp"
android:layout_height="103dp"
android:layout_marginStart="60dp"
android:layout_marginLeft="60dp"
android:layout_marginBottom="64dp"
android:tag="4"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView3"
tools:srcCompat="#tools:sample/avatars" />
<ImageView
android:id="#+id/imageView5"
android:layout_width="92dp"
android:layout_height="103dp"
android:layout_marginStart="46dp"
android:layout_marginLeft="46dp"
android:layout_marginEnd="40dp"
android:layout_marginRight="40dp"
android:layout_marginBottom="64dp"
android:tag="5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.833"
app:layout_constraintStart_toEndOf="#+id/imageView4"
tools:srcCompat="#tools:sample/avatars" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="92dp"
android:layout_height="103dp"
android:layout_marginStart="48dp"
android:layout_marginLeft="48dp"
android:layout_marginBottom="63dp"
android:tag="2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView1"
tools:srcCompat="#tools:sample/avatars" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="92dp"
android:layout_height="103dp"
android:layout_marginStart="53dp"
android:layout_marginLeft="53dp"
android:layout_marginTop="80dp"
android:layout_marginEnd="49dp"
android:layout_marginRight="49dp"
android:tag="3"
app:layout_constraintEnd_toStartOf="#+id/imageView4"
app:layout_constraintStart_toEndOf="#+id/imageView2"
app:layout_constraintTop_toBottomOf="#+id/textView"
tools:srcCompat="#tools:sample/avatars" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="333dp"
android:layout_marginLeft="333dp"
android:layout_marginTop="41dp"
android:layout_marginEnd="340dp"
android:layout_marginRight="340dp"
android:layout_marginBottom="75dp"
android:text="Were you satisfied with our hygiene standards?"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="#+id/imageView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editTextPersonName" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java -
public class MainActivity extends AppCompatActivity {
EditText name;
ImageView oneStar;
ImageView twoStar;
ImageView threeStar;
ImageView fourStar;
ImageView fiveStar;
Intent intent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText) findViewById(R.id.editTextPersonName);
oneStar = (ImageView) findViewById(R.id.imageView1);
oneStar.setTag(1);
oneStar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String username = name.getText().toString()+"\n";
Toast.makeText(MainActivity.this, (Integer) oneStar.getTag(), Toast.LENGTH_SHORT).show();
}
});
}
}
Try to replace oneStar.getTag() with view.getTag()
If you want to get the tag, and display it as a Toast message, the instead of casting it into an Integer object convert it to String using .toString method.
Second parameter of Toast.makeText is interger, but you can't pass any integer. It must be the resource Id of a string (R.string.your_string). Remove "(Integer)" and it should solve your problem.
I don't know exactly what are your requirements and in case you missed it, there is a build in rating barin Android. You can check the tutorial here
it is less code and less error prone if you just add this to ImageView: android:onClick="imageViewClick", and create a handler in MainActivity like this:
public void imageViewClick(View view) {
String username = name.getText().toString()+"\n";
Toast.makeText(MainActivity.this, (Integer)view.getTag(),Toast.LENGTH_SHORT).show();
}

How to change background of a child in view

I'm trying to change the background of a particular constraint layout in a cardview but I can't figure out how to do it.
I have several cards with several layouts (all of them have id like unit1Layout,unit2Layout...)
I want to change the background of the clicked card.
I tried changing the cardview's background but I'm getting as shown in the photo.
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="256"
android:fillViewport="true">
<GridLayout
android:id="#+id/mainGrid"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:alignmentMode="alignMargins"
android:columnCount="1"
android:columnOrderPreserved="false"
android:padding="14dp"
android:rowCount="10">
<!-- Row 1 -->
<android.support.v7.widget.CardView
android:id="#+id/unit1Card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:layout_rowWeight="1"
android:background="#drawable/cardbackground"
app:cardCornerRadius="8dp"
app:cardElevation="5dp">
<android.support.constraint.ConstraintLayout
android:id="#+id/unit1Layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#drawable/cardbackground">
<TextView
android:id="#+id/unit1Label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:fontFamily="#font/myfont"
android:text="Unit 1"
android:textColor="#color/fontColor"
android:textSize="28sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/unit1Words"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:fontFamily="#font/myfont"
android:text="423 Words"
android:textColor="#color/fontColor"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/unit1Label" />
<TextView
android:id="#+id/unit1Green"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:fontFamily="#font/myfont"
android:text="358"
android:textColor="#color/fontColor"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/unit1Yellow"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/unit1Label" />
<TextView
android:id="#+id/unit1Yellow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:fontFamily="#font/myfont"
android:text="32"
android:textColor="#color/fontColor"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/unit1Label" />
<TextView
android:id="#+id/unit1Red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:fontFamily="#font/myfont"
android:text="8"
android:textColor="#color/fontColor"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/unit1Yellow"
app:layout_constraintTop_toBottomOf="#+id/unit1Label" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
this is how Im changing the cardview's background
private void setSingleEvent(GridLayout mainGrid){
for(int i = 0;i<mainGrid.getChildCount();i++){
final int finalI = i;
final CardView cardView = (CardView)mainGrid.getChildAt(i);
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(unitNumbers[finalI+1] == 0) {
unitNumbers[finalI+1] = finalI+1;
cardView.setBackground(ContextCompat.getDrawable(cardView.getContext(), R.drawable.cardbackgroundselected));
}
else {
unitNumbers[finalI+1] = 0;
[![enter image description here][1]][1]
cardView.setBackground(ContextCompat.getDrawable(cardView.getContext(), R.drawable.cardbackground));
}
}
});
}
}
I just found the answer:
private void setSingleEvent(GridLayout mainGrid){
for(int i = 0;i<mainGrid.getChildCount();i++){
final int finalI = i;
final CardView cardView = (CardView)mainGrid.getChildAt(i);
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String layoutID = "unit"+ (finalI + 1) + "Layout";
ConstraintLayout constraintLayout = findViewById(getResources()
.getIdentifier(layoutID,"id",getPackageName()));
Log.d("unit i layout", "unit"+ (finalI + 1) + "Layout");
if(unitNumbers[finalI+1] == 0) {
unitNumbers[finalI+1] = finalI+1;
constraintLayout.setBackground(ContextCompat
.getDrawable(constraintLayout.getContext(),R.drawable.cardbackgroundselected));
}
else {
unitNumbers[finalI+1] = 0;
constraintLayout.setBackground(ContextCompat
.getDrawable(constraintLayout.getContext(),R.drawable.cardbackground));
}
}
});
}
}

How to make a button reappear in different place when clicked?

I started to learn coding in Android studio few weeks ago. Now I'm trying to make an application which has 9 buttons, but you always see just one of them. When you click on visible button it should disappear and show another one and so on.
This is what I've made so far:
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button shownButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
shownButton = (Button) findViewById(R.id.b1);
shownButton.setVisibility(View.VISIBLE);
}
public void buttonDisappear(View view){
shownButton.setVisibility(View.INVISIBLE);
Random generatedNumber = new Random();
int buttonNumber = generatedNumber.nextInt(10 - 0) + 10;
String nextShownButton = "b"+String.valueOf(buttonNumber);
int nextShownButtonId = getResources().getIdentifier(nextShownButton, "id", getPackageName());
Button shownButton = (Button) findViewById(nextShownButtonId);
shownButton.setVisibility(View.VISIBLE);
}
}
In every button property "onClick" I set a function "buttonDisappear". Everything looks fine to me, no errors, but when I run the program and click on the first button (b1), application just stops. Can you tell me why it is happening and how can I fix it? Thank you.
UPDATE
activity_main.xml
<Button
android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:onClick="buttonDisappear"
android:text="#string/mygtukas"
android:visibility="invisible"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="48dp"
android:onClick="buttonDisappear"
android:text="#string/mygtukas"
android:visibility="invisible"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/b9" />
<Button
android:id="#+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_marginTop="48dp"
android:onClick="buttonDisappear"
android:text="#string/mygtukas"
android:visibility="invisible"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/b5" />
<Button
android:id="#+id/b4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="48dp"
android:onClick="buttonDisappear"
android:text="#string/mygtukas"
android:visibility="invisible"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/b5" />
<Button
android:id="#+id/b5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="48dp"
android:onClick="buttonDisappear"
android:text="#string/mygtukas"
android:visibility="invisible"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/b7" />
<Button
android:id="#+id/b6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_marginTop="48dp"
android:onClick="buttonDisappear"
android:text="#string/mygtukas"
android:visibility="invisible"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/b2" />
<Button
android:id="#+id/b7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="48dp"
android:onClick="buttonDisappear"
android:text="#string/mygtukas"
android:visibility="invisible"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/b2" />
<Button
android:id="#+id/b9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:onClick="buttonDisappear"
android:text="#string/mygtukas"
android:visibility="invisible"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
you said you have only 9 buttons and the name starts from b1-b9
while you are generating a number that is always greater or equal to than 10
public void buttonDisappear(Button view){
shownButton.setVisibility(View.INVISIBLE);
Random generatedNumber = new Random();
int buttonNumber = generatedNumber.nextInt(9)+1;
String nextShownButton = "b"+String.valueOf(buttonNumber);
int nextShownButtonId = getResources().getIdentifier(nextShownButton, "id", getPackageName());
Button shownButton = (Button) findViewById(nextShownButtonId);
shownButton.setVisibility(View.VISIBLE);
}

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