create drawable from xml file - android

I am working with map and trying to create marker as Drawable object from my layout file.
I need my marker view to change depends on the the type of point, and hence i declared my first image as static and my second image as dynamic(Change dynamically),but it is not working.
My layout file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<ImageView
android:id="#+id/marker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/marker" >
</ImageView>
<ImageView
android:id="#+id/category"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/category_for_children" />
</RelativeLayout>
My code:
Resources res = getResources();
try {
marker = Drawable.createFromXml(res, res.getXml(R.layout.marker_on_map));
} catch (NotFoundException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

Drawable.createFromXml is not for loading Layouts or View into drawable!!
see this sample of its usage:
http://spearhend.blogspot.com/2012/04/load-android-drawable-from-xml.html

Related

How to take screenshot in ConstraintLayout?

I have recently started using ConstraintLayout and am loving it. But have come across a problem to be solved. Need to take a screenshot of a part view of the layout and that is easy in relative or linear layout as we make a layout around the view needed screenshot which is not the case in ConstraintLayout.
This is my view :
<android.support.constraint.ConstraintLayout 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"
android:background="#color/white">
<View
android:id="#+id/sharable_container"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="#+id/tv_streak_best"
app:layout_constraintLeft_toLeftOf="#id/total_distance_steps_left_guideline"
app:layout_constraintRight_toRightOf="#id/total_distance_steps_right_guideline"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="#+id/streak_progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="0dp"
android:max="360"
android:progress="0"
android:progressDrawable="#drawable/progress_gradient_bg"
android:secondaryProgress="360"
app:layout_constraintBottom_toBottomOf="#id/streak_bottom_guideline"
app:layout_constraintDimensionRatio="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.sharesmile.share.views.LBTextView
android:id="#+id/tv_streak_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textColor="#color/clr_5d"
android:textSize="58sp"
app:layout_constraintBottom_toBottomOf="#id/streak_progress"
app:layout_constraintLeft_toLeftOf="#id/streak_progress"
app:layout_constraintRight_toRightOf="#id/streak_progress"
app:layout_constraintTop_toTopOf="#id/streak_progress"
app:layout_constraintVertical_bias="0.35" />
<com.sharesmile.share.views.LBITextView
android:id="#+id/tv_streak_count_days"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="#string/day_streak"
android:textColor="#color/clr_5d"
android:textSize="20sp"
app:layout_constraintLeft_toLeftOf="#id/streak_progress"
app:layout_constraintRight_toRightOf="#id/streak_progress"
app:layout_constraintTop_toBottomOf="#+id/tv_streak_count" />
<LinearLayout
android:id="#+id/streak_goal_icons_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:orientation="horizontal"
app:layout_constraintLeft_toLeftOf="#id/tv_streak_count_days"
app:layout_constraintRight_toRightOf="#id/tv_streak_count_days"
app:layout_constraintTop_toBottomOf="#id/tv_streak_count_days">
</LinearLayout>
... more code here
</android.support.constraint.ConstraintLayout>
Now I wish to take screen shot of the progressbar and few elements below it. I tried to create a view around it and take screenshot of that but it doesn't work. What can be done to take the screenshot?
You can use
tools:visibility="gone"
to hide the view as below
<View
android:id="#+id/sharable_container"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="#+id/tv_streak_best"
app:layout_constraintLeft_toLeftOf="#id/total_distance_steps_left_guideline"
app:layout_constraintRight_toRightOf="#id/total_distance_steps_right_guideline"
app:layout_constraintTop_toTopOf="parent"
tools:visibility="gone" />
add below as attribute at root view
xmlns:tools="http://schemas.android.com/tools"
Do not need to hide/show your view
private void captureScreen(View v) {
v.setDrawingCacheEnabled(true);
Bitmap bitmap = v.getDrawingCache();
String extr = Environment.getExternalStorageDirectory().toString();
File file = new File(extr, "fff" + ".jpg");
FileOutputStream f = null;
try {
f = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, f);
f.flush();
f.close();
MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "Screen", "screen");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
pass your view to above function
captureScreen(view you want to take a screen shot);

How to Display Images from Assets - Image Name from CursorAdapter?

have db with _id,hName,lName,detail,images
images stored in assets
images name in db (SQLite)
list layout without images working fine, but with images layout display's on names not images (no error in logcat)
wanted to bind CursorAdpater with `ImageView'
public void bindView(View view, Context context, Cursor cursor) {
TextView hName = view.findViewById(R.id.hName);
hName.setText(cursor.getString(cursor.getColumnIndex("hName")));
TextView lName = view.findViewById(R.id.lName);
lName.setText(cursor.getString(cursor.getColumnIndex("lName")));
ImageView image = view.findViewById(R.id.image);
InputStream is = null;
try {
is = context.getAssets().open("birds/" + cursor.getColumnIndex("images"));
} catch (IOException e) {
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(is);
image.setImageBitmap(bitmap);
}
XML file:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/image"
android:scaleType="fitCenter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:padding="24dp"
/>
<TextView
android:id="#+id/hName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/image"
android:layout_marginLeft="21dp"
android:layout_marginStart="10dp"
android:layout_toEndOf="#+id/image"
android:layout_toRightOf="#+id/image"
android:text="Name"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/lName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/image"
android:layout_alignLeft="#+id/hName"
android:layout_alignStart="#+id/hName"
android:textSize="16sp"
android:text="About" />
check the image extension is used in database
then try this
try {
is = context.getAssets().open("birds/" + cursor.getColumnIndex("images"));
Drawable d = Drawable.createFromStream(is, null);
image.setImageDrawable(d);
} catch (IOException e) {
e.printStackTrace();
}

Need to take screenshot without Imagebuttons

I am trying to take screenshot of my app screen programatically. The Layout file has some ImageButtons and a Textview field. When the capture screen happens, the resulted image contains the buttons too. Is there a way to take screenshot without the imagebuttons?
Below are the code snippet and layout files
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="xyz.abc.def.MainActivity">
<include
layout="#layout/buttons_layout"
android:id="#+id/includedLayout"
android:visibility="visible"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello World!"
android:id="#+id/text1"
android:gravity="center"
android:textSize="32dp"
android:shadowColor="#edeff2"
android:textStyle="bold"
android:shadowDx="0.0"
android:shadowDy="0.0"
android:shadowRadius="25"
/>
</RelativeLayout>
button_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/buttonLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="right"
>
<ImageButton
android:id="#+id/refreshButton"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_margin="10dp"
android:background="#drawable/refresh"
/>
<ImageButton
android:id="#+id/shareButton"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_margin="10dp"
android:background="#drawable/share"
/>
</LinearLayout>
</LinearLayout>
Code to take screenshot
private void captureScreen() {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
String filePath = Environment.getExternalStorageDirectory() + File.separator +now+".jpg";
View view = findViewById(R.id.activity_main);
view.setDrawingCacheEnabled(true);
mBitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
File imagePath = new File(filePath);
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
sendScreen(filePath);
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
Any help will be appreciated. Thanks :)
If it is just the text view you want as I can see it is full screen.
Try using
View view = findViewById(R.id.text1); // instead of R.id.activity_main
view.setDrawingCacheEnabled(true);
Basically point is keep those views in separate container whose screen shot you want and then get the drawable of that particular container.
You can make the image button invisible just before taking the screenshot and then make it visible.
private void captureScreen() {
//make unwanted views invisible
findViewById(R.id.refreshButton).setVisibility(View.INVISIBLE);
findViewById(R.id.shareButton).setVisibility(View.INVISIBLE);
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
String filePath = Environment.getExternalStorageDirectory() + File.separator +now+".jpg";
View view = findViewById(R.id.activity_main);
view.setDrawingCacheEnabled(true);
mBitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
File imagePath = new File(filePath);
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
sendScreen(filePath);
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
//make those views visible again
findViewById(R.id.refreshButton).setVisibility(View.VISIBLE);
findViewById(R.id.shareButton).setVisibility(View.VISIBLE);
}

Image is not fullscreen

I want to display an image to be fullscreen :
public class PhotoPleinEcranActivity extends Activity {
private Bundle data;
private ImageView img;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.photo_plein_ecran);
img = (ImageView)findViewById(R.id.zoom);
BitmapFactory.Options bfOptions = new BitmapFactory.Options();
bfOptions.inDither = true; //Disable Dithering mode
bfOptions.inPurgeable = true; //Tell to gc that whether it needs free memory, the Bitmap can be cleared
bfOptions.inInputShareable = true; //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
bfOptions.inTempStorage = new byte[32 * 1024];
data = getIntent().getExtras();
FileInputStream fs = null;
Bitmap bm;
try {
fs = new FileInputStream(new File(data.getString("path")));
if(fs != null) {
bm = BitmapFactory.decodeFileDescriptor(fs.getFD(), null, bfOptions);
img.setImageBitmap(bm);
}
} catch (IOException e) {
} finally {
if(fs != null) {
try {
fs.close();
} catch (IOException e) {
}
}
}
}
}
The layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="0dip"
android:layout_marginBottom="0dip"
android:paddingTop="0dip"
android:paddingBottom="0dip"
android:orientation="horizontal">
<ImageView
android:id="#+id/zoom"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="0dip"
android:layout_marginBottom="0dip"
android:paddingTop="0dip"
android:paddingBottom="0dip"
/>
</LinearLayout>
Manifest file :
<activity
android:name="com.ambre.impots.PhotoPleinEcranActivity"
android:label="#string/galleryPhotos"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
</activity>
At runtime there is a grey field at the bottom of the image ( I surrounded the grey field by a yellow color ) :
So how to make that grey field disappear ?
This will solve your problem.
Add below line in ImageView.
android:scaleType="fitXY"
add android:scaleType="fitXY" to your ImageView, fitXY, will fill width and height independently to match the destination
add property scaleType to fitXY like this
<ImageView
android:id="#+id/zoom"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="0dip"
android:layout_marginBottom="0dip"
android:paddingTop="0dip"
android:paddingBottom="0dip"
android:scaleType="fitXY" />
Change your image view like this
<ImageView
android:id="#+id/zoom"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="0dip"
android:layout_marginTop="0dip"
android:adjustViewBounds="true"
android:paddingBottom="0dip"
android:paddingTop="0dip"
android:scaleType="fitXY" />

Implementing onclick listener for app widget

I have an app widget which displays some rss feeds.It contains a text view which will display news title and an image view to diplay image.In that widget I want to add two buttons which function as next and previous to navigate through news titles.I know how to do this in an activity but how can I implement onclick listener for these buttons in widget
Here is my 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" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:scaleType="centerCrop"
android:src="#drawable/logo" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/imageView1"
android:scaleType="centerCrop"
android:src="#drawable/titlebg2" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_below="#+id/imageView2"
android:layout_centerHorizontal="true"
android:scaleType="centerCrop"
android:src="#drawable/foot" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/imageView2"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:scaleType="centerCrop"
android:src="#drawable/img" />
<Button
android:id="#+id/next"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_below="#+id/imageView1"
android:layout_marginRight="3dp"
android:layout_marginTop="3dp"
android:background="#drawable/up" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/up"
android:layout_alignBottom="#+id/up"
android:layout_marginLeft="16dp"
android:layout_marginRight="25dp"
android:layout_toRightOf="#+id/imageView4"
android:text="TextView"
android:textColor="#ffff" />
<Button
android:id="#+id/previous"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignBottom="#+id/imageView2"
android:layout_alignLeft="#+id/up"
android:layout_marginBottom="3dp"
android:background="#drawable/down" />
</RelativeLayout>
</LinearLayout>
Follwing is my onupdate function,
=>
public void onUpdate( Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds )
{
parse();//Here Iam parsing the xml
RemoteViews remoteViews;
ComponentName watchWidget;
remoteViews = new RemoteViews( context.getPackageName(), R.layout.widget_main );
watchWidget = new ComponentName( context, madhyamambig.class );
remoteViews.setTextViewText( R.id.title, Title[0]);
Bitmap bitmap;
try {
bitmap = BitmapFactory.decodeStream((InputStream)new URL(image[0]).getContent());
remoteViews.setImageViewBitmap(R.id.imageView4, bitmap);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
appWidgetManager.updateAppWidget( watchWidget, remoteViews );
}
On an activity, we can simply do this like,
int i=0;
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Bitmap bitmap;
try {
bitmap = BitmapFactory.decodeStream((InputStream)new URL(image[i+1]).getContent());
remoteViews.setImageViewBitmap(R.id.imageView4, bitmap);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
appWidgetManager.updateAppWidget( watchWidget, remoteViews );
}
}
});
I want to implement the same in widget.Somebody please help me...Thanks in advance..
You can use RemoteViews.setOnClickPendingIntent method.
Then launch a PendingIntent that will be caught by a service, a broadcast receiver or a service. The context targeted by this intent will be able to manipulate the source widget by using RemoteViews again.
Click processing is a bit more complicated as everything that is a stake here is to manipulate UI elements in a remote process.

Categories

Resources