Skip to content Skip to sidebar Skip to footer

How To Append Text With Onclick Method

I would like to create a string whose components are appended once an ImageView is pressed. I have an ImageView called imageView and named ImageView on clicking which I am appendin

Solution 1:

the frirst error for this line

ImageViewImageview= (ImageView) findViewById(R.id.imageView);

you can't use findById in your class block you have to use it in a function like onCreate so change your code .... your code have to something like this

ImageView imageView;
    TextView textView;
    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        imageView= (ImageView) findViewById(R.id.imageView);
        textView=(TextView) findViewById(R.id.tv_show);
        imageView.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View v) {
                String test=textView.getText().toString();
                textView.setText(test);

            }
        });
    }

just remember you have to add a textView to your xml file and set this id to tv_show;

Solution 2:

Your Main Activity Java class have somthing like this

publicclassMainActivityextendsAppCompatActivity {

    Button button;
    Button click;
    Stringresult="";

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        click = (Button) findViewById(R.id.button);
        finalintvalue1=300;
        finaldoublevalue2=3.14;
        finalshortvalue3=5;
        finalcharvalue4='A';

        Toolbartoolbar= (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        FloatingActionButtonfab= (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View view) {
                //create the send intentIntentshareIntent=newIntent(android.content.Intent.ACTION_SEND);

//set the type
                shareIntent.setType("text/plain");

//add a subject
                shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
                        "Insert Subject Here");

//build the body of the message to be sharedStringshareMessage="Insert message body here.";

//add the message
                shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,
                        shareMessage);

//start the chooser for sharing
                startActivity(Intent.createChooser(shareIntent, getResources().getString(R.string.FinalString)));
            }
        });
        // Create StringBuilder and add four values to it.finalStringBuilderbuilder=newStringBuilder();
        builder.append(value2).append("\n");
        builder.append(value3).append("\n");
        builder.append(value4);

        ImageViewimageview= (ImageView) findViewById(R.id.imageView);
        imageview.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View v) {
                builder.append(value1).append("\n");

            }
        });
        click.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View v) {
                Toast.makeText(MainActivity.this,builder.toString(),Toast.LENGTH_LONG).show();
            }
        });


    }




}

i change Your XMl File To This iths better

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity"><TextViewandroid:id="@+id/textView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello World!" /><ImageViewandroid:id="@+id/imageView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="88dp"android:clickable="true"android:src="@android:drawable/ic_input_add" /><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="New Button" /><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Large Text"android:textAppearance="?android:attr/textAppearanceLarge" /></LinearLayout>

I Test It every things Fine

And Show comment on Your Error there

Solution 3:

Make TextView then append(); :

in XML:

<TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical"
    android:text="" />

in Java:

publicvoidonClick(View v) {
    Imageview.setOnClickListener(newView.OnClickListener() {
        TextViewtv= (TextView) findViewById(R.id.textView);
        tv.setMovementMethod(newScrollingMovementMethod());
        tv.append(value1 + "\n");
    }
}

Solution 4:

You can append the string like this on button click

StringstringName="";
Imageview.setOnClickListener(newView.OnClickListener() {
    strigName = stringName + "New appended data";
}

button.setOnClickListener(newView.OnClickListener() {
    Toast.makeText(MainActivity.this, stringName, Toast.LENGTH_SHORT).show();
}

Post a Comment for "How To Append Text With Onclick Method"