Is it possible, to use GridView for activities? - android

I want show activity on gridView like screenshoot.
When I click on item it open activity, when I click back it show gridView again.
When I click newItem button it create new activity and show it on gridView.
Is it posible or I need use something other, not gridView?

Of course you can, and it's quite simple too.
1 - Create an activity named "GridHome.java" for instance. Make the layout for this activity be only a GridView.
2 - Fill the gridview with pre-loaded images from your other activities. Say for instance, take a screenshot of the other activities and store them as PNG, then add them as drawables into your application, and lastly add them to the gridview via a customer adapter.
3 - Set an OnItemClickListener to the gridview so that when the user clicks on one, you can start a new intent to the activity you wish to open. For this one you can add a method like this to your custom grid adapter:
public String getItemActivityName(int pos) {
return activityNames.get(pos);
}
where "activityNames" is an Array of strings corresponding to the activities you have on the grid view.
And from the "GridHome.java" you would do this to the GridVIew:
mGridView = (GridView) view.findViewById(R.id.home_grid);
mGridView.setAdapter(mAdapter);
mGridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
String activityName = mAdapter.getActivityName(position);
if (activityName.equals(ACTIVITY_ONE) {
Intent intent = new Intent(getActivity(), ActivityOne.class);
startActivity(intent);
}
}
});
Where ACTIVITY_ONE is a constant that represents the name of one your activities.
private static final String ACTIVITY_ONE = "Contacts";
private static final String ACTIVITY_TWO = "Groups";
And so forth and on.

Related

How to get the id resource id of a view in a List view

I have Listview which is showing different images of animals,birds,reptiles. The list view is working fine. Now I want when user click to any picture it should appear in the ImageView.The ImageView is just below the listview. so when ever user will click any image in the listview it should appear in the Imageview.
Also there is a button. Now I want to achieve that when user select any image and press Ok button that paticular image should show on the image view of other activity also. I Know I can send the Id through intent.putextra(), but the problem is first place how to get the id of particular picture.
Source code
public class MainActivity2 extends ActionBarActivity {
private TypedArray ListIcons;
private HorizontalListView listView;
private ArrayList<AnimalsListItems> SuitItems;
private AnimalsListAdapter adapter1 = null;
/** An array of strings to populate dropdown list */
String[] actions = new String[] {
"Bookmark",
"Subscribe",
"Share"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
listView = (HorizontalListView) findViewById(R.id.suits_list);
AnimalsItems = new ArrayList<AnimalsListItems>();
/** Create an array adapter to populate dropdownlist */
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_dropdown_item, actions);
/** Enabling dropdown list navigation for the action bar */
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getSupportActionBar().setDisplayShowTitleEnabled(false)
;
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(" "+" "+" ");
/** Defining Navigation listener */
android.support.v7.app.ActionBar.OnNavigationListener navigationListener = new android.support.v7.app.ActionBar.OnNavigationListener() {
#Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
Toast.makeText(getBaseContext(), "You selected : " + actions[itemPosition], Toast.LENGTH_SHORT).show();
ListIcons = getResources()
.obtainTypedArray(R.array.ic_launcher);// load icons from
// strings.xml
for (int i = 0; i<=ListIcons.length(); i++) {
AnimalsItems.add(new AnimalsListItems(ListIcons.getResourceId(i,-1)));
}
adapter1 = new SuitsListAdapter(getApplicationContext(),SuitItems);
Log.v(adapter1.getCount()+"");
listView.setAdapter(adapter1);
return false;
}
};
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//With the position parameter, you can fetch the item at the clicked position like below. Cast it to whatever type your ListView is.
Object yourItem = (Object) listView.getItemAtPosition(position);
//Now you can assign the image to an imageview
}
});
/** Setting dropdown items and item navigation listener for the actionbar */
getSupportActionBar().setListNavigationCallbacks(adapter, navigationListener);
this.overridePendingTransition(R.anim.anim_slide_in_left,
R.anim.anim_slide_out_left);
}
}
For your ListView, you can set an onItemClickListener as below
ListView listView1 = (ListView) findViewById(R.id.listView1);
listView1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//With the position parameter, you can fetch the item at the clicked position like below.
AnimalsListItems yourItem = (AnimalsListItems) listView1.getItemAtPosition(position);
//Now you can assign the image to an imageview
}
}
Not sure what you mean by "id", if you're talking about drawable id's, path to image files, or just any way to identify your image. It depends a lot on how you populate your list. You didn't supply any code at all.
But since you have a ListView I suppose you have an Adapter, and that you're backing that adapter with some data, such as an array, database etc. When you click on an item, you get an onItemClick callback with the index of the item you clicked on. You should be able to use this index to find which image you clicked on from your data source.
Then when it comes to passing that to another activity, well, again it depends on how you represent your images but you should be able to pass whatever data you need to represent the image in an intent extra to the second activity.
You can use the setOnItemClickListener method of ListView to get witch item clicked.Because one item one image, you can get the target image.
1.Step
Rather getting resource id you can create one integer array containing resource id's like,
int resourceID[] = [R.drawable.image1, R.drawable.image2, ...]
Assign this array to your ListView image item
2.Step
Now can easily get the postion of selected list item or image using Lists on item click method
listView.setOnItemClickListener(new OnItemClickListener() {} )
3.Step
Just pass your ResourceID array & tapped position to next/target activity & use it, rather passing resourceID
or
Otherwise make your ResourceID as static, pass only tapped position to next Activity & use this static ResourceID from previous Activity
i.e -> yourImageView.setImageResource(FirstActivity.ResourceID[position])

how to get the saved items for a particular listitem in android?

I am developing an application in which when user clicks on a item of a listview, a new page appears and in this page only the selected items are displayed. I mean for a particular list item only previously saved items are displayed.
In this new page i have 3 checkboxes. So when user clicks on a particular list item the new page display the checkbox with previous saved state for that particular item.
Now I dont know how to do that.
thanx in advance!!
This is my list onItemClick :-
OnItemClickListener ocl= new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
String name= list.get(arg2)[2];
Toast.makeText(Profile.this, "session of.." + name, Toast.LENGTH_LONG).show();
Settings.setName(name);
Intent intent= new Intent(Profile.this, ProfileConfig.class);
startActivity(intent);
}
};
lvChildProfile.setOnItemClickListener(ocl);
Now i want that when clicking the saved instance of checkbox appear.
You can keep the state within your objects itself.
Model implements Serializable {
boolean isOptionOneChecked
boolean isOptionTwoChecked
boolean isOptionThreeChecked
int id
...
// Constructor
// Getters & setters
}
This means your Adapater will have a list of objects from the type Model. How you want to display these in your ListView, can be done with a custom Adapter but that's not the real question.
When you click on an Item in your list, you can retrieve the object (like you already did) with
Model lModel = list.get(position);
In order to display the Model in a different Activity, you can send it along with your Intent as such:
Intent intent= new Intent(Profile.this, ProfileConfig.class);
intent.putExtra("MySelectedObject", lModel);
startActivity(intent);
Because our class implements the Serializable interface, we can retrieve the object in our new Activity as followed:
getIntent().getSerializableExtra("MySelectedObject");
You will have all the information about this object so you can create you layout accordingly.

How to call different intents from each listview item in android?

I am working on android applications. In my project I need to create Listview. My layout i.e info.xml contains topbar with one image view, footer with other imageview. Also in the center of the layout I kept an imageview and on that I have created the Listview. Also I have created row.xml for the textview to display listitems. Now when I click on each listitem a new intent should be called...i.e when I click on 1st listitem page1 should open. Similarly if I click on 2nd listitem page2 should open and so on. So how could I do that. I am struggling for this since 3 days but didnt find any correct solution. Please hgelp me regarding this.
My Code:
public class Information extends Activity
{
private String[] Countries;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.information);
Countries = getResources().getStringArray(R.array.countries);
ListView list = (ListView)findViewById(R.id.listnew);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.row, Countries);
list.setAdapter(adapter);
registerForContextMenu(list); }
}
All you have to add is
list.setOnItemClickListener(this);
Then you let your class implement the OnItemClickListener interface and create this method:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch(position){
case 0:
Intent firstIntent = new Intent(this, MyClass.class);
startActivity(firstIntentIntent);
break;
case 1:
Intent secondIntent = new Intent(this, MySecondClass.class);
startActivity(secondIntentIntent);
break;
[... etc ...]
}
}
In this case, if the first item is clicked, it will launch the MyClass Activity, if the second item is clicked, the MySecondClass Activity will be launched, etc.
Yes, it is a bit tedious but it is the best way.

How can i start a new activity within an Activity?

For example i have a video gallery. I scroll to a certain video click on it and a whole new activity starts. Whats the most efficient way to code this?
If you have a list, I'm sure you are getting a hold of it in the code and assigning an Adapter. You can define what happens (starting an activity) by defining setOnItemClickListener() as such:
final ListView list = (ListView)findViewById(R.id.myVideoList);
MyVideoAdapter adapter = new MyVideAdapter(this);
list.setAdapter(adapter);
list.setOnItemClickListener(new ListView.OnItemClickListener(){
public void onItemClick(AdapterView<?> av, View v, int position, long id){
MyVideoObject video = (MyVideoObject)av.getItemAtPosition(position);
Intent intent = new Intent(CurrentActivity.this, SelectedVideo.class);
intent.putExtra("video_id, video.getID());
startActivity(intent);
}
});
All you're doing above is to initializing the list and assigning what happens when the user clicks on the list item.
When a click happens, you're getting a hold of the video item (however you're juggling them between activities) and passing perhaps its ID to the new Activity you wanna launch. The new activity (SelectedVideo.class) can receive the ID in onCreate and perhaps play the video.
Hope this helps,
-serkan

Passing listView row positions through Intents to another class

I'm wondering how to pass a row position(pos) value from, say, a CHOICE_MODE_SINGLE list Activity(A) to another Activity(B) using Intents? (I want to change ActivityB to show another list depending on what row in ActivityA is clicked). Here's my code:
final ListView listView = getListView();
listView.setItemsCanFocus(false);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setOnItemClickListener(
new android.widget.AdapterView.OnItemClickListener(){
#Override
public final void onItemClick(AdapterView<?> listView, View cell, int position, long id) {
Intent Courses = new Intent(this, ExpandableList.class);
Courses.putExtra(//I'm not sure what to put in here//)
});
private static final String[] GENRES = new String[] {"Barre","Buffumville","Hodges","Newton Hill"};
}
THANKS :)
Courses.putExtra("position",position);
Then to get the position in the next activity:
getIntent.getExtras().getInt("position");
You can pass the position through the intent putExtra.
please see below Code.
Intent Courses = new Intent(this, ExpandableList.class);
Courses.putExtra("position",position)
startActivity(Courses);
Now you can get this value in another activity like this.
getIntent.getExtras().getInt("position");
It will return the integer position you passed from the first activity.

Categories

Resources