0

I am trying to hide the TextView (Bigger One) whatever has opened before it opens the corrosponding textview(Bigger One) on clicking another textview (Smaller One)

I did the program like below its not hiding the opened TextView(Bigger One) before opening the corresponding textview(BiggerOne) under the TextView(Samller One).

I am new to Android please help me to solve this problem.

Program:

public class lessonOne extends AppCompatActivity {

    Toolbar toolbar;
    private CharSequence mTitle;
    LinearLayout linearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lesson_one);


        //Start a new Activity on clicking the TextView
        final TextView Engtxt = (TextView) findViewById(R.id.jan);
        Engtxt.setOnClickListener(new View.OnClickListener(){

            public void onClick(View v) {

                TextView txt = (TextView) findViewById(R.id.jan);
                //Define the map to iterate the visibility TextView
                Map<Integer, Boolean> map = new HashMap<Integer, Boolean>();
                map.put(v.getId(), FALSE);
                if (map.get(v.getId()) == FALSE) {

                    Engtxt.setVisibility(View.GONE);
                    TextView titleText = (TextView) findViewById(R.id.lesOneTitle);
                    titleText.setVisibility(View.GONE);
                    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear);
                    linearLayout.setOrientation(LinearLayout.VERTICAL);
                    /* Add textview 1 */
                    TextView textView1 = new TextView(lessonOne.this);
                    //            textView1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    //                    LinearLayout.LayoutParams.MATCH_PARENT));
                    LinearLayout.LayoutParams textviewLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 200);
                    textviewLayoutParams.setMargins(0, 9, 0, 0);
                    //linearLayout.addView(textView1, 1, textviewLayoutParams);
                    textView1.setLayoutParams(textviewLayoutParams);
                    textView1.setBackgroundResource(R.drawable.shapelineinsideroundedrect);
                    textView1.setText("JANUARY - ஜனவரி\n\nजनवरी - JANUARY - ஜனவரி");
                    textView1.setGravity(Gravity.TOP | Gravity.CENTER);
                    textView1.setTextSize(20);
                    textView1.setTypeface(null, Typeface.BOLD);
                    textView1.setPadding(0, 25, 0, 0);
                    textView1.setVisibility(View.VISIBLE);
                    linearLayout.addView(textView1, 0);
                    //.setContentView(linearLayout, new LinearLayout.LayoutParams(
                    //LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
                    map.put(v.getId(),TRUE);
                }else {
                    TextView extendedText = (TextView) findViewById(v.getId());
                    extendedText.setVisibility(View.GONE);
                    map.put(v.getId(),FALSE);
                }
            }
        });

        //Call the Toolbar method to set and the getTitle method to be visible
        setupToolbar();
        mTitle = getTitle();

    }

    void setupToolbar(){
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }

    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        getSupportActionBar().setTitle(mTitle);
    }
}
Benkerroum Mohamed
  • 1,867
  • 3
  • 13
  • 19
  • First thing, you should declare your `HashMap` object outside of your `OnClickListener`. With your actual code, your map is always reset, so it could cause your issue. – Lodoss Jul 25 '18 at 14:41
  • Ya i declared the HashMap object outside the OnClickListener. But how to hide all the TextView _2 which has opened by clicking the TextView_1.Could you please explain it – Mohamed Noushad Jul 25 '18 at 16:27
  • Loop over the children of `linearLayout` like it is shown [here](https://stackoverflow.com/questions/8395168/android-get-children-inside-a-view/11263152#11263152). Set their visibility as required. – Bö macht Blau Jul 25 '18 at 18:15
  • thanks for the response. I have loop over children of linear layout.but while randomly clicking some textview is not opening the other textview under that instead it is displaying in some other location.(due to the Index i have given to the textview that i have given) . How to make it like it will open the particular textview under the corresponding textview on clicking the random textview – Mohamed Noushad Jul 31 '18 at 10:36
  • thanks it got worked by using view group. – Mohamed Noushad Oct 31 '18 at 05:17

0 Answers0