How to Extend Two Classes in Android? - java

I need to extend Two classes from a Single Class.My class Wants to extend Both ListActivity & MainActivity.
I found a question similar to this.
But i don't know how to Implement this https://stackoverflow.com/a/5836735/2781359
Thanks for your Help.
The Class which has to be Extended is ConnectionEditActivity.
public class ConnectionEditActivity extends ListActivity implements OnClickListener
{
public static Connection connectionParam;
private Connection connection;
private Button save;
private EditText name;
private EditText password;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.connection = connectionParam;
this.save = (Button) this.findViewById(R.id.save);
this.save.setOnClickListener(this);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD)
{
// Don't need the Save button on newer devices
android.widget.LinearLayout.LayoutParams a = (LayoutParams) this.save.getLayoutParams();
a.height = 0;
this.save.setLayoutParams(a);
this.save.forceLayout();
}
this.name = (EditText) this.findViewById(R.id.name);
this.password = (EditText) this.findViewById(R.id.password);
}
#Override
public boolean onCreateOptionsMenu(android.view.Menu menu)
{
// Inflate the menu items for use in the action bar
android.view.MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.connection_edit_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(android.view.MenuItem item)
{
// Handle presses on the action bar items
switch (item.getItemId())
{
case R.id.action_save:
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
protected void onResume()
{
super.onResume();
this.name.setText(this.connection.getName());
this.password.setText(this.connection.getPassword());
}
protected void onPause()
{
super.onPause();
this.connection.setName(this.name.getText().toString());
this.connection.setPassword(this.password.getText().toString());
finish();
}
public void onClick(View v)
{
if (v == this.save)
{
this.finish();
}
}
}
Mainactivity
public abstract class MainActivity extends ActionBarActivity
{
protected ListView mDrawerList;
protected DrawerLayout mDrawer;
private CustomActionBarDrawerToggle mDrawerToggle;
private String[] menuItems;
String LOG_TAG = "Remote It";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
supportRequestWindowFeature(WindowCompat.FEATURE_ACTION_BAR);
// getSupportActionBar().hide();
setContentView(R.layout.activity_main_drawer);
// enable ActionBar app icon to behave as action to toggle nav drawer
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
// set a custom shadow that overlays the main content when the drawer
// opens
mDrawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
_initMenu();
mDrawerToggle = new CustomActionBarDrawerToggle(this, mDrawer);
mDrawer.setDrawerListener(mDrawerToggle);
}
private void _initMenu()
{
NsMenuAdapter mAdapter = new NsMenuAdapter(this);
// Add Header
mAdapter.addHeader(R.string.ns_menu_main_header);
// Add first block
menuItems = getResources().getStringArray(R.array.ns_menu_items);
String[] menuItemsIcon = getResources().getStringArray(R.array.ns_menu_items_icon);
int res = 0;
for (String item : menuItems)
{
int id_title = getResources().getIdentifier(item, "string", this.getPackageName());
int id_icon = getResources().getIdentifier(menuItemsIcon[res], "drawable", this.getPackageName());
NsMenuItemModel mItem = new NsMenuItemModel(id_title, id_icon);
// if (res==1) mItem.counter=12; //it is just an example...
// if (res==3) mItem.counter=3; //it is just an example...
mAdapter.addItem(mItem);
res++;
}
// Second Block
mAdapter.addHeader(R.string.ns_menu_main_header2);
menuItems = getResources().getStringArray(R.array.ns_menu_itemss);
String[] menuItemsIcons = getResources().getStringArray(R.array.ns_menu_items_iconss);
int ress = 0;
for (String item : menuItems)
{
int id_title = getResources().getIdentifier(item, "string", this.getPackageName());
int id_icon = getResources().getIdentifier(menuItemsIcons[ress], "drawable", this.getPackageName());
NsMenuItemModel mItem = new NsMenuItemModel(id_title, id_icon);
// if (res==1) mItem.counter=12; //it is just an example...
// if (res==3) mItem.counter=3; //it is just an example...
mAdapter.addItem(mItem);
res++;
}
mDrawerList = (ListView) findViewById(R.id.drawer);
if (mDrawerList != null)
mDrawerList.setAdapter(mAdapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
}
#Override
protected void onPostCreate(Bundle savedInstanceState)
{
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
/*
* The action bar home/up should open or close the drawer.
* ActionBarDrawerToggle will take care of this.
*/
if (mDrawerToggle.onOptionsItemSelected(item))
{
return true;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
private class CustomActionBarDrawerToggle extends ActionBarDrawerToggle
{
public CustomActionBarDrawerToggle(Activity mActivity, DrawerLayout mDrawerLayout)
{
super(mActivity, mDrawerLayout, R.drawable.ic_drawer, R.string.ns_menu_open, R.string.ns_menu_close);
}
#Override
public void onDrawerClosed(View view)
{
getSupportActionBar().setTitle(getString(R.string.ns_menu_close));
supportInvalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
#Override
public void onDrawerOpened(View drawerView)
{
getSupportActionBar().setTitle(getString(R.string.ns_menu_open));
supportInvalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
}
private class DrawerItemClickListener implements ListView.OnItemClickListener
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
mDrawer.closeDrawer(mDrawerList);
switch (position)
{
case 1:
Intent a = new Intent(MainActivity.this, Home.class);
startActivity(a);
break;
case 2:
Intent ac = new Intent(MainActivity.this, ConnectionListActivity.class);
startActivity(ac);
break;
default:
}
}
EDIT
I need to Extend it.Because the MainActivity has the navigation drawer.Now ConnectionEditActivity
doesn't shows the navigationDrawer nor the ActionBar .But i need to show the ActionBar
Any Suggestions ??

In Java you can't extend multiple classes, and for a good reason. Take for example what you are trying to accomplish by extending MainActivity and ListActivity. In your new class, when you call:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
...
}
Which onCreate() are you overriding? The one from ListActivity, or the one from MainActivity?
What the link you posted is saying is that instead of inheriting from another object, you compose your new object of the one you are trying to use. For example:
public class NewClass extends OldClass1 {
private OldClass2 mOldClass2 = new OldClass2();
#Override
public methodFromOldClass1() {
}
public methodFromOldClass2() {
mOldClass2.methodFromOldClass2();
}
}
The problem with this approach is that the methods from MainActivity and ListActivity are still going to have the same name, which although you can work around, it will become a headache quickly.
So the problem is a result of how you designed your class hierarchy. You will need to think about what functions you need from MainActivity, and what functions from ListActivity and choose how to reimplement your objects.
My Suggestion, since ListActivity only makes it slightly easier to work with lists (not that much easier) you can just skip it and implement the code related to the list on your own, and that way you can just extend MainActivity

You need to start by identifying what parts of MainActivity you need to inherit from, and what do you need from ListActivity.
Then, you have various possibilities:
Trivially, not extending ListActivity. Extending ListActivity only provides you with utility methods to work with the ListView, but you can totally have a ListView in an Activity without it being a ListActivity.
Create a utility class that contains extracted methods you need from MainActivity and call these methods from both your new class and MainActivity.
Modify MainActivity so that it extends ListActivity. After all it does contain a ListView (you'd loose the ActionBar thing, though).

Related

How to access drawer from another activity

Here is my drawerActivity
public class DrawerActivity extends AppCompatActivity {
public Toolbar toolbar;
public static DrawerLayout drawerLayout;
public ActionBarDrawerToggle drawerToggle;
public NavigationView navigationView;
public Context mContext;
private TextView empname, businame;
private ImageView busiimg;
private String[] drawerItem;
public NavigationView getNavigationView() {
return navigationView;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = DrawerActivity.this;
setContentView(R.layout.activity_drawer);
}
#Override
public void setContentView(int layoutResID) {
drawerLayout = (DrawerLayout)getLayoutInflater().inflate(R.layout.activity_drawer, null);
FrameLayout activityContainer = (FrameLayout) drawerLayout.findViewById(R.id.activity_content);
getLayoutInflater().inflate(layoutResID, activityContainer, true);
super.setContentView(drawerLayout);
initToolbar();
}
public void initToolbar() {
try{
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Manage vendor");
setSupportActionBar(toolbar);
}catch (Exception e){
e.printStackTrace();
}
}
private void setUpNav() {
drawerLayout = findViewById(R.id.activity_container);
drawerToggle = new ActionBarDrawerToggle(DrawerActivity.this, drawerLayout, R.string.app_name, R.string.app_name);
drawerLayout.setDrawerListener(drawerToggle);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
navigationView = findViewById(R.id.nav_main);
drawerToggle.syncState();
}catch (Exception e){
e.printStackTrace();
}
}
#Override
public void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
setUpNav();
drawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
/* #Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_search, menu);
return true;
}*/
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item))
return true;
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_search) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static void openDrawer(){
drawerLayout.openDrawer(drawerLayout);
}
#Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
}
Here is the code where I am accessing drawer when image clicked in another activity
drawerImg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DrawerActivity.openDrawer();
}
});
I am implementing navigation drawer in my android application. I have a separate activity for navigation drawer. I will extend the drawer activity where ever I want.
Now the issue is I have drawer image in bottom bar. When I click the image in bottom app bar I have to open the drawer. For one activity I want to open drawer by clicking the image in bottom bar. I know if I extend the drawer activity the hamburger icon will visible in toolbar that I don't want to achieve in this activity.
So how to avoid this scenario by using bottom bar?
Extend your draweractivity to your other activity.
try this:
public class MainActivity extends DrawerActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity);
drawerImg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openDrawer();
}
});
}
}
Try implementating an interface.
Create an interface where you want to access the drawer activity.
public interface OnDrawerListener{
void onDrawerClick();
}
OnDrawerListener onDrawerListener;
drawerImg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onDrawerListener.onDrawerClick();
}
});
Then implement this interface in your DrawerActivity.
public class DrawerActivity extends AppCompatActivity implements InterfaceActivity.OnDrawerListener{ //InterfaceActivity is activity where you have created your interface.
private InterfaceActivity.OnDrawerListener someInterface;
#Override
protected void onCreate(Bundle savedInstanceState) {
someInterface = (InterfaceActivity.OnDrawerListener) this;
}
//Your interface override
#Override
public void onDrawerClick(){
openDrawer();
}
}

Android - Navigation Drawer to implement activity changing

I've been reading a lot of questions and tutorials in regards to implementing a Navigation Drawer to change between Fragments. For this question, these implementations are not wanted as I wish to change from one activity, to the next.
I've implemented a custom Sidebar Adaptor with extends ArrayAdapter<String>. This allows to to implement my own styling to my navigation drawer. See the first code snippet below.
public class SidebarAdaptor extends ArrayAdapter<String> {
Context context;
int layoutResourceId;
String data[] = null;
public SidebarAdaptor(Context context, int layoutResourceId, String[] data)
{
// Initiate the ArrayAdapter
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ControlHolder holder;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ControlHolder();
TextView control = (TextView)row.findViewById(R.id.textItem);
holder.control = control;
row.setTag(holder);
}
else
{
holder = (ControlHolder)row.getTag();
}
String text = data[position];
holder.control.setText(text);
return row;
}
static class ControlHolder
{
TextView control;
}
}
This adapter is then set in the onCreate function in my MainActivity class. See below.
private void customiseSidebar() {
SidebarAdaptor sidebarAdaptor = new SidebarAdaptor(
this,
R.layout.side_bar_custom_row,
new String[]{"Login", "Offline Access", "Register", "Forgotten Password"}
);
ListView listView1 = (ListView) findViewById(R.id.navList);
listView1.setAdapter(sidebarAdaptor);
listView1.setOnItemClickListener(new DrawerItemClickListener());
}
The DrawerItemClickListener class then holds what activity to switch to once the user pressed any of the options in the Navigation Drawer. See the final code snippet below.
package ap.classes;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import ap.ForgottenPassword;
import apMainActivity;
import ap.OfflineAccess;
import ap.RegisterAccount;
public class DrawerItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
/** Swaps fragments in the main content view */
private void selectItem(int position) {
switch(position)
{
case 0:
Intent intent = new Intent(MainActivity.this, OfflineAccess.class);
startActivity(intent);
break;
case 1:
Intent intent = new Intent(MainActivity.this, RegisterAccount.class);
startActivity(intent);
break;
case 2:
Intent intent = new Intent(MainActivity.this, ForgottenPassword.class);
startActivity(intent);
break;
}
}
Now I seem to be having three quite substantial issues:
The first being the following error message: MainActivity is not an enclossing class
The second states that it; Cannont resolve method startActivity in android.content.Intent
The final issue is, as soon as I click anything in the NavigationDrawer, the drawer then closes without any keypresses being registered.
Thank you Stackoverflow.
First fix first two issues:
The first being the following error message: MainActivity is not an enclossing class
The second states that it; Cannont resolve method startActivity in android.content.Intent
According to code you provided you are not in the scope of the Activity class when making call
Intent intent = new Intent(MainActivity.this, OfflineAccess.class);
startActivity(intent);
above tells compiler to call method DrawerItemClickListener.startActivity(Intent intent), and there is no such method. Instead you should pass instance of an activity and call its startActivty method.
Second one MainActivity is not an enclossing class error is thrown because of usage MainActivity.this instead of passing instance of context.
To sum up you could do:
public class DrawerItemClickListener implements ListView.OnItemClickListener {
private Activity mActivity;
public DrawerItemClickListener(Activity activity){
mActivity = activity;
}
/** Swaps fragments in the main content view */
private void selectItem(int position) {
switch(position)
{
case 0:
Intent intent = new Intent(mActivity, OfflineAccess.class);
mActivity.startActivity(intent);
break;
/.../
}
}
Yes i had faced similar problem.While Fragments with navigation drawer it will be easy but using Activity. You must create a Custom Navigation Drawer. I give my idea.enter code here
public class DrawerItemClickListener extends AppCompatActivity{
private ListView mDrawerList;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private String mActivityTitle;
private List<DrawerList> drawerListList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getContentView());
drawerListList = new ArrayList<>();
mDrawerList = (ListView) findViewById(R.id.navList);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mActivityTitle = getTitle().toString();
addDrawerItems();
setupDrawer();
}
#Override
protected void onPostResume() {
super.onPostResume();
}
protected abstract int getContentView();
private void addDrawerItems() {
drawerListList.add(new DrawerList("1", R.drawable.1));
drawerListList.add(new DrawerList("2", R.drawable.2));
drawerListList.add(new DrawerList("3", R.drawable.3));
drawerListList.add(new DrawerList("4", R.drawable.4));
drawerListList.add(new DrawerList("5", R.drawable.5));
CustomDrawerAdapter adapter = new CustomDrawerAdapter(this, R.layout.list_view_menu, drawerListList);
mDrawerList.setAdapter(adapter);
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (id == 0) {
intent = new Intent(DrawerItemClickListener .this, FirstActivity.class);
startActivity(intent);
onDrawerClose();
} else if (id == 1) {
intent = new Intent(DrawerItemClickListener .this, SecondActivity.class);
startActivity(intent);
onDrawerClose();
} else if (id == 2) {
intent = new Intent(DrawerItemClickListener .this, ThirdActivity.class);
startActivity(intent);
onDrawerClose();
} else if (id == 3) {
intent = new Intent(DrawerItemClickListener .this, FouthActivity.class);
startActivity(intent);
onDrawerClose();
}
}
}
});
}
private void setupDrawer() {
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle("Navigation!");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getSupportActionBar().setTitle(mActivityTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
public void onDrawerClose() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
onBackPressed();
}
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
// Activate the navigation drawer toggle
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And Custom Adapter is below
public class CustomDrawerAdapter extends ArrayAdapter<DrawerList> {
private Context context;
private List<DrawerList> drawerItemList;
private int layoutResID;
public CustomDrawerAdapter(Context context, int layoutResourceID, List<DrawerList> listItems) {
super(context, layoutResourceID, listItems);
this.context = context;
this.drawerItemList = listItems;
this.layoutResID = layoutResourceID;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
DrawerItemHolder drawerHolder;
View view = convertView;
if (view == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
drawerHolder = new DrawerItemHolder();
view = inflater.inflate(layoutResID, parent, false);
drawerHolder.ItemName = (TextView) view.findViewById(R.id.drawer_itemName);
drawerHolder.icon = (ImageView) view.findViewById(R.id.drawer_icon);
view.setTag(drawerHolder);
} else {
drawerHolder = (DrawerItemHolder) view.getTag();
}
DrawerList dItem = this.drawerItemList.get(position);
drawerHolder.icon.setImageDrawable(view.getResources().getDrawable(dItem.getMenuImage()));
drawerHolder.ItemName.setText(dItem.getMenuName());
return view;
}
private static class DrawerItemHolder {
TextView ItemName;
ImageView icon;
}
}
And you extend it from each activity like
public class FirstActivity extends DrawerItemClickListener {
..
}
And also Listview Added in each xml layout.
Hope It will be helpful.

Combining ListActivity and ActionBarActivity

I am currently building for a minimum SDK of 10, so I have to use the android-support-v7-appcompat library to implement ActionBar. I have setup the ActionBar, but I want to now add a ListActivity, however this requires extending my class and Java doesn't have multiple inheritance. What should I do?
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
getSupportActionBar().setIcon(R.drawable.ic_action_search);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
}
ListActivity hasn't been ported to AppCompat. Probably because you should consider it 'deprecated', and instead use a ListFragment.
Fragments will work with a ActionBarActivity, just make sure they are fragments from the support library.
Have a read through this link about fragments.
For your use case, I would just define the fragment in xml.
The easiest way to do this is to use a ListFragment inside of the ActionBarActivity. I did it like this:
public class MyActivity extends ActionBarActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
MyFragment fragment = new MyFragment();
getSupportFragmentManager().beginTransaction().replace(android.R.id.content, fragment).commit();
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home: {
finish();
break;
}
default: {
break;
}
}
return true;
}
public static class MyFragment extends ListFragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
}
public void onListItemClick(ListView listView, View view, int position, long id) {
...
}
}
}
This way you don't even need an xml for it, and it works well.

How to work with SlidingMenu library

I am using the [SlidingMenu][1] Library in my App and have made a menu but I am not sure how to customize this.
First, the Up Navigation button on the action bar does not pull up the menu. It just does not do anything when clicked. The menu works by sliding in anywhere on the screen but not with that button.
Also, is there a way to change that up navigation '<' icon and make it work like Google + or others with the animation of the three horizontal lines?
My code is as follows:
public class BaseActivity extends SlidingFragmentActivity {
private int mTitleRes;
protected ListFragment mFrag;
public BaseActivity(int titleRes) {
mTitleRes = titleRes;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(mTitleRes);
// set the Behind View
setBehindContentView(R.layout.menu_frame);
if (savedInstanceState == null) {
FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
mFrag = new MenuListFragment();
t.replace(R.id.menu_frame, mFrag);
t.commit();
} else {
mFrag = (ListFragment)this.getSupportFragmentManager().findFragmentById(R.id.menu_frame);
}
// customize the SlidingMenu
SlidingMenu sm = getSlidingMenu();
sm.setShadowWidthRes(R.dimen.shadow_width);
sm.setShadowDrawable(R.drawable.shadow);
sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
sm.setFadeDegree(0.35f);
sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
And the main activity:
public class StatusActivity extends BaseActivity {
public StatusActivity() {
super(R.string.title_status_page);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_status);
setSlidingActionBarEnabled(false);
..
..
Try this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
getSlidingMenu().showContent();
}
return false;
}

ListView not displaying -- ListView Item Selection into webView

I am trying to make a ListView that lists all League of Legends Champions.
My current problem is that right now just the listview itself will not show on the screen and cannot figure out why.
Here is my MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
}
Here is my champListAcivity:
public class champListActivity extends ListActivity {
#Override
public void onCreate(Bundle fdsa) {
super.onCreate(fdsa);
ArrayAdapter<Champ> aa;
ArrayList<Champ> champList = new ArrayList<Champ>();
ListView listView = (ListView) findViewById(R.id.ChampListView);
// I SKIPPED THE CODE CONTAINING THE ADDING OF THE CHAMPIONS
// TO THE LIST BECAUSE ITS TOO LONG AND ALREADY RIGHT BECAUSE IT
// WORKED BEFORE AND I HAVENT CHANGED IT
int layoutID = android.R.layout.simple_list_item_1;
aa = new ArrayAdapter<Champ>(this, layoutID, champList);
listView.setAdapter(aa);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l,v,position,id);
Champ selChamp = (Champ) getListAdapter().getItem(position);
String champURL = selChamp.getLink();
Intent intent = new Intent(this,webViewActivity.class);
intent.putExtra("url", champURL);
startActivity(intent);
}
You're not setting your content view in the champListActivity:
public class champListActivity extends ListActivity {
#Override
public void onCreate(Bundle fdsa) {
super.onCreate(fdsa);
setContentView(R.layout.activity_your_champ_list_name); // <- missing!

Categories

Resources