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();
}
Related
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.
Hi everyone I'm trying to create a menu that comes down when clicking on the profile picture:
but as you can see that ImageView is inside a cardView which is in turn inside a tool bar and as the initial parent I have an App Bar layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/primaryPopHome"
android:minHeight="?attr/actionBarSize"
android:theme="#style/AppTheme.bar"
>
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:scaleType="centerInside"
android:src="#drawable/poplogo"
/>
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="300dp"
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="1.9"
app:cardCornerRadius="80dp">
<ImageView
android:id="#+id/img_profilomain"
android:layout_width="42dp"
android:layout_height="42dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:backgroundTint="#color/white"
android:scaleType="centerCrop"
/>
</androidx.cardview.widget.CardView>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<androidx.appcompat.widget.ActionMenuView
android:id="#+id/mnuItem"
android:layout_width="match_parent"
android:layout_height="250dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/appBarLayout"
android:background="#color/def2"
android:visibility="invisible"
/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/add_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:src="#drawable/poplike"
android:backgroundTint="#color/redButton2"
app:borderWidth="0dp"
app:fabSize="normal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<!--Floating action button for add alarm-->
<!--Make sure that you are constraining this
button to the parent button-->
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/add_alarm_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
app:fabSize="normal"
app:layout_constraintBottom_toTopOf="#+id/add_fab"
app:layout_constraintEnd_toEndOf="#+id/add_fab"
app:layout_constraintStart_toStartOf="#+id/add_fab"
app:srcCompat="#drawable/ic_baseline_add_24"
app:borderWidth="0dp"
android:backgroundTint="#color/redButton2"
/>
<!--Action name text for the add alarm button-->
<!--Make sure that you are constraining this Text to
the add Alarm FAB button-->
<TextView
android:id="#+id/add_alarm_action_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="Crea Post"
app:layout_constraintBottom_toBottomOf="#+id/add_alarm_fab"
app:layout_constraintEnd_toStartOf="#+id/add_alarm_fab"
app:layout_constraintTop_toTopOf="#+id/add_alarm_fab" />
<!--Floating action button for add person-->
<!--Make sure that you are constraining this
button to the add Alarm FAB button-->
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/add_person_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
app:fabSize="normal"
app:layout_constraintBottom_toTopOf="#+id/add_alarm_fab"
app:layout_constraintEnd_toEndOf="#+id/add_alarm_fab"
app:layout_constraintStart_toStartOf="#+id/add_alarm_fab"
app:srcCompat="#drawable/ic_baseline_search_24"
app:borderWidth="0dp"
android:backgroundTint="#color/redButton2"
/>
<!--Action name text for the add person button-->
<!--Make sure that you are constraining this Text
to the add Person FAB button-->
<TextView
android:id="#+id/add_person_action_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="Cerca utenti"
app:layout_constraintBottom_toBottomOf="#+id/add_person_fab"
app:layout_constraintEnd_toStartOf="#+id/add_person_fab"
app:layout_constraintTop_toTopOf="#+id/add_person_fab" />
<Button
android:id="#+id/btn_logout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="152dp"
android:text="Logout"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/appBarLayout"
app:layout_constraintVertical_bias="0.111" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rcViewPostMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="56dp"
app:layout_constraintTop_toBottomOf="#+id/appBarLayout"
tools:layout_editor_absoluteX="-16dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
I tried to set an OnClick listener from the java code but when I go to run the app the onclick doesn't work when I click on the image:
ImageView img_profilo = findViewById(R.id.img_profilomain);
img_profilo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "d", Toast.LENGTH_SHORT).show();
mnuItem.setVisibility(View.VISIBLE);
}
});
I tried to do it with the toolbar too but it doesn't work anyway:
tlb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "sad", Toast.LENGTH_SHORT).show();
}
});
Edit:
It doesn't work with this either
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
if(item.getItemId() == R.id.img_profilomain)
Toast.makeText(MainActivity.this, "wds", Toast.LENGTH_SHORT).show();
return true;
}
});
Would anyone know how to solve this problem?
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>
I am creating an Faq page using TextViews, So the textviews become visible and hide automatically when clicked, but this was overlapping the upcoming question so I researched and found marginlayoutparams solution but this code won't reset back when clicked again.
Initial state:
After I click Fare Charges:
After I click on it again:
The code:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class faq extends AppCompatActivity {
TextView mfaq,mAns,mfaq2,mAns2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_faq);
mfaq=findViewById(R.id.faq1);
mAns=findViewById(R.id.ans);
mfaq2=findViewById(R.id.faq2);
mAns2=findViewById(R.id.ans2);
mfaq.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(mAns.getVisibility()== View.VISIBLE){
ViewGroup.MarginLayoutParams marginParams = (ViewGroup.MarginLayoutParams) mfaq2.getLayoutParams();
marginParams.setMargins(marginParams.leftMargin,
-400, //setting it back to 0 this part isnt working
marginParams.rightMargin,
marginParams.bottomMargin);
mAns.setVisibility(View.GONE);
}
else
mAns.setVisibility(View.VISIBLE);
ViewGroup.MarginLayoutParams marginParams = (ViewGroup.MarginLayoutParams) mfaq2.getLayoutParams();
marginParams.setMargins(marginParams.leftMargin,
400, //only changing top margin
marginParams.rightMargin,
marginParams.bottomMargin);
}
});
mfaq2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(mAns2.getVisibility()== View.VISIBLE){
mAns2.setVisibility(View.GONE);
}
else
mAns2.setVisibility(View.VISIBLE);
}
});
}
}
I don't know how to set it back to initial state like first image. Please help. Note- I am a beginner so please explain me so I can learn and understand.
Can you print the xml ? try with "wrap_content" in parent layout
You can use Cardview to encapsulate your Faq element :
https://developer.android.com/guide/topics/ui/layout/cardview
Then change the visibility of the component that you want to hide as gone and set a listner to change the visibility if the textView (or your cardView ) is clicked
Textview label = (TextView ) myPosts.findViewById(R.id.cancel);
cancelButton.setOnClickListener(view->{
if(newPostCard.getVisibility() == View.VISIBLE){
TransitionManager.beginDelayedTransition(cardView,
new AutoTransition());
newPostCard.setVisibility(View.GONE);
newPostButton.setVisibility(View.VISIBLE);
}
else{
TransitionManager.beginDelayedTransition(cardView,
new AutoTransition());
newPostCard.setVisibility(View.VISIBLE);
newPostButton.setVisibility(View.GONE);
}
});
The way I got around to this problem was to add a linear layout to the questions and answer fields. here is the xml code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
android:background="#android:color/holo_orange_dark"
tools:context=".faq">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="300dp"
tools:context=".faq">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="#string/Welcome"
android:textColor="#000000"
android:textColorHighlight="#000000"
android:textColorHint="#000000"
android:textSize="36sp"
app:fontFamily="#font/racing_sans_one"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/frequentlyaskedquestions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="#string/faqs"
android:textColor="#FFFFFF"
android:textColorHighlight="#FFFFFF"
android:textColorHint="#FFFFFF"
android:textColorLink="#FFFFFF"
android:textSize="18sp"
app:fontFamily="#font/racing_sans_one"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
<TextView
android:id="#+id/clickonthem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="#string/clickonthem"
android:textColor="#FFFFFF"
android:textColorHighlight="#FFFFFF"
android:textColorHint="#FFFFFF"
android:textColorLink="#FFFFFF"
android:textSize="18sp"
app:fontFamily="#font/racing_sans_one"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/frequentlyaskedquestions" />
<LinearLayout
android:layout_width="409dp"
android:layout_height="294dp"
android:layout_marginTop="28dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/clickonthem">
<TextView
android:id="#+id/faq1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:clickable="true"
android:focusable="true"
android:text="#string/q1"
android:textColor="#000000"
android:textColorHighlight="#000000"
android:textColorHint="#000000"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:id="#+id/ans"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:layout_marginRight="20dp"
android:text="#string/a1"
android:textColor="#000000"
android:textColorHighlight="#000000"
android:textColorHint="#000000"
android:textColorLink="#000000"
android:textSize="18sp"
android:textStyle="bold|italic"
android:visibility="gone" />
<TextView
android:id="#+id/faq2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:clickable="true"
android:focusable="true"
android:text="#string/q2"
android:textColor="#000000"
android:textColorHighlight="#000000"
android:textColorHint="#000000"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:id="#+id/ans2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:layout_marginRight="20dp"
android:text="#string/a2"
android:textColor="#000000"
android:textColorHighlight="#000000"
android:textColorHint="#000000"
android:textColorLink="#000000"
android:textSize="18sp"
android:textStyle="bold|italic"
android:visibility="gone" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
Im a little stuck, how can i get a variable from main activity to be displayed on a second activity?
a code example would be great.
Also a second Problem:
How can i create a function in mainactivty when a button is pressed in a second activity?
This is what i have so far, but when i press the button in the second activity, the app crashes.
the button's function needs to be able to change a variable's value in MainActivity and Run a toast saying it was selected.
Main Activity
//SETTING THE DRINK SIZE BASED ON POPUP BUTTONS
public int DrinkSize;
public void SetDrinkSize_Small(View view) {
DrinkSize = 1;
Toast Small = Toast.makeText(getApplicationContext(),
"Drink Size Set To Small",
Toast.LENGTH_SHORT);
Small.show();
}
public void SetDrinkSize_Medium(View view) {
DrinkSize = 2;
Toast Medium = Toast.makeText(getApplicationContext(),
"Drink Size Set To Medium",
Toast.LENGTH_SHORT);
Medium.show();
}
public void SetDrinkSize_Large(View view) {
DrinkSize = 3;
Toast Large = Toast.makeText(getApplicationContext(),
"Drink Size Set To Large",
Toast.LENGTH_SHORT);
Large.show();
}
CustomPopUp.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:background="#color/Orange"
android:gravity="center_horizontal"
android:orientation="vertical"
android:onClick="SetDrinkSize_Small">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small"
android:textColor="#color/White"
android:textSize="18dp"
android:textStyle="bold" />
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="15dp"
android:src="#drawable/drop" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:background="#color/Green"
android:gravity="center_horizontal"
android:orientation="vertical"
android:onClick="SetDrinkSize_Medium">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium"
android:textColor="#color/White"
android:textSize="18dp"
android:textStyle="bold" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="15dp"
android:src="#drawable/drop" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:background="#color/Orange"
android:gravity="center_horizontal"
android:orientation="vertical"
android:onClick="SetDrinkSize_Large">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large"
android:textColor="#color/White"
android:textSize="18dp"
android:textStyle="bold" />
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginTop="15dp"
android:src="#drawable/drop" />
</LinearLayout>
You can easily send and receive data using intents, as you can see in this Android tutorial: Intent tutorial.
And in case you want to send some data back to the first one, you can use this post: Sending info back.