Two clickable imageview in the listview - android

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);
}
});
}
}
});

Related

Saving image from viewpager into a custom gridview gallery

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.

Unable to show list-view when clicking back button in android

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
}

Setting images in gridview to transparent

So I have images in gridview. These images are shown in an activity. When one of the images is clicked, new activity is started with this image. In this activity, If user inputs correct answer, new activity is started that indicates the answer is correct. After user inputs correct answer, I want to set this image more transparent with a check mark in gridwiew. If the user clicks again to this correct view, I want to show same activity which indicates it was solved correct. How can I do that?
Here is my activity which contains grid view:
public class LogoSelectionActivity extends Activity {
.
.
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent intent = new Intent(LogoSelectionActivity.this, LogoActivity.class);
intent.putExtra ("clicked_position", position);
startActivity(intent);
}
Here is my activity where user enter inputs:
public class LogoActivity extends Activity{
EditText text;
Button check;
Boolean a;
int id;
Names name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_logo);
check = (Button) findViewById(R.id.Check_button);
text = (EditText) findViewById(R.id.editText1);
ImageView view = (ImageView)findViewById(R.id.imageView1);
final ImageView incorrect = (ImageView)findViewById(R.id.incorrect);
switch (getIntent().getIntExtra ("clicked_position", -1))
{
case 0:
view.setImageResource(R.drawable.adese);
id = R.drawable.adese;
break;
case 1:
view.setImageResource(R.drawable.birvar);
id = R.drawable.birvar;
break;
case 2:
view.setImageResource(R.drawable.agaoglu);
break;
case 3:
view.setImageResource(R.drawable.akinsoft);
break;
default:
view.setImageResource(R.drawable.afra);
id = R.drawable.afra;
}
name = Names.forDrawable(id);
check.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
a=name.isCorrect(text.getText().toString());
if(a==true){
Intent intent = new Intent(LogoActivity.this, CorrectActivity.class);
startActivity(intent);
}
else{
incorrect.setVisibility(0);
incorrect.setVisibility(4);
}
}
});
}
}
I hope I could explain my intention clearly.
There are number of ways to do this. Because this is not problem this is functionality that you want in your project.
According to my logic I am giving you one solution.
Steps:
Create one static array with the length of your items in grid view with default value is 0.
static int[] items = {0,0,0,...};
Now check in getView methode of grid view with the value according to position in array that if it is 0 then it will looks like normal otherwise it will look with different (Here you want transparent with check box this will you get by adding one more image on it.)
If(items[position] == 0){
}else{
}
Now when user taps any image and navigate to other screen (Suppose Activity B) that time pass value of that position from that array and check in Activity B's OnCreate().
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent intent = new Intent(LogoSelectionActivity.this, LogoActivity.class);
intent.putExtra ("clicked_position", position);
intent.putExtra ("answered_or_not", items[position]);
startActivity(intent);
}
And if answered means if its value is 1 then show accordingly other wise as it is.
And when it user give answer unanswered question make its value 1 in that array.
I hope it will help you can ask if any question.

Capturing TextView clicks in ListView

I’ve a custom ListView containing 4 TextViews. Now, the TextViews have to be Linkifiable. Since Linkify wasn’t working in ListView, I made a callIntent function to see if the link is clickable or not. But if there’s no clickable link, I want to start a new Activity. I use the following code:
lvMembersList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
final TextView txtLine1 = (TextView) arg1.findViewById(R.id.tv_member_details_line1);
final TextView txtLine2 = (TextView) arg1.findViewById(R.id.tv_member_details_line2);
final TextView txtLine3 = (TextView) arg1.findViewById(R.id.tv_member_details_line3);
final TextView txtLine4 = (TextView) arg1.findViewById(R.id.tv_member_details_line4);
txtLine1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
callIntent(1);
}
});
txtLine2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
callIntent(2);
}
});
txtLine3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
callIntent(3);
}
});
txtLine4.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
callIntent(4);
}
});
}}
Now, when I click on an item for the first time, the TextView listeners are getting set. The individual clicks only works after the second time. What should I do in such a case?
Put this code in the getView() method in the adapter. And in your code onItemClick is called when you click on a row of the listview.
Add the following attribute for each of the textviews(The four textviews in the layout which going to use as the custom list item)
android:onClick="onTextView1_click"
- You have to do this for all the 4 textviews to track the click event seperately-
Then in the code add the following method to trigger when you click the textview1
public void onTextView1_click(View view)
{
final int position = (Integer) view.getTag();
//implementation (Which needs to be done when someone click textview1)
}
Also most importantly You have to add the position as a tag from the getViewMethod in adapter class. this will help to track the clicked item in the listview
holder.textView1.setTag(position);
and you can access this value as I have indicated in the "onTextView1_click(View view)" method "Position variable"
In the List view --->
android:focusable="false"
In the code add the "setItemsCanFocus(true)" soon after initialize list view. This indicates focusable items contains within each item in list view.
lvMembersList = (ListView) findViewById(R.id.NameOfListTView);
lvMembersList.setItemsCanFocus(true); // <---------[X]

Android: how do i switch from one activity to another by clicking items from the ListView

i have made menu list containing play, settings, exit. but clicking the button doesn take me to the desired activity the listner is not working..can any one help me solve the problem.. will b thankfull..
yes its a list view control..
there are two errors in my code one is that
## The type new AdapterView.OnItemClickListener(){} must implement the inherited abstract method AdapterView.OnItemClickListener.onItemClick(AdapterView, View, int, long)## in LINE1
And the other one is ## View cannot be resolved to a type## in LINE 2
"Actually i want to shift from one screen to another when i click on the item in main menu screen"
Here is the code of main menu
public class MenuActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
ListView menuList = (ListView) findViewById(R.id.list);
String[] items = {
getResources().getString(R.string.pla),
getResources().getString(R.string.sco),
getResources().getString(R.string.set),
getResources().getString(R.string.hel),
getResources().getString(R.string.qui)
};
ArrayAdapter < String > adapt = new ArrayAdapter < String > (this, R.layout.menu_items, items);
menuList.setAdapter(adapt);
menuList.setOnItemClickListener(new AdapterView.OnItemClickListener() { //LINE 1 error
public void onItemClick(AdapterView <? > parent, View itemClicked, //LINE 2 error
int position, long id) {
TextView textView = (TextView) itemClicked;
String strText = textView.getText().toString();
if (strText.equalsIgnoreCase(getResources().getString(
R.string.pla))) {
// Game
startActivity(new Intent(MenuActivity.this,
GameActivity.class));
} else if (strText.equalsIgnoreCase(getResources().getString(
R.string.hel))) {
// Help
startActivity(new Intent(MenuActivity.this,
HelpActivity.class));
} else if (strText.equalsIgnoreCase(getResources().getString(
R.string.set))) {
//Settings
startActivity(new Intent(MenuActivity.this,
SettingsActivity.class));
} else if (strText.equalsIgnoreCase(getResources().getString(
R.string.sco))) {
// Scores
startActivity(new Intent(MenuActivity.this,
ScoresActivity.class));
}
}
});
}
}
ListView lv = ...;
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0,View arg1, int arg2, long arg3)
{
//Do Your Stuff
}
});
or you can make your custom adapter and write the button's click event
use intent
Intent play= new Intent(getApplicationContext(),Play.class);
startActivity(play);
there's lots of tutorial about menu
This task can be accomplished using one of the android's main building block named as Intents and One of the methods public void startActivity (Intent intent) which belongs to your Activity class.
An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.
An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.
Refer the official docs -- http://developer.android.com/reference/android/content/Intent.html
public void startActivity (Intent intent) -- Used to launch a new activity.
So suppose you have two Activity class and on a button click's OnClickListener() you wanna move from one Activity to another then --
PresentActivity -- This is your current activity from which you want to go the second activity.
NextActivity -- This is your next Activity on which you want to move.
So the Intent would be like this
Intent(PresentActivity.this, NextActivity.class)
Finally this will be the complete code
public class PresentActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Intent activityChangeIntent = new Intent(PresentActivity.this, NextActivity.class);
// currentContext.startActivity(activityChangeIntent);
PresentActivity.this.startActivity(activityChangeIntent);
}
});
}
}
This exmple is related to button click you can use the code anywhere which is written inside button click's OnClickListener() at any place where you want to switch between your activities like in your setOnItemClickListener.

Categories

Resources