I am designing a password system based on images which are displayed in the grid view. Since I am not so much familiar with android I want your help.My images are displayed dynamically on the grid i.e they keep on changing their position.
i am not to figure out how to define image as password.I want four images to be clicked in order and save them at the time of registration to the user.I am having difficulty in the saving part like which method to use and where to save.
what i want to do is this.
I want phone to display 15 images dynamically in grid view. then the user clicks 4 images in sequence to set the pass code for registration and it is saved offline. so the user next time clicks 4 images in same order to get unlocked. I am having difficulty in storing the corresponding image displayed on the particular grid because in need images to save the pass code and how to store the data of the user like username, name , and the images which he has clicked.
You can use ImageButton's in your Gridlayout or use a button and set the image as background.Further write a adapter class(I am assuming that you have written one).
Set OnClick listener inside adapter and listen to the same click..
In your adapter class
holder.buttonone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((GridView)parent).performItemClick(v,position,0);
}
});
holder.buttontwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((GridView)parent).performItemClick(v,position,0);
}
});//further for your 15 buttons
And in your activity:
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch(view.getId)
{
case R.id.buttonone:
//set the code in your edittext
break;
case R.id.buttontwo:
//do your stuff
break;
}
});
I am assuming you have a textview/edittext that will print the passcode or atleast xxxx ..So you can display it there and further handle that data.
Use edittext.getText().toString() and store it.
Hope that helps
Related
[update] I fix the mistake I made. now the spinner can update dynamically. But every time I launch this new activity, the spinner will be empty, how can I keep the old data when the activity be created again?
I have an EditView, a Button, and a spinner. When the user types in an item in EditView that is not in the spinner, they can click the button to add to the spinner for the next time to save time.
I search for some solutions in StackOverflow and google, but after I click the button, it still not work, the data still not add to the spinner. Here is the part of the code:
purchasedProduct=(EditText)findViewById(R.id.purchasedP);
spinner=(Spinner)findViewById(R.id.spinner);
spinnerBtn=(Button)findViewById(R.id.purchasedDBtn);
items =new ArrayList<String>();
items.add("");
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinnerBtn.setOnClickListener(spinnerBtnListener);
spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
purchasedProduct.setText(adapterView.getSelectedItem().toString());
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
private Button.OnClickListener spinnerBtnListener = new Button.OnClickListener(){
#Override
public void onClick(View view) {
String listItem = purchasedProduct.getText().toString();
//check if already there
for(int i = 0;i < adapter.getCount();i++)
{
if(listItem.equals(adapter.getItem(i)))
return;
}
//check if type in is empty
if(!listItem.equals(""))
{
adapter.clear();
items.add(listItem);
adapter.addAll(items);
adapter.notifyDataSetChanged();
}
}
};
Can anyone tell me how to fix it? thank you!
You need to store new data in SQLite database. On start activity you just get data from database. All data in database save on device. See how to use SQLite in Android Studio.
Also you can try store data in XML JSON or text file, but this is bad idea.
You can use Room Persistence Library, Look Here for details.
This library helps you create a cache of your app's data on a device that's running your app. This cache, which serves as your app's single source of truth, allows users to view a consistent copy of key information within your app, regardless of whether users have an internet connection.
I have bingo app where there is a GridLayout showing all the numbers done, left.
I want to take a screenshot of it and then share it.
I have tried many posibillities and tried to do many thing, but none of them worked
case R.id.share:
//the answer should be here
the name of the GridLayout is gridLayout
if you need any other code of explanation, please tell me
Considering that we want to take the screenshot when a button is clicked, the code will look like this:
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Bitmap bitmap = takeScreenshot();
saveBitmap(bitmap);
}
});
Calling getDrawingCache(); will return the bitmap representing the view or null if cache is disabled, that’s why setDrawingCacheEnabled(true); should be set to true prior invoking getDrawingCache().
public Bitmap takeScreenshot() {
View rootView = findViewById(android.R.id.content).getRootView();
rootView.setDrawingCacheEnabled(true);
return rootView.getDrawingCache();
}
This code is from this website
The website also contains the code to save the screenshot taken.
Also question is possible duplicate of how to take a screen shot on a button click can anyone provide a android code
I'm trying to read an element from the list shown below (using onClick), with the data being read from a JSON file, the data itself only holds strings. However, I'm having trouble actually registering the array elements to the onClick procedure.
I have absolutely no experience with JSON and am trying to familiarise myself with reading a JSON file and storing it in an Array (which I've done), but I then want to click the individual fields and make it do something.
below is what I have so far:
public void onBindViewHolder(Adapter.ViewHolder holder, int position) {
final ItemFruit itemFruit = fruitList.get(position);
holder.tvFruit.setText(itemFruit.getFruit());
holder.tvPrice.setText(itemFruit.getPrice());
holder.linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//when item is clicked, TOAST displays the field name
}
});
}
Image of array:
array of fruit
Any help whatsoever would be greatly appreciated.
Thanks in advance
put this inside onclick
Toast.makeText(adapterContext, itemFruit.getFruit(),
Toast.LENGTH_LONG).show();
Here adapterContext you have to maintain at adapter level.
Create global variable
Context adapterContext
on onCreateViewHolder initialize it
adapterContext = parent.getContext();
and then use it.
I am loading list view by listview adapter class.
Within that list view I have button called favorite.
ImageButton mFavorite = (ImageButton) convertView.findViewById(R.id.method_fav_btn);
There are multiple image buttons under the same id. I want to identify which button was pressed by means of setting some extra parameters to it. I am doing this for that:
mFavorite.setId(pm.getId());
And on click:
mFavorite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("Id is: "+mFavorite.getId());
}
});
But, the problem is, I have 3 items in list view. Every time I am getting same id. How to get different ids on different clicks?
Thanks.
You can use the setTag() + getTag() methods.
See here for a similar question and here for the official documentation.
i know the brainy people wont like my petty questions but im trying to learn
I,m trying to make a pairs game i have been using int so far on my apps but this game needs a different approach ive created the pairs game with ints but confusing code and a floor that pushing same button twice will delete the pair as below ive been trying with tags the code all looks clean as in no errors
public class MainActivity extends Activity {
//added Tag here for the if (pic2.getTag()==(beck));
Tag beck;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageButton pic1 = (ImageButton ) findViewById(R.id.imageButton1);
final ImageButton pic2 = (ImageButton ) findViewById(R.id.imageButton2);
pic1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pic1.setBackgroundResource(R.drawable.becks);
pic1.setTag(R.drawable.becks);
if (pic2.getTag() == pic1.getTag()){
pic1.setVisibility(View.INVISIBLE);
pic2.setVisibility(View.INVISIBLE);}
}});
pic2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pic2.setBackgroundResource(R.drawable.becks);
pic2.setTag(R.drawable.becks);
if (pic1.getTag() == pic2.getTag()){
pic1.setVisibility(View.INVISIBLE);
pic2.setVisibility(View.INVISIBLE);
}
}});
}}
ive tried since my original post to work out how to do ive shown code for 2 buttons all i want to do is compare and make invisible after the second button is clicked
if (pic1.getTag().equals(pic2.getTag())){
pic1.setVisibility(View.INVISIBLE);
pic2.setVisibility(View.INVISIBLE); }
the .equals crashes the app
pic1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pic1.setBackgroundResource(R.drawable.becks);
pic1.setTag(beck);
if (pic2.getTag()==(beck));{
pic1.setVisibility(View.INVISIBLE);
pic2.setVisibility(View.INVISIBLE);}
}});
this works with or without semi but both buttons dissapear when either button clicked
if (pic1.getTag()==(pic2.getTag())){
pic1.setVisibility(View.INVISIBLE);
pic2.setVisibility(View.INVISIBLE); }
this changes the image but the buttons don,t disappear when second image clicked trying to not use ints if possible
this line works with comma to
if (pic1.getTag()==(pic2.getTag()))
if (pic1.getTag()==(pic2.getTag()));
with effect of both button disappear on 1 click of either button dread moving to the else if lol
Also can a Tag be removed if the pair of images compared if false eg
if no match remove the button tag and reset all remaining images to Default image as when i put all 24 buttons on i need a reset method
i,m finding the semi colon at end of if statement has different effects to not having can anyone point correct way when and when not to use semi colons
Use tags for saving your image-id:
pic1.setTag(R.drawable.becks);
pic2.setTag(R.drawable.becks);
You can then check and compare those by calling getTag() on the buttons that have been clicked:
public boolean isMatch(View x, View y) {
return x.getTag() == y.getTag();
}