Question

how to create a class function and call it on other classes that needs the function?

I have this code below, which works okay in a single class.

But is there a way to make a function, so I will just replace the source which is the id of EditText.

So there is no need to copy this code to every class that needs copy function.

final EditText Editsrc = (EditText)findViewById(R.id.XXtxtview);

Button copynPaste = (Button)findViewById(R.id.copynpaste);

final ClipboardManager clipBoard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);

copynPaste.setOnClickListener(new Button.OnClickListener(){

@SuppressWarnings("deprecation")
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Copied to clipboard", Toast.LENGTH_SHORT).show();
clipBoard.setText(Editsrc.getText()); 
}});

Thank you for any help :)

Was it helpful?

Solution

Just create a CopyButton class that extends Android's Button class. Implement the methods you just mentioned... Set the edit text in that class by sending the EditText object to CopyButton class..

OTHER TIPS

You don't need to refer to an existing EditText in a layout. You could programmatically create one every time you want and just include it as a child view in your layout.

EditText myEditText = new EditText(context); // Pass it an Activity or Context
myEditText.setLayoutParams(new LayoutParams(..., ...)); // Pass two args; must be LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, or an integer pixel value.
myLayout.addView(myEditText);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top