Getting this error in Android Studio - Unresolved Reference: nameInput - android-studio

unresolved reference
val name = nameInput.editableText.toString() // getting error in this line
val intent = Intent (this, BirthdayGreetingsActivity::class.java)
startActivity(intent)

Try this
in your XML, you will have a view like
<EditText
android:id="#+id/nameInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:ems="10"
android:inputType="textPersonName"
android:minHeight="48dp"
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
in your activity class
add this
val name : EditText = findViewById(R.id.nameInput)
and get the input text with
name.text.toString()

Related

How can I create and style a clickable inline link in a TextView paragraph in Android Studio without styling the full TextView?

I want to create a TextView with just a few words clickable. When user clicks on that link a Webview is created.
I also would like to change the color of the link as well.
In HTML I can do this:
<p>By checking this box, I acknowledge that I have reviewed the Online Payment Terms & Conditions and agree.</p>
How do I create this in Android studio?
What I have so far:
Layout XML:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/terms_condition_message"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
Strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name"terms_condition_message"><![CDATA[By checking this box, I acknowledge that I have reviewed the %1$s<color="#fff3670b4">Online Payment Terms & Conditions</color>%2$s and agree.
Add android:linksClickable="true" and android:textColorLink="#color/colorLink" in your text view.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/terms_condition_message"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:linksClickable="true"
android:textColorLink="#color/colorLink" />
Set movementMethod to TextView in Java or Kotlin class.
In Kotlin:
textView.movementMethod = LinkMovementMethod.getInstance()
In Java:
textView.setMovementMethod(LinkMovementMethod.getInstance());
Here is how I did it.
TextView:
<TextView
android:id="#+id/forgot_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Forgot Password"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="#id/sign_up_prompt"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/sign_up_prompt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/forgot_password" />
Main Activity:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val forgotPasswordTextView = findViewById<TextView>(R.id.forgot_password)
forgotPasswordTextView.setOnClickListener {
val myIntent = Intent(this, ForgotPasswordActivity::class.java)
startActivity(myIntent)
forgotPasswordTextView.movementMethod = LinkMovementMethod.getInstance()
}
/*************************************************/
val ss = SpannableString("By checking this box, I acknowledge that I have reviewed the Online Payment Terms & Conditions.")
val clickableSpan: ClickableSpan = object : ClickableSpan() {
override fun onClick(textView: View) {
startActivity(Intent(this#MainActivity, SignUpActivity::class.java))
}
override fun updateDrawState(ds: TextPaint) {
super.updateDrawState(ds)
ds.setColor(Color.BLUE)
ds.isUnderlineText = true
}
}
ss.setSpan(clickableSpan, 61, 94, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
val boldSpan = StyleSpan(Typeface.NORMAL)
ss.setSpan(boldSpan, 61, 94, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
val textView = findViewById<TextView>(R.id.sign_up_prompt)
textView.text = ss
textView.movementMethod = LinkMovementMethod.getInstance()
textView.highlightColor = Color.TRANSPARENT
}
}
Blog that I used to help:
https://medium.com/#sairamravuri/clickable-textview-in-kotlin-a242f7168b89

android studio- selecting which radio button was pressed in a radio group is not working

current uni student and I am working on a simple pizza app to consolidate my knowledge on radio buttons and groups
I have a radio group and inside I have 3 buttons, I am then trying to print which radio button was pressed inside my text view.
Here is the XML code for the radio group/button
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="320dp"
android:layout_height="53dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.939"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.787">
<RadioButton
android:id="#+id/tomato"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="tomato"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.079"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.664" />
<RadioButton
android:id="#+id/chilli"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="chilli"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.412"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.664" />
<RadioButton
android:id="#+id/BBQ"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="BBQ"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.74"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.664" />
</RadioGroup>
<Button
android:id="#+id/sauce"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sauce"
android:onClick="showSelectedItem"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.517"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.691" />
<TextView
android:id="#+id/sauceResult"
android:layout_width="346dp"
android:layout_height="36dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.92" />
then my java code is the following:
public void showSelectedItem(View view){
RadioGroup rg = view.findViewById(R.id.radioGroup);
RadioButton rb;
int id = rg.getCheckedRadioButtonId();
rb = view.findViewById(id);
TextView text = view.findViewById(R.id.sauceResult);
text.setText("your sauce is: " + rb.getText());
}
my current output is when I select an option and press the button to display the text the app closes and the prompt displayed is "tut3_1 has closed unexpectedly"
set this
rb = view.findViewById(id);
TextView text = view.findViewById(R.id.sauceResult);
before
int id = rg.getCheckedRadioButtonId();

How do I get a new TextView text to appear every time a button is clicked

I am new to this sorry. I am trying to create an Android Studio app that will have a different text appear for a question and answer TextView box field when a button is clicked. I was able to figure out how to get the text to change once, but how do I get it to cycle repeatedly every time the button is clicked?
Example Question and Answer I Want To Include for each time the button is clicked:
What is your Name?
My Name is Alyssa!
What is your age?
I am 28 years old.
What did you eat today?
I ate Spaghetti and Meatballs.
Here is my code:
MainActivity.kt
package example.alyssa.com.askalyssa
import android.app.Activity
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.TextView
import kotlin.random.Random
class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun buttonClick(v: View) {
val tv = findViewById(R.id.welcome) as TextView
tv.text = "Hi! I'm Alyssa"
val question = findViewById(R.id.questionfield) as TextView
question.text = "What is your name?"
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:text="Avatar Response Field"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/welcome" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:text="Question Pop Up Field"
android:layout_width="wrap_content"
android:layout_height="37dp"
android:id="#+id/questionfield"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
app:layout_constraintHorizontal_bias="0.497"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/button"/>
<Button
android:text="Click to Ask Alyssa a Question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button" android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="#+id/textView2"
android:layout_marginBottom="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp"
android:onClick="buttonClick"/>
<ImageView
android:src="#drawable/ic_launcher_background"
android:layout_width="249dp"
android:layout_height="301dp"
android:id="#+id/imageView2"
app:layout_constraintTop_toBottomOf="#+id/welcome"
android:layout_marginTop="48dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/questionfield"/>
</android.support.constraint.ConstraintLayout>
i didn't understand your question completely but what i found is you are not listening the click of a button. So either implements the setonclickListner with MainActivity or you can directly use btn.setOnClickListner and perform the action on the textview accordingly.
If you still face any issue please comment here with proper query . i will surely help you.

Geckoview Doesn't work

I am working on a project using geckoview in android studio. The app keeps crashing when I reach the containing activity. using log cat I found out that it gives me the following error
FATAL EXCEPTION: main
Process: app, PID: 2815
java.lang.RuntimeException: Unable to start activity ComponentInfo{project name}: android.view.InflateException: Binary XML file line #27: Binary XML file line #27: Error inflating class org.mozilla.gecko.GeckoView
I dont know how to fix this or what does it mean or what to do...
Here is my code, I hope it helps
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_page);
Button Nav=(Button) findViewById(R.id.Nav);
GeckoView geckoView = (GeckoView) findViewById(R.id.geckoview);
GeckoSession session = new GeckoSession();
geckoView.setSession(session);
session.loadUri("http://google.com");
}
XML
<org.mozilla.gecko.GeckoView
android:id="#+id/geckoview"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="82dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/guideline2" />
Thank you in advance..
Change Geckoview in xml as below
<org.mozilla.geckoview.GeckoView
android:id="#+id/geckoview"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="82dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/guideline2" />

Show PopupWindow on ListItem Click dynamically change ContentView data based on item click

I have a ListView in SomeActivity conatins employee names. On item list click i want to popup a window which displays employee details of clicked item.
i tried this popupwindow.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:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="First Name"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="Last Name"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
<TextView
android:id="#+id/firstName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="TextView"
android:textSize="18sp"
app:layout_constraintStart_toEndOf="#+id/textView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/lastName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="TextView"
android:textSize="18sp"
app:layout_constraintStart_toEndOf="#+id/textView2"
app:layout_constraintTop_toBottomOf="#+id/firstName" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text=" email address"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2" />
<TextView
android:id="#+id/emailAdress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="TextView"
android:textSize="18sp"
app:layout_constraintStart_toEndOf="#+id/textView6"
app:layout_constraintTop_toBottomOf="#+id/lastName" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="phone number"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView6" />
<TextView
android:id="#+id/phoneNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="TextView"
android:textSize="18sp"
app:layout_constraintStart_toEndOf="#+id/textView8"
app:layout_constraintTop_toBottomOf="#+id/emailAdress" />
In SomeActivity i wrote code like this
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
String poName = ((TextView)view.findViewById(R.id.rowPOName)).getText().toString();
// View layout = inflater.inflate(R.layout.detailspopup, null);
initiatePopupWindow(poName);
}
});
private void initiatePopupWindow(String name) {
try {
Cursor cursor2 = null;
LayoutInflater inflater = (LayoutInflater) SomeSample.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.detailspopup, null);
String[] selectedArgs = {name};
try {
cursor2 = db.rawQuery("my query to get employee details", selectedArgs);
if (cursor2.moveToFirst()) {
String firstname = cursor2.getString(cursor2.getColumnIndex("firstname"));
String lastname = cursor2.getString(cursor2.getColumnIndex("lastname"));
String email = cursor2.getString(cursor2.getColumnIndex("email"));
String phone = cursor2.getString(cursor2.getColumnIndex("phone"));
TextView fNname = layout.findViewById(R.id.firstName);
TextView lName = layout.findViewById(R.id.lastName);
TextView emailAdd = layout.findViewById(R.id.emailAdress);
TextView phoneNumber = layout.findViewById(R.id.phoneNumber);
fNname.setText(firstname);
lName.setText(lastname);
emailAdd.setText(email);
phoneNumber.setText(phone);
}
} catch (Exception e) {
Log.d(TAG, e.getMessage());
} finally {
if (cursor2 != null && !cursor2.isClosed()) {
cursor2.close();
}
}
popupWindow = new PopupWindow(layout, 370, 450, true);
popupWindow.showAtLocation(layout, Gravity.CENTER, 0, 0);
} catch (Exception e) {
e.printStackTrace();
}
}
After executing code I got popup window without my employee details populated i.e TextViews are not displaying employee data. It displays only default texts i.e TextView.
need help to solve this

Resources