How to put images on ListView buttons? I found some tutorials but don't know how to implement in my xml that I have already. I still working only with xml files and didn't touch anything in the code of the generated MainActivity.java.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.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;
}
#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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
This is the current 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"
android:weightSum="1" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5" >
<ImageView
android:contentDescription="#string/food"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:scaleType="centerCrop"
android:src="#drawable/food" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="35dp"
android:padding="4dp"
android:layout_marginTop="20dp"
android:gravity="left"
android:background="#drawable/bg_button"
android:text="#string/Menu1" />
<!-- android:background="#c5e1b0" -->
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="35dp"
android:padding="4dp"
android:layout_marginTop="5dp"
android:gravity="left"
android:background="#drawable/bg_button"
android:text="#string/Menu2" />
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="35dp"
android:padding="4dp"
android:layout_marginTop="5dp"
android:gravity="left"
android:background="#drawable/bg_button"
android:text="#string/Menu3" />
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="35dp"
android:padding="4dp"
android:layout_marginTop="5dp"
android:gravity="left"
android:background="#drawable/bg_button"
android:text="#string/Menu4" />
</LinearLayout>
</LinearLayout>
So I want to add images on the left side of this 4 buttons.
Anyone can help with this task?
Update:
Currently I have this structure on the left. Half of the screen (upper) is image. Second half I have 4 buttons. I want to put image on each button on the left side like is in the right picture with where is red aquare.
Here is how you can have image and Text both on same button
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Testing" android:drawableLeft="#drawable/logo"/>
Related
I had toolbar with options in activity_one toolbar and everything worked well it was created and also clickable. All buttons were working good. But after I moved it to another activity it doesn't work. I am talking about this button
I have checked another topics with the same questions but at that topics there were mistakes in code. But I can't find any mistakes in my code. Because it was working well in another activity. Maybe I do something wrong but I can't solve this problem with that information which I have.
The code of XML
<RelativeLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.borisruzanov.social.ui.ChatActivity">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:id="#+id/toolbar"
android:background="?attr/colorPrimary"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.v7.widget.Toolbar>
<ListView
android:id="#+id/messageListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/linearLayout"
android:stackFromBottom="true"
android:divider="#android:color/transparent"
android:transcriptMode="alwaysScroll"
tools:listitem="#layout/item_message"/>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:orientation="horizontal">
<ImageButton
android:id="#+id/photoPickerButton"
android:layout_width="36dp"
android:layout_height="36dp"
android:background="#android:drawable/ic_menu_gallery" />
<EditText
android:id="#+id/messageEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1" />
<Button
android:id="#+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:enabled="false"
android:text="#string/send_button_label"/>
</LinearLayout>
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
The code of class
Toolbar toolbar;
in OnCreate
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
And two methods
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
Log.v("========>", "In oncreateMenu");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.v("========>", "In item selected");
int id = item.getItemId();
if (id == R.id.menu_log_out){
mAuth.signOut();
}else if (id == R.id.menu_chat){
Intent intent = new Intent(this, ChatActivity.class);
startActivity(intent);
}
return true;
Little confused on how to show an actionbar on my second activity. Hopefully I didn't provide too much. Basically to clarify, the actionbar works perfect on my MainActivity (title shows and settings button), but I can only see the action bar on my other activities (NO title or buttons) I also took a look in menu_main.xml and noticed the context was set to my MainActivity, so I made menu_attractions.xml and changed the context to the proper activity, but that still didn't show the title or settings button. any help would be greatly appreciated!
Nothing fancy for my MainActivity (this is where I assume the magic happens for the title/buttons??)
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<include layout="#layout/content_main" />
<include layout="#layout/app_bar"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
And for the MainActivity class, I search for the toolbar below... and inflate the menu
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
attractionButton = (Button) findViewById(R.id.activityButton);
}
#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;
}
Here's my actionbar
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.v7.widget.Toolbar>
Now... here's my second activity
public class AttractionsActivity extends AppCompatActivity {
private Button scheduleButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.d_activities);
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
scheduleButton = (Button) findViewById(R.id.activies_Schedule);
scheduleButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View view){
Intent scheduleIntent = new Intent(AttractionsActivity.this, ScheduleActivity.class);
startActivity(scheduleIntent);
}
});
}
#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_attractions, 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);
}
here's my layout for my second activity activity
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="#drawable/img_252413"
tools:context="AttractionsActivity"
tools:showIn="#layout/d_activities">
<include layout="#layout/app_bar"
android:id="#+id/include" />
<LinearLayout
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/include">
<FrameLayout
android:background="#ffffff"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginTop="-5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_marginTop="-5dp"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginBottom="-5dp"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Schedule"
android:id="#+id/activies_Schedule" />
</FrameLayout>
<Space
android:layout_width="0dp"
android:layout_height="10dp" />
<FrameLayout
android:background="#ffffff"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginTop="-5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_marginTop="-5dp"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginBottom="-5dp"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Maps"/>
</FrameLayout>
<Space
android:layout_width="0dp"
android:layout_height="10dp" />
<FrameLayout
android:background="#ffffff"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginTop="-5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_marginTop="-5dp"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginBottom="-5dp"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Performers"/>
</FrameLayout>
<Space
android:layout_width="0dp"
android:layout_height="10dp" />
<FrameLayout
android:background="#ffffff"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginTop="-5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_marginTop="-5dp"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginBottom="-5dp"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Sponsors"/>
</FrameLayout>
<Space
android:layout_width="0dp"
android:layout_height="10dp" />
<FrameLayout
android:background="#ffffff"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginTop="-5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_marginTop="-5dp"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginBottom="-5dp"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Statics"/>
</FrameLayout>
<Space
android:layout_width="0dp"
android:layout_height="10dp" />
<FrameLayout
android:background="#ffffff"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginTop="-5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_marginTop="-5dp"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginBottom="-5dp"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Exibitors"/>
</FrameLayout>
<Space
android:layout_width="0dp"
android:layout_height="10dp" />
<FrameLayout
android:background="#ffffff"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginTop="-5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_marginTop="-5dp"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginBottom="-5dp"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Food"/>
</FrameLayout>
</LinearLayout>
</RelativeLayout>
It seems You Have Changed the Theme for Your Second Activity.Try the following
Step 1: Add this in Your Styles.xml
<style name="AppTheme.AttractionsActivity" parent="Theme.AppCompat.Light.DarkActionBar">
Step 2: Add this in Manifest.Xml
<activity
android:name=".AttractionsActivity"
android:theme="#style/AppTheme.AttractionsActivity"
/>
I am developing an android app and it is static now. To make it dynamic, i am thinking to fetch images from web server or from google drive. I have few images with me. How can i fetch images from web server or google drive? Here is my code given below -
Bed.java
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class Bed extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bed);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
#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_bed, 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 == android.R.id.home) {
this.finish();
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
}
activity_bed.xml
(currently, i have given 5 ImageViews with corresponding 5 images and currently, it is static. I want to fetch these 5 images from web server/google drive).
<LinearLayout 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:orientation="vertical"
android:paddingLeft="10dp"
android:background="#00ffaa"
tools:context="com.example.rahulshaw.medizy.Bed">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_marginTop="5dp"
android:paddingRight="10dp"
android:layout_gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hospital 1,Marathahalli"
android:id="#+id/hosp1"
android:gravity="center|bottom"
android:textStyle="bold"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#ffffff"
android:background="#drawable/picture1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hospital 2,Domlur"
android:id="#+id/hosp2"
android:layout_marginTop="4dp"
android:gravity="center|bottom"
android:textStyle="bold"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#ffffff"
android:background="#drawable/picture2"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hospital 3,Kundanahalli"
android:id="#+id/hosp3"
android:layout_marginTop="4dp"
android:gravity="center|bottom"
android:textStyle="bold"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#ffffff"
android:background="#drawable/picture3"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hospital 4,Indiranagar"
android:id="#+id/hosp4"
android:layout_marginTop="4dp"
android:gravity="center|bottom"
android:textStyle="bold"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#ffffff"
android:background="#drawable/picture1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hospital 5,Koramangala"
android:id="#+id/hosp5"
android:layout_marginTop="4dp"
android:gravity="center|bottom"
android:textStyle="bold"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#ffffff"
android:background="#drawable/picture5"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
Please help me...
Picasso is by far the easiest way to fetch images. It will handle threading for you http://square.github.io/picasso/
I have an activity that displays a drawer layout when a menu item is clicked thus its direction is from right to left. I manage to display the navigation menu using the following code.
public class Forecast_details extends ActionBarActivity implements OnChartValueSelectedListener {
....
#Override
public boolean onOptionsItemSelected(MenuItem item) {
//return false;
// 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_settings) {
return true;
}
if(id==R.id.Options){
if (mDrawerlayout.isDrawerOpen(Gravity.END)){
mDrawerlayout.closeDrawer(Gravity.END);
}else
mDrawerlayout.openDrawer(Gravity.END);
}
return super.onOptionsItemSelected(item);
}
My Navigation drawer contains a static list so I did not bother to make a listview out of it as seen below,
However I cannot click my items inside the Drawer Layout, or anything in the view even v4.widget.DrawerLayout in my xml is unclickable. Below is the two xml file that I used.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar"
></include>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout_right"
android:layout_gravity="end"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true">
<include layout="#layout/nav_drawer_right" />
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2.36" >
....
</LinearLayout>
</ScrollView>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
nav_drawer_right.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView_right"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#2c3e50"
android:paddingTop="20dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/container_fragment2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginTop="1dp" >
</FrameLayout>
<LinearLayout
android:id="#+id/linearLayout_menuconatiner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:clickable="true"
android:orientation="vertical"
android:paddingLeft="2dp"
android:paddingRight="2dp" >
<ImageView
android:id="#+id/imageView_forward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/darkblue"
android:clickable="true"
android:onClick="SendClick"
android:padding="5dp"
android:scaleType="fitCenter"
android:src="#drawable/ic_forward" />
<View
android:layout_width="fill_parent"
android:layout_height="3dip"
android:background="#color/separatorcolor"
android:paddingLeft="2dp"
android:paddingRight="2dp" />
<ImageView
android:id="#+id/imageView_request"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/darkblue"
android:padding="5dp"
android:src="#drawable/ic_request" />
<View
android:layout_width="fill_parent"
android:layout_height="3dip"
android:background="#color/separatorcolor"
android:paddingLeft="2dp"
android:paddingRight="2dp" />
<ImageView
android:id="#+id/imageView_reloadvnet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/darkblue"
android:padding="5dp"
android:src="#drawable/ic_updatevnet" />
<View
android:layout_width="fill_parent"
android:layout_height="3dip"
android:background="#color/separatorcolor"
android:paddingLeft="2dp"
android:paddingRight="2dp" />
<ImageView
android:id="#+id/imageView_reloadvsms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/darkblue"
android:padding="5dp"
android:src="#drawable/ic_updatevsms" />
<View
android:layout_width="fill_parent"
android:layout_height="3dip"
android:background="#color/separatorcolor"
android:paddingLeft="2dp"
android:paddingRight="2dp" />
<ImageView
android:id="#+id/imageView_feedback"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/darkblue"
android:padding="5dp"
android:src="#drawable/ic_feedback" />
<View
android:layout_width="fill_parent"
android:layout_height="3dip"
android:background="#color/separatorcolor"
android:paddingLeft="2dp"
android:paddingRight="2dp" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
Please help.. I tried a lot of things to make it work like using a listview instead but I still have no luck.. please do help, Im new at using drawer layout so there may be some more things I need to know.. Thank you in advance..
To prevent return OnClickListener to parent layout require set android:clickable="true" in drawerLayout container.
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout_right"
android:layout_gravity="end"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2.36" >
....
</ScrollView>
<include layout="#layout/nav_drawer_right" />
</android.support.v4.widget.DrawerLayout>
nav_drawer_right.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView_right"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#2c3e50"
android:paddingTop="20dp" >
....
</ScrollView>
in your Activity
public class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DrawerLayout mDrawerLayout= (DrawerLayout) findViewById(R.id.drawer_layout_right);
ScrollView sV = (ScrollView) findViewById(R.id.scrollView1);
mDrawerLayout.openDrawer(Gravity.RIGHT);
sV.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("drawer click >>>>>>>>>>>>>>>>>>");
}
});
}
}
I have a grid view , it's item is formed by an icon and tow textview. the code of my item is given below. I would like to add a context menu for each item, like this used in google playstore as mentioned by the image link for the image
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/round_boutton"
android:orientation="vertical"
android:padding="5dp">
<ImageView
android:layout_height="96dp"
android:id="#+id/imageView1"
android:layout_width="96dp"
android:src="#drawable/icon"
android:layout_marginTop="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
</ImageView>
<LinearLayout
android:id="#+id/l1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView1"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:paddingTop="4dp"
android:singleLine="true"
android:text="TextView1"
android:textColor="#color/orangemill"
android:layout_weight="1"
android:textSize="14dip" >
</TextView>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textColor="#color/white"
android:maxLines="2"
android:layout_weight="1"
android:text="TextView2" >
</TextView>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_below="#+id/l1"
android:background="#android:color/transparent" />
</RelativeLayout>
create click listener for the menu button/image on the grid item adapter class and add the code to create,show popup menu.
Then implement MenuItem click listener for the activity.
holder.menu_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PopupMenu popupMenu = new PopupMenu(Activity.this, view);
popupMenu.setOnMenuItemClickListener(Activity.this);
popupMenu.setGravity(Gravity.END);
popupMenu.inflate(R.menu.menu_item);
popupMenu.show();
}
});
Then implement MenuItem Selection
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_edit:
//your code
break;
case R.id.action_delete:
//your code
return true;
}
return true;
}
Add implements PopupMenu.OnMenuItemClickListener on the Activity.
Try this...