Instantly share code, notes, and snippets.
Created
May 23, 2020 12:41
-
Star
1
(1)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save BetelGeuseee/bbd9c877dc0203200927d590e871d0bd to your computer and use it in GitHub Desktop.
Navigation Drawer in android using Base Adapter list view
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.fragmenttwo; | |
import androidx.annotation.NonNull; | |
import androidx.annotation.Nullable; | |
import androidx.annotation.RequiresApi; | |
import androidx.appcompat.app.ActionBar; | |
import androidx.appcompat.app.ActionBarDrawerToggle; | |
import androidx.appcompat.app.AppCompatActivity; | |
import androidx.drawerlayout.widget.DrawerLayout; | |
import android.content.Context; | |
import android.content.res.Configuration; | |
import android.content.res.Resources; | |
import android.os.Build; | |
import android.os.Bundle; | |
import android.os.PersistableBundle; | |
import android.view.LayoutInflater; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.AdapterView; | |
import android.widget.ArrayAdapter; | |
import android.widget.BaseAdapter; | |
import android.widget.ImageView; | |
import android.widget.ListView; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import android.widget.Toolbar; | |
/* @Override | |
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | |
selectItem(position); | |
} | |
public void selectItem(int position) { | |
listView.setItemChecked(position,true); | |
//making sure that particular item is checked | |
setTitle(position); | |
} | |
public void setTitle(String title){ | |
getSupportActionBar().setTitle(title); | |
}*/ | |
public class MainActivity extends AppCompatActivity { | |
DrawerLayout drawerLayout; | |
ListView listView; | |
ActionBarDrawerToggle drawerListener; | |
String[] planetTemp; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
drawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout); | |
listView = (ListView)findViewById(R.id.listView); | |
Resources res = getResources(); | |
planetTemp=res.getStringArray(R.array.planets); | |
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | |
getSupportActionBar().setTitle(planetTemp[position]); | |
} | |
}); | |
listView.setAdapter(new MyAdapter(this)); | |
drawerListener = new ActionBarDrawerToggle(this,drawerLayout,R.string.drawer_open,R.string.drawer_close){ | |
@Override | |
public void onDrawerClosed(View drawerView) { | |
Toast.makeText(MainActivity.this,"Drawer Closed",Toast.LENGTH_SHORT).show(); | |
} | |
@Override | |
public void onDrawerOpened(View drawerView) { | |
Toast.makeText(MainActivity.this,"Drawer Opened",Toast.LENGTH_SHORT).show(); | |
} | |
}; | |
drawerLayout.addDrawerListener(drawerListener); | |
getSupportActionBar().setHomeButtonEnabled(true); | |
getSupportActionBar().setDisplayHomeAsUpEnabled(true); | |
} | |
@Override | |
public void onConfigurationChanged(@NonNull Configuration newConfig) { | |
super.onConfigurationChanged(newConfig); | |
drawerListener.onConfigurationChanged(newConfig); | |
} | |
@Override | |
public boolean onOptionsItemSelected(@NonNull MenuItem item) { | |
if(drawerListener.onOptionsItemSelected(item)) | |
return true; | |
return super.onOptionsItemSelected(item); | |
} | |
@Override | |
protected void onPostCreate(@Nullable Bundle savedInstanceState) { | |
super.onPostCreate(savedInstanceState); | |
drawerListener.syncState(); | |
} | |
public void titleSetting(String n){ | |
getSupportActionBar().setTitle(n); | |
} | |
} | |
class MyAdapter extends BaseAdapter { | |
MainActivity activity; | |
Context context; | |
String[] planets; | |
int[] images={R.drawable.pic1,R.drawable.pic2,R.drawable.pic3,R.drawable.pic4,R.drawable.pic5,R.drawable.pic1,R.drawable.pic2,R.drawable.pic3}; | |
MyAdapter(Context c){ | |
this.context=c; | |
Resources res= context.getResources(); | |
planets=res.getStringArray(R.array.planets); | |
} | |
@Override | |
public int getCount() { | |
return planets.length; | |
} | |
@Override | |
public Object getItem(int position) { | |
return planets[position]; | |
} | |
@Override | |
public long getItemId(int position) { | |
return position; | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
View row = null; | |
if(convertView==null){ | |
LayoutInflater inflater =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
row=inflater.inflate(R.layout.custom_row,parent,false); | |
} | |
else{ | |
row=convertView; | |
} | |
TextView titleTextView = (TextView)row.findViewById(R.id.textView); | |
ImageView imageView =(ImageView)row.findViewById(R.id.imageView); | |
titleTextView.setText(planets[position]); | |
imageView.setImageResource(images[position]); | |
return row; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment