Im really confused when trying to do this:
Lets say i want to build a app which allow you to read information about animals.
on my main class i have a listview which items: Cat, Dog, Snake, Rabbit and so on.
If i tap example Dog my other class and my other layout.xml will pop up. In this details.xml layout got a Header text, a picture and a maintext.
However the text is just null or nothing. So how do i do so the text will change depend on which item i clicked.
For Example i dont want text about cat and a cat picture when o pressed the Rabbit item.
Is this possible? Ive tried to call methods but i just get a FC if the method is not in the current class. ive also tried the intent extra but that try didnt went well.
thank you!
Your Friend ;D
If i tap example Dog my other class
and my other layout.xml will pop up
I guess you have implemented the onListItemClick
protected void onListItemClick(ListView l, View v, int position, long id)
This method has an id that you can add in the intent to send to the next activity to know which row (so which item) has been clicked.
So in the next activity, you get the right layout.xml and information depending on the id received.
Edit
Let's say MyFirstActivity is your list activity:
public class MyFirstActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new String[] {"Hello","World","Foo","Bar"}));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent intent = new Intent(this,MySecondActivity.class);
intent.putExtra("id",id);
startActivity(intent);
}
}
Then, in onListItemClick, it sends an intent to MySecondActivity with the id as extra where I can retrieve it and get the info according to it
public class MySecondActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Long lngId = getIntent().getExtras().getLong("id");
Integer id = lngId.intValue();
String message = null;
switch(id) {
case 0:
message = "Hello";
break;
case 1:
message = "World";
break;
case 2:
message = "Foo";
break;
case 3:
message = "Bar";
break;
}
Toast.makeText(this,message + " was clicked",Toast.LENGTH_SHORT).show();
}
}
Hope this helps
Related
What i am trying to do:
I have an activity, on the top instead of app title and icon i am
trying to implement a button like a back button shown in figure
below
I tried with menu feature but all icons appear on the right side of
actionbar
Is there any already posted SO-Question on this ?
Otherwise how to achieve this
I have the class below
MainActivityList.java
public class MainActivityList extends ListActivity {
TextView content;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity_list);
content = (TextView)findViewById(R.id.output);
String[] values = getResources().getStringArray(R.array.list_of_selections);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
switch (position) {
case 0:
Intent intent=new Intent(this,AndroidMediaPlayer.class);
startActivity(intent);
break;
default:
break;
}
}
}
From my experience, using action bar needs to follow up design guideline.
For your case, i think that there is no need to use it.
You can use a custom View as ActionBar, it will be simple to customize and design.
see this sample library as an example.
https://github.com/johannilsson/android-actionbar
I'm developing a Androidapplication and I want to be able to start an activity through a spinner value the user have selected.
As an example: In the main activity you'll see a spinner with the values "Color", "Animals". If you choose "Color" and click on a button called "Proceed", you will come to an activity that list different colors in a listview, but if you choose "Animal" from the spinner, you will come to the same activity, but this time it shows a listview of animals instead.
Can anybody give me a hint on how to do this?
(PS this is an example on how I want it to work, I actually need to call everything from a Web Service)
you can implement android.widget.AdapterView.OnItemSelectedListener and override the method
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
// Call the following method to get the selected value of the spinner and perform your
//task to start your desired activity
String selectedValue=parent.getItemAtPosition(pos).toString();
if(selectedValue.equals("Colors"))
{
//do your task using color
}
else if(selectedValue.equals("Animal"))
{
//do your task using animal
}
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
//Do Nothing
}
The simplest Solution would be for example:
Spinner yourSpinner = (Spinner)view.findViewById(R.id.your_spinner);
String value = yourSpinner.getSelectedItem().toString();
if(value.equalsIgnoreCase("ANIMAL")){
Intent intent = new Intent(yourActivity.this, yourNextActivity.class);
intent.putExtra("VALUE", value);
startActivity(intent)
}
Then in that Activity that You want to start:
Intent intent = getIntent();
String value= intent.getStringExtra("VALUE");
if(value.equalsIgnoreCase("ANIMAL")){
//start the part with animals
}
Its really simple just use bundle and send the appropriate data to your next activity. A small sample would look like this:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String spinnerText = spinner.getSelectedItem().toString();
Intent intent = new Intent(A.this,B.class);
intent.putExtra("selection",spinnerText);
}
});
Then in your next activity get the sent text and manipulate accordingly.
I am kind of new to Android development. Could you please tell me, how to navigate to another screen using onListItemClick. I have 200 items in a ListView. If i click an item from the ListView, it has to navigate to another screen which should show the details of the clicked item.
To get what you want you can do something like this...
1>create another activity with certains controls in which you can place your values...
2>on itemclick() of listview...
a>create intent and set values to pass to new activity
b>start new activity with this intent.
3>in oncreate() of new activity
a>retrive the values from intent
b>populate your controls using the values...
I am assuming you are getting the list and detail of actor from database.
I have used the same walk through to display recipe detail of selected recipe in my app.
public class ActorList extends Activity {
ListView myActorList=null;
public final static String selectedActor_ID="will Keep Actor ID";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_actor_list);
//initialize controls
myActorList.setAdapter(adapter);
myActorList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v,
int position, long id) {
// TODO Auto-generated method stub
Intent i= new Intent(ActorList.this,ActorDetail.class);
// you have to pass the actor id to next activity
// you can get this actor id from argument of type "long"
i.putExtra(selectedActor_ID, String.valueOf(id));
startActivity(i);
}
});
}
}
//Other Activity To show Detail..
//get the ID of Selected Actor on other activity say "ActorDetail"
public class ActorDetail extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_actor_detail);
ActorID=getIntent().getStringExtra(ActorList.selectedActor_ID);
// now as you have the id here for that particular actor
//fetch the detail of that selected actor thru id and bind it to your layout.
}
}
You need to register the other activity in Manifest file also like this
<activity
android:name=".otherscreenActivity"
</activity>
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.
I'm working on a project at school, and I need help implementing ListViews properly. I have to do the Settings and Help sections on the project and as of now, I can display what's on my list and it displays Toast messages of the selected item when I click it. My question is, how can I create and show the content that is inside that specific item? For example, I have an option "Edit Password" and that when I click it, it should display my "Edit Password" screen. This applies to both my sections.
This is what I have so far. It's basically the android ListView tutorial but I added my options on there. My question is, when I click one of the options on my list, how can I display its specific details? Like what I said before, if I click "Edit Password", I want it to go to a "Edit Password" screen. Or on Help, if I click let's say, "Credits", I want it direct me to the Credits page.
public class Settings extends ListActivity {
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.settings, SETTINGS));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id)
{
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
static final String[] SETTINGS = new String[]
{"Set Refresh Rate", "Edit Password", "Delete Account"};
}
When you extend ListActivity you already have the OnItemClickListener implemented, you should override the method onListItemClick. In that method you should use an Intent to get to a new Activity where you will display the stuff you want:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Intent i = new Intent(this, SecondActivityName.class);
i.putExtra("pos", position); //pass the position of the clicked row so we know what to show.
startActivity(i); // let's go to the other activity
}
SeconActivityName is an Activity that you should create and where you would show the other screen you want(remember to add the activity to the manifest file!):
public class SecondActivityName extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = getIntent();
int rowPosition = i.getIntExtra("pos", -1); //we now have the row's position that was clicked in the other activity.
// based on that you show the screen you want for example after a switch
switch (rowPosition) {
case -1:
//this is the default value, something has gone terrible wrong
//finish the activity and hide:
finish();
break;
case 0:
//row 0 was clicked so show the "Set Refresh Rate" screen
setContentView(R.layout.refresh_rate__screen);
break;
//do the same for the other rows;
//...
This will work if the screens for the various settings are not that different. If they are you'll probably have to implement a different activity for each one and start the appropriate activity in the onListItemClick based on the position parameter.