Android findViewById() in Custom View
View Custmv; private void initViews() { inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); Custmv = inflater.inflate(R.layout.id_number_edit_text_custom, this, true); editText = (EditText) findViewById(R.id.id_number_custom); loadButton = (ImageButton) findViewById(R.id.load_data_button); loadButton.setVisibility(RelativeLayout.INVISIBLE); loadData(); }private void loadData(){ loadButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { EditText firstName = (EditText) Custmv.getParent().findViewById(R.id.display_name); firstName.setText("Some Text"); } }); }
try like this.
Change your Method as following and check it will work
private void initViews() { inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(R.layout.id_number_edit_text_custom, this, true); View view = (View) inflater.inflate(R.layout.main, null); editText = (EditText) view.findViewById(R.id.id_number_custom); loadButton = (ImageButton) view.findViewById(R.id.load_data_button); loadButton.setVisibility(RelativeLayout.INVISIBLE); loadData();}
If it's fixed layout you can do like that:
public void onClick(View v) { ViewGroup parent = (ViewGroup) IdNumber.this.getParent(); EditText firstName = (EditText) parent.findViewById(R.id.display_name); firstName.setText("Some Text");}
If you want find the EditText in flexible layout, I will help you later. Hope this help.