Im building an application in Android Studio. My MainActivity is fairly simple, existing out of;
Imageview(Logo)
Viewpager(Swipeable images (10x)
ImageButton(Dislike image)
ImageButton(Like image)
Button (My personal cookbook) This button sends the user to their personal cookbook where all the images are "Liked".
The thing that I'm trying to achieve is to be able to press the ImageButton(Like) and save the image that is being showed from the ViewPager in "My personal Cookbook".
The next thing that I also want to achieve is, that when the Dislike is pressed that the Imageviewer deletes the image and goes on to the next image in the PageViewer.
I've tried searching everywhere on how to get this going but I can't find it.
Therefor I hope that someone here will be able to help me with these 2 questions.
With kind regards,
A.
// Locate the ViewPager in viewpager_main.xml
viewPager = (ViewPager) findViewById(R.id.pager);
// Pass results to ViewPagerAdapter Class
adapter = new ViewPagerAdapter(MainActivity.this, mealname, mealpicture);
// Binds the Adapter to the ViewPager
viewPager.setAdapter(adapter);
Button btn = (Button) findViewById(R.id.button3);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, CookbookActivity.class));
}
});
// Check Authentication
mRef = new Firebase(com.example.sick.foodinspiration.Constants.FIREBASE_URL);
if (mRef.getAuth() == null) {
loadLoginView();
}
}
// Dont pay attention to this
//public void DislikeMethod(View view){
//Toast.makeText(MainActivity.this, "Dislike :(", Toast.LENGTH_SHORT).show();
}
// Dont pay attention to this
//public void LikeMethod (View view){
//Toast.makeText(MainActivity.this, "Like :)", Toast.LENGTH_SHORT).show();}
private void loadLoginView() {
Intent intent = new Intent(this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
Button likebtn = (Button) findViewById(R.id.likebtn);
Button dislikebtn = (Button) findViewById(R.id.dislikebtn);
likebtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//LikeMethod();
}
});
dislikebtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewPager.setCurrentItem(viewPager.getCurrentItem()+1);//move to next image
//DisLikeMethod();
}
});
Use sqlite database for achieve this
First create a table called MyCookBook and create the columns such as ID(integer),ImagePath/Url(text),isLike(int 0,1)
Once user click the like button insert the image path and set isLike column as 1
If user click dislike button set isLike column as 0 vice versa( like on/off)
Select the MyCookBook table which are the rows isLike as 1 is your "My personal Cookbook"
I hope this can help you.
Related
I am trying to have a checkbox on my homepage, which when checked, should take me to an another view, else, displays an error message on the same view.
This works fine the first time only. If unchecked, it displays the error, and if checked, renders the next view.
However, when I return to this homepage using back button, this time the checkbox becomes redundant. Even unchecking it renders the next page correctly.
If I remove all the views of the ViewGroup, it removes them permanently, and the homepage is empty once I return to it.
I believe the checkbox needs to be reset every time I return to the view.
However, I am unable to do the same.
Please find my code below (I am a beginner to Android development):
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE="com.android.AI";
public boolean isCheckBoxClicked=false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//isChecked is the onClick attribute for checkbox in the main.xml here
public void isChecked(View view){
boolean isChecked = ((CheckBox) view).isChecked();
if(isChecked){
isCheckBoxClicked = true;
} else {
isCheckBoxClicked = false;
}
}
//Send message is onClick for a submit button
public void sendMessage(View view){
if(isCheckBoxClicked) {
Intent intent = new Intent(this, SeniorTeam.class);
startActivity(intent);
} else {
TextView acceptTerms = new TextView(this);
acceptTerms.setTextSize(15);
acceptTerms.setText("Please accpet terms and conditions before proceeding");
ViewGroup terms = (ViewGroup) findViewById(R.id.activity_main);
terms.addView(acceptTerms);
}
}
}
As far as I see this, you don't need to be able to go back by using the back Button, do you?
In case you don't there are 2 solutions
Easy:
Add this line to your verry last if
this.finish();
so your Activity should look like this:
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE="com.android.AI";
public boolean isCheckBoxClicked=false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//isChecked is the onClick attribute for checkbox in the main.xml here
public void isChecked(View view){
boolean isChecked= ((CheckBox) view).isChecked();
if(isChecked){
isCheckBoxClicked=true;
}
else{
isCheckBoxClicked=false;
}
}
//Send message is onClick for a submit button
public void sendMessage(View view){
if(isCheckBoxClicked) {
Intent intent = new Intent(this, SeniorTeam.class);
startActivity(intent);
this.finish();
}
else {
TextView acceptTerms = new TextView(this);
acceptTerms.setTextSize(15);
acceptTerms.setText("Please accpet terms and conditions before proceeding");
ViewGroup terms = (ViewGroup) findViewById(R.id.activity_main);
terms.addView(acceptTerms);
}
}
A little more complicated:
If you use the first method pressing back will actually kick the user out of your App so if you want you can just override the method when back is pressed in your next activity for that you want to add this code int your NEXT activity(SeniorTeam.class)
#Override
public void onBackPressed(){
//custom actions here leave blank to render back useless
}
You should completely remove the isCheckBoxClicked variable, the isChecked function (and remove it from the attributes). In the sendMessage function, just call ((CheckBox)findViewById(id)).isChecked() instead of isCheckBoxClicked.
The way you have it coded, there is too much room for disconnect between the variables and the UI. It is not necessary.
new to android development and run into a hurdle,im making an app that will have a lot of pictures, what i want to achieve is after i have pressed a button in the main activity to take me to another activity, i would like a page full of buttons on this new activity that once pressed each button will show a image and have some text i would like to add, i would like to do this without having to create a new activity for each photo so i am hoping somebody could kindly help me with this! think of it like a sounbank app that plays a sound when the buttons from each row is pressed! in my case it will show an image for each button, could this be done with just the main activity and an extra activity or will i end up with an activity open in project explorer for each picture.
your help is greatly appreaciated thank you.
Ok lets assume you are using drawables from your resource folder.
In that way you deal with integer ids that represent the images.
These id can be transmitted per Intent to a second activity, that
retrieves the intent, parses the id and loads / shows the image.
In your Activity A, where you have all your buttons, I assume further that
you've added OnClicklisteners to the button like:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//something
}
});
In your click listener you replace //something with:
Intent i = new Intent(getApplicationContext, ActivityToShowImage.class);
i.putExtra(ActivityToShowImage.IMAGE_KEY_SOME_STRING, R.drawable.oneOfYourImages);
startActivity(i);
The first argument is the key to retrieve the id later and the second is the id.
The R.drawable.oneOfYourImages depends on which button got clicked and shoud
be different for every button.
You can do this for every button, or you create a method that returns an
OnClickListener with the id as parameter.
private View.OnClickListener showImageFor(final int resIdOfImage) {
return new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext, ActivityToShowImage.class);
i.putExtra(ActivityToShowImage.IMAGE_KEY_SOME_STRING, resIdOfImage);
startActivity(i);
}
};
}
Now, in Your ActivityToShowImage, you override the onCreate like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourLayout);
int imageId = 0;
Bundle extras = getIntent().getExtras();
if(extras != null){
imageId = extras.getInt(IMAGE_KEY_SOME_STRING, 0);
}
if(imageId == 0){
//some log message or exception and don't forget to finish in onResume()
}else{
loadImage(imageId);
}
}
And in your loadImage method you load the image via the id into an ImageView.
I hope that is the answer to your question and sorry for the late response.
I have 20 images and 20 icons to set in the List view. I know all the procedure How to make the List view. But I have several Problems. Let me tell You my case What I wanted to do.
Case :
I want a list view with these twenty icons, two in each row, let say first row has two icons of two fags , say India and America , Now I want when User click on India Flag it should appear in next activity's image view and when user click on America Flag them it should appear. and so on with the other icons of other counteries flags.
What I Have Done :
I have created a custom adapter but that working good for list view for single item in a row. Now How to implement the list view in the case I stated above. Some one please share me the Source code or Help me directly. I know its not too lengthy work but little tricky.
you can set intent on onClickListener of imageview which will take user to desired activity..
for example :
holder.ivFlagIndia.setOnClickListener(new OnClickListener{
#Override
public void onClick(View view)(
Intent intent= new Intent(context,IndiaFlagActivity.class);
context.startActivity(intent);
)
})
holder.ivFlagAmerica.setOnClickListener(new OnClickListener{
#Override
public void onClick(View view)(
Intent intent= new Intent(context,AmericaFlagActivity.class);
context.startActivity(intent);
)
})
Hope this may help.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
im1 = (ImageView)view.findViewById(R.id.image_view1);
im2 = (ImageView)view.findViewById(R.id.image_view2);
if (id == 0)
{
im1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(),"Tutorials",Toast.LENGTH_SHORT).show();
Intent i = new Intent(v.getContext(),Tutorial.class);
startActivity(i);
}
});
im2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(),"Message",Toast.LENGTH_SHORT).show();
Intent i = new Intent(v.getContext(),YourNewActivity.class);
startActivity(i);
}
});
}
}
});
I am showing some data in list-view,actually i am getting data from the web service by consuming it.
If i click any item from that list-view it should go to the next page and will have to show some data on the next page (say next_activity.java),well its working fine.
But on the next_activity.java page is having one back button,if i click that button it should have to go to previous list-view activity okay.
Well the intent is working fine,
but
its not showing the list-view.why?
Here is the code i have used for the back button.
btn_back = (Button)findViewById(R.id.button_back_tab);
btn_back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i_listagain = new Intent(TabhostActivity.this,StationsListActivity.class);
startActivity(i_listagain);
}
see what you are doing .
1- you are creating new Intent for previous List-activity .there is no need of that.
2- instead of creating new Intent use.
finish()//this ll finish your current activity
3-This will finish your current activity and will go your last activity on stack i.e your List-activity
Just write this:
list-view activity
mlistView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(this,new_Activity.class);
startActivity(intent);
}
});
new_activity.java
btn_back = (Button)findViewById(R.id.button_back_tab);
btn_back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
this.finish(); // just write this
}
I want to create a button in the child of expandable list.
However i get Null Pointer exception.
btnmore = (Button) findViewById(R.id.btn_more);
btnmore.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(getBaseContext(), ParticularNewsDetail.class); <-- got problem here
startActivity(i);
}
});
I want to click the button to go to next activity.
What to do?
instead of giving getBaseContext() try giving your yourclassname.this. this might work i guess..