user3623979
user3623979

Reputation:

how to get a layout on click of imageview?

enter image description here

Hello.. I have a layout in which I have ImageView and EditText view. I want to add a different layout on click of ImageView of arrow.

enter image description here

It should look like this after click event of ImageView. How can I get this?? Pleas help...

Upvotes: 0

Views: 396

Answers (2)

Chirag Savsani
Chirag Savsani

Reputation: 6140

For perfect answer please add your xml file and java file source code.

Example

MainActivity.java

public class MainActivity extends Activity {

    LinearLayout linear1,linear2;
    ImageView imgArrow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        linear1 = (LinearLayout) findViewById(R.id.linear1);
        linear2 = (LinearLayout) findViewById(R.id.linear2);
        imgArrow = (ImageView) findViewById(R.id.imgArrow);

        linear1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if(linear2.getVisibility() == View.GONE){
                    linear2.setVisibility(View.VISIBLE);
                    imgArrow.setImageResource(R.drawable.up);
                }else{
                    linear2.setVisibility(View.GONE);
                    imgArrow.setImageResource(R.drawable.down);
                }
            }
        });
    }
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linear1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/btnClick"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.3"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/tv1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="0.5"
            android:text="Additional Contact" />

        <ImageView
            android:id="@+id/imgArrow"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="0.2"
            android:src="@drawable/down" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linear2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:visibility="gone"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/txt1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="User Name here" />


        <TextView
            android:id="@+id/txt2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="Phone number here" />

        <Button
            android:id="@+id/btnone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="Click Here" />
    </LinearLayout>

</LinearLayout>

Here down and up is image file for arrow. so put that both arrow file in drawable folder.

enter image description here enter image description here

Upvotes: 1

Sebastian Walla
Sebastian Walla

Reputation: 1124

You could just set an OnClickListener to the ImageView, which contains the arrow. Then assign a method, which handles the click and produces your different layout

Upvotes: 0

Related Questions