How do I affect another view's property when ImageView is pressed? - java

I have an ImageView inside a LinearLayout view. I want to change the color of the LinearLayout view while the ImageView is pressed.
I know I can swap out the ImageView image when the state changes through drawables, but I cannot seem to find the ideal way to affect another view in the layout while isPressed is true on this specific image view.
Ultimately, I am trying to create a bottom ActionBar and simulate the regular ActionBar highlight box (that is, when you press a menu item in the ActionBar you get the highlight box). Right now I have the ImageView a LinearLayout with a small (8dp) padding on the top and bottom. I can replace the image in the ImageView with one that has a 50% white background, but I cannot do it this way if I want to keep the images device density independent. Instead, I'd like to have a square layout the button exists in that I would change the color of as needed.
Ideas?

"I have an ImageView inside a LinearLayout view. I want to change the color of the LinearLayout view while the ImageView is pressed."
for this :
you define your ImageView in XML as clickable android:clickable="true"
you affect an OnClickListener to this ImageView in your Activity onCreat() :
ImageView yourImage = (ImageView) findViewById(R.id.your_image);
yourImage.setClickable(true); // if you want to define it here
yourImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
findViewById(R.id.your_linear_layout).setBackgroundColor(your_color);
}
});
Else if you want to change the color only when it is clicked and restore the old color after the click, you can implement OnTouchListener :
yourImage.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(final View v, MotionEvent event) {
int action = event.getAction() & MotionEvent.ACTION_MASK;
if (action == MotionEvent.ACTION_DOWN) {
// when the click begins
findViewById(R.id.your_linear_layout).setBackgroundColor(your_click_color);
return true;
} else {
// when the click finishs
findViewById(R.id.your_linear_layout).setBackgroundColor(your_init_color);
return true;
}
return false;
}
});
I hope I helped...

Related

how to hide a view in all recyclerview row after button clicked?

I want to create a recycler view like below images, when the user clicks on radio Button seek bar visible and radio button invisible in all recycler view's items.
brfore click image and after click image
this is my Recycler view adapter code:
#Override
public void onBindViewHolder(#NonNull final VoteRvViewHolder voteRvViewHolder, final int i) {
final VoteRecyclerViewDataModel dataModel = voteRecyclerViewDataModels.get(i);
voteRvViewHolder.optionContent.setText(dataModel.getOptionContent());
voteRvViewHolder.optionPercent.setText(dataModel.getPercentOption());
voteRvViewHolder.seekbar.setProgress(Integer.parseInt(dataModel.getPercentOption()));
voteRvViewHolder.seekbar.setVisibility(View.INVISIBLE);
voteRvViewHolder.optionPercent.setVisibility(View.INVISIBLE);
voteRvViewHolder.radioBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (voteOptionClickListener != null) {
if (i != RecyclerView.NO_POSITION) {
if (wholeView != null) {
voteOptionClickListener.OnVoteOprionClick(wholeView, voteRecyclerViewDataModels.get(i), i);
voteRvViewHolder.seekbar.setVisibility(View.VISIBLE);
voteRvViewHolder.optionPercent.setVisibility(View.VISIBLE);
}
}
}
}
});
Problem is when I click on one of the radio button that row is changed I want all rows changed
Maintain click state in data list of Recycler view using variable and put layout(seekbar/ radio button) in Frame layout.On radio button click change the variable in data list .On the basis of that variable you can display radio button/Seekbar in onBindView holder.

Get click event from gridview's child item in main activity from adapter

I have a GridView in my activity. I am having 2 elements in the GridView. One is an ImageView and the other is a TextView.
I want to perform an action when clicking the ImageView only, but I want this to happen in the activity and not in the GridView adapter.
I handle the ImageView click in the getView() of my adapter, but I do not want it that way. I want to handle it in the activity when calling:
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this, items));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//THIS WORKS FINE
String string = ((TextView) v.findViewById(R.id.text)).getText().toString();
Log.d("string",string);
//THE PROBLEM OCCURS HERE
ImageView capture = (ImageView) v.findViewById(R.id.capture);
capture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}
});
//THE PROBLEM OCCURS HERE
The action is supposed to happen the first time I click the ImageView, but it only happens on the second click and further.
This is where I am stuck with the issue. I want this action to occur on the first click and not on the second click.
You can see the code block-
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
String string = ((TextView) v.findViewById(R.id.text)).getText().toString();
Log.d("string",string);
//THE PROBLEM OCCURS HERE
ImageView capture = (ImageView) v.findViewById(R.id.capture);
capture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}
Your imageview capture is adding any action after gridview is being clicked. So, after clicking first time it is executing and then setting click listener to imageview and then next click its executing its onClick block. So better, handle imageview click event inside adapter.
You can call method inside your activity from adapter class but you should implement setOnClickListener inside your adapter class.
with the help of interface, you can do that do one thing create an interface and implement it on your activity and get imageview click on the adapter and here initiate the interface and you will get the click inside the activity
I think the problem is that on the first click you just set OnClickListener on your imageView, and so its onClick() method is not getting called. On the second click though, as the listener is already set, onClick() is invoked.
Instead of setting new Listener to your imageView each time an item in the grid clicked (which does not make any sense by the way), you should do either of these:
If imageView in each item has to be handled the same way, set OnClickListener to the imageView in the adapter, when creating a view.
If not, pass the interface for handling imageView clicks to the constructor of the adapter, and then, in the activity, implement this interface when creating an adapter.
Create a method in you activity.
Now, in adapter, onClick call that method using
((Activityname)context).methodname(parameters);

Get access to another View from a Button's onClick method

I have a ListView in my app.
Its items have a Button and an invisible text field; I want to make the text field visible when the Button is pressed.
How can I do it from the onClick(View v) method of OnClickListener's object?
Maybe it's something like "getParent().findViewById()", but getParent() doesn't return a View..
You could set the tag of the button view to the TextView.
Then you can use v.getTag() to get it back, then do what you want with it.
Otherwise, make the TextView final in your adapter getView method after finding it , then simply use it within the click event
If you want to visible the textView
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn:
yourtextView.setVisibility(View.VISIBLE);
break;
}
}
try this
View view = listview.getAdapter().getView(0,null,null);
TextView textView = view.findViewById(R.id.textView);

How to set an onClickListener in android for anywhere except an element on the screen

So, I have an activity in android with an attach button on the Action bar.
When I click the button, two floating action buttons appear (one for image, one for camera). When I click the button again, they disappear.
Now, I want to make it such that if the user clicks anywhere on the screen that isn't one of those two FABs, that they will disappear. I already have the action / commands to make them disappear; however, I am looking for how to set up an onClickListener for the whole screen EXCEPT the region covered by the two FABs.
I was solved the same scenario you can compare it with my scenario and can apply on yours :
my scenario : Hide the keyboard on touch of screen any where accept EditText.
Solution : Set up touch listener for non-text box views to hide keyboard.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
setupUI(findViewById(R.id.scrolview)); // === scrolview is container of all views inside it ===
}
public void setupUI(View view) {
//Set up touch listener for non-text box views to hide keyboard.
if(!(view instanceof EditText)) {
view.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
hideSoftKeyboard(SignupActivity.this);
findViewById( R.id.first_linear_layout ).requestFocus();
return false;
}
});
}
//If a layout container, iterate over children and seed recursion.
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
View innerView = ((ViewGroup) view).getChildAt(i);
setupUI(innerView);
}
}
}
public static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}

Android: Gridview Items and making changes to individual items

I'm building an app similar to instagram just for training purposes and basically I have a list of images that I populate through a grid view adapter. In my XML file for 'single_list_item' I have defined a button to be added along with the image for each single item on the grid view list.
However, I want to be able to click on these buttons to 'place a like' on these images separately (similar to instagram's like system). In other words: a user could place a like on the first image and not like the rest of the images. I display a like using a different image resource.
Images get populated by the grid view
Each image has a Like button beneath it
I click on a random like button belonging to a specific image
that image should be selected as 'liked'
What I tried so far:
My grid adapter:
public class ProductGridInflator extends ArrayAdapter<Product>{
.......
.......
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View child = convertView;
final RecordHolder holder;
LayoutInflater inflater = ((Activity) ctx).getLayoutInflater();
if (child == null) {
child = inflater.inflate(R.layout.grid_single, parent, false);
holder = new RecordHolder();
holder.productName = (TextView) child.findViewById(R.id.grid_text);
holder.image = (ImageView) child.findViewById(R.id.grid_image);
holder.heartButton = (ImageButton) child.findViewById(R.id.btnHeartLike);
holder.heartButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (v.isSelected()){
v.setSelected(false);
} else {
holder.heartButton.setSelected(false);
v.setSelected(true);
}
}
});
child.setTag(holder);
}else{
holder = (RecordHolder) child.getTag();
}
}
}
And I'm selecting the appropriate image from a selector (Liked or unliked)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/heart_clicked" android:state_selected="true"/>
<item android:drawable="#drawable/heart304"/>
</selector>
However this code is misbehaving when I test it. When I click on the like button of a specific image list item, two or three other list item's like buttons get clicked as well. Is there a better way to get this done? Individual buttons for each list item?
THe Problem is that you are reusing the onclicklisteners .. Remove this from your getView
holder.heartButton.setOnClickListener(new OnClickListener() {
And instead write in your activity
YouGridView.setOnItemClickListener
And add what you want to do when an item is clicked.

Categories