hello i am new on android. Please help.
My SingleViewActivity java
package com.example.helloworld;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
public class SingleViewActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.single_view);
// Get intent
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView =(ImageView) findViewById(R.id.SingleView);
imageView.setImageResource(imageAdapter.mThumbIds[position]);}}
This singleview 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">
<ImageViewandroid:id="#+id/SingleView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
So my problem is, when thumb image clicked is not show full image
Sorry my bad english
add scale type to ImageView like this,
android:scaleType="fitXY"
Related
I want to pass images from one activity to another when button clicked. I have one button "Show Image" in first activity. When I click on it, it should pass two images from my mipmap folder of my project and go to second activity and show one of the passed image on the ImageView of that activity. On second activity, I have two buttons which are supposed to receive images and show those images when clicked on each button. I tried using intent to pass the image, however, it didn't work. Is there other way to send images from mipmap folder from one activity to another?
Here is my code:
MainActivity.java
package com.example.abina.myapplication;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import java.io.ByteArrayOutputStream;
public class MainActivity extends AppCompatActivity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showmyImage();
}
});
}
public void showmyImage(){
Intent intent = new Intent(this, Main2Activity.class);
Bitmap bitmap; // your bitmap
bitmap = null;
ByteArrayOutputStream _bs = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 50, _bs);
intent.putExtra("byteArray", _bs.toByteArray());
startActivity(intent);
}
}
activity_main.xml
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="137dp"
android:text="Show Image" />
</RelativeLayout>
Main2Activity.java
package com.example.abina.myapplication;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
public class Main2Activity extends AppCompatActivity {
Button image1;
Button image2;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
image1 =(Button) findViewById(R.id.image1);
image2 = (Button) findViewById(R.id.image2);
imageView =(ImageView) findViewById(R.id.imageView);
if(getIntent().hasExtra("byteArray")) {
Bitmap _bitmap = BitmapFactory.decodeByteArray(
getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);
imageView.setImageBitmap(_bitmap);
}
}
}
activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".Main2Activity">
<Button
android:id="#+id/image1"
android:layout_width="199dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="Image1" />
<Button
android:id="#+id/image2"
android:layout_width="183dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="0dp"
android:text="Image2" />
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
Images
First the mipmap folders are for placing your app/launcher icons (which are shown on the homescreen) in only. Any other drawable assets you use should be placed in the relevant drawable folders.
Next since they will be in your drawables, I would just pass the #DrawableRes id e.g. the R.id.image_name value.
Intent intent = new Intent(this, Main2Activity.class);
intent.putExtra(IMAGE_RES_ID_KEY, R.id.imageName);
startActivity(intent);
Also I would recommend that you use a public static variable IMAGE_RES_ID_KEY for your extra key to avoid typos.
Then on the other side you can simply
if(getIntent().hasExtra(MainActivity.IMAGE_RES_ID_KEY)) {
imageView.setImageResource(getIntent().getIntExtra(MainActivity.IMAGE_RES_ID_KEY, 0));
}
I'd like to use the ImageViewTopCrop class described in this question:
ImageView scaling TOP_CROP
My custom view is based on this code:
Custom Image View class
Here is a layout file using this custom view:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_background"
android:background="#000"
android:orientation="vertical">
<FrameLayout
android:id="#+id/details_frame"
android:background="#color/white"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6">
<com.grayraven.imagetest.ImageViewTopCrop
android:id="#+id/grid_item_image"
android:src="#drawable/hobbit"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</FrameLayout>
Here's how I'm trying to display it in code:
com.grayraven.imagetest;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.text.Html;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
public class DetailsActivity extends ActionBarActivity {
private TextView titleTextView;
private com.grayraven.imagetest.ImageViewTopCrop imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details_view);
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
String title = getIntent().getStringExtra("title");
String imageUrl = getIntent().getStringExtra("imageUrl");
titleTextView = (TextView) findViewById(R.id.title);
imageView = (com.grayraven.project1.ImageViewTopCrop) findViewById(R.id.grid_item_image);
//Exception thrown here:
//android.widget.ImageView cannot be cast to com.grayraven.imagetest.ImageViewTopCrop
//can't go any further
}
}
What simple thing am I missing?
I think you are type casting with wrong class.
You used
com.grayraven.imagetest.ImageViewTopCrop
in your xml but type casting with com.grayraven.project1.ImageViewTopCrop
I was missing a constructor in my custom view class. Never mind.
I am trying to display thumbnail of a video but I am not able to generate the thumbnail for the video.
My activity_main contains
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/Thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
My Main Activity has the following code
package com.example.video1;
import android.app.Activity;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.os.Bundle;
import android.provider.MediaStore.Video.Thumbnails;
import android.widget.ImageView;
public class MainActivity extends Activity {
String filePath = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView thumbnail_mini = (ImageView) findViewById(R.id.Thumbnail);
Bitmap bmThumbnail = ThumbnailUtils.createVideoThumbnail(filePath,
Thumbnails.MINI_KIND);
thumbnail_mini.setImageBitmap(bmThumbnail);
}
}
Its displaying nothing . How can I display the thumbnail of video?
You can't get the thumbnail from a link with the android SDK. You would have to download it locally and then use the thumbnail.utils
Edit: I cannot get this to work correctly. Probably because I have no idea what I am doing. Anyways, here's my code. If anyone could help, I'd be very grateful: I needs to get it to display the battery mood image for the corresponding mod...
Themes:
package com.cydeon.plasmamodz;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class Themes extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.themes);
ActionBar actionBar = getActionBar();
actionBar.hide();
Button Plus = (Button) findViewById(R.id.button1);
Button Blue = (Button) findViewById(R.id.button2);
Plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(Themes.this, Bmod.class);
i.putExtra("drawableResource", R.drawable.blue);
Themes.this.startActivity(i);
}
});
Blue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent a = new Intent(Themes.this, Bmod.class);
a.putExtra("drawableResource1", R.drawable.plus);
Themes.this.startActivity(a);
}
});
}
}
Bmods:
package com.cydeon.plasmamodz;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
public class Bmod extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.battery);
Intent i = getIntent();
int drawableResource = i.getIntExtra("drawableResource", R.drawable.blue);
ImageView img = (ImageView) findViewById(R.id.iv1);
img.setImageResource(R.drawable.blue);
Intent a = getIntent();
int drawableResource1 = a.getIntExtra("drawableResource1", R.drawable.plus);
ImageView img1 = (ImageView) findViewById(R.id.iv1);
img1.setImageResource(R.drawable.plus);
}
}
battery(xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/bInstall"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Install" />
<Button
android:id="#+id/bReturn"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Return" />
<ImageView
android:id="#+id/iv1"
android:layout_width="match_parent"
android:layout_height="800dp" />
</RelativeLayout>
Intent i = new Intent(this, BaseClassForMod);
i.putExtra("drawableResource", R.drawable.this_mod_drawable);
startActivity(i);
Then in that Activity's onCreate():
Intent i = getIntent();
int drawableResource = i.getIntExtra("drawableResource", R.drawable.default);
//Get a reference to your ImageView...
imageView.setImageResource(drawableResource);
Don't trust this code to compile, but that's the general idea. Use the same Activity for all of them, pass along the proper resource ID in the intent (e.g. for mod1, send the drawable ID for mod1), then in the activity, check for that resource ID and set it programmatically.
You shouldn't need 50 classes and 50 xml layouts if every one of them does the same thing. Make one activity and one layout. When the user selects something, pass an id of some kind as an Intent extra to the second activity so it can load whatever item is appropriate. I don't know how your data is modeled, but there should be a way to uniquely identify each option (and if there isn't, you should implement one).
Your first activity also doesn't need a button for each item. Use a ListView and an Adapter, and then you just need to provide a layout for one row.
make a single activity only. Inside of it get a reference to your image:
ImageView iv = (ImageView)findViewById(R.id.yourImgId);
Then set the picture to whichever one you want like:
iv.setImageResource(R.drawable.battery_img_1);
i am creating an application in which i want set different background images in main xml linearlayout.I have stored 5 image files on sd card .now i want to select a pic and set it as my maim xml linearlayout background.so it will replace the previous image and display the new image as background.
First assign an id to the main xml linearlayout, for example in the following case it is named" container"
<!-- main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/container">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
</LinearLayout>
Then in the .java code you can find the layout object and set a drawable as its background:
package org.example.app;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String pathName = "/sdcard/gif001.gif";
Resources res = getResources();
Bitmap bitmap = BitmapFactory.decodeFile(pathName);
BitmapDrawable bd = new BitmapDrawable(res, bitmap);
View view = findViewById(R.id.container);
view.setBackgroundDrawable(bd);
}
}
Regards
Ziteng Chen