I have a ScrollView with LinearLayout into it.After adding the Layout into my LinearLayout, I wanted to remove the view but it removes only the first view.
I have tried parent.removeView(myView)
My Code for Layout adding :
LayoutInflater inflater = getLayoutInflater();
final View view_top = inflater.inflate(R.layout.layout_top, linear_layout,false);
final View view_bottom = inflater.inflate(R.layout.layout_bottom,linear_layout,false);
final RelativeLayout rel_layout_bottom = (RelativeLayout) view_bottom.findViewById(R.id.relative_bottom);
Button btn_update = (Button) rel_layout_bottom.findViewById(R.id.lst_todo_update);
ImageButton btn_remove = (ImageButton) rel_layout_bottom.findViewById(R.id.lst_btn_delete);
ImageButton btn_Color = (ImageButton) rel_layout_bottom.findViewById(R.id.lst_btn_color);
final RelativeLayout rel_layout_top = (RelativeLayout) view_top.findViewById(R.id.relative_top );
final TextView note_Title = (TextView) rel_layout_top.findViewById(R.id.lst_title );
final TextView note_color = (TextView) rel_layout_top.findViewById(R.id.lst_color_type );
note_Title.setText(note_title);
linear_layout.addView(rel_layout_bottom, 0);
JSONArray jsonArray=queryResult.getResultObjects().get(count).getJSONArray("todo_item");
for (int i = 0; i < jsonArray.length(); i++) {
final View view_middle = inflater.inflate(R.layout.layout_middle, linear_layout, false);
final RelativeLayout rel_layout_middle = (RelativeLayout) view_middle.findViewById(R.id.relative_middle);
final CheckBox note_check ;
final TextView note_content ;
note_content = (TextView) rel_layout_middle.findViewById(R.id.lst_content);
note_check = (CheckBox) rel_layout_middle.findViewById(R.id.lst_check);
btn_remove.setOnClickListener(null);
try {
//Getting data correctly here
linear_layout.addView(view_middle,0);
btn_remove.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
linear_layout.removeView(view_middle); //Not able to remove the view here i.e view_middle
linear_layout.removeView(view_top);
linear_layout.removeView(view_bottom);
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
linear_layout.addView(rel_layout_top , 0);
}
Any answer Appreciated...Thks
Something like below will solve your problem.
final View[] view_middleArr;
for (int i = 0; i < jsonArray.length(); i++) {
view_middleArr = new View[jsonArray.length()];
view_middleArr[i] = inflater.inflate(R.layout.layout_middle, linear_layout, false);
// ...
btn_remove.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
for (View view_middle : view_middleArr) {
linear_layout.removeView(view_middle);
}
linear_layout.removeView(view_top);
linear_layout.removeView(view_bottom);
}
});
}
Edit: You are using same view_middle reference for all your view_middle istances inside your loop, so you can only remove last view_middle instance with your code.
The code
btn_remove.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
linear_layout.removeView(view_middle); //Not able to remove the view here i.e view_middle
linear_layout.removeView(view_top);
linear_layout.removeView(view_bottom);
}
});
is in a for loop.
It means the click of the button will only remove the view_middle that was lastly added to the LinearLayout, as you are using the same object name.
Related
I created a Custom View and add setOnClickListener...
View's touch range is strange
how do i fix that?
red square is touch range...
XML file(custom.xml)
<LinearLayout>
<TextView> ~~
<TextView> ~~
</LinearLayout>
JAVA code(custom.java)
public class Custom_Lab_Button extends LinearLayout {
LinearLayout cus_lab_ll;
TextView cus_lab_tv1, cus_lab_tv2;
private void initView(Context context) {
inflate(getContext(), R.layout.custom_lab_button, this);
// String infService = Context.LAYOUT_INFLATER_SERVICE;
// LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(infService);
//// layoutInflater.inflate(R.layout.custom_lab_button, this, false);
// View v = layoutInflater.inflate(R.layout.custom_lab_button, getContent(), false);
// addView(v);
cus_lab_ll = (LinearLayout) findViewById(R.id.cus_lab_ll);
cus_lab_tv1 = (TextView) findViewById(R.id.cus_lab_tv1);
cus_lab_tv2 = (TextView) findViewById(R.id.cus_lab_tv2);
}
public void setCus_lab_ll_back(int cus_ll_resID) {
cus_lab_ll.setBackgroundColor(cus_ll_resID);
}
public void setCus_lab_ll_margin(LayoutParams layoutParams){
cus_lab_ll.setLayoutParams(layoutParams);
}
public LayoutParams getCus_lab_ll_margin(){
return (LayoutParams) cus_lab_ll.getLayoutParams();
}
}
Main Java Code
RelativeLayout cl_img = (RelativeLayout)view.findbyid(R.id.cl_img);
labButton = new Custom_Lab_Button(getActivity());
LinearLayout.LayoutParams tttt = labButton.getCus_lab_ll_margin();
tttt.leftMargin = 300;
tttt.topMargin = 300;
labButton.setCus_lab_ll_back(Color.BLUE);
labButton.setCus_lab_tv1_txt("lab1");
labButton.setCus_lab_ll_margin(tttt);
cl_img.addView(labButton);
labButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "it is lab button 1/", Toast.LENGTH_SHORT).show();
}
});
I had similar problem, use padding instead of margin.
I solved.. myself :)
labButton = new Custom_Lab_Button(getContext());
LinearLayout.LayoutParams tttt = labButton.getCus_lab_ll_margin();
tttt.leftMargin = 300;
tttt.topMargin = 300;
labButton.setCus_lab_tv1_txt("lab1");
labButton.setCus_lab_ll_margin(tttt);
labButton.setCus_lab_ll_back(Color.BLUE);
cl_img.setBackgroundColor(Color.GREEN);
cl_img.addView(labButton);
labButton.cus_lab_ll.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "it is lab button 1", Toast.LENGTH_SHORT).show();
}
});
[labButton.cus_lab_ll.setOnClickListener] is reason..
I need the value of inflated editText, I am inflating the design layout which is the child layout and MainActivity is a parent layout.
below is the MainActivity class
public class MainActivity extends AppCompatActivity {
View array[];
String text;
LinearLayout container;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText edittext1 = (EditText) findViewById(R.id.email);
for (int i = 0; i < 4; i++) {
LinearLayout myLayout = (LinearLayout) findViewById(R.id.linearlayout);
View v = getLayoutInflater().inflate(R.layout.design, myLayout, false);
myLayout.addView(v);
int id = View.generateViewId();
v.setId(id);
EditText edittext = (EditText) v.findViewById(R.id.edittext);
text = edittext.getText().toString();
}
edittext1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplication(), "" + text, Toast.LENGTH_SHORT).show();
}
});
}
}
Here is total code of store object and retrieval,
read all comments please,
public class MainActivity extends AppCompatActivity {
View array[];
String text;
LinearLayout container;
// list of edittext
List<EditText> edtlist = new ArrayList()<EditText>;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText edittext1 = (EditText) findViewById(R.id.email);
for (int i = 0; i < 4; i++) {
LinearLayout myLayout = (LinearLayout) findViewById(R.id.linearlayout);
View v = getLayoutInflater().inflate(R.layout.design, myLayout, false);
myLayout.addView(v);
int id = View.generateViewId();
v.setId(id);
EditText edittext = (EditText) v.findViewById(R.id.edittext);
// store object in list
edtlist.add(edittext);
text = edittext.getText().toString();
}
edittext1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplication(), "" + text, Toast.LENGTH_SHORT).show();
}
});
// call this whenever you want in any event
// here 0 is first edittext
// you can get all text value using for loop
// list.get(0).getText().toString();
}
}
You are looking for something like ,I'm not sure about it maybe it works maybe it wont.! it just a rough concept based code havnt Implemented yet.!
List<EditText> edtxtList;
List<String> edtxtStrList;
for (int i = 0; i < 4; i++) {
LinearLayout myLayout = (LinearLayout) findViewById(R.id.linearlayout);
View v = getLayoutInflater().inflate(R.layout.design, myLayout, false);
myLayout.addView(v);
int id = View.generateViewId();
v.setId(id);
EditText edittext = (EditText) v.findViewById(R.id.edittext);
text = edittext.getText().toString();
edtxtList.add(edittext);// try something like this.!
}
Then use Foreach loop on EditText;
for (EditText edText : edtxtList) {
if(!edText.getText().trim()=null){
edtxtStrList.add(edText.getText().trim());
}
}
I havnt tried It yet but maybe this ruff concept may helps you.!
Maybe this Method helps You.!
ArrayList<EditText> myEditTextList = new ArrayList<EditText>();
for( int i = 0; i < myLayout.getChildCount(); i++ )
if( myLayout.getChildAt( i ) instanceof EditText )
myEditTextList.add( (EditText) myLayout.getChildAt( i ) );
In Above code mLayout is your layout.!
<LinearLayout android:id="#+id/myLinearLayout" >
//Here is where your EditTexts would be declared
</LinearLayout>
i have 4 button create in programmatically button in for loop, is it possible to disable the onclicklistener function or ontouchlistener function by using button create in xml ? Sorry for my einglish, wish can be understand my question.
Sample Image
Code
public class Tab1 extends Fragment {
TextView textView, test1;
Button button, button2;
LinearLayout rl;
//Overriden method onCreateView
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View h = inflater.inflate(R.layout.tab1, container, false);
//Returning the layout file after inflating
//Change R.layout.tab1 in you classest
textView = (TextView) h.findViewById(R.id.textView);
test1 = (TextView) h.findViewById(R.id.test1);
button = (Button) h.findViewById(R.id.button);
button2 = (Button) h.findViewById(R.id.button2);
rl = (LinearLayout) h.findViewById(R.id.tlayout);
for (int i = 1; i < 5; i++) {
final Button btnAddARoom = new Button(getActivity());
RelativeLayout.LayoutParams params;
params = new RelativeLayout.LayoutParams
(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
btnAddARoom.setText("Add");
btnAddARoom.setLayoutParams(params);
rl.addView(btnAddARoom);
btnAddARoom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
test1.setText("qweqweqweqwe");
}
});
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btnAddARoom.setOnClickListener(null);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
test1.setText("111111111111111");
}
});
}
return h;
}
}
Add all the buttons you created from the for loop into an array.
ArrayList<Buttons> buttons = new ArrayList<Buttons>();
for(int i =1 ; i<5 ; i++) {
final Button btnAddARoom = new Button(getActivity());
RelativeLayout.LayoutParams params;
params = new RelativeLayout.LayoutParams
(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
btnAddARoom.setText("Add");
btnAddARoom.setLayoutParams(params);
rl.addView(btnAddARoom);
buttons.add(YourDynamicallyCreatedButton)
}
Now in your onClick of xml button:
onClick(){
for(Button button: buttons){
button.setEnable(false)
}
}
Note:
When we add a view(button or text..) via xml we give it an id. We can access these elements with this ID. Similarly, when we create buttons dynamically, we need to set id to the buttons/views.
Call setEnable(false) on your Button reference
Example:
Button b ;
..
..
b.setEnable(false);
Keep reference of your programatically added buttons:
Button[] buttons = new Buttons[5];
for(int i =0 ; i<buttons.length ; i++) {
buttons[i] = new Button(getActivity());
final Button btnAddARoom = buttons[i];
RelativeLayout.LayoutParams params;
params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
btnAddARoom.setText("Add");
btnAddARoom.setLayoutParams(params);
rl.addView(btnAddARoom);
btnAddARoom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
test1.setText("qweqweqweqwe");
}
});
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btnAddARoom.setOnClickListener(null);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
test1.setText("111111111111111");
}
});
}
Now when you want to disable these buttons:
private disableButtons() {
for(int i=0;i<buttons.length;i++) {
buttons[i].setEnabled(false);
}
}
Hope it will help you!
If you have a dedicated parent for the four buttons then you can eliminate the Arralist or array.
LinearLayout ll = ..//your linear layout or any other parent
final int childcount = ll.getChildCount();
for (int i = 0; i < childcount; i++) {
View view = ll.getChildAt(i);
if (view instanceof Button) {
view.setEnabled(false);
}
}
I am trying to change ImageView background when clicked(like Duolingo)
Here is my code from fragment :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.frag_repeat, container, false);
final int[] a1 = {0};
final int[] a2 = {0};
final int[] a3 = {0};
final int[] a4 = {0};
TypedArray itemsIcon = getResources().obtainTypedArray(R.array.nav_drawer_icons);
ImageView wer1 = (ImageView) rootView.findViewById(R.id.imageView);
ImageView wer2 = (ImageView) rootView.findViewById(R.id.imageView2);
ImageView wer3 = (ImageView) rootView.findViewById(R.id.imageView23);
ImageView wer4 = (ImageView) rootView.findViewById(R.id.imageView43);
TextView textView1 = (TextView) rootView.findViewById(R.id.textView711);
textView1.setText(ss[i]);
wer1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a1[0] = 1;
a2[0] = 0;
a3[0] = 0;
a4[0] = 0;
}
});
wer2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a2[0] = 1;
a1[0] = 0;
a3[0] = 0;
a4[0] = 0;
}
});
wer3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a3[0] = 1;
a2[0] = 0;
a1[0] = 0;
a4[0] = 0;
}
});
wer4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a4[0] = 1;
a2[0] = 0;
a3[0] = 0;
a1[0] = 0;
}
});
if(a1[0] > 0){
wer3.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer2.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer1.setBackgroundColor(Color.parseColor("#42A5F5"));
wer4.setBackgroundColor(Color.parseColor("#FFFFFF"));
} else if(a2[0] > 0){
wer1.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer3.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer2.setBackgroundColor(Color.parseColor("#42A5F5"));
wer4.setBackgroundColor(Color.parseColor("#FFFFFF"));
}else if(a3[0] > 0){
wer1.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer2.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer3.setBackgroundColor(Color.parseColor("#42A5F5"));
wer4.setBackgroundColor(Color.parseColor("#FFFFFF"));
} else if(a4[0] > 0){
wer1.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer2.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer4.setBackgroundColor(Color.parseColor("#42A5F5"));
wer3.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
if(i1 == 1){
wer1.setImageResource(itemsIcon.getResourceId(i, -1));
} else if(i1 == 2){
wer2.setImageResource(itemsIcon.getResourceId(i, -1));
} else if(i1 == 3){
wer3.setImageResource(itemsIcon.getResourceId(i, -1));
} else if(i1 == 4){
wer4.setImageResource(itemsIcon.getResourceId(i, -1));
}
return rootView;
}
But ImageView background doesn't change! I have CardViews inside RelativeLayout and inside RelativeLayout I have ImageView.
Please share xml file. Maybe you don't set background for ImageView in xml
Each of your onClick listeners is just toggling values in an array. I suspect what you really want is to invoke the setBackground color code inside each handler.
From your code, I think what you are trying to do is this:
Whichever button was clicked - change it's background color from white to #42A5F5.
For the other three buttons not clicked, change the background color back to white.
I don't know what the name of your class is (I'll refer to it as "YourClass"). But make your 4 ImageViews member variables.
class YourClass extends Fragment // I'm guessing this is the declaration, it doesn't matter - use whatever you have now
{
ImageView _wer1;
ImageView _wer2;
ImageView _wer3;
ImageView _wer4;
Then inside onCreateView, assign each one of these member variables exactly where you assign the local variables:
_wer1 = (ImageView) rootView.findViewById(R.id.imageView);
_wer2 = (ImageView) rootView.findViewById(R.id.imageView2);
_wer3 = (ImageView) rootView.findViewById(R.id.imageView23);
_wer4 = (ImageView) rootView.findViewById(R.id.imageView43);
Now set each of your click listeners to invoke a "ToggleBackgroundColors". Notice that there's a different index value passed to this function in each callback handler.
_wer1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
YourClass.this.ToggleBackgroundColors(0);
}
});
_wer2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
YourClass.this.ToggleBackgroundColors(1);
}
});
_wer3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
YourClass.this.ToggleBackgroundColors(2);
}
});
_wer4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
YourClass.this.ToggleBackgroundColors(3);
}
});
Then implement ToggleBackgroundColors:
void ToggleBackgroundColors(int which)
{
int highlight = Color.parseColor("#42A5F5");
ImageView [] views = {_wer1, _wer2, _wer3, _wer4};
for (int x = 0; x < views.length; x++)
{
int background = (which == x) ? highlight : Color.WHITE;
views[x].setBackgroundColor(background);
}
}
In your onClickListener, you can also set colorFilter on your imageView when you onClick it.
Consider using the onTouchListener instead though as you would like the imageView to change color when you touch the button and not when you click on it.
ImageView iv = (ImageView)findViewById(resIdOfImageToFilter);
iv.setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
I am using a recyclerView inside my navigation drawer and I am using this library Twoway-view to get click and selection support.
It works perfectly and I can change the color of the text and the icons without problems inside OnClick method for each position:
itemClickSupport.setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
#Override
public void onItemClick(RecyclerView parent, View view, int position, long id) {
TypedValue typedValue = new TypedValue();
MainActivity.this.getTheme().resolveAttribute(R.attr.colorAccent, typedValue, true);
final int color = typedValue.data;
//TODO Icon and text colors
for (int i = 0; i < drawerTitles.length; i++){
if (i == position){
ImageView imageViewDrawerIcon = (ImageView) recyclerViewDrawer.getChildAt(i).findViewById(R.id.imageViewDrawerIcon);
TextView textViewDrawerTitle = (TextView) recyclerViewDrawer.getChildAt(i).findViewById(R.id.textViewDrawerItemTitle);
imageViewDrawerIcon.setColorFilter(color);
if(Build.VERSION.SDK_INT > 15){
imageViewDrawerIcon.setImageAlpha(255);
}else{
imageViewDrawerIcon.setAlpha(255);
}
textViewDrawerTitle.setTextColor(color);
RelativeLayout relativeLayoutDrawerItem = (RelativeLayout) recyclerViewDrawer.getChildAt(i).findViewById(R.id.relativeLayoutDrawerItem);
relativeLayoutDrawerItem.setFocusableInTouchMode(true);
}else{
ImageView imageViewDrawerIcon = (ImageView) recyclerViewDrawer.getChildAt(i).findViewById(R.id.imageViewDrawerIcon);
TextView textViewDrawerTitle = (TextView) recyclerViewDrawer.getChildAt(i).findViewById(R.id.textViewDrawerItemTitle);
imageViewDrawerIcon.setColorFilter(getResources().getColor(R.color.md_text));
if(Build.VERSION.SDK_INT > 15){
imageViewDrawerIcon.setImageAlpha(138);
}else{
imageViewDrawerIcon.setAlpha(138);
}
textViewDrawerTitle.setTextColor(getResources().getColor(R.color.md_text));
RelativeLayout relativeLayoutDrawerItem = (RelativeLayout) recyclerViewDrawer.getChildAt(i).findViewById(R.id.relativeLayoutDrawerItem);
relativeLayoutDrawerItem.setFocusableInTouchMode(false);
}
}
//TODO Fragments (closedrawers before setfragment)
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// Do something after some time
}
}, 250);
mDrawerLayout.closeDrawers();
}
});
The problem is that I want to access to the first item like in that method, but before it (to set the first item selected, with different color, etc).
But I get a crash (null reference) when I try to get the first item outside OnClick method:
ImageView imageViewDrawerIcon = (ImageView) recyclerViewDrawer.getChildAt(0).findViewById(R.id.imageViewDrawerIcon);
All code:
// Setup RecyclerView inside drawer
recyclerViewDrawer = (RecyclerView) findViewById(R.id.recyclerViewDrawer);
recyclerViewDrawer.setHasFixedSize(true);
recyclerViewDrawer.setLayoutManager(new LinearLayoutManager(MainActivity.this));
ArrayList<DrawerItem> drawerItems = new ArrayList<>();
final String[] drawerTitles = getResources().getStringArray(R.array.drawer);
final TypedArray drawerIcons = getResources().obtainTypedArray(R.array.drawerIcons);
for (int i = 0; i < drawerTitles.length; i++) {
drawerItems.add(new DrawerItem(drawerTitles[i], drawerIcons.getDrawable(i)));
}
drawerIcons.recycle();
adapterDrawer = new DrawerAdapter(drawerItems);
recyclerViewDrawer.setAdapter(adapterDrawer);
// Here is the problem
ImageView imageViewDrawerIcon2 = (ImageView) recyclerViewDrawer.getChildAt(0).findViewById(R.id.imageViewDrawerIcon);
// RecyclerView item listener.
ItemClickSupport itemClickSupport = ItemClickSupport.addTo(recyclerViewDrawer);
itemClickSupport.setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
#Override
public void onItemClick(RecyclerView parent, View view, int position, long id) {
TypedValue typedValue = new TypedValue();
MainActivity.this.getTheme().resolveAttribute(R.attr.colorAccent, typedValue, true);
final int color = typedValue.data;
//TODO Icon and text colors
for (int i = 0; i < drawerTitles.length; i++){
if (i == position){
ImageView imageViewDrawerIcon = (ImageView) recyclerViewDrawer.getChildAt(i).findViewById(R.id.imageViewDrawerIcon);
TextView textViewDrawerTitle = (TextView) recyclerViewDrawer.getChildAt(i).findViewById(R.id.textViewDrawerItemTitle);
imageViewDrawerIcon.setColorFilter(color);
if(Build.VERSION.SDK_INT > 15){
imageViewDrawerIcon.setImageAlpha(255);
}else{
imageViewDrawerIcon.setAlpha(255);
}
textViewDrawerTitle.setTextColor(color);
RelativeLayout relativeLayoutDrawerItem = (RelativeLayout) recyclerViewDrawer.getChildAt(i).findViewById(R.id.relativeLayoutDrawerItem);
relativeLayoutDrawerItem.setFocusableInTouchMode(true);
}else{
ImageView imageViewDrawerIcon = (ImageView) recyclerViewDrawer.getChildAt(i).findViewById(R.id.imageViewDrawerIcon);
TextView textViewDrawerTitle = (TextView) recyclerViewDrawer.getChildAt(i).findViewById(R.id.textViewDrawerItemTitle);
imageViewDrawerIcon.setColorFilter(getResources().getColor(R.color.md_text));
if(Build.VERSION.SDK_INT > 15){
imageViewDrawerIcon.setImageAlpha(138);
}else{
imageViewDrawerIcon.setAlpha(138);
}
textViewDrawerTitle.setTextColor(getResources().getColor(R.color.md_text));
RelativeLayout relativeLayoutDrawerItem = (RelativeLayout) recyclerViewDrawer.getChildAt(i).findViewById(R.id.relativeLayoutDrawerItem);
relativeLayoutDrawerItem.setFocusableInTouchMode(false);
}
}
//TODO Fragments (closedrawers before setfragment)
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// Do something after some time
}
}, 250);
mDrawerLayout.closeDrawers();
}
});
In my case it,is work like a charm
View viewItem = recycleView.getLayoutManager().findViewByPosition(position);
View icon = viewItem.findViewById(R.id.view);
I solved finally with this (source):
recyclerViewDrawer.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
ImageView imageViewDrawerIcon = (ImageView) recyclerViewDrawer.getChildAt(0).findViewById(R.id.imageViewDrawerIcon);
TextView textViewDrawerTitle = (TextView) recyclerViewDrawer.getChildAt(0).findViewById(R.id.textViewDrawerItemTitle);
imageViewDrawerIcon.setColorFilter(color);
if (Build.VERSION.SDK_INT > 15) {
imageViewDrawerIcon.setImageAlpha(255);
} else {
imageViewDrawerIcon.setAlpha(255);
}
textViewDrawerTitle.setTextColor(color);
RelativeLayout relativeLayoutDrawerItem = (RelativeLayout) recyclerViewDrawer.getChildAt(0).findViewById(R.id.relativeLayoutDrawerItem);
relativeLayoutDrawerItem.setFocusableInTouchMode(true);
// unregister listener (this is important)
recyclerViewDrawer.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
By the way, if someone have a better solution I would like to know it.
This worked for me.
recyclerView.post(new Runnable() {
#Override
public void run() {
View viewItem = recycleView.getLayoutManager().findViewByPosition(position);
View icon = viewItem.findViewById(R.id.view);
}
});
I would use findViewHolderForAdapterPosition() or findViewByPosition() instead to find the root view at that index. The way you ensure the data is loaded is up to you, however.
findViewByPosition is a method of LayoutManager, which works with all LayoutManagers.
So you can use recycleView.getLayoutManager() and findViewByPosition(pos).
Also recyclerView.findViewHolderForAdapterPosition(pos) works too.
For more information, look at this RecyclerView - Get view at particular position