Ronak Mehta
Ronak Mehta

Reputation: 5979

How to move image view in android

in my application i want to move "plus" image dynamically when iI click on plus image it comes beside my new generated EditText box in android and my textview and edittext box both dynamically coming on new row see my image. I want it in single row.

My code:

package com.test;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Test_testActivity extends Activity {
    /** Called when the activity is first created. */
     private LinearLayout mLayout;
     private EditText mEditText;
     private ImageView mButton;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mLayout = (LinearLayout) findViewById(R.id.linearLayout1);  
        mButton = (ImageView) findViewById(R.id.imageView1);   
        mButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //mButton.setVisibility(Button.GONE);
                 mLayout.addView(createEditText());              
                 mLayout.addView(seconftext());     
            }
        });      
    }
    private EditText createEditText()
    {
        final LayoutParams lparams = new LayoutParams(150,100); // Width , height
        final EditText edittext = new EditText(this);
        edittext.setLayoutParams(lparams);
        return edittext;
    }
    private TextView seconftext()
    {
        final LayoutParams iparams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        final TextView tv = new TextView(this);
        tv.setLayoutParams(iparams);
        tv.setText("Second");
        return tv;
    }
}

and .xml is

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
        android:id="@+id/linearlayout">
    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" 
        android:background="#00ffff">
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/plus" />
    </LinearLayout>
    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:orientation="vertical">
        </LinearLayout>
    </ScrollView>
</LinearLayout>

enter image description here

Upvotes: 0

Views: 649

Answers (1)

Gangnus
Gangnus

Reputation: 24484

If you mean the movement around over the other boxes, make a root FrameLayout, put 2 layouts in it: in the first there will be only your plus in a RelativeLayout, in the second all other boxes. On plus touching add a listener and launch animation by it. As the plus is in its own layer, you can move it as you wish.

"it must be removed from old postition and comes beside new EdiText box when i click that plus image it create new image and once again comes beside a new EdiText box"

If you only want it to disappear in one place and appear in another, it needn't be the same object. Make two different views with one image. And make one of them visible and other invisible, according to need.

Beware of using LayoutParams without saying what class are they from. It crushes the application. The class should be that of the parent layout. For example, LinearLayout.LayoutParams.

Upvotes: 1

Related Questions