-2

In my page I have two parts. One is drop down which contains multiple categories. And other one contains products of all categories. Like I showed in image. When user Clicking any category then the page get scroll to the appropriate products of choosed category.

Up to now I have tried:- First of all, I need to bind dynamically Category names in the spinner. After that I need to bind the products with the Category name having the id of spinner's value. So, when user choosing anything I need to get the value from the spinner and after that I need to match that value with the products elements. Once it get matched then my page should scroll to that particular element. For this anybody having any idea?enter image description here

Ranjith Kumar
  • 171
  • 3
  • 21

3 Answers3

0

You can check your view visibility:

if (youtTextView.getVisibility() == View.VISIBLE) {
     //Your TextView is visible
} else {
     //Your TextView is not visible (invisible or gone)
}
epileptic
  • 27
  • 5
Evyatar Cohen
  • 292
  • 1
  • 9
  • 25
  • Here, When user choose an item in spinner, then I am just taking that value from the spinner and after that I need to check that is there any text view is present in the same name of choosed value of the spinner. For this Could you help me?@Evyatar Cohen – Ranjith Kumar Oct 08 '19 at 06:55
  • Do you mean that the TextView have the same value like the spinner? Like, if the user choose from the spiner "my string" then you need to find if a string with the value "my string" is visible? – Evyatar Cohen Oct 08 '19 at 07:09
  • Yes.. you are right... for ex: if user choose a value like "Brain" then i need to find the text view which having the id as same as he choosed like "Brain". – Ranjith Kumar Oct 08 '19 at 07:28
  • So maybe you can use loop to find which TextView is equal to the string from your spinner. and then check for the visibility of the specific TextView – Evyatar Cohen Oct 08 '19 at 07:42
  • yes.. for that only i don't know how to put my textview in loop for checking? – Ranjith Kumar Oct 08 '19 at 07:45
  • Hope this will help you: https://stackoverflow.com/questions/3221135/how-to-get-all-views-in-an-activity – Evyatar Cohen Oct 08 '19 at 09:00
0

A TextView needs an id to be assigned in your XML layout.

android:id="+id/SomeName"

Now in your main java class, you need to declare a TextView type variable.

private TextView textView;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.SomeName);

Now while typing the lastline if you see R.id."SomeName" appearing in the template of android studio , then that means your textview is present in the current layout which you have defined in the setContentView(....);

If its not present it will throw a redline or null error. As for Visibility, you can use the .getVisibility() function.

**UPDATED ANSWER **

  View parent = layout.getParent();
    for (int i = 0; i < parent.getChildCount(); i++) {
        View v = parent.getChildAt(i);
        if(v instanceof TextView)
        {
              String spinner= mySpinner.getSelectedItem().toString();
              String tv= v.getText().toString();
              if (str1.equals(str2)) {
            // Show code you want
        }
        }
    }
  • Here, When user choose an item in spinner, then I am just taking that value from the spinner and after that I need to check that is there any text view is present in the same name of choosed value of the spinner. For this Could you help me? @Sourish Mukherjee – Ranjith Kumar Oct 08 '19 at 06:56
  • You mean TextView with the same id name or TextView with the same text or content name? – Sourish Mukherjee Oct 08 '19 at 06:58
  • Let me give me a second to explain briefly... Spinner and text views available in the app page are having same value and ids. Like Spinner Value = Text View id...@Sourish Mukherjee – Ranjith Kumar Oct 08 '19 at 07:01
  • make sure your parent layout has child views. Also check the error being shown in the android studio. – Sourish Mukherjee Oct 08 '19 at 07:34
  • May I know why you are not using ListView or Recycler View – Sourish Mukherjee Oct 08 '19 at 13:00
  • What you can do is use a ListView. Set all of your categories in ListView by using a Custom Adapter Class. You can use the function .smoothScrollToPosition(int position); – Sourish Mukherjee Oct 08 '19 at 13:02
0

So you want to check text in TextView and Spinner, right? My suggestions:

mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            String str1 = mySpinner.getSelectedItem().toString();
            String str2 = textview.getText().toString();
            if (str1.equals(str2)) {
                // Show code you want
            }
        }
    });
DinhNguyen
  • 303
  • 2
  • 14
  • Thanks for your code... but in your code at line no 5, textview means which one? @DinhNguyen – Ranjith Kumar Oct 08 '19 at 07:23
  • **textview** is your Text View's id you want to check – DinhNguyen Oct 08 '19 at 07:26
  • but here you didn't check the textview is present or not using the spinner value? – Ranjith Kumar Oct 08 '19 at 07:31
  • if the textview don't have any text, so **str2** = "" (empty string). – DinhNguyen Oct 08 '19 at 07:36
  • here i need to check whether any text view having same id of spinner value which is user choosed – Ranjith Kumar Oct 08 '19 at 07:39
  • in your code ` String str1 = mySpinner.getSelectedItem().toString(); String str2 = textview.getText().toString(); if (str1.equals(str2)) { // Show code you want } }`..... you are taking the choosed value form the spinner and converting to string after that you stored it in `str1`. But in my case after this i need to check any text views are available or not with the same id of he choosed.. – Ranjith Kumar Oct 08 '19 at 07:44
  • I see, so you need to create a list of the textview you want to check, and use **for()** method to check if any textview equal selected item of spinner – DinhNguyen Oct 08 '19 at 07:50
  • By the OP 's comment in another answers – DinhNguyen Oct 08 '19 at 08:31