I have button inside of LinearLayout which takes photos from gallery and displays at that same LinearLayout :
The problem now is if I want to remove some images just by the tapping the image, that is my current code :
public void AddNewImages(Context context,Bitmap bitmap){
ImageView img = new ImageView(context);
img.setImageBitmap(bitmap);
linearImages.addView(img);
bitmapArray.add(bitmap);
}
Try this listener:
img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
linearImages.removeView(v);
}
});
Related
I want to let a Button identify which of my TextViews got the current focus. And after that the Button needs to apply changes to the focused TextView.
TextView textView11 = (TextView) findViewById(R.id.frame1_1);
textView11.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView11.setBackgroundColor(Color.LTGRAY);
textView11.setFocusableInTouchMode(true);
textView11.requestFocus();
}
});
MaterialButton pb1 = (MaterialButton) findViewById(R.id.pin_button_1);
pb1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//1. get the focused textview or scan for the focused textView
//2. apply button function to the focused textView
}
});
So how can i identify here
//1. get the focused textview or scan for the focused textView
which TextView has the focus currently?
I think it might be possible with some help variable but maybe there os another way, because then i still need a onClickListener for each of the 22 TextView
Untested but should work
private TextView hasFocusTextView;
private View.OnFocusChangeListener onFocusChangedListener = new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
hasFocusTextView = (TextView) v;
}
};
..
TextView textView11 = (TextView) findViewById(R.id.frame1_1);
//Use same listener on all textview you want to include with the focus logic
textView11.setOnFocusChangeListener(onFocusChangedListener);
pb1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
hasFocusTextView.setText("Changing text of the focused TextView");
}
});
You can simply place these TextView in the same ViewGroup, then just traverse the ViewGroup's child views like this:
for (child in container.children){
if (child.isFocused){
//do your jobs here
}
}
Best way is to use a variable to store which TextView currently has focus.
TextView currentTextView = null;
void initView() {
TextView textView11 = (TextView) findViewById(R.id.frame1_1);
textView11.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView11.setBackgroundColor(Color.LTGRAY);
textView11.setFocusableInTouchMode(true);
textView11.requestFocus();
// Set the currentTextView to the currently focused
// this must be used in every focusable textview
currentTextView = (TextView) view;
}
});
MaterialButton pb1 = (MaterialButton) findViewById(R.id.pin_button_1);
pb1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//1. get the focused textview or scan for the focused textView
// focused textview is currentTextView
//2. apply button function to the focused textView
// Edit: Check for nullability
if (currentTextView != null) {
// use the variable currentTextView
}
}
});
}
This is where I want to change the image when animation blinks. I'm not able to set more than one Image in it, I'm new to Android development.
enter image description here
public class MainActivity extends AppCompatActivity {
RelativeLayout suggestion;
CircleImageView suggestionImage;
AnimationDrawable animation;
BitmapDrawable frame;
String[] imageUrl = new String[]{"https://i.picsum.photos/id/663/200/200.jpg", "https://i.picsum.photos/id/1026/200/200.jpg", "https://i.picsum.photos/id/1037/200/200.jpg"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
suggestion = (RelativeLayout) findViewById(R.id.suggestionContainer);
suggestionImage = (CircleImageView) findViewById(R.id.suggestionImage);
final Animation moveOut = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.move_out);
final Animation blink = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.blink);
moveOut.setFillAfter(true);
final Handler slide = new Handler();
slide.postDelayed(new Runnable() {
#Override
public void run() {
suggestion.startAnimation(moveOut);
}
}, 2100);
suggestion.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"touched", Toast.LENGTH_SHORT).show();
}
});
Glide.with(this)
.asBitmap()
.load(imageUrl[1])
.into(new CustomTarget<Bitmap>() {
#Override
public void onResourceReady(#NonNull Bitmap resource, #Nullable Transition<? super Bitmap> transition) {
frame = new BitmapDrawable(resource);
animation.addFrame(frame, 1000);
}
#Override
public void onLoadCleared(#Nullable Drawable placeholder) {
}
});
animation = new AnimationDrawable();
animation.setOneShot(true);
suggestionImage.setBackgroundDrawable(animation);
suggestionImage.startAnimation(blink);
animation.start();
}
}
check if this can help.
image = (ImageView) findViewById(R.id.image);
Animation animation = new AlphaAnimation((float) 0.5, 0); // Change alpha from fully visible to invisible
animation.setDuration(500); // duration - half a second
animation.setInterpolator(new LinearInterpolator()); // do not alter
// animation
// rate
animation.setRepeatCount(Animation.INFINITE); // Repeat animation
// infinitely
animation.setRepeatMode(Animation.REVERSE); // Reverse animation at the
// end so the button will
// fade back in
image.startAnimation(animation);
Actually I am Making an App Of Pin Lock Screen and I made only two activities. On Activity1, I want that If I click On ImageView Then On
Activity2 Another image Display From drawable
On TextView and it saved On SharedPreference I am a beginner to this any Help please I'm going to stuck.
First Activity
public class MainActivity extends Activity implements View.OnClickListener{
ImageView mpink,mpurple;
int view;
String display;
private ImageView results;
private Object get_input;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mpink=(ImageView) findViewById(R.id.imageView);
mpurple=(ImageView) findViewById(R.id.imageView2);
mpink.setOnClickListener(this);
mpurple.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (results == mpurple) {
display = "get_input";
view = R.drawable.comb1;
}
else if (results == mpink) {
display = "get_input";
view = R.drawable.comb2;
}
else {
display = "get_input";
view = R.drawable.comb3;
}
Intent home_intent=new Intent(getApplicationContext(),Main2Activity.class);
home_intent.putExtra("home_store_image_src", (Parcelable) mpink);
home_intent.putExtra("home_store_image_src", (Parcelable) mpurple);
startActivity(home_intent);
}}
Second Activity
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
get_result=(TextView)findViewById(R.id.displayresult);
Intent result_intent=getIntent();
String text=result_intent.getStringExtra("get_input");
get_result.setText(text);// display what the user has enter
// get_image.setImageResource
//OR other coding to display imageview by using the image_id from activty 1?
int defaultIdWhenCouldNotFindImageSourceId = -1;//should use a negative value
int imageId = result_intent.getIntExtra("home_store_image_src", defaultIdWhenCouldNotFindImageSourceId);
if( defaultIdWhenCouldNotFindImageSourceId != imageId) {
//when you can find an valid image id in intent extra, display it
get_image.setImageResource(imageId);
}
}}
please check this question to help you about sending drawables from one activity to another
How to pass drawable between activities
So when you use onClick
#Override
public void onClick(View v) {
Bitmap bitmap;
if (results == mpurple) {
display = "get_input";
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.comb1);
}
else if (results == mpink) {
display = "get_input";
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.comb2);
}
else {
display = "get_input";
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.comb3);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
Intent home_intent=new Intent(getApplicationContext(),Main2Activity.class);
home_intent.putExtra("picture", b);
home_intent.putStringExtra("getInput", display);
startActivity(home_intent);
}}
Also, you need to edit your second activity and put an ImageView in its XML, you can't pass ImageView from Activity to another, but you can pass a drawable and set it to the ImageView in the second Activity
I have a dialog wherein a user can add EditText and also remove it. I have successfully added EditText programmatically but my code for removing it doesn't work. I'm following this tutorial but in my case setup is inside a dialog.
and also i want to get all the the texts on those EditTexts and store it inside an Array.
This is my code:
public void showSurveyDialog(Context context) {
ImageButton btnAddChoices, btnRemoveChoice;
Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.survey_content);
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
btnAddChoices = dialog.findViewById(R.id.btn_add_choices);
LinearLayout choiceLayout = dialog.findViewById(R.id.choice_layout);
btnAddChoices.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = dialog.getLayoutInflater().inflate(R.layout.choice_item, null);
// Add the new row before the add field button.
choiceLayout.addView(rowView, choiceLayout.getChildCount() - 1);
ImageButton imageButton = dialog.findViewById(R.id.btn_choice_close);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e("asdass","ASDASd");
choiceLayout.removeView((View)v.getParent());
}
});
}
});
dialog.show();
}
When pressing btnAddChoices a layout with EditText and Button(for removing) is automatically added to a linear layout. I'am trying to make the remove button to work but it doesn't remove the view.
you can try my add and remove code for references, this can add or remove view programmatically:
private void addressField(View view){
final int[] textCount = {0};
LinearLayout addressLayout = view.findViewById(R.id.address_layout);
addressScroll.removeView(addressLayout);
View layout2 = LayoutInflater.from(view.getContext()).inflate(R.layout.address_text_field
, addressLayout,false);
layout2.setTag("address"+Integer.toString(counter));
addressLayout.addView(layout2);
addressScroll.addView(addressLayout);
ImageView deleteButton = layout2.findViewWithTag("address"+Integer.toString(counter))
.findViewById(R.id.delete_address);
TextFieldBoxes addressText = layout2.findViewWithTag("address"+Integer.toString(counter))
.findViewById(R.id.address_wrapper);
addressText.setSimpleTextChangeWatcher((theNewText, isError) -> {
if(Objects.equals(theNewText, "")){
textCount[0] = 0;
addressLayout.removeView(layout2);
}
if(theNewText.length() == 1 && textCount[0] == 0){
addressField(view);
ExtendedEditText addressText2 = layout2.findViewWithTag("address"+Integer.toString(counter-1))
.findViewById(R.id.address_text);
addressText2.requestFocus();
counter++;
}
textCount[0]++;
});
deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addressLayout.removeView(layout2);
}
});
}
hope can solve your problems
I'm trying to create a simple android app, which has multiple buttons but just 1 imageView. Each button will have a unique image. It displays in the imageView when clicked on. Here is my java code:
public void poundedYamFunction(View view){
ImageView swallowImage = (ImageView) view;
swallowImage.setImageResource(R.drawable.poundedyam);
}
public void ebaFunction(View view){
ImageView swallowImage = (ImageView) view;
swallowImage.setImageResource(R.drawable.eba);
}
public void semoFunction(View view){
ImageView swallowImage = (ImageView) view;
swallowImage.setImageResource(R.drawable.semo);
}
As per the details and code , multiple buttons are associated with different views and the View view reference here is the reference to the Buttons not the imageview so you need to initialize the imageView reference and set the image accordingly.
like
ImageView img;
...oncreate(..){
setContentView(..);
img = (ImageView)findViewById(R.id.id_of_image_view);
}
public void poundedYamFunction(View view){
img.setImageResource(R.drawable.poundedyam);
}
public void ebaFunction(View view){
img.setImageResource(R.drawable.eba);
}
You can achieve it with below code. Implement activity with View.OnClickListener and on onClick(View view) change image.
public class MainActivity2 extends AppCompatActivity implements View.OnClickListener {
ImageView imageView;
Button button1, button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.image_view);
button1 = findViewById(R.id.button_1);
button2 = findViewById(R.id.button_2);
imageView.setOnClickListener(this);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}
#Override
public void onClick(View view) {
int id = view.getId();
switch (id) {
case R.id.button_1:
imageView.setImageResource(R.drawable.poundedyam);
break;
case R.id.button_2:
imageView.setImageResource(R.drawable.eba);
break;
}
}
}