-1

I have two LinearLayouts in my application side by side. In 1st LinearLayout (left side),I have some name lists.I want to implement the connection or relationship between them in such a way that when I clicked on one of the name list items, a text message or some design will display into the 2nd Linear Layout (right side). I have implemented OnClickListener with Toast but I don't have an idea to display a text message or some design into the 2nd Linear Layout. Thanks in advance for any help!

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:id="@+id/relLayoutMiddle"  android:orientation="horizontal"
            android:layout_below="@+id/relLayoutTopBar"
            android:layout_above="@+id/relLayoutBottomBar">

            <!-- Items List-->
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="80dp"
                android:layout_height="fill_parent"
                android:layout_marginRight="5dp"
                android:background="@drawable/border_design">    
                <ListView
                    android:id="@+id/item_List" android:scrollbars="none"
                    android:layout_width="match_parent"
                    android:focusable="true"
                    android:layout_height="fill_parent">
                </ListView>
            </LinearLayout>

            <!-- Display Items-->
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1" android:layout_marginLeft="5dp"
                android:background="@drawable/border_design">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="Items  will be Display here"/>
            </LinearLayout>
        </LinearLayout>

4 Answers4

0

you should have an id for every view that you want to work with in your Java code. then you can find that view with findViewById method in your activity. so for your view , you should consider an id for your second textView for example tvDisplay, and then in your onClickListener you should set text into your textView by a code like this:

((TextView)findViewById(R.id.tvDisplay)).setText("hello")
Reza.Abedini
  • 2,227
  • 2
  • 16
  • 18
  • It works for showing a text message.Thanks a lot @Reza.Abedini. But what do I do if I want to implement RecyclerView for displaying images in grid form? – Preetam Khangembam Sep 14 '18 at 06:22
  • your welcome if it solved your problem please make this post as answer. for implementing grid recyclerView please check this post: https://stackoverflow.com/questions/40587168/simple-android-grid-example-using-recyclerview-with-gridlayoutmanager-like-the – Reza.Abedini Sep 14 '18 at 06:24
0

Supposedly, you have implemented the code to check if a list item is clicked, you only need to assign an id to your target text view. Ex:

<TextView
 android:id="@+id/targetID"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:text="Items  will be Display here"/>

Then you need to find the TextView using its id in code. Ex:

TextView tv = (TextView)View.findViewById(R.id.targetID);

And change the content of tv using :

 tv.setText("What you want to write here");

Note: The code isn't meant to copy paste. It is a suggestion or approach you can take.

waterbyte
  • 241
  • 1
  • 16
0
 listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            textView.setText(listview.getItemAtPosition(position).toString());
        }

use this code and implement listview click listener

Archu Mohan
  • 199
  • 1
  • 5
  • 14
0

You can use this way to implement it.

In XML layout :

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:id="@+id/relLayoutMiddle"
    android:orientation="horizontal">

    <!-- Items List-->
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="80dp"
        android:layout_height="fill_parent"
        android:layout_marginRight="5dp">
        <ListView
            android:id="@+id/item_List"
            android:scrollbars="none"
            android:layout_width="match_parent"
            android:focusable="true"
            android:layout_height="fill_parent">
        </ListView>
    </LinearLayout>

    <!-- Display Items-->
    <LinearLayout
        android:id="@+id/ll_second"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:layout_marginLeft="5dp">
        <TextView
            android:id="@+id/tv_message"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Items  will be Display here"/>
    </LinearLayout>
</LinearLayout>

In the code :

    final String[] names = {"one", "two", "three"};
    llSecond = findViewById(R.id.ll_second);
    tvMessage = findViewById(R.id.tv_message);
    ListView lvItem = findViewById(R.id.item_List);
    lvItem.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_activated_1, android.R.id.text1, names));
    lvItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // display a text message that selected in ListView into TextView
            tvMessage.setText(names[position]);

            // Or call user defined function that make to second LinearLayout dynamically with programmatical.
            // makeLayout();
        }
    });
ld-sr-dev
  • 41
  • 1
  • 5