-1

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 :)

JJJCR
  • 11
  • 8

2 Answers2

0

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);
AndyFaizan
  • 1,833
  • 21
  • 30
  • Hi AndyFaizan, thank you for your reply. create programmatically the EditText but how can I call the clipboard method to be used on the new EditText? sorry still learning and confused. :) – JJJCR Feb 23 '14 at 07:36
  • Oh I missed that. I thought you just wanted to add an EditText. In that case, himanshu virmani's answer will do it correctly – AndyFaizan Feb 23 '14 at 07:39
0

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..

Himanshu Virmani
  • 2,450
  • 1
  • 24
  • 34
  • hi himanshu, thanks for your reply. i had tried to create a class of the copy button. but i had an error on this line: clipBoard.setText(Editsrc.getText());, how can i set the Editsrc as a temporary variable, so it can be replace when called from other class? thank you. so sorry still crawling to grasp this things.. :) – JJJCR Feb 23 '14 at 07:42
  • just call copyButton.setEditText(Editsrc) in every class.. And in the CopyButton class create a method setEditText(EditText editSrc) – Himanshu Virmani Feb 23 '14 at 08:05
  • i know this must be too naive, but i got problem initializing the editSrc variable in CopyButton class. it throws an error. if set to null, nothing is returned. how to initialize editSrc in copybutton class? Thanks. – JJJCR Feb 23 '14 at 10:51
  • It will be set to null initially but we will set it to the edittext object that is sent by the using class by calling setEditText method as written in previous answer – Himanshu Virmani Feb 23 '14 at 13:49
  • i have this code but its empty, but clipboard is empty. EditText src1 = null; android.text.ClipboardManager clipBoard = (android.text.ClipboardManager) ((Activity) ClipboardManager).getSystemService(CLIPBOARD_SERVICE); EditText mEdit = src1; clipBoard.setText(mEdit.getText()); – JJJCR Feb 23 '14 at 15:05