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
}
}
});
}
I am trying to pass some data from one activity to another and I have made a toast in the other activity to see if data is being passed.When the toast shows up it is blank.I have done everything right and tried many different times with different methods I have also created another app but the problem still stays.It gives me null.
My java code in the first activity
public class titleandcuisine extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
public static final String TITLE_GET = "title";
public static final String CUISINE_GET = "cuisine";
ImageView imageView;
Button button;
Spinner spinner;
EditText dish;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_titleandcuisine);
dish = findViewById(R.id.editText);
spinner = findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.cuisines,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
button = findViewById(R.id.next);
imageView = findViewById(R.id.close);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), homepage.class));
}
});
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text = dish.getText().toString();
String text2 = spinner.getSelectedItem().toString();
Intent data = new Intent(getBaseContext(),imagepost.class);
data.putExtra(TITLE_GET,text);
data.putExtra(CUISINE_GET,text2);
startActivity(data);
}
});
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
Second activity java code
public class reviewactivity extends AppCompatActivity {
TextView cuisine,title;
ImageView displayimage,back;
Uri myUri;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reviewactivity);
Intent intent = getIntent();
String titleget = intent.getStringExtra(titleandcuisine.TITLE_GET);
String cuisineget = intent.getStringExtra(titleandcuisine.CUISINE_GET);
Toast.makeText(this, titleget + cuisineget, Toast.LENGTH_SHORT).show();
cuisine = findViewById(R.id.reviewcuisine);
title = findViewById(R.id.reviewtitle);
back = findViewById(R.id.backtopostvideo);
displayimage = findViewById(R.id.reviewdisplaimage);
// cuisine.setText(cuisineget);
// title.setText(titleget);
// myUri = Uri.parse(uri);
displayimage.setImageURI(myUri);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(),videopost.class));
}
});
}
}
You need to read the text from the EditText when the button is clicked. Something like:
title = dish.getText().toString(); // Add this
Intent data = new Intent(getBaseContext(),imagepost.class);
data.putExtra(Intent.EXTRA_TEXT, title);
...
One more thing: You are trying to read the Intent.EXTRA_TEXT at the reviewactivity activity. However, nothing is starting that activity (at least within the piece of code that you shared).
I have this ImageView that displays a picture from a URL. I want to have a button that when clicked will display the image from the URL. Could anyone teach me how to do this?
Here's my code:
String url = "https://www.google.com.ph/url?sa=i&rct=j&q=&esrc=s&source=images&cd=&ved=0ahUKEwiGzJm-l5PWAhVJl5QKHegTAg0QjBwIBA&url=https%3A%2F%2Fwww.enterprise.co.uk%2Fcontent%2Fdam%2Fglobal-vehicle-images%2Fcars%2FVAUX_INSI_2014.png&psig=AFQjCNGerQpF4NHcx50OFhQ2AGUlJYQCpQ&ust=1504877444951754";
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
imageView = (ImageView)findViewById(R.id.imageView);
loadImageFromUrl(url);
}
private void loadImageFromUrl(String url) {
Picasso.with(this) .load(url) .placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.into(imageView, new com.squareup.picasso.Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
}
});
}
Picasso:
compile 'com.squareup.picasso:picasso:2.5.2'
Called when a view has been clicked.
imageView = (ImageView)findViewById(R.id.imageView);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Do something here
loadImageFromUrl(url);
}
});
Add a button to your xml file. Let it be with id: btn_load;
Inside the activity add
Button btn = findViewById(R.id.btn_load);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
loadImageFromUrl(url);
}
});
Create a button in your xml layout file. Then in onCreate call:
final String url = "https://www.google.com.ph/url?sa=i&rct=j&q=&esrc=s&source=images&cd=&ved=0ahUKEwiGzJm-l5PWAhVJl5QKHegTAg0QjBwIBA&url=https%3A%2F%2Fwww.enterprise.co.uk%2Fcontent%2Fdam%2Fglobal-vehicle-images%2Fcars%2FVAUX_INSI_2014.png&psig=AFQjCNGerQpF4NHcx50OFhQ2AGUlJYQCpQ&ust=1504877444951754";
ImageView imageView;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
imageView = (ImageView)findViewById(R.id.imageView);
button = findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
loadImageFromUrl(url);
}
});
}
Note: for this to work you will have to declare your url String as final, because you're using it inside an anonymous class (OnClickListener).
Note2: The image url you have may not work. Use this one instead: https://www.enterprise.co.uk/content/dam/global-vehicle-images/cars/VAUX_INSI_2014.png
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);
}
});
I have three Activities with one button in each of them. Act1 with btn1, Act2 with btn2, and Act3 with btn3. I have another Activity as MainActivity with three imageViews: ImageView1, imageView2 and imageView3, all of which are initially invisible. I want it so when I click on btn1 in act1, imageView1 in MainActivity will be visible and when click on btn1 again, imageView1 will be invisible again. And similarly for imageView2 and imageView3.
I have this code so far:
Activity1
public class Activity1 extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
Button btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String visibilityStr = PublicSharedPreferences.getDefaults("keyVisibility", getApplicationContext());
if (visibilityStr != null) {
if (visibilityStr.equals("0")) {
Toast.makeText(act1.this, "it visibled", Toast.LENGTH_SHORT).show();
visibilityStr = "1";
btn1.setImageResource(R.mipmap.img1);
} else {
visibilityStr = "0";
Toast.makeText(act1.this, "it invisibled", Toast.LENGTH_SHORT).show();
btn1.setImageResource(R.mipmap.img2);
}
} else {
visibilityStr = "1";
Toast.makeText(act1.this, "it visibled", Toast.LENGTH_SHORT).show();
btn1.setImageResource(R.mipmap.img1);
}
PublicSharedPreferences.setDefaults("keyVisibility", visibilityStr, getApplicationContext());
}
});
}
Activity2
public class Activity2 extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
Button btn1 = (Button) findViewById(R.id.btn2);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String visibilityStr = PublicSharedPreferences.getDefaults("keyVisibility", getApplicationContext());
if (visibilityStr != null) {
if (visibilityStr.equals("0")) {
Toast.makeText(act2.this, "it visibled", Toast.LENGTH_SHORT).show();
btn2.setImageResource(R.mipmap.img1);
visibilityStr = "1";
} else {
visibilityStr = "0";
Toast.makeText(act2.this, "it invisibled", Toast.LENGTH_SHORT).show();
btn2.setImageResource(R.mipmap.img2);
}
} else {
visibilityStr = "1";
Toast.makeText(act2.this, "it visibled", Toast.LENGTH_SHORT).show();
btn2.setImageResource(R.mipmap.img1);
}
PublicSharedPreferences.setDefaults("keyVisibility", visibilityStr, getApplicationContext());
}
});
}
Activity3
public class Activity3 extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity3);
Button btn1 = (Button) findViewById(R.id.btn3);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String visibilityStr = PublicSharedPreferences.getDefaults("keyVisibility", getApplicationContext());
if (visibilityStr != null) {
if (visibilityStr.equals("0")) {
Toast.makeText(act3.this, "it visibled", Toast.LENGTH_SHORT).show();
Btn3.setImageResource(R.mipmap.img1);
visibilityStr = "1";
} else {
visibilityStr = "0";
Toast.makeText(act3.this, "it invisibled", Toast.LENGTH_SHORT).show();
btn3.setImageResource(R.mipmap.img2);
}
} else {
visibilityStr = "1";
Toast.makeText(act3.this, "it visibled", Toast.LENGTH_SHORT).show();
btn3.setImageResource(R.mipmap.img1);
}
PublicSharedPreferences.setDefaults("keyVisibility", visibilityStr, getApplicationContext());
}
});
}
MainActivity with three imageViews:
ImageView imgView1 = (ImageView) findViewById(R.id.imgView1);
String visibilityStr= PublicSharedPreferences.getDefaults("keyVisibility", getApplicationContext());
if (visibilityStr.equals("0"))
imgView1.setVisibility(View.INVISIBLE);
else
imgView1.setVisibility(View.VISIBLE);
ImageView imgView2 = (ImageView) findViewById(R.id.imgView2);
String visibilityStr= PublicSharedPreferences.getDefaults("keyVisibility", getApplicationContext());
if (visibilityStr.equals("0"))
imgView2.setVisibility(View.INVISIBLE);
else
imgView2.setVisibility(View.VISIBLE);
ImageView imgView3 = (ImageView) findViewById(R.id.imgView3);
String visibilityStr= PublicSharedPreferences.getDefaults("keyVisibility", getApplicationContext());
if (visibilityStr.equals("0"))
imgView3.setVisibility(View.INVISIBLE);
else
imgView3.setVisibility(View.VISIBLE);
They work well. But the problem is that when I click on btn1, all imageViews in MainActivity change (become visible or invisible) or when I click on btn3, all imageViews change. I want it so btn1 just changes imageView1 and btn2 just changes imageView2 and btn3 just changes imageView3, instead of one of the buttons changing all of the imageViews. How can I do that? Which part of the code is wrong?
the problem is you only have 1key Preference its all keyVisibility so when you click on any of your button this key changes to either 1 or 0 base on your code
here
ImageView imgView1 = (ImageView) findViewById(R.id.imgView1);
String visibilityStr1= PublicSharedPreferences.getDefaults("keyVisibility1", getApplicationContext());
if (visibilityStr1.equals("0"))
imgView1.setVisibility(View.INVISIBLE);
else
imgView1.setVisibility(View.VISIBLE);
ImageView imgView2 = (ImageView) findViewById(R.id.imgView2);
String visibilityStr2= PublicSharedPreferences.getDefaults("keyVisibility2", getApplicationContext());
if (visibilityStr2.equals("0"))
imgView2.setVisibility(View.INVISIBLE);
else
imgView2.setVisibility(View.VISIBLE);
ImageView imgView3 = (ImageView) findViewById(R.id.imgView3);
`String visibilityStr3= PublicSharedPreferences.getDefaults("keyVisibility3",` `getApplicationContext());`
if (visibilityStr3.equals("0"))
imgView3.setVisibility(View.INVISIBLE);
else
imgView3.setVisibility(View.VISIBLE);
and ofcoure you should change the keyVisibility on your act1,act2 and act3
First of all, please do a little more study on Android SharedPreferences (how to save/get a sharepreference value properly in/from your local storage).
After you are done with your research, here's some hint for you. Hope you will be able to implement this on your own.
Save value - save your boolean value when you click the button
Context mContext = getApplicationContext();
SharedPreferences mPrefs = mContext.getSharedPreferences("MySharedPrefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean("IsImageViewVisible", true);
// use different keys for different imageviews;
//ex: for ImageView1, use IsImageView_1_Visible; for ImageView2, use IsImageView_2_Visible etc
editor.commit();
Get values in your MainActivity
Context mContext = getApplicationContext();
SharedPreferences mPrefs = mContext.getSharedPreferences("MySharedPrefs", Context.MODE_PRIVATE);
Boolean isImageViewVisible = mPrefs.getBoolean("IsImageViewVisible", false); // here,false is default value
Then, check all imageview's visibility in MainActivity:
if(isImageViewVisible){
// image is visible
} else{
// image is invisible
}
this is very easy ,you can use one of this methods
1 : Shared Preferences :
in your Activities (Act1,Act2,Act3)
write this codes on onClick event :
// on Act1
final SharedPreferences sharedPreferences=getSharedPreferences("mainconf",MODE_PRIVATE);
buttonOnAct1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// check if isVisable and change value
if (sharedPreferences.getBoolean("isImg1Visable",true)){
sharedPreferences.edit().putBoolean("isImg1Visable",false).apply();
}else {
// show it again
sharedPreferences.edit().putBoolean("isImg1Visable",true).apply();
}
}
});
// on Act2
final SharedPreferences sharedPreferences=getSharedPreferences("mainconf",MODE_PRIVATE);
buttonOnAct2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// check if isVisable and change value
if (sharedPreferences.getBoolean("isImg2Visable",true)){
sharedPreferences.edit().putBoolean("isImg2Visable",false).apply();
}else {
// show it again
sharedPreferences.edit().putBoolean("isImg2Visable",true).apply();
}
}
});
// on Act3
final SharedPreferences sharedPreferences=getSharedPreferences("mainconf",MODE_PRIVATE);
buttonOnAct3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// check if isVisable and change value
if (sharedPreferences.getBoolean("isImg3Visable",true)){
sharedPreferences.edit().putBoolean("isImg3Visable",false).apply();
}else {
// show it again
sharedPreferences.edit().putBoolean("isImg3Visable",true).apply();
}
}
});
And code of your MainActivity :
public class MainActivity extends AppCompatActivity {
private ImageView imageView1;
private ImageView imageView2;
private ImageView imageView3;
private SharedPreferences sharedPreferences;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
imageView1=(ImageView)findViewById(R.id.img1);
imageView2=(ImageView)findViewById(R.id.img2);
imageView3=(ImageView)findViewById(R.id.img3);
sharedPreferences=getSharedPreferences("mainconf",MODE_PRIVATE);
}
// important PART OF CODE
#Override
protected void onResume() {
super.onResume();
if (sharedPreferences.getBoolean("isImg1Visable",true)){
imageView1.setVisibility(View.VISIBLE);
}else {
imageView1.setVisibility(View.GONE);
}
if (sharedPreferences.getBoolean("isImg2Visable",true)){
imageView2.setVisibility(View.VISIBLE);
}else {
imageView2.setVisibility(View.GONE);
}
if (sharedPreferences.getBoolean("isImg3Visable",true)){
imageView3.setVisibility(View.VISIBLE);
}else {
imageView3.setVisibility(View.GONE);
}
}
}
2 : public static Variables:
write your variables as static
example :
// in Your MainActivty under class
public static ImageView imageView1;
public static ImageView imageView2;
public static ImageView imageView3;
// in your Act1 Act2 Act3
// for hiding from mainactivity
MainActivity.imageView1.setVisibility(View.GONE); // or View.INVISIBLE
// for showing on main activity
MainActivity.imageView1.setVisibility(View.VISIBLE);