NullPointerException Java android studio setMovementMethod(LinkMovementMethod.getInstance()) - java

I'm still new with coding but I have a project with android studio right now. What I wanted to do was to add hyperlinks in a page. I've followed the steps and tutorials to do this very carefully but I can't seem to find the problem. In my Logcat it says
PID: 17644 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setMovementMethod(android.text.method.MovementMethod)' on a null object reference
but I don't understand how it was caused. I have read some similar problems stating the findByViewId might be wrong but I don't think that is the case for my problem but I would love to be wrong. Here is my code. Any help is much appreciated.
public class LearnFragment extends Fragment {
private Button search;
private TextView faq_link_1, faq_link_2, faq_link_3, faq_link_4, faq_link_5;
EditText editSearch;
public LearnFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View viewRoot = inflater.inflate(R.layout.fragment_account, container, false);
faq_link_1 = (TextView) viewRoot.findViewById(R.id.faq1);
faq_link_2 = (TextView) viewRoot.findViewById(R.id.faq2);
faq_link_3 = (TextView) viewRoot.findViewById(R.id.faq3);
faq_link_4 = (TextView) viewRoot.findViewById(R.id.faq4);
faq_link_5 = (TextView) viewRoot.findViewById(R.id.faq5);
faq_link_1.setMovementMethod(LinkMovementMethod.getInstance());
faq_link_2.setMovementMethod(LinkMovementMethod.getInstance());
faq_link_3.setMovementMethod(LinkMovementMethod.getInstance());
faq_link_4.setMovementMethod(LinkMovementMethod.getInstance());
and this is the xml file
<?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/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".boundary.LearnFragment">
<AutoCompleteTextView
android:id="#+id/search_bar"
android:layout_width="0dp"
android:layout_height="44dp"
android:layout_marginStart="16dp"
android:layout_marginTop="28dp"
android:hint="Search..."
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintEnd_toStartOf="#+id/b_search"
app:layout_constraintHorizontal_bias="0.158"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/b_search"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="28dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="14dp"
android:layout_weight="1"
android:text="Search"
app:layout_constraintBottom_toTopOf="#+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/search_bar"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/faq1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="37dp"
android:text="#string/link_period_pain"
android:textSize="23sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2"
android:linksClickable="true"/>
<TextView
android:id="#+id/faq2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="27dp"
android:text="#string/menstrual_cramp_link"
android:textSize="23sp"
app:layout_constraintBottom_toTopOf="#+id/faq3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/faq1"
android:linksClickable="true" />
<TextView
android:id="#+id/faq3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
android:text="#string/late_period_link"
android:textSize="23sp"
app:layout_constraintBottom_toTopOf="#+id/faq4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/faq2"
android:linksClickable="true" />
<TextView
android:id="#+id/faq4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:text="#string/cervical_screening_link"
android:textSize="23sp"
app:layout_constraintBottom_toTopOf="#+id/faq5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/faq3"
android:linksClickable="true" />
<TextView
android:id="#+id/faq5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="35dp"
android:text="#string/pap_smear_link"
android:textSize="23sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/faq4"
android:linksClickable="true" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="13dp"
android:text="Not looking what you're looking for? Use the search bar to find out more!"
android:textSize="23sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/search_bar" />
</androidx.constraintlayout.widget.ConstraintLayout>

Related

Nested Spinner Android Design Issues ConstraintLayout

I'm working with spinner for the first time, and I don't understand why the second spinner doesn't look exactly like the first one even though they were created practically the same (the only difference is the data). These are nested.
The design implemented it with "constraint layout"
<Spinner
android:id="#+id/spCategorie"
android:layout_width="0dp"
android:layout_height="28dp"
android:layout_marginStart="10dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="32dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#+id/categorieText"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/categorieText"
android:layout_width="73dp"
android:layout_height="28dp"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:text="Categorie:"
android:textAlignment="viewStart"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Spinner
android:id="#+id/spProduct"
android:layout_width="0dp"
android:layout_height="28dp"
android:layout_marginStart="10dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="32dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/productText"
app:layout_constraintTop_toBottomOf="#+id/spCategorie" />
This is how it looks in the emulator
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, productsCat);
arrayAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
categorie.setAdapter(arrayAdapter);
categorie.setOnItemSelectedListener(new SpinnersEvents());
product.setOnItemSelectedListener(new SpinnersEvents());
private class SpinnersEvents implements AdapterView.OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getId() == R.id.spCategorie){
String[] productsName = getProductName(productsCat[position]);
ArrayAdapter<String> arrayAdapterChild = new ArrayAdapter<String>(getBaseContext(),R.layout.support_simple_spinner_dropdown_item, productsName);
arrayAdapterChild.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
product.setAdapter(arrayAdapterChild);
}else{
price.setText(String.valueOf(tempList.get(position).getPrice()));
imgPrd.setImageResource(tempList.get(position).getImage());
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
Please try this in your code may help you, you can also set wrapcontent in spinner or increase the size to view text proper
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:ignore="MissingConstraints">
<TextView
android:id="#+id/categorieText"
android:layout_width="73dp"
android:layout_height="28dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="Categorie:"
android:textAlignment="viewStart"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="RtlCompat" />
<Spinner
android:id="#+id/spCategorie"
android:layout_width="0dp"
android:layout_height="28dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="#+id/categorieText"/>
<TextView
android:id="#+id/productText"
android:layout_width="73dp"
android:layout_height="28dp"
android:layout_marginTop="32dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:text="Product:"
android:textAlignment="viewStart"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/categorieText"/>
<Spinner
android:id="#+id/spproduct"
android:layout_width="0dp"
android:layout_height="28dp"
android:layout_marginTop="32dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintTop_toBottomOf="#+id/spCategorie"
app:layout_constraintStart_toEndOf="#+id/categorieText"/>
</androidx.constraintlayout.widget.ConstraintLayout>
The solution was in the code, not in the layout.
The second spinner must be:
ArrayAdapter<String> arrayAdapterChild = new ArrayAdapter<String>(MainActivity.this,R.layout.support_simple_spinner_dropdown_item, productsName);
instead:
ArrayAdapter<String> arrayAdapterChild = new ArrayAdapter<String>(getBaseContext(),R.layout.support_simple_spinner_dropdown_item, productsName);
In this case, calling the getBaseContext() method generated that particular problem.
Sorry for the confusion.

How can I change the color of my edit text which is entered by user?

I have created a signup page and I select the hint color blue now I want to change the color of text which is entered in the edit text please tell me how I can change the color of text
Here is the code of my XML file
<?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=".Signup"
android:background="#color/purple_500">
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="660dp"
android:layout_marginTop="80dp"
android:background="#drawable/linear_round"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="Please Signup..."
android:textColor="#color/white"
android:textStyle="bold"
android:textSize="29sp"
android:gravity="center"
android:layout_marginTop="16dp"
android:id="#+id/textView">
</TextView>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textColor="#color/black"
android:hint="Enter your name"
android:layout_marginTop="250dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:gravity="center"
android:textColorHint="#color/purple_500"
android:id="#+id/name"/>
<EditText
android:id="#+id/pass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:gravity="center"
android:hint="Enter your password"
android:inputType="textPassword"
android:textColor="#color/black"
android:textColorHint="#color/purple_500"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/name" />
<EditText
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:gravity="center"
android:hint="Enter your valid e-mail"
android:inputType="textEmailAddress"
android:textColor="#color/black"
android:textColorHint="#color/purple_500"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/pass" />
<EditText
android:id="#+id/number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:gravity="center"
android:hint="Enter your cell number"
android:inputType="phone"
android:textColor="#color/black"
android:textColorHint="#color/purple_500"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/email" />
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/submit"
android:layout_width="445dp"
android:layout_height="163dp"
android:layout_centerInParent="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/number"
android:layout_marginBottom="100dp"
app:lottie_autoPlay="true"
app:lottie_loop="true"
app:lottie_rawRes="#raw/submit" />
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/trial"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_centerInParent="true"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/submit"
app:lottie_autoPlay="true"
app:lottie_loop="true"
app:lottie_rawRes="#raw/trial" />
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/signup_animation"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
app:layout_constraintBottom_toTopOf="#+id/name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="70dp"
app:lottie_autoPlay="true"
app:lottie_loop="true"
app:lottie_rawRes="#raw/signup" />
</androidx.constraintlayout.widget.ConstraintLayout>
And how can I add the marquee effect on my text view? Can I make text view scrolling from right to left?
If TextView fill whole width you need to add these lines of code
<TextView
.
.
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
/>
also do not forget to set selected for TexrtView as below
textview.setSelected(true);
and if TextView does not fill width the only thing is to call this method and pass TextView to it.
Kotlin
fun addMarquee(textView: TextView) {
textView.viewTreeObserver.addOnGlobalLayoutListener(object :
ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
val pixels = textView.measuredWidth - 1
val params = textView.layoutParams
params.width = pixels
textView.layoutParams = params
textView.isSelected = true
textView.ellipsize = TextUtils.TruncateAt.MARQUEE
textView.isSingleLine = true
textView.marqueeRepeatLimit = -1
textView.viewTreeObserver.removeOnGlobalLayoutListener(this)
}
})
}
Java
public void addMarquee(TextView textView) {
textView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int pixels = textView.getMeasuredWidth() - 1;
ViewGroup.LayoutParams params = textView.getLayoutParams();
params.width = pixels;
textView.setLayoutParams(params);
textView.setSelected(true);
textView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
textView.setSingleLine(true);
textView.setMarqueeRepeatLimit(-1);
textView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
}
NOTE: this method only work when textView width is wrap_content.
textColor attribute changes color of text which user entered in.
android:textColor="#color/black"
HorizontalScrollView has one chid view, and it can be scrolled horizontally.
There's many ways to move view.
Search with keyword like
move view animation android or move view programmatically android

Attempt to invoke ListView.setAdapter on a null object reference

I am trying to show am Integer Arraylist on a ListView. But I fail due to a null object error. My object is not null. Basically I am trying to make a scoreboard for 2 teams.
Here is my class:
public class PointsList extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.points_list);
Bundle b;
b = getIntent().getExtras();
ArrayList<Integer> scoreA = new ArrayList<Integer>();
ArrayList<Integer> scoreB = new ArrayList<Integer>();
ListView ListA, ListB;
scoreA = b.getIntegerArrayList("scoreA");
scoreB = b.getIntegerArrayList("scoreB");
ArrayAdapter arrayAdapterA = new ArrayAdapter<Integer>(
this, R.layout.points_list, R.id.scoreAtext, scoreA );
ArrayAdapter arrayAdapterB = new ArrayAdapter<Integer>(
this, R.layout.points_list, R.id.scoreBtext, scoreB );
ListA = (ListView) findViewById(R.id.scoreAList);
ListB = (ListView) findViewById(R.id.scoreBList);
ListA.setAdapter(arrayAdapterA);
ListB.setAdapter(arrayAdapterB);
}
}
And here is my xml file:
<?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/scrollView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background"
android:fillViewport="true">
<ListView
android:id="#+id/scoreBList"
android:layout_width="189dp"
android:layout_height="571dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.927"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.593" >
<TextView
android:id="#+id/scoreBtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</ListView>
<ImageView
android:id="#+id/imageView2"
style="#style/custom_image"
android:layout_width="376dp"
android:layout_height="619dp"
android:background="#drawable/dragon"
android:contentDescription="#string/logoDescription"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/teamNameA"
android:layout_width="150dp"
android:layout_height="62dp"
android:autoSizeTextType="none"
android:background="#drawable/red_button"
android:gravity="center_horizontal|center_vertical"
android:inputType="textShortMessage"
android:maxLines="1"
android:singleLine="true"
android:text="#string/team_a"
android:textColor="#000000"
android:textColorHint="#000000"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.103"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.049" />
<TextView
android:id="#+id/teamNameB"
android:layout_width="150dp"
android:layout_height="62dp"
android:autoSizeTextType="none"
android:background="#drawable/red_button"
android:gravity="center_horizontal|center_vertical"
android:inputType="textShortMessage"
android:maxLines="1"
android:singleLine="true"
android:text="#string/team_b"
android:textColor="#000000"
android:textColorHint="#000000"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.934"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.049" />
<View
android:id="#+id/divider"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ListView
android:id="#+id/scoreAList"
android:layout_width="189dp"
android:layout_height="571dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.072"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.593" >
<TextView
android:id="#+id/scoreAtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</ListView>
</androidx.constraintlayout.widget.ConstraintLayout>
I have tried entering just a blank xml with a textview as I saw on other threads that this is what ArrayAdapter expects. But failed. Any help is greatly appreciated!

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

Fragment is not added to the back stack when replaced

Above is the path I follow until I reach the BeginnerFragment.
Above is the hierarchy of activity / fragments
Current Situation
In my application I have a Bottom Navigation, and in each fragment of the navigation, I have two tabs.
The Problem
My problem is that when I'm at BeginnerFragment and I press the cell back button, it returns to the StatusFragment.
I expected it to return to EducationHomeFragment, as I added it to BackStack, according to the codes below.
EducationFragment
public class EducationFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View root = inflater.inflate(R.layout.fragment_education, container, false);
// Instanciando o Fragmento de página inicial da Educação
EducationHomeFragment educationHomeFragment = new EducationHomeFragment();
getActivity().getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, educationHomeFragment).addToBackStack(null).commit();
return root;
}
}
fragment_education
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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/fragment_container"
tools:context=".education.EducationFragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
EducationHomeFragment
public class EducationHomeFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View root = inflater.inflate(R.layout.fragment_education_home, container, false);
CardView beginnerFreelance = root.findViewById(R.id.cardview_beginner_skills);
beginnerFreelance.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BeginnerFragment fragment = new BeginnerFragment();
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container , fragment)
.addToBackStack(null)
.commit();
}
});
return root;
}
}
fragment_education_home
<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/fragment_education_home_container"
tools:context=".education.EducationHomeFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EEE"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/text_academic_education"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="#string/academic_education"
android:textColor="#color/colorPrimaryDark"
app:fontFamily="#font/exo_2_semibold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardview_advanced_skills" />
<TextView
android:id="#+id/title_courses_available"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="#string/general_courses"
android:textColor="#color/colorPrimaryDark"
app:fontFamily="#font/exo_2_semibold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardview_qualifications" />
<androidx.cardview.widget.CardView
android:id="#+id/cardview_doctoral_courses"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="#dimen/default_cardview_mgsides"
android:layout_marginTop="#dimen/cardview_list_margin"
android:layout_marginRight="#dimen/default_cardview_mgsides"
android:layout_marginBottom="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardview_master_courses">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView39"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/doctoral_courses"
app:fontFamily="#font/exo_2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardview_master_courses"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="#dimen/default_cardview_mgsides"
android:layout_marginTop="#dimen/cardview_list_margin"
android:layout_marginRight="#dimen/default_cardview_mgsides"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardview_specialization_courses">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView38"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/master_courses"
app:fontFamily="#font/exo_2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardview_specialization_courses"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="#dimen/default_cardview_mgsides"
android:layout_marginTop="#dimen/cardview_list_margin"
android:layout_marginRight="#dimen/default_cardview_mgsides"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardview_higher_courses">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/text_doctoral_courses"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/specialization_courses"
app:fontFamily="#font/exo_2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardview_higher_courses"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="#dimen/default_cardview_mgsides"
android:layout_marginTop="#dimen/cardview_list_margin"
android:layout_marginRight="#dimen/default_cardview_mgsides"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/text_academic_education">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/text_master_courses"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/higher_level_courses"
app:fontFamily="#font/exo_2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardview_advanced_skills"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="#dimen/default_cardview_mgsides"
android:layout_marginTop="#dimen/cardview_list_margin"
android:layout_marginRight="#dimen/default_cardview_mgsides"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardview_intermediate_skills">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/text_specialization_courses"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/advanced"
app:fontFamily="#font/exo_2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardview_study_status"
android:layout_width="match_parent"
android:layout_height="85dp"
android:layout_marginLeft="#dimen/default_cardview_mgsides"
android:layout_marginTop="#dimen/default_cardview_mgtop"
android:layout_marginRight="#dimen/default_cardview_mgsides"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/image_study_status"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginTop="10dp"
android:tint="#color/colorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/outline_menu_book_24" />
<TextView
android:id="#+id/text_study_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="#string/not_studying"
android:textColor="#color/colorPrimaryDark"
app:fontFamily="#font/exo_2_semibold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/image_study_status" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardview_qualifications"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/default_cardview_mgsides"
android:layout_marginTop="#dimen/default_cardview_mgtop"
android:layout_marginRight="#dimen/default_cardview_mgsides"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardview_study_status">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="12dp"
android:paddingBottom="12dp">
<TextView
android:id="#+id/title_obtained_qualifications"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/qualifications_obtained"
android:textColor="#color/colorPrimaryDark"
app:fontFamily="#font/exo_2_semibold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/obtained_qualification_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="#string/no_course_completed"
app:fontFamily="#font/exo_2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/title_obtained_qualifications" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardview_beginner_skills"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="#dimen/default_cardview_mgsides"
android:layout_marginTop="#dimen/cardview_list_margin"
android:layout_marginRight="#dimen/default_cardview_mgsides"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/title_courses_available">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/text_basic_courses"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/beginner"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardview_intermediate_skills"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="#dimen/default_cardview_mgsides"
android:layout_marginTop="#dimen/cardview_list_margin"
android:layout_marginRight="#dimen/default_cardview_mgsides"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardview_beginner_skills">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/text_higher_courses"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/intermediate"
app:fontFamily="#font/exo_2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Why isn't FragmentEducationHome being added to the stack? I want to return to FragmentEducationHome when I click on the BeginnerFragment back button.
This is not a direct answer to the question.
But Google suggests to use Jetpack Navigation approach.
Which will be single Activity and multiple fragments.
Google Code Labs Link
This avoids lot of bipolarate code , were we had to handle backstack, keeping fragment count etc...
At first anyone will feel bit difficult to start with, but in a while , you can grasp and move on to new approach which is suggestible.
Nav_graph avoids programmers responsibility to add/ remove fragments
Allows passing data (raw data or data classes with Parcelable) from one fragment to another
Moving from one child nav graph to another
And much more...
I would suggest you do checkout this once starting with a new trial/project
EDIT
Even though one activity is suggested, we may have more than one Activity and its corresponding child fragment graphs if required. This purely depends on the requirement. In my last project we started with single activity concept. But as the app complexity increased, it was decided to move for two activity and its corresponding child frags.
With respect to Clean Code Architecture this is easily adaptable as well...
Good luck!
Please refer to this question:
Override onBackPressed() into your activity and call this in order to remove current fragment from backstack, since you add it.
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStackImmediate()
} else {
finish();
}
Make sure R.id.fragment_container lives in the parent activity xml (not the fragment xml) and in EducationHomeFragment use .add instead of .replace to add BeginnerFragment to the back stack
You already replace current container fragment by BeginnerFragment, so you can't back to previous fragment. Try to use add(id, fragment, tag) and addToBackStack(tag). Hope help you.

Categories

Resources