How to add Image Dynamically android - android

I Am Having Base class which is called common activity,In that i am returning the String in every activity.By using the following code.
txtHeader = (TextView)findViewById(R.id.txtHeaderText);
txtHeader.setText(getScreenHeader());
and i am returning the empty string
protected String getScreenHeader(){
return "";
}
In another activity i will do in this way.
public final static String SCREEN_NAME = "Dashboard";
#Override
protected String getScreenHeader() {
return SCREEN_NAME;
}
How can i do this For IMAGE-VIEW.
Sorry for my English.
Thansks,
Nikhil.

You will need to use a Bitmap object such as this:
Bitmap b = getBitmapImage();
ImageView img = (ImageView )findViewById(R.id.imgView);
img.setImageBitmap(b);

Related

How to get Image

I am new in Android and have very small issue, i want to know what code i need to use to get image.
like if we use this to get text :
name = txtname.getText().toString();
So what we use to get Image ?
CODE:
public static final String TAG_NAME = "name";
public static final String TAG_IMAGEURL = "imageurl"
String name, image ;
Intent in = getIntent();
String mname = in.getStringExtra(TAG_NAME);
String mimage = in.getStringExtra(TAG_IMAGEURL);
ImageLoader imageLoader = new ImageLoader(getApplicationContext());
final ImageView imgv = (ImageView) findViewById(R.id.image);
final TextView txtn = (TextView) findViewById(R.id.name);
txtn.setText(mname);
imageLoader.DisplayImage(mimage, imgv);
name = txttitle.getText().toString();
image = // here i want to know what i should need to use to get image
// like i am getting text using :::txttitle.getText().toString();
ImageView.getDrawable() will return the Drawable set in an ImageView
if you've used setImageDrawable() to set image to the ImageView then you can use imgv.getDrawable() method to get the image.

how to adapt data in Arraylist to viewflipper using android [duplicate]

In my scenario i am having 3 records in sqlite which were fetched from websercive that contains id,name,description and image now i want to display the first record and second one after flipping the first record.and 3 one after second record flips..
What i have done so far is
while (cursor.moveToNext())
{
String temp_bookid = cursor.getString(0);
Log.i("temp_bookid",temp_bookid);
String temp_desc=cursor.getString(1);
byte[] temp_image1 = cursor.getBlob(2);
byte[] temp_image2 = cursor.getBlob(3);
String temp_id=cursor.getString(4);
String temp_name = cursor.getString(5);
String temp_bname=cursor.getString(6);
mapId.add(temp_id);
mapBname.add(temp_bname);
mapBookId.add(temp_bookid);
mapName.add(temp_name);
mapDesc.add(temp_desc);
map2.add(temp_image2);
map1.add(temp_image1);
Log.i("temp_id of the page in sqlite", temp_id);
TextView txtDesc = (TextView) findViewById(R.id.tDescription);
text = (TextView) findViewById(R.id.tTitle);
text.setText(temp_name);
txtDesc.setText(temp_desc);
ImageView img = (ImageView)findViewById(R.id.smallImage);
img.setImageBitmap(BitmapFactory.decodeByteArray(temp_image1, 0,temp_image1.length));
txtName1 = (TextView) findViewById(R.id.tvName);
txtName1.setText(temp_bname);
break;
}
mContext = this;
vf = (ViewFlipper) this.findViewById(R.id.vfShow);
vf.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(final View view, final MotionEvent event) {
detector.onTouchEvent(event);
Log.i("ew","ae");
return true;
}
});
I am able to display first record data when i set break; statement otherwise it is displaying last record data..How can proceed for this approach?
First fetch all the records and store in List<YourDataClass> myList object from onCreate method. As I have just giving you the examples
Now after fetching the data and stored in your list object you need to implement flipper change event so when flipper was change get the current position and using this position get the data as object from your list object. As you move right/left it will giving you the current position and you just need to use this position to fetch the records from list object
For example
Your POJO class will store the data MyData.java
public class MyData{
public long id = -1;
public String name = "";
public String description = "";
public String imagePath = ""; /// I don't know you want exactly, you need to implement image as per your requirement.
}
In your activity
private List<MyData> myList = new ArrayList<MyData>();
oncreate(){
fetchData(); // you need to implement this in AsyncTask so UI was not block just implement AsyncTask and call this method in doInBackground().
// after this you can implment View flipper and add view and set the data by fetch the list object using current view flipper position.
}
private void fetchData(){
while (cursor.moveToNext()){
MyData myData = new MyData();
myData.id = cursor.getLong(0);
myData.desc=cursor.getString(1);
myData.name = cursor.getString(5);
list.add(myData);
myData = null;
}
}

Issue with displaying data in viewflipper from sqlite [duplicate]

In my scenario i am having 3 records in sqlite which were fetched from websercive that contains id,name,description and image now i want to display the first record and second one after flipping the first record.and 3 one after second record flips..
What i have done so far is
while (cursor.moveToNext())
{
String temp_bookid = cursor.getString(0);
Log.i("temp_bookid",temp_bookid);
String temp_desc=cursor.getString(1);
byte[] temp_image1 = cursor.getBlob(2);
byte[] temp_image2 = cursor.getBlob(3);
String temp_id=cursor.getString(4);
String temp_name = cursor.getString(5);
String temp_bname=cursor.getString(6);
mapId.add(temp_id);
mapBname.add(temp_bname);
mapBookId.add(temp_bookid);
mapName.add(temp_name);
mapDesc.add(temp_desc);
map2.add(temp_image2);
map1.add(temp_image1);
Log.i("temp_id of the page in sqlite", temp_id);
TextView txtDesc = (TextView) findViewById(R.id.tDescription);
text = (TextView) findViewById(R.id.tTitle);
text.setText(temp_name);
txtDesc.setText(temp_desc);
ImageView img = (ImageView)findViewById(R.id.smallImage);
img.setImageBitmap(BitmapFactory.decodeByteArray(temp_image1, 0,temp_image1.length));
txtName1 = (TextView) findViewById(R.id.tvName);
txtName1.setText(temp_bname);
break;
}
mContext = this;
vf = (ViewFlipper) this.findViewById(R.id.vfShow);
vf.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(final View view, final MotionEvent event) {
detector.onTouchEvent(event);
Log.i("ew","ae");
return true;
}
});
I am able to display first record data when i set break; statement otherwise it is displaying last record data..How can proceed for this approach?
First fetch all the records and store in List<YourDataClass> myList object from onCreate method. As I have just giving you the examples
Now after fetching the data and stored in your list object you need to implement flipper change event so when flipper was change get the current position and using this position get the data as object from your list object. As you move right/left it will giving you the current position and you just need to use this position to fetch the records from list object
For example
Your POJO class will store the data MyData.java
public class MyData{
public long id = -1;
public String name = "";
public String description = "";
public String imagePath = ""; /// I don't know you want exactly, you need to implement image as per your requirement.
}
In your activity
private List<MyData> myList = new ArrayList<MyData>();
oncreate(){
fetchData(); // you need to implement this in AsyncTask so UI was not block just implement AsyncTask and call this method in doInBackground().
// after this you can implment View flipper and add view and set the data by fetch the list object using current view flipper position.
}
private void fetchData(){
while (cursor.moveToNext()){
MyData myData = new MyData();
myData.id = cursor.getLong(0);
myData.desc=cursor.getString(1);
myData.name = cursor.getString(5);
list.add(myData);
myData = null;
}
}

Drawable Images in SQLite android

I've got a working TextView using database populating text, but I want to connect the image path name in my local database. How can this be incorporated in java?
In the main activity I am setting :
String image = currentQ.getImage();
ImageView iView = (ImageView) findViewById(R.id.IS);
Bitmap bMap = BitmapFactory.decodeFile(image);
iView.setImageBitmap(bMap);
In the question:
private String image;
/**
* #return the image
*/
public String getImage() {
return image;
}
/**
* #param image the image to set
*/
public void setImage(String image) {
this.image = image;
}
And in the database I am declaring it as a text:
file.path.name/drawable/i1.jpg and so on for the others.
How can I attach an image file location in my drawable folder for each question in the database and connect it in the java?
Thanks in advance.
ImageView imageView = (ImageView) findViewById(R.id.imageView);
String imageName = "NameofImage"; // store only image name in database(means not store "R.drawable.image") like "image".
int id = getResources().getIdentifier("projectPackageName:drawable/" + imageName, null, null);
imageView.setImageResource(id);

R.id.image to string in android

In my project I'm trying to display several image files by manipulating the filename of one of the images programatically.
ie, I may have:
filename.jpg, filename_top.jpg, filename_middle.jpg
I receive input of an drawable int and am trying to find the filename of the displayed image before manipulating this filename and trying to display the programatically generated filenames.. problem is that the manipulated filename does not display.
ie. there is something wrong with this:
imageView2.setImageResource(getImageId(this, namebottom));
Any ideas how getImageId can be modified to make setImageResource work properly?
The code would look something like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bun = getIntent().getExtras();
int imagenumber = bun.getInt("imagenumber");
String extension = bun.getString("extension");
// int become a val from 0 to 20 (array size)
setContentView(R.layout.clickeditem);
final int[] imgIds = new int[{
R.drawable.image0,R.drawable.image1,R.drawable.image2,,,R.drawable.image20};
//The first image with id top in the layout is set ok:
ImageView imageView1 = (ImageView)findViewById(R.id.top);
imageView1.setImageResource(imgIds [ imagenumber ] );
// problem here:
//try to get the name of this file: ie: filename.jpg
// and then manipulate the filename:
String name = imageView1.getResources().getString(R.id.image0);
//try to convert this to the filename_middle.jpg
String namemiddle = name.replace(".jpg", "_middle.jpg");
imageViewt.setImageResource(getImageId(this, namemiddle));
//try to convert this to filename_bottom.jpg
String namebottom = name.replace(".jpg", "_bottom.jpg");
imageView2.setImageResource(getImageId(this, namebottom));
}
//where getImageId is defines as follows:
public static int getImageId(Context context, String imageName)
{
return context.getResources().getIdentifier("drawable/" + imageName,
null, context.getPackageName());
}
return context.getResources().getIdentifier("drawable/" + imageName,
null, context.getPackageName());
replace this by
return context.getResources().getIdentifier(imageName,
"drawable", context.getPackageName());

Categories

Resources