I'm using SherlockActionbar and I wanted to fill dynamically the ListNavigation spinner in the actionbar. The problem is that my list navigation doesn't display anything. However the app is running on the emulator without any errors.
Here is the activity code:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.content.Context;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
public class TestView extends SherlockFragmentActivity {
private ArrayAdapter<String> listnav;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.test_view_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ActionBar ab = getSupportActionBar();
ab.setDisplayShowTitleEnabled(false);
ab.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
Context context = ab.getThemedContext();
setContentView(R.layout.test_view);
List<String> items = new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.testarray)));
listnav = new ArrayAdapter<String>(context, R.layout.sherlock_spinner_item, items);
listnav.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
}
}
Here is the code for the array resource:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="testarray">
<item >2011</item>
<item >2012</item>
</string-array>
</resources>
I solved this problem. The spinner in the actionbar can be accessed with the setListNavigationCallbacks.
For more Information about this method click here
I solved this problem. The spinner in the actionbar can be accessed with the setListNavigationCallbacks. For more Information about this method click here
Related
I am watching a android studio tutorial but have an error message.
i found out strings.xml have string-array tag with name of "items". However, they cannot be brought to MainActivity.java.
Error is "mergeDebugResources"
strings.xml
<resources>
<string name="app_name">List App</string>
<String-array name="items">
<item>peach</item>
<item>tomato</item>
<item>squash</item>
</String-array>
</resources>
MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
ListView myListView;
String[] items;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res=getResources();
myListView=(ListView) findViewById(R.id.myListView);
items=res.getStringArray(R.array.items);
}
}
Please explain to me why the error happened! Thank you so much!
I use this tutorial to make Navigation Drawer: http://www.tutecentral.com/android-custom-navigation-drawer/
so now i want to add my navigation to other activities but unfortunately i cant.
i also follow this : How to Display Navigation Drawer in all activities?
and do this on my second activity but it have some error:
package com.example.uniapp;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class ShowAll extends Description {
MyDatabase MyDataBase;
SQLiteDatabase mydb;
TextView title,content;
ImageView image;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.showall, null, false);
mDrawerLayout.addView(contentView, 0); //null pointer error
}
}
Do you have any suggestion?
This means your drawerLayout is null. This is probably because it can;t find it. If you want your navigationdrawer in multiple activities, you also need to add the drawerLayout the all the activities its xml files.
I am using Xamarin and I have a ListView activity with some Custom Views. I also have a map activity. The map activity starts as the start up project. After the map activity has started up, I wish to display a ListView (that is currently in an activity).
My question is this: Do I need to start a new activity, with an intent, to display this ListView? Can I display a ListView from my map activity?
Thanks in advance
You can remove all views from content view of current Activity and create your ListView in code then add it to your content view.Or you can use switchView to show it.Also you can create a layout that has your map and list,at first set your list invisible and then set your map invisible and change list visible.
1-
ViewGroup vg = (ViewGroup)(mapView.getParent());
vg.removeView(mapView);
//then create your listview and add it:
...
vg.addView(myListView);
2-If you want change visibility,see Android : difference between invisible and gone?
Finally you can search in about switching views in android,It will be useful.
You can make an AlertDialog which can be launched from current Activity by creating a Custom Layout for AlertDialog.
MainActivity.java
package com.example.alertlist;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.view.LayoutInflater;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
AlertDialog al;
ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlertDialog.Builder b = new AlertDialog.Builder(this);
LayoutInflater i = getLayoutInflater();
b.setView(i.inflate(R.layout.alert_list, null));
al = b.create();
al.show();
lv = (ListView) al.findViewById(R.id.listView1);
String[] myStringArray = {"this", "is", "it"};
ArrayAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myStringArray);
lv.setAdapter(adapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
alert_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
I want to add action items to actionbar sherlock from shelock fragment and need to implement the click listener also. I have used following code for the fragment activity.
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
public class MyTasksFragment extends SherlockFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.mytask_fragment_layout, container, false);
return view;
}
#Override
public void onCreateOptionsMenu( Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.action_mytask, menu);
}
}
And follwing is the code of action_mytask.xml (ic_action_edit image is also in the drawable folder)
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_task"
android:icon="#drawable/ic_action_edit"
android:title="#string/add_task"
android:showAsAction="ifRoom" />
</menu>
The added icon is not coming to the ABS. Here is the image
I want to know
How to add action items for the actionbar sherlock from sherlock fragment ?
How to implement click listners for those activities ?
Please help,
Thank you
you should call setHasOptionsMenu(true); in onCreate and you should all call super.onCreateOptionsMenu(menu, inflater)
Override onCreate method and put setHasOptionsMenu(true); in it. It will tell the activity that fragment has it's own option menu. For click listener override onOptionsItemSelected method.
I'm using android 2.3.3. I set up a layout like,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="#+id/mainList" android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
And I'm manipulating it with
package org.dewsworld.ui;
import org.dewsworld.core.NBConfig;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
public class NewsBotActivity extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter( new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
NBConfig.topics));
ListView listView = (ListView) findViewById(R.id.mainList) ;
listView.setOnItemClickListener( new OnItemClickListener() {
});
}
}
Using eclise IDE, when I setthe OnItemClickListener it gives me the error
The method setOnItemClickListener(AdapterView.OnItemClickListener) in the type AdapterView<ListAdapter> is not applicable for the arguments (new
OnItemClickListener(){})
I can't fix this. [I've added an image with the error]
It seems you have imported the wrong OnItemClickListener, try this one instead, and remove import of android.view.View.OnClickListener
import android.widget.AdapterView.OnItemClickListener;
how about filling in the body of your new object by defining the onItemClick() function:
public void onItemClick(AdapterView parent, View v, int position, long id)
{
// Display a messagebox.
Toast.makeText(this,"Your Listener Works!",Toast.LENGTH_SHORT).show();
}
try using ctrl+shift+o in eclipse to organize all your imports automatically...
Just implement OnItemClickListener to your class.
Like This:
public class ClassName extends Activity implements OnItemClickListener{}