Button instead of appicon and title in android actionbar - android

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

Related

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.

Android: handle onItemClick() in listView-based app

I'm new to Android. I'm working on a listView-based app: you have a main menu, when you click an item, a new activity starts with another menu; when you click again, a new activity starts with the content you selected. Since I've quite a lot of menu items, I've to create a listener which handles all possible cases, so I've something similar:
#Override public void onItemClick(AdapterView<?> listAdapter, View v, int position, long id) {
String text = ((TextView)v).getText().toString();
//main menu
if (text.equals(K.getStringById(K.ID_MAIN_THEORY))) {
...
} else if (text.equals(K.getStringById(K.ID_MAIN_EXERCISE))) {
...
}
else if (text.equals(K.getStringById(K.ID_MAIN_TABLES))) {
...
}
//here other menus' items: lots of items
...
//back item
else if (text.equals(K.getStringById(K.ID_BACK))) {
activity.finish();
}
Intent i = new Intent(...);
startActivity(i);
}
* K is a class that holds ids' references
There's a way I can avoid hard-coding listener behavior?
** PS: the lists' TextViews don't render properly: text appears light grey, not black! O.o
It's more useful to use the position rather than the view to switch the action of the click event. Your ListView should be populated from some sort of List, therefore when the onClick fires, giving you the position, you can reference that position in the List to know what was clicked.
If you want to click on item and in result it goes to another activity, for example if you have three item and when you click on item A it will redirect you to activity A and if B then to B and so on,
lstview.setOnItemClickListener(new OnItemClickListener()
{
private String input;
public void onItemClick(AdapterView<?> v, View arg1, int index,long arg3)
{
if(index==1)
{
Intent intnt=new Intent(v.getContext(),A.class);
startActivity(intnt);
}
else if (index==2)
{
Intent intnt=new Intent(v.getContext(),B.class);
startActivity(intnt);
}
else
{
Intent intnt=new Intent(v.getContext(),C.class);
startActivity(intnt);
}
}
}
Here in this case you are going to different activities on selection the items in a list view, put your comments for further query.
I solved this way: I think it's pretty good.
//This in K class, where MAPPED_HIERARCHY is a static field
static void activity(android.app.Activity a) {
MAPPED_HIERARCHY = new HashMap<String, ArrayList<String>>();
MAPPED_HIERARCHY.put(a.getResources().getString(R.string.app_name), K.array2list(a.getResources().getStringArray(R.array.menu_main)));
MAPPED_HIERARCHY.put(a.getResources().getString(R.string.main_theory), K.array2list(a.getResources().getStringArray(R.array.menu_theory)));
MAPPED_HIERARCHY.put(a.getResources().getString(R.string.main_exercise), K.array2list(a.getResources().getStringArray(R.array.menu_exercise)));
....
}
static ArrayList<String> getArrayListByString(String string) {
ArrayList<String> copy = new ArrayList<String>(MAPPED_HIERARCHY.get(string).size());
for (String s : MAPPED_HIERARCHY.get(string))
copy.add(s);
return copy; //defensive copy
}
//This in my listener class
#Override public void onItemClick(AdapterView<?> listAdapter, View v, int position, long id) {
String text = ((TextView)v).getText().toString();
ArrayList<String> params = K.getArrayListByString(text);
Intent i = new Intent(...);
i.putExtra("params", params);
activity.startActivity(i);
}
I have a map which links an arraylist containing the menu items with the string wihich causes the menu to be displayed; on the click event, I simply retrieve the arraylist according with the selected item's text.

ListView assistance

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.

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.

Android Development: Passing information

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

Categories

Resources