How to change background of a child in view - java

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

Related

Android keyboard brings up textview in Dialog how to fix it

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.

DialogFragment is expanding the XML to full screen?

i've implemented a DialogFragment , but it comes out looking like this:
instead of this (from XML preview):
fragment's onViewCreated
val button = view.findViewById(R.id.button_register) as Button
button.setOnClickListener{
Log.d(TAG, "Clicked")
val dialog = SuccessDialog()
dialog.show(fragmentManager, "success dialog")
}
SuccessDialog class
class SuccessDialog: DialogFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.dialog_success, container, false)
val button: Button = view.findViewById(R.id.button_ok)
button.setOnClickListener{
dialog.dismiss()
}
return view
}
}
dialog's XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:orientation="vertical"
android:layout_width="250dp"
android:layout_height="200dp"
android:background="#color/colorPrimaryDark"
android:padding="5dp"
android:layout_gravity="center">
<ImageView
android:id="#+id/imageView"
android:layout_width="47dp"
android:layout_height="46dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="#drawable/ic_check_mark_24dp"
android:contentDescription="#string/checkmark" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="#string/success"
android:textColor="#android:color/white"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView" />
<TextView
android:id="#+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:gravity="center"
android:text="#string/thank_you"
android:textColor="#android:color/white"
android:textStyle="italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3" />
<Button
android:id="#+id/button_ok"
android:layout_width="86dp"
android:layout_height="40dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="#string/ok"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView6" />
</android.support.constraint.ConstraintLayout>
tools:...
is just for xml preview . So please use :
app:srcCompat="..."
instead of
tools:srcCompat="..."
To change the dimensions use this in onCreateView of your SuccessDialog
if (getDialog() != null && getDialog().getWindow() != null) {
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}

NullPointerException on defined TextView

I am working on an app using a CardView menu & I am running into some troubles.
The CardView menu presents different tools & among those, there is "location" which uses a third-party library that I used very recently to retrieve the longitude and latitude.
When the user clicks into this card "location", a popup shows up & after pressing the button "update", I set two textviews in that popup to the retrieved longitude and latitude.
However, I seem to run into a NullPointerException when I try:
longitudeTv.setText("Longitude: " + deviceLongitude);
Here are the main parts of my code:
The Menu Class - the launcher activity:
public class Menu extends AppCompatActivity {
GridLayout menuGrid;
SimpleLocation myLocation;
public static double deviceLongitude;
public static double deviceLatitude;
public static Dialog infoPopupDialog;
static TextView messageTv;
public static Dialog locationPopupDialog;
public static TextView longitudeTv;
public static TextView latitudeTv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
menuGrid = (GridLayout) findViewById(R.id.menuGrid);
myLocation = new SimpleLocation(this);
infoPopupDialog = new Dialog(this);
messageTv = (TextView) findViewById(R.id.messageTv);
locationPopupDialog = new Dialog(this);
longitudeTv = (TextView) findViewById(R.id.longitudeTv);
latitudeTv = (TextView) findViewById(R.id.latitudeTv);
// if we can't access the location yet
if (!myLocation.hasLocationEnabled()) {
// ask the user to enable location access
SimpleLocation.openSettings(this);
}
CardView dataCard = (CardView) findViewById(R.id.dataCard);
CardView locationCard = (CardView) findViewById(R.id.locationCard);
CardView timeCard = (CardView) findViewById(R.id.timeCard);
CardView websiteCard = (CardView) findViewById(R.id.websiteCard);
CardView emailCard = (CardView) findViewById(R.id.emailCard);
CardView infoCard = (CardView) findViewById(R.id.infoCard);
infoCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MainActivity.showInfoPopup();
}
});
dataCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent dataActivity = new Intent(getApplicationContext(), MainActivity.class);
startActivity(dataActivity);
}
});
locationCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final double longit = roundCoordinates(myLocation.getLongitude());
final double latit = roundCoordinates(myLocation.getLatitude());
deviceLongitude = longit;
deviceLatitude = latit;
MainActivity.showLocationPopup();
}
});
timeCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(Menu.this, "Next update in 11 mns", Toast.LENGTH_SHORT).show();
}
});
websiteCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent openWebsite = new Intent(Intent.ACTION_VIEW, Uri.parse("http://159.203.78.94/rpilog/weatherstation.txt")); //Insert website.
startActivity(openWebsite);
}
});
emailCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "Weather Station Data Report");
intent.putExtra(Intent.EXTRA_TEXT, MainActivity.dataFromURL); //Add string variable holding entire data here.
intent.setData(Uri.parse("mailto:RHellstrom#bridgew.edu"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // this will make such that when user returns to your app, your app is displayed, instead of the email app.
startActivity(intent);
}
});
}
//function to round to 2 decimal places our GPS coordinates.
public static double roundCoordinates(double coordinate){
String result = String.format("%.2f", coordinate);
double roundedValue = Double.parseDouble(result);
return roundedValue;
}
public void closePopup(View v){ //ONCLICK OF CLOSE ICON
infoPopupDialog.dismiss();
locationPopupDialog.dismiss();
}
public void updateCoordinates(View v){ //ONCLICK BTN "UPDATE"
Menu.latitudeTv.setText("Latitude: " + Menu.deviceLatitude);
Menu.longitudeTv.setText("Longitude: " + Menu.deviceLongitude);
}
}
Menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="com.myapps.toualbiamine.weathertracker.Menu"
android:background="#drawable/bg"
android:weightSum="10"
android:orientation="vertical"
android:backgroundTintMode="multiply"
android:backgroundTint="#color/background"
>
<RelativeLayout
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="0dp">
<TextView
android:id="#+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Weather Tracker"
android:textSize="34sp"
android:textColor="#color/titleColor"
android:layout_centerInParent="true"/>
</RelativeLayout>
<GridLayout
android:id="#+id/menuGrid"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8"
android:alignmentMode="alignMargins"
android:columnCount="2"
android:columnOrderPreserved="false"
android:padding="14dp"
android:rowCount="3"
>
<!-- Row 1 -->
<!-- Column 1 -->
<android.support.v7.widget.CardView
android:id="#+id/dataCard"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardCornerRadius="20dp"
app:cardElevation="20dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginLeft="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/data"
android:layout_gravity="center_horizontal"
android:layout_marginRight="18dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginRight="32dp"
android:text="Data"
android:textColor="#color/textColor"
android:textSize="18sp"
android:layout_marginTop="10dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Column 2 -->
<android.support.v7.widget.CardView
android:id="#+id/locationCard"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardCornerRadius="20dp"
app:cardElevation="20dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginLeft="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/location"
android:layout_gravity="center_horizontal"
android:layout_marginRight="18dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginRight="29dp"
android:text="Location"
android:textColor="#color/textColor"
android:textSize="18sp"
android:layout_marginTop="10dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Row 2 -->
<!-- Column 1 -->
<android.support.v7.widget.CardView
android:id="#+id/timeCard"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardCornerRadius="20dp"
app:cardElevation="20dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginLeft="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/time"
android:layout_gravity="center_horizontal"
android:layout_marginRight="18dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginRight="30dp"
android:text="Update Time"
android:textColor="#color/textColor"
android:textSize="18sp"
android:layout_marginTop="10dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Column 2 -->
<android.support.v7.widget.CardView
android:id="#+id/emailCard"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardCornerRadius="20dp"
app:cardElevation="20dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginLeft="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/email"
android:layout_gravity="center_horizontal"
android:layout_marginRight="18dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginRight="35dp"
android:text="Email"
android:textColor="#color/textColor"
android:textSize="18sp"
android:layout_marginTop="10dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Row 3 -->
<!-- Column 1 -->
<android.support.v7.widget.CardView
android:id="#+id/websiteCard"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardCornerRadius="20dp"
app:cardElevation="20dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginLeft="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/web"
android:layout_gravity="center_horizontal"
android:layout_marginRight="18dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginRight="30dp"
android:text="Website"
android:textColor="#color/textColor"
android:textSize="18sp"
android:layout_marginTop="10dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Column 2 -->
<android.support.v7.widget.CardView
android:id="#+id/infoCard"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardCornerRadius="20dp"
app:cardElevation="20dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginLeft="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/info"
android:layout_gravity="center_horizontal"
android:layout_marginRight="17dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginRight="34dp"
android:text="Info"
android:textColor="#color/textColor"
android:textSize="18sp"
android:layout_marginTop="10dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</GridLayout>
Popup_location.xml:
<?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"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:gravity="center">
<ImageView
android:id="#+id/closePopup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/close"
android:layout_alignParentRight="true"
android:elevation="5dp"
android:layout_marginTop="7dp"
android:layout_marginRight="7dp"
android:onClick="closePopup"/>
<android.support.v7.widget.CardView
android:id="#+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="15dp"
app:cardBackgroundColor="#color/background"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="25dp"
android:layout_marginBottom="25dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="25dp"
android:layout_marginBottom="25dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:src="#drawable/location_popup"
/>
<TextView
android:id="#+id/longitudeTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:layout_marginTop="10dp"
android:textAlignment="center"
android:textSize="18dp"
android:text="Longitude: "
/>
<TextView
android:id="#+id/latitudeTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:textAlignment="center"
android:textSize="18dp"
android:text="Latitude: "
/>
<Button
android:id="#+id/btn"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="UPDATE"
android:textColor="#color/colorWhite"
android:background="#drawable/update_btn_circle"
android:layout_gravity="center_horizontal"
android:onClick="updateCoordinates"
/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
StackTrace:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapps.toualbiamine.weathertracker, PID: 24053
java.lang.IllegalStateException: Could not execute method for
android:onClick
at
android.support.v7.app.AppCompatViewInflater$
DeclaredOnClickListener.
onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.
run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$
DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:6294) 
at android.view.View$PerformClick.run(View.java:24770) 
at android.os.Handler.handleCallback(Handler.java:790) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6494) 
at java.lang.reflect.Method.invoke(Native Method) 
at
com.android.internal.os.RuntimeInit$MethodAndArgsCaller
.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'void android.widget.TextView.setText(java.lang.CharSequence)'
on a null object reference
at
com.myapps.weathertracker.Menu.updateCoordinates(Menu.java:149)
So, the problematics textviews are defined in the popup.xml.
Please, help. I tried, I searched. I can't find anything.
Community, you're my last hope.
PSA: I also have 3 other fragment classes, a viewpager and a a class retrieving data from a URL using the Volley library by Google but they don't seem to coincide with the problem here. Let me know if you need them.
If the views are in your dialog you have to call findViewById on the dialog, as in:
longitudeTv = (TextView) locationPopupDialog.findViewById(R.id.longitudeTv);
They can't be found in the menu activity and are null.
You can't call this right away though, it has to be after you have set the layout of the dialog. However, I don't see where you actually set the layout of the dialog to popup_location.xml in the code you posted. Presumably that's in the showLocationPopup method.
The longitudeTv is childview of locationPopupDialog,you need set the layout of the dialog (popup_location.xml). then init with longitudeTv = (TextView) locationPopupDialog.findViewById(R.id.longitudeTv);

Resources$NotFoundException: String resource ID [duplicate]

This question already has answers here:
android.content.res.Resources$NotFoundException: String resource ID #0x0
(8 answers)
Closed 5 years ago.
I try to show in MainActivity from another class by getter a value.
1st I make object and insert to him value from editText.
App crash with android.content.res.Resources$NotFoundException: String resource ID #0x28 What is wrong here ?
Check code:
public class MainActivity extends AppCompatActivity {
EditText editText, editText2;
Button button, button2;
TextView tV;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button2 = findViewById(R.id.button2);
editText = findViewById(R.id.editText);
editText2 = findViewById(R.id.editText2);
tV = findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int a = Integer.valueOf(editText.getText().toString());
Calculations calc = new Calculations(a){
};
tV.setText(calc.getResult());
}
});
}
2nd Class
public class Calculations {
public int getResult() {
return result;
}
public int result;
public Calculations(int a){
result = a * 10;
}
public Calculations(int a, int b){
result = a * b;
}
}
Here is my layout, maybe this will help
Here is my layout, maybe this will help
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="ge.ger.training.MainActivity">
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.222"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.22" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.222"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.351" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Transfer value 1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.121"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.498" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Transfer value 1 and 2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.12"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.668" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Result"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.109"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.838" />
</android.support.constraint.ConstraintLayout>
Replace your line with below in onClickListener
Calculations calc=new Calculations(a);
tV.setText(String.valueOf(calc.getResult()));

Error in data Binding in Android

activity_main.xml
<data class="LoginDataBinding">
<variable
name="login"
type="com.example.itstym.logina.LoginViewModel" />
<variable
name="ModelClickListener"
type="com.example.itstym.logina.interface.ClickListener" />
</data>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textStyle="bold"
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome, Back"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="24dp"/>
<EditText
app:error="#{login.errorEmail}"
android:hint="#{login.emailHint}"
android:text="#{login.userEmailAddress}"
android:id="#+id/user_email_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:ems="10"
android:inputType="textEmailAddress"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="24dp"
app:layout_constraintTop_toBottomOf="#+id/textView"
android:layout_marginStart="8dp"/>
<EditText
app:error="#{login.errorPassword}"
android:text="#{login.userPassword}"
android:hint="#{login.passwordHint}"
android:id="#+id/user_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:ems="10"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/user_email_address"/>
<Button
android:onClick="#{(v)->ModelClickListener.onSubmitButtonClick()}"
android:text="#{login.loginButtonText}"
android:id="#+id/login_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
app:layout_constraintTop_toBottomOf="#+id/user_password"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"/>
</android.support.constraint.ConstraintLayout>
MainActivity.java
public class SampleActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final LoginDataBinding loginDataBinding= DataBindingUtil.setContentView(this,R.layout.activity_main);
LoginViewModel viewModel = new LoginViewModel("Enter Email Address","Enter password","Login");
loginDataBinding.setLogin(viewModel);
loginDataBinding.setModelClickListener(new ClickListener() {
#Override
public void onClick() {
Toast.makeText(getApplicationContext(), loginDataBinding.getLogin().getUserEmailAddress(), Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), loginDataBinding.getLogin().getUserPassword(), Toast.LENGTH_SHORT).show();
}
});
}
}
But I am getting the error unresolved reference for setModelClickListener() but the setLogin() works perfertly normal.
One more thing why the DataBindingUtil.setContentView(this,R.layout.activity_main); return the LoginDataBinding instead of MainActivityBinding?

Categories

Resources