java android Multiple addViews on a layout with one xml drawable - java

I'm trying get multiple copies of a drawable shape on the layout, but change the color each time. I can't tell if the 2nd imageview is just landing on top of the first. I haven't been able to get them placed in distinct locations to troubleshoot further. Does each instance needs it's own params?
#drawable/circle (circle.xml)
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#FF888888"/>
<size
android:width="60dp"
android:height="60dp"/>
</shape>
activity_main.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"
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"
android:id="#+id/root">
</RelativeLayout>
MainActivity.java
package com.example.thowell09156277.newcircleaddview;
import android.app.Activity;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Let's create the missing ImageView
ImageView image = new ImageView(this);
ImageView image2 = new ImageView(this);
// Now the layout parameters, these are a little tricky at first
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params.addRule(RelativeLayout.RIGHT_OF, RelativeLayout.TRUE);
image.setScaleType(ImageView.ScaleType.MATRIX);
image.setImageResource(R.drawable.circle);
image.setImageTintMode(PorterDuff.Mode.OVERLAY);
image.setImageTintList(ColorStateList.valueOf(Color.YELLOW));
int imageId;
imageId = image.generateViewId();
image.setId(imageId);
int imageId2;
imageId2 = image2.generateViewId();
image2.setId(imageId2);
image2.setScaleType(ImageView.ScaleType.MATRIX);
image2.setImageResource(R.drawable.circle);
image2.setImageTintMode(PorterDuff.Mode.OVERLAY);
image2.setImageTintList(ColorStateList.valueOf(Color.MAGENTA));
image2.setId(imageId2);
//image.setOnTouchListener(this);
// Let's get the root layout and add our ImageView
RelativeLayout layout = (RelativeLayout) findViewById(R.id.root);
//params.setMargins(0, 0, 80, 80);
layout.addView(image, 0, params);
//params.setMargins(80, 0, 80, 160);
//params.addRule(RelativeLayout.ALIGN_BOTTOM, RelativeLayout.TRUE);
layout.addView(image2, 1, params);
}

Related

Setting style for dynamically added buttons

I'm trying to set the style to a dynamically added buttons in an activity but the buttons are still with the default stlye.
Here is my activity
package com.sample.Learn_German_Quiz;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v4.view.ViewPropertyAnimatorCompat;
import android.view.ContextThemeWrapper;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
import static android.R.interpolator.linear;
public class LessonChooserActivity extends AppCompatActivity {
private GrammarBank grammarBank = new GrammarBank();
public String getSelectedLesson() {
return selectedLesson;
}
public void setSelectedLesson(String selectedLesson) {
this.selectedLesson = selectedLesson;
}
private String selectedLesson;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lesson_chooser);
grammarBank.initLessons(getApplicationContext());
setupButton();
}
private void setupButton() {
Context newContext = new ContextThemeWrapper(getBaseContext(), R.style.button_learn);
for (int i = 0; i <= 5; i++) {
LinearLayout layout = (LinearLayout) findViewById(R.id.linearTest);
layout.setOrientation(LinearLayout.VERTICAL);
Button btn = new Button(newContext);
//Button btn = new Button(this);
btn.setText(grammarBank.getLessonTitle(i));
layout.addView(btn);
}
}
}
and the layout file for the activity
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="#dimen/fab_margin"
android:id="#+id/linearTest">
<Button
style="#style/button_learn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/ButtonTest"
android:ems="10"
android:text="Test"
android:textColor="#ffff"
android:padding="16dp"
android:onClick="onClick"
/>
</LinearLayout>
and the style I want to give to my buttons:
<style name="button_learn" parent="Theme.AppCompat.DayNight.NoActionBar">
<item name="android:background">#drawable/button_rounded</item>
<item name="android:layout_margin">20dp</item>
</style>
button_rounded.xml file
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<corners android:radius="50dp"></corners>
<solid android:color="#4F535F"></solid>
</shape>
</item>
</selector>
They are showing like this:
and I want them like this:
Try this one
LinearLayout layout = (LinearLayout) findViewById(R.id.linearTest);
layout.setOrientation(LinearLayout.VERTICAL);
for (int i = 0; i <= 5; i++) {
Button btn= new Button(this, null,R.style.button_learn);
btn.setText(grammarBank.getLessonTitle(i));
layout.addView(btn);
}
Have you tried using another Button constructor that takes also int defStyleAttr? If you pass R.style.button_learn as defStyleAttr then it should work.

onClickListener on imageview to start new intent

So Im trying to use an onClickListener to pass an image from one activity to a new activity but I cannot figure out how to do it. How it works is the app starts with a listview where you can select an item and it opens a new activity that displays some information and an image below. I want to be able to click the image to open it in a new activity.
Heres my routeDetails.java this is where the image I want to click is located
package com.example.zach.listview;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import static com.example.zach.listview.R.id.image;
public class RouteDetails extends AppCompatActivity {
TouchImageView routeImage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_route_details);
//back button for route details view
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//TextView for route details
final TextView routeDetailsView = (TextView) findViewById(R.id.routeDetailsView);
routeDetailsView.setText(getIntent().getExtras().getString("route"));
//ImageView for route details
routeImage = (TouchImageView) findViewById(R.id.routeImage);
routeImage.setImageResource(getIntent().getIntExtra("imageResourceId", 0));
////////// Trying to pass image to new activity
routeImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
int imageId = (int) image.getResourceId(i, -1);
Intent intent = new Intent(v.getContext(), FullScreenImage.class);
intent.putExtra("imageResourceId", imageId);
startActivity(intent);
}
});
////////////
}
//back button for route details view
#Override
public boolean onSupportNavigateUp(){
finish();
return true;
}
}
and heres the activity_route_details.xml that goes with it
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_route_details"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.zach.listview.RouteDetails">
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/routeDetailsView"
android:textSize="18sp"
android:textAlignment="center" />
<com.example.zach.listview.TouchImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/routeImage"
android:scaleType="fitCenter"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:adjustViewBounds="false"
android:layout_below="#+id/routeDetailsView"/>
</RelativeLayout>
</ScrollView>
heres my FullScreenImage.java that I want the image to open in
package com.example.zach.listview;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
/**
* Created by Zach on 11/15/2016.
*/
public class FullScreenImage extends AppCompatActivity {
TouchImageView routeImage;
#Override
protected void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
setContentView(R.layout.full_screen_image);
routeImage = (TouchImageView) findViewById(R.id.fullScreenImageView);
routeImage.setImageResource(getIntent().getIntExtra("imageFullScreen", 0));
}
}
And heres the full_scren_image.xml to go with it
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.zach.listview.TouchImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#+id/routeImage"
android:id="#+id/fullScreenImageView"
android:layout_weight="1" />
</LinearLayout>
You're putting in the extra with the key imageResourceId but you're trying to pull it out with the key imageFullScreen. Try using the same key in both places.
Use your image as ByteArray. This worked for me without any problem. In your click listener of the sender activity do the following,
Intent senderint = new Intent(context, receiveractivity.class);
Bitmap bitmap = drawableToBitmap(view.getBackground());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] b = baos.toByteArray();
senderint.putExtra("myimage", b);
startActivity(senderint);
And in your receiver activity do the following.
private void getImage(){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = null;
v = inflater.inflate(R.layout.receiverlayout, null);
ViewGroup vg = (ViewGroup) findViewById(R.id.yourmainlayout);
Bundle bundle = getIntent().getExtras();
byte[] ba = bundle.getByteArray("myimage");
Bitmap bm = BitmapFactory.decodeByteArray(ba, 0, ba.length());
Drawable dr = new BitmapDrawable(getResources, bm);
vg.setBackground(dr);
vg.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
call getImage() in your oncreate method of your receiver activity. The context is your application context. Good luck:)

making an image exactly circular without borders

I want a circular image for my Profile activity. I have already tried these answers but it does n't work in my case.
How to create a circular ImageView in Android?
How to make an ImageView with rounded corners?
How to make an image fit into a circular frame in android
How to Make an ImageView in Circular Shape?
It's not a duplicate question,using all these methods , Here is what i get :-
Problem : image is not exactly circular.
here is my Profile.java code:-
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
/**
* Created by kundan on 6/4/2015.
*/
public class Profile extends ActionBarActivity {
TextView username;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);
username= (TextView) findViewById(R.id.txt);
String recievedusername=getIntent().getExtras().getString("toname");
username.setText(recievedusername);
Bitmap bm = BitmapFactory.decodeResource(getResources(),
R.mipmap.gomez);
Bitmap resized= Bitmap.createScaledBitmap(bm,200,200,true);
Bitmap conv_bm=getCircleBitmap(resized,100);
// set circle bitmap
ImageView mImage = (ImageView) findViewById(R.id.profile_image);
mImage.setImageBitmap(conv_bm);
// TODO Auto-generated method stub
}
private Bitmap getCircleBitmap(Bitmap bitmap , int pixels) {
final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(),bitmap.getHeight());
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawCircle(100, 100, 100, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
bitmap.recycle();
return output;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_apploud, menu);
return 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_addnew) {
Intent i;
i=new Intent(Profile.this,ApplaudSomeone.class);
startActivity(i);
return true;
}
return super.onOptionsItemSelected(item);
}
}
Needed portion of my profile.xml is :-
<?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="wrap_content"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#ff17a088"
android:weightSum="1"
android:paddingTop="5dp"
android:gravity="center">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:id="#+id/profile_image"
android:contentDescription="#string/img"
android:layout_gravity="center"
android:scaleType="centerCrop"
android:background="#drawable/img"
android:layout_alignParentTop="true"
android:layout_alignEnd="#+id/txt2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="suman"
android:id="#+id/txt"
android:textSize="25sp"
android:padding="5dp"
android:layout_gravity="center_horizontal"
android:textColor="#fffff9"
android:layout_below="#+id/profile_image"
android:layout_centerHorizontal="true" />
code of img.xml is:-
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
android:useLevel="false"
>
<stroke android:width="5dp"
android:color="#ffffff"/>
</shape>
</item>
</layer-list>
Thanks in Advance!!
Yes, finally i solved this problem. If any of you are facing the same problem then use canvas.drawCircle(100, 100, 90, paint); instead of canvas.drawCircle(100, 100, 100, paint); this will definitely solve your problem.

How to randomly rotate an image in Android?

I'm new at Android Programing and I want to know how to rotate an image by a random angle. I need this for my "Spin the Bottle" game. Here is my code so far:
package example.com.bottlegame;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.RotateDrawable;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import java.lang.Object;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import java.util.Random;
public class MainActivity extends ActionBarActivity {
ImageView spin,bottle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spin = (ImageView) findViewById(R.id.ivSpin);
bottle = (ImageView) findViewById(R.id.ivBottle);
final Animation animRotate = AnimationUtils.loadAnimation(this, R.anim.roatate);
spin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
bottle.startAnimation(animRotate);
}
});
}
}
Now I'm rotating an image with XML, but can you help me to rotate it with just Java code. Here is the XML code in anim folder:
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator">
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:pivotX="50%"
android:pivotY="50%"
android:fromDegrees="0"
android:toDegrees="360"
android:drawable="#drawable/bottle"
android:duration="1000"
android:repeatCount="1"
android:repeatMode="reverse"
android:startOffset="0"
/>
</set>`
And here is the activity_main.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"
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"
android:background="#drawable/bg_background">
<ImageView
android:layout_width="wrap_content"
android:layout_height="100sp"
android:layout_centerHorizontal="true"
android:id="#+id/ivSpin"
android:src="#drawable/spin_up"
android:layout_marginTop="400sp"
android:contentDescription="#string/spin"
/>
<ImageView
android:layout_width="65sp"
android:layout_height="200sp"
android:id="#+id/ivBottle"
android:src="#drawable/bottle"
android:layout_marginTop="80dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:contentDescription="#string/bottle"
/>
</RelativeLayout>
You can easily configure your animation in Java code as well as in xml. For your case you could use class RotateAnimation and its' constructor public RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)
So, try somehting like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spin = (ImageView) findViewById(R.id.ivSpin);
bottle = (ImageView) findViewById(R.id.ivBottle);
float toDegrees = new Random().nextFloat() * Integer.MAX_VALUE % 360;
final Animation animRotate = new RotateAnimation(0, toDegrees, 50, 50);
animRotate.setDuration(1000);
animRotate.setRepeatCount(1);
animRotate.setRepeatMode(Animation.REVERSE);
spin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
bottle.startAnimation(animRotate);
}
});
}
I think, that no comments are required.

How do I add an ImageView to the root layout?

I am following this tutorial into making an app for Android and ran into a problem. Disclaimer: I know next to nothing about Java or Eclipse, so bear with me.
I created a bitmap, which I put into an ImageView(?), and now the tutorial says to add the ImageView to the root_layout, but to be fair, I have no idea what the root_layout is (I Googled some, but couldn't find the right answer). Also, Eclipse gives me the 'layoutroot cannot be resolved or is not a field'-error, which I do not know how to solve. My question then is, how do I get the image to display on the screen? Thanks in advance :-)
Here is my (full) code:
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class ShowImage extends ActionBarActivity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.show_image, 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);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
// shows the activity
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_image);
try {
// load large image from resources
Bitmap game_image = BitmapFactory.decodeResource(this.getResources(), R.drawable.sample_0);
// create cropped image from loaded image
Bitmap cropped = Bitmap.createBitmap(game_image, 0, 0, 100, 100);
// no longer need larger image
game_image.recycle();
// create ImageView to display image
ImageView imageView = new ImageView(this);
imageView.setImageBitmap(cropped);
// add ImageView to root layout
LinearLayout root = (LinearLayout)this.findViewById(R.id.root_layout);
root.addView(imageView);
}
// catch comes here
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
// shows GamePlay-activity after three seconds
Intent intent = new Intent(ShowImage.this, GamePlay.class);
ShowImage.this.startActivity(intent);
ShowImage.this.finish();
}
}, 3000);
}
}
Added .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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="nl.mprog.projects.nPuzzle10206353.ShowImage" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Wait three seconds..." />
</RelativeLayout>
root_layout is basically the existing layout in your xml in which you want to add newly created View.
Assign id root_layout to the RelativeLayout in your xml file.
OR
Keep a new Layout inside it with root_layout id.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="nl.mprog.projects.nPuzzle10206353.ShowImage" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Wait three seconds..." />
</RelativeLayout>
So now when you use this in Activity:
RelativeLayout root = (RelativeLayout)this.findViewById(R.id.root_layout);
root.addView(imageView);
imageView would be added to that RelativeLayout with id root_layout
add the imageView to your xml:
<ImageView android:id="#+id/mImageView"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:contentDescription="solecito"
android:layout_weight = "1"
android:layout_gravity="center"
/>
then add it on you onCreate Method
ImageView im = (ImageView)findViewById(R.id.mImageView);
after you can add the image
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
im.setImageBitmap(bitmap);
package com.tommymacwilliam.androidwalkthroughapp3;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.ImageView;
import android.widget.Toast;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class AndroidWalkthroughApp3 extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
// load large image from resources
Bitmap background = BitmapFactory.decodeResource(this.getResources(), R.drawable.puzzle_0);
// create cropped image from loaded image
Bitmap cropped = Bitmap.createBitmap(background, 0, 0, 230, 230);
// no longer need larger image
background.recycle();
// create ImageView to display image
ImageView imageView = new ImageView(this);
imageView.setImageBitmap(cropped);
// add ImageView to root layout
LinearLayout root = (LinearLayout)this.findViewById(R.id.root_layout);
root.addView(imageView);
int screenWidth = this.getResources().getDisplayMetrics().widthPixels;
Toast.makeText(this, String.valueOf(screenWidth), Toast.LENGTH_LONG).show();
}
catch (OutOfMemoryError e) {
// uh oh.
}
}
}

Categories