How to hide the whole RecyclerView? - android

I am trying to hide the RecyclerView until the user passes some valid information. But it is not happening. Some weird things happen when I try this but it doesn't show any error.
Here's my code:
MainActivity.java
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private LinearLayout recyclerRow;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Paper.init(getApplicationContext());
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), (Toolbar) findViewById(R.id.app_bar));
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.goalList);
RVAdapter adapter = new RVAdapter(getApplicationContext(), getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
recyclerRow = (LinearLayout) findViewById(R.id.goal_row);
if(Paper.get("goalTitle") == null){
recyclerView.setVisibility(View.GONE);
recyclerRow.setVisibility(View.GONE);
}
}
public List<RVData> getData() {
Log.d("Check6", Paper.get("goalTitle") + "");
List<RVData> data = new ArrayList<>();
String[] titles = {(String) Paper.get("goalTitle")};
for (int i = 0; i < titles.length; i++) {
RVData current = new RVData();
current.goalTitle = titles[i];
data.add(current);
}
return data;
}
public void newGoal(View view) {
Intent intent = new Intent(MainActivity.this, NewGoal.class);
startActivity(intent);
}
#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_main, menu);
return true;
}
#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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I am using Paper for my database.

Instead of setVisibility, You can use setAlpha(0) to hide and setAlpha(1) to show. This is what I ended up doing. It is quite working well. It will be the same effect as if you use visibility Invisible.

Related

Getting error in setGridViewItemClickListener and also how can show images from drawable

I have created android app using GridView with ViewPager for images like gallery or small book which have 14 images
am showing all image from URL. i want also help that how can show my image from drawable
GridView got open successfully and i am able to launch the application.
Functionality is also working fine but when I want to open single image from GridView then my app close and showing message that app has stopped and open app again.
Please review my code and guide me how can I solve this problem. Here is my code:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private GridView gridView;
private GridViewAdapter gridViewAdapter;
private ArrayList<String> listImageURLs = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addImageURLs();
gridView = findViewById(R.id.gridView);
gridViewAdapter = new GridViewAdapter(this, listImageURLs);
gridView.setAdapter(gridViewAdapter);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
setGridViewItemClickListener();
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#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;
}
#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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private void setGridViewItemClickListener(){
gridView.setOnItemClickListener((parent, view, position, id) -> {
Bundle bundle = new Bundle();
bundle.putInt("position", position);
bundle.putStringArrayList("imageURLs", listImageURLs);
Intent intent = new Intent(this, ImageActivity.class);
intent.putExtras(bundle);
startActivity(intent);
});
}
private void addImageURLs(){
listImageURLs.add("http://i.dailymail.co.uk/i/pix/2016/04/12/23/3319F89C00000578-3536787-image-m-19_1460498410943.jpg");
listImageURLs.add("https://www.w3schools.com/css/img_forest.jpg");
listImageURLs.add("https://www.w3schools.com/css/trolltunga.jpg");
listImageURLs.add("https://www.w3schools.com/css/pineapple.jpg");
listImageURLs.add("https://cdn.arstechnica.net/wp-content/uploads/2016/02/5718897981_10faa45ac3_b-640x624.jpg");
listImageURLs.add("https://www.w3schools.com/css/paris.jpg");
listImageURLs.add("https://www.w3schools.com/css/paris.jpg");
listImageURLs.add("https://www.w3schools.com/css/trolltunga.jpg");
listImageURLs.add("https://www.w3schools.com/css/lights600x400.jpg");
listImageURLs.add("http://wallpaper-gallery.net/images/image/image-11.jpg");
listImageURLs.add("http://wallpaper-gallery.net/images/image/image-15.jpg");
listImageURLs.add("http://wallpaper-gallery.net/images/image/image-19.jpg");
listImageURLs.add("https://cdn.spacetelescope.org/archives/images/thumb700x/heic1509a.jpg");
listImageURLs.add("http://wallpaper-gallery.net/images/image/image-12.jpg");
//listImageURLs.addAll(listImageURLs);
}
}
You need to include the ImageActivity in the AndroidManifest.
I have done a similar project with you and I use this way:
private ArrayList<String> urlImage = new ArrayList<>();
Remember, add data to urlImage
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getApplicationContext(), ShowImageActivity.class);
intent.putExtra(KEY_URL_IMAGE, urlImage);
intent.putExtra(KEY_POSITION, String.valueOf(position));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
In ShowImageActivity:
Intent intent = getIntent();
if (intent.getExtras() != null) {
url = (ArrayList<String>) intent.getExtras().get(KEY_URL_IMAGE);
position = Integer.parseInt(String.valueOf(intent.getExtras().get(KEY_POSITION)));
Toast.makeText(getApplicationContext(), "URL image" + url.get(position) + "position " + position, Toast.LENGTH_LONG).show();
}
final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
viewPager.setAdapter(new PagerAdapter(getSupportFragmentManager(), url));
final ViewPager.LayoutParams layoutParams = new ViewPager.LayoutParams();
layoutParams.width = ViewPager.LayoutParams.MATCH_PARENT;
layoutParams.height = ViewPager.LayoutParams.WRAP_CONTENT;
layoutParams.gravity = Gravity.BOTTOM;
viewPager.setCurrentItem(position);
and show image from drawable
imageView.setImageResource(getImageId(view.getContext(),resourceImage));
public static int getImageId(Context context, String imageName) {
return context.getResources().getIdentifier("drawable/" + imageName, null, context.getPackageName());
}
You are passing OnItemClickListener in the place of context. You need to use the instance of the class like below.
Intent intent = new Intent(MainActivity.this, ImageActivity.class);
Note: If you use this, then it would have the instance of the class it is written in. In your code, you are using this inside an anonymous class of OnItemClickListener
Also, you need to check if you have defined your activity in the manifest file.

I can't navigate to another activity

Android Error I'm a real noob when it comes to Android Studio, and I'm trying to make it so when I click a button in IntroActivity, it will direct me to the FeaturedActivity page, which contains a list and navigation drawer, if that's important. But when I click the first time in the emulator, the app stops, and when I go back and click it again, nothing happens. I have Java Code for both activities. Could someone help me please?
<activity
android:name=".FeaturedActivity"
android:label="#string/title_activity_featured"
android:theme="#style/AppTheme.NoActionBar"></activity>
The first activity
public class IntroActivity extends AppCompatActivity {
Button ComeInButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intro);
ComeInButton = (Button) findViewById(R.id.ComeIn);
ComeInButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(IntroActivity.this, FeaturedActivity.class);
startActivity(i);
}
});
}
}
Featured Activity
public class FeaturedActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
String [] art_Names;
TypedArray pics;
String[] artist_Names;
String[] desc;
List<RowItem> rowItems;
ListView myListView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_featured);
rowItems=new ArrayList<RowItem>();
art_Names=getResources().getStringArray(R.array.Featured_Arts);
pics=getResources().obtainTypedArray(R.array.Art_Pics);
artist_Names=getResources().getStringArray(R.array.Artist_Names);
desc=getResources().getStringArray(R.array.Descriptions);
for(int i=0;i<art_Names.length;i++){
RowItem item=new RowItem(art_Names[i], pics.getResourceId(i, -1),artist_Names[i],desc[i]);
rowItems.add(item);
}
myListView=(ListView)findViewById(R.id.list);
CustomerAdapter adapter = new CustomerAdapter(this,rowItems);
myListView.setAdapter(adapter);
myListView.setOnClickListener((View.OnClickListener) this);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
String art_name=rowItems.get(position).GetArtName();
Toast.makeText(getApplicationContext(),""+art_name,Toast.LENGTH_SHORT).show();
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.featured, menu);
return true;
}
#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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
The question is a little vague on the details, but taking a shot in the dark, did you register FeaturedActivity in the app manifest?
Something like:
<activity
android:name=".FeaturedActivity"
android:label="#string/title_activity_featured"
android:theme="#style/AppTheme">
should be in your AndroidManifest.xml file.
You are probably getting a nice error message in logcat, so please look there :)
I think the crash cause is where you are casting FeaturedActivity to View.OnClickListener inside FeatureActivity#onCreate(). since it does not implement View.OnClickListener, you get ClassCastException and since you do not catch this exception, then your app crashes.
EDIT I meant this line:
public class FeaturedActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
...
myListView.setOnClickListener((View.OnClickListener) this); // this line
}
...
}
EDIT 2 You can fix it by implementing View.OnClickListener in FeatureActivity like this:
public class FeaturedActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener, View.OnClickListener {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
...
myListView.setOnClickListener(this); // cast is not needed anymore
}
#Override
protected void onClick(View v) {
// handle click
}
...
}
By the way, If you want to handle item click for ListView you should use AdapterView#setOnItemClickListener() instead so you get notified when one of the items in ListView is clicked

How can I limit Google Places Android API to return only gyms?

When using the Google Places Android API, the API normally gets the user's location and returns a list of nearby places. How can I limit this list to only gyms? While the solution for web is readily available, I can't seem to find how to do it for Android.
This is my current code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
int PLACE_PICKER_REQUEST = 1;
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
try {
startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
ArrayList<Integer> types = new ArrayList<>();
types.add(Places.TYPE_GYM);
for(PlaceLikelihood likelihood : result) {
if(hasMatchingType(result.getPlace(), types)) {
//This is a gym, do whatever you need to here
}
}
}
private boolean hasMatchingType (Place place, List < Integer > allowedTypes){
List<Integer> types = place.getPlaceTypes();
for (int type : types) {
if (allowedTypes.contains(type)) {
return true;
}
}
return false;
}
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
#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_main, menu);
return true;
}
#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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

Extend Navigation Bar into another activity (no fragments)

I read some question regarding this but all the answers are about fragments and there is question similar to this one but the answer is incomplete, I want to reuse a set of layout or codes into multiple activities, i created a baseActivity that extends into Activity with the code below.
I also read that you need to put the code in the onCreateOptionMenu but it is still not working. (the code in baseacitivty xml is working, and homepage xml is working but does not show the navigation_layout)
public class BaseActivity extends Activity {
private ImageButton ibButtonHome;
private ImageButton ibButtonFavorite;
private ImageButton ibButtonRandomize;
private ImageButton ibButtonHistory;
private ImageButton ibButtonLogOut;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.navigation_layout);
}
View.OnClickListener Navigation = new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent();
if (v.equals(ibButtonHome)) {
i.setClass(getBaseContext(), HomePage.class);
} else if (v.equals(ibButtonFavorite)) {
i.setClass(getBaseContext(), Favorite.class);
} else if (v.equals(ibButtonHome)) {
i.setClass(getBaseContext(), HomePage.class);
} else if (v.equals(ibButtonRandomize)) {
i.setClass(getBaseContext(), Randomize.class);
} else if (v.equals(ibButtonHistory)) {
i.setClass(getBaseContext(), History.class);
} else if (v.equals(ibButtonLogOut)) {
//TODO: something code here to not crash on activity exit??
i.setClass(getBaseContext(), MainActivity.class);
}
startActivity(i);
}
};
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
super.onCreateOptionsMenu(menu);
ibButtonHome = (ImageButton) findViewById(R.id.button_Home);
ibButtonFavorite = (ImageButton) findViewById(R.id.button_favorites);
ibButtonRandomize = (ImageButton) findViewById(R.id.button_randomize);
ibButtonHistory = (ImageButton) findViewById(R.id.button_history);
ibButtonLogOut = (ImageButton) findViewById(R.id.button_logout);
ibButtonFavorite.setOnClickListener(Navigation);
ibButtonRandomize.setOnClickListener(Navigation);
ibButtonHome.setOnClickListener(Navigation);
ibButtonHistory.setOnClickListener(Navigation);
ibButtonLogOut.setOnClickListener(Navigation);
getMenuInflater().inflate(R.menu.menu_filter_menus, menu);
return true;
}
#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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
Homepage activity
public class HomePage extends BaseActivity {
private CustomAdpaterFoodFeed ExpAdapter;
private ArrayList<FoodFeed> foodFeeds;
private ExpandableListView ExpandList;
//Onclick listener for the Navigation Bar
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
ExpandList = (ExpandableListView) findViewById(R.id.evFoodFeed);
//runs the function and returns the data to foodFeeds
foodFeeds = SetStandardGroups();
//Adapter for ExapadableListView
ExpAdapter = new CustomAdpaterFoodFeed(HomePage.this, foodFeeds);
ExpandList.setAdapter(ExpAdapter);
}
// Dummy data method for pictures and comments
public ArrayList<FoodFeed> SetStandardGroups() {
String names[] = {"Geraldine", "Marielle", "Gina", "Bryan",
"Pat", "Eugene", "Shermaine", "Kook"};
String comments[] = {"TasteGood", "Nah", "DONT EAT HERE", "Cameroon",
"Nice place", "chill", "woah Spain", "lalala"};
int Images[] = {R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher
};
ArrayList<FoodFeed> list = new ArrayList<FoodFeed>();
ArrayList<Comments> comments_list;
for (int images : Images) {
FoodFeed gru = new FoodFeed();
gru.setIcon(images);
comments_list = new ArrayList<Comments>();
for (int j = 0; j < 4; j++) {
Comments comments1 = new Comments();
comments1.setName(names[j]);
comments1.setComments(comments[j]);
comments_list.add(comments1);
}
gru.setComments(comments_list);
list.add(gru);
}
return list;
}
#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_home_page, menu);
return true;
}
#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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Base activity to extend the navigation drawer in other activities you can follow this link , described well. it is well tested i followed the same :)
http://androiddeveloperdemo.blogspot.in/2014/08/android-navigation-drawer-with-multiple.html
You can get the same action bar in other activities by declaring in AndroidManifest.xml like this
<activity
android:name=".SettingsActivity"
android:label="#string/activity_title"
android:theme="#style/AppTheme" />
For different menu options define a xml file under menu folder in android studio and inflate that file in
onCreateOptionsMenu(Menu) overridden method of your activty

How to make click on ActionBar Logo in android

I tried to do something on Click on icon in ActionBar but do nothing.I also like to do open drawer_layout on click on a icon in action bar. How to make it clickable and handle the event of it`s pressing?
public class MainActivity extends AppCompatActivity {
ViewPager viewPager,viewPager01;
CustomSwipAdapter swip_adapter;
CustomSwipAdapter01 swip_adapter01;
String[] menu;
DrawerLayout dLayout;
ListView dList;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try{
getSupportActionBar().setLogo(R.drawable.sample_01);
getSupportActionBar().setDisplayUseLogoEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setTitle("");
}
catch (Exception e) {
}
viewPager = (ViewPager)findViewById(R.id.viewpager);
swip_adapter =new CustomSwipAdapter(this);
viewPager.setAdapter(swip_adapter);
/* viewPager01 = (ViewPager)findViewById(R.id.viewpager01);
swip_adapter01 =new CustomSwipAdapter01(this);
viewPager01.setAdapter(swip_adapter01);*/
menu = new String[]{"Home","E-Gift Voucher"};
dLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
dList = (ListView)findViewById(R.id.left_drawer);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,menu);
dList.setAdapter(adapter);
dList.setSelector(android.R.color.holo_blue_dark);
dList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
dLayout.closeDrawers();
Bundle args = new Bundle();
args.putString("Menu", menu[position]);
Fragment detail = new DetailFragment();
detail.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, detail).commit();
}
});
}
#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_main, menu);
MenuInflater inf = getMenuInflater();
inf.inflate(R.menu.main_activity_action,menu);
return super.onCreateOptionsMenu(menu);*/
MenuInflater inf = getMenuInflater();
inf.inflate(R.menu.main_activity_action,menu);
return true;//super.onCreateOptionsMenu(menu);
}
#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();
switch (item.getItemId()){
case android.R.id.home:
Toast.makeText(MainActivity.this,"Click on Logo",Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
layout xml
<RelativeLayout
android:id="#+id/rl_main_search_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/transparent_semi"
tools:context="com.worldofmoms.views.fragments.search_and_explore.SearchFragment">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:theme="#style/ActionBar_Light"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/toolbar_search"
android:background="#android:color/white"
android:visibility="gone"
/>
</RelativeLayout>
Add in your activity onCreate()
mToolbar = (Toolbar) view.findViewById(R.id.toolbar_search);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
and Add
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id==android.R.id.home){
// home button from toolbar clicked
}
}
Add in your activity onCreate(): i am quite sure it may help you...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Custom code for image icon which redirects to homepage
ActionBar actionBar=getSupportActionBar();
actionBar.setDisplayOptions(actionBar.getDisplayOptions()
| ActionBar.DISPLAY_SHOW_CUSTOM);
ImageView imageView = new ImageView(actionBar.getThemedContext());
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setImageResource(R.mipmap.ic_launcher);
imageView.setOnClickListener(new OnClickListener() {
#Override public void onClick(View v) {
webViewPlaceholder.loadUrl("http://www.salebhai.com");
} });
ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(
ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.WRAP_CONTENT, Gravity.LEFT
| Gravity.CENTER_VERTICAL);
layoutParams.leftMargin = 20;
imageView.setLayoutParams(layoutParams);
actionBar.setCustomView(imageView);
actionBar.setDisplayHomeAsUpEnabled(true);
Thank you ,
happy Coding.....

Categories

Resources