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/
Related
Hello everyone and sorry if this question is answered.
I'm doing a geography game and I want to know how to change my ImageView depending on the button I press.
For example, I have an Europe map with an arrow that points to Spain. I want that when I press the Spain button, the map changes into a map with an arrow that points to Portugal.
I've tried this code but I can't make it work:
public void cargar_por(View v){
ImageView iv = (ImageView)findViewById(R.id.imageView);
if(flag==false){
Drawable por=setFeatureDrawable(iv,por);
iv.setImageDrawable(R.drawable.por);
flag = true;
}
else{
flag=false;
}
}
How could I do it?
Thanks in advance.
EDIT: I'm going to add the full code.
The xml file looks like this:
<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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:weightSum="1">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="0.91"
android:weightSum="1"
android:focusableInTouchMode="true"
android:focusable="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<Chronometer
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/chronometer"
android:layout_gravity="right"
android:enabled="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Nivel 1"
android:id="#+id/textView5"
android:layout_gravity="center_horizontal"
android:background="#ffffffff" />
</LinearLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/map"
android:src="#drawable/spa"
android:visibility="visible"
android:layout_weight="0.93" />
</TableLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="131dp"
android:layout_gravity="center_horizontal"
android:background="#3f073dff">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/layout1">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:weightSum="1">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:weightSum="1">
<Button
android:layout_width="181dp"
android:layout_height="match_parent"
android:text="ESPAÑA"
android:id="#+id/button5"
android:onClick="cargar_por" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="PORTUGAL"
android:id="#+id/button6" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:weightSum="1">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.40"
android:weightSum="1">
<Button
android:layout_width="match_parent"
android:layout_height="58dp"
android:text="FRANCIA"
android:id="#+id/button7"
android:layout_weight="0.86" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.40">
<Button
android:layout_width="match_parent"
android:layout_height="59dp"
android:text="ITALIA"
android:id="#+id/button8" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
And the Java code like this:
package marti.victor.geografia;
import android.graphics.drawable.Drawable;
import android.os.SystemClock;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.*;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.ImageButton;
import android.widget.ImageView;
public class Juego extends ActionBarActivity {
// boolean flag = false;
ImageView image = (ImageView) findViewById(R.id.map);
Button but;
int a = 0;
Chronometer myChronometer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.juego);
myChronometer = (Chronometer)findViewById(R.id.chronometer);
myChronometer.start();
}
#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_activity2, 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);
}
/*public void cargar_por(View v){
ImageView iv = (ImageView)findViewById(R.id.imageView);
but=(Button)findViewById(R.id.button5);
but.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
iv.setImageDrawable(R.drawable.por);
}
});
}*/
}
try this
ImageView iv = (ImageView)findViewById(R.id.imageView);
spainbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
iv.setImageDrawable(R.drawable.por);
}
});
in your case I think the flag is not working properly.
I am basically trying to create a recycler view that has a linear layout manager with a horizontal scrolling. When I was using a linear layout manager with vertical scrolling, the design of the recycler view rows was properly aligned as per provided in the layout file but the design elements gets placed in completely weird places when I am using horizontal scrolling
Here is my code for the recycler view row :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:orientation="vertical"
card_view:cardCornerRadius="2dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="250dp">
<ImageView
android:id="#+id/shop_product_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView
android:id="#+id/favorite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="12dp"
android:padding="10dp"
android:src="#drawable/ic_favorite_selected" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#color/black80"
android:padding="8dp"
android:text="1200 INR"
android:textColor="#color/white"
android:textSize="18sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/black80">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/shop_product_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingTop="4dp"
android:paddingBottom="2dp"
android:textColor="#color/white"
android:textSize="16sp" />
<TextView
android:id="#+id/shop_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingBottom="4dp"
android:text="The Indian Shop"
android:textColor="#color/white"
android:textSize="12sp" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="150 likes"
android:textColor="#color/white"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:paddingRight="8dp"/>
</RelativeLayout>
</RelativeLayout>
The layout file for the activity layout is :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.paaltao.activity.MyShopActivity">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/product_view"
android:layout_below="#+id/app_bar"
>
<RelativeLayout
android:id="#+id/shop_cover_area"
android:layout_width="match_parent"
android:layout_height="150dp"
android:visibility="visible">
<ImageView
android:id="#+id/shop_cover_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/shop_cover_image"
android:scaleType="centerCrop"
android:src="#drawable/apple_small" />
</RelativeLayout>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:orientation="vertical"
android:visibility="gone"
card_view:cardCornerRadius="5dp"
card_view:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/shop_start_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="16dp"
android:text="#string/product_first_launch_text"
android:textColor="#color/black" />
</LinearLayout>
</android.support.v7.widget.CardView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/shop_cover_area"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/shop_products_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:padding="8dp"
/>
</RelativeLayout>
</RelativeLayout>
<com.paaltao.classes.AddFloatingActionButton
android:id="#+id/multiple_actions1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_gravity="end"
fab:fab_addButtonColorPressed="#color/white_pressed"
fab:fab_addButtonPlusIconColor="#color/white"
fab:fab_labelStyle="#style/menu_labels_style"
app:fab_addButtonColorNormal="#color/lightGreen"/>
</RelativeLayout>
The activity code is as follows :
package com.paaltao.activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.paaltao.Adapters.ShopProductAdapter;
import com.paaltao.R;
import com.paaltao.classes.AddFloatingActionButton;
import com.paaltao.classes.Product;
import java.util.ArrayList;
import java.util.List;
public class MyShopActivity extends ActionBarActivity {
private RecyclerView mRecyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_shop);
mRecyclerView = (RecyclerView) findViewById(R.id.shop_products_recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.HORIZONTAL,false));
mRecyclerView.setAdapter(new ShopProductAdapter(getApplicationContext(), getData()));
Toolbar toolbar = (Toolbar) this.findViewById(R.id.app_bar);
toolbar.setTitleTextColor(Color.WHITE);
this.setSupportActionBar(toolbar);
this.setTitle("Shop");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
AddFloatingActionButton addProductButton = (AddFloatingActionButton)findViewById(R.id.multiple_actions1);
addProductButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("Tag","Clicked");
Intent intent = new Intent(MyShopActivity.this, AddProductActivity.class);
startActivity(intent);
}
});
}
public static List<Product> getData(){
ArrayList data = new ArrayList();
int[] icons = {R.drawable.apple_small,R.drawable.ic_launcher,R.drawable.bag_icon,R.drawable.notify_icon,R.drawable.apple_small,
R.drawable.bag_icon,R.drawable.apple_small,R.drawable.notify_icon,R.drawable.apple_small,R.drawable.ic_launcher};
String[] product_name = {"Handmade","Photography","Electronics","Electronics","Electronics","Electronics","Electronics",
"Electronics","Electronics","Electronics"};
for(int i=0; i<icons.length && i< product_name.length;i++){
Product current = new Product();
current.setProduct_name(product_name[i]);
current.setProduct_id(icons[i]);
data.add(current);
}
return data;
}
#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_my_shop, 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);
}
}
When I use vertical orientation in recycled view the design works fine. The problem only occurs when I set the orientation to LinearLayoutManager.HORIZONTAL.
Please help
It was very foolish of me to ask such a question :P
In the layout containing the design of recycler view row, The width and height cannot be set as "match parent". Instead the thing works simply for "wrap content". To enable horizontal scrolling the row must be given fixed width and height.
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"/>
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...
I am having troubles getting a button to work from an external website in a webView. The button appears to use PHP Post. One thing I’m noticing is that the URL uses parameters and these parameters get changed when a button is pressed. I tested out the external site in the android browser and had no problem, however when I use the webView I have problems with it.
Sorry if anything in this post is formatted wrong, this is my first time posting here. I have extensively search the internet for this problem and have not found and solutions.
Here is the Java code
package com.example.jbzapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
public class ScavHuntActivity extends Activity implements OnClickListener {
Button events, scavHunt, gInfo, donations, home, animals, map, social;
WebView webQuiz;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initLayout();
}
private void initLayout(){
setContentView(R.layout.activity_scav_hunt);
//load quiz from web
webQuiz = (WebView)findViewById(R.id.webViewQuiz);
//Alternative site, not going to use but it works fine.
//webQuiz.loadUrl("http://www.qfeast.com/scored/quiz/10285/Quiz-Test");
webQuiz.loadUrl("http://www.proprofs.com/quiz-school/story.php?title=quiz-test_103");
WebSettings webSettings = webQuiz.getSettings();
//This did nothing
//webSettings.setSupportMultipleWindows(true);
webSettings.setJavaScriptEnabled(true);
//webQuiz.reload();
this.webQuiz.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
view.loadUrl("javascript:window.location.reload( true )" );
return true;
}
});
events = (Button) findViewById(R.id.eventsButton); //Creating the button referencing it to the xml button created by ID.
events.setOnClickListener(this); //Creates the on click listener that listens for the button click.
scavHunt = (Button) findViewById(R.id.scavangerButton);
scavHunt.setOnClickListener(this);
gInfo = (Button) findViewById(R.id.generalButton);
gInfo.setOnClickListener(this);
donations = (Button) findViewById(R.id.donationsButton);
donations.setOnClickListener(this);
home = (Button) findViewById(R.id.homeButton);
home.setOnClickListener(this);
animals = (Button) findViewById(R.id.animalsButton);
animals.setOnClickListener(this);
map = (Button) findViewById(R.id.mapButton);
map.setOnClickListener(this);
social = (Button) findViewById(R.id.socialButton);
social.setOnClickListener(this);
}
#Override
public void onClick(View v) {
final Intent eventsIntent = new Intent(this, EventsActivity.class); //Creating an intent that connects the HomeActivity.class with the EventsActivity.class
final Intent scavHuntIntent = new Intent(this, ScavHuntActivity.class);
final Intent gInfoIntent = new Intent(this, GInfoActivity.class);
final Intent donationsIntent = new Intent(this, DonationsActivity.class);
final Intent homeIntent = new Intent(this, HomeActivity.class);
final Intent animalsIntent = new Intent(this, AnimalsActivity.class);
final Intent mapIntent = new Intent(this, MapActivity.class);
final Intent socialIntent = new Intent(this, SocialActivity.class);
switch(v.getId()){
case R.id.eventsButton:
startActivity(eventsIntent); //on button click start activity with the intent to change view to EventsActivity.class
break;
case R.id.scavangerButton:
startActivity(scavHuntIntent);
break;
case R.id.generalButton:
startActivity(gInfoIntent);
break;
case R.id.donationsButton:
startActivity(donationsIntent);
break;
case R.id.homeButton:
startActivity(homeIntent);
break;
case R.id.animalsButton:
startActivity(animalsIntent);
break;
case R.id.mapButton:
startActivity(mapIntent);
break;
case R.id.socialButton:
startActivity(socialIntent);
break;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_scav_hunt, menu);
return true;
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && webQuiz.canGoBack()) {
webQuiz.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
}
and here is my 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"
tools:context=".ScavHuntActivity" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:maxHeight="75dp"
android:weightSum="4" >
<Button
android:id="#+id/homeButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginBottom="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:layout_weight="1"
android:background="#color/beige"
android:gravity="top|left"
android:minHeight="75dp"
android:minWidth="75dp"
android:text="#string/home"
android:textColor="#color/white"
android:textSize="20sp" />
<Button
android:id="#+id/animalsButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginBottom="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:layout_weight="1"
android:background="#color/Yay"
android:gravity="top|left"
android:minHeight="75dp"
android:minWidth="75dp"
android:text="#string/animals"
android:textColor="#color/white"
android:textSize="20sp" />
<Button
android:id="#+id/scavangerButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginBottom="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:layout_weight="1"
android:background="#color/Yo"
android:ellipsize="none"
android:gravity="top|left"
android:maxLines="100"
android:minHeight="75dp"
android:minWidth="75dp"
android:scrollHorizontally="false"
android:text="#string/scavanger_hunt"
android:textColor="#color/white"
android:textSize="16sp" />
<Button
android:id="#+id/eventsButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginBottom="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:layout_weight="1"
android:background="#color/green"
android:gravity="top|left"
android:minHeight="75dp"
android:minWidth="75dp"
android:text="#string/events"
android:textColor="#color/white"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="top"
android:minHeight="75dp"
android:weightSum="4" >
<Button
android:id="#+id/mapButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginBottom="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:layout_weight="1"
android:background="#color/lightGreen"
android:gravity="top|left"
android:minHeight="75dp"
android:text="#string/map"
android:textColor="#color/white"
android:textSize="20sp" />
<Button
android:id="#+id/generalButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:layout_weight="1"
android:background="#color/haha"
android:ellipsize="none"
android:gravity="top|left"
android:maxLines="100"
android:minHeight="75dp"
android:scrollHorizontally="false"
android:text="#string/general_information"
android:textColor="#color/white"
android:textSize="20sp" />
<Button
android:id="#+id/donationsButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginBottom="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:layout_weight="1"
android:background="#color/bla"
android:ellipsize="none"
android:gravity="top|left"
android:maxLines="100"
android:minHeight="75dp"
android:minWidth="75dp"
android:scrollHorizontally="false"
android:text="#string/donations"
android:textColor="#color/white"
android:textSize="16sp" />
<Button
android:id="#+id/socialButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginBottom="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:layout_weight="1"
android:background="#color/meh"
android:gravity="top|left"
android:minHeight="75dp"
android:text="#string/social"
android:textColor="#color/white"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/linearLayout1"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/otter" >
<WebView
android:id="#+id/webViewQuiz"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</RelativeLayout>