Android - Capture ScreenShot Programmatically without title bar - android

I am developing an app in that I want to take a screen shot of current screen but with out title bar. I know the code to Capture a ScreenShot, but unable to customize it.
Code:
munchscreen.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
View v1 = L1.getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bm = v1.getDrawingCache();
BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
image = (ImageView) findViewById(R.id.screenshots);
image.setBackgroundDrawable(bitmapDrawable);
}
});
When I click on Clip then it should capture a screen shot of the screen shown below but should not include the part which I have circled..
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:background="#D8D5AA"
>
<LinearLayout
android:id="#+id/clip_from_web_linearlayout1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/header"
android:orientation="horizontal"
>
<TextView
android:id="#+id/clip_from_web_textview_back"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Back"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:textColor="#FFFFFF"
android:textSize="18sp"
/>
<TextView
android:id="#+id/clip_from_web_textview_header_title"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Clip From Web"
android:layout_gravity="center"
android:layout_marginLeft="60dp"
android:textColor="#FFFFFF"
android:textSize="18sp"
/>
<TextView
android:id="#+id/clip_from_web_textview_clip"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Clip"
android:layout_gravity="center"
android:layout_marginLeft="60dp"
android:textColor="#FFFFFF"
android:textSize="18sp"
/>
</LinearLayout>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/munch"
android:id="#+id/munchscreen"
android:layout_below="#+id/clip_from_web_linearlayout1"
/>
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="#+id/screenshots"
android:layout_below="#+id/munchscreen"
android:contentDescription="#string/app_name"
android:layout_centerInParent="true"
/>
<RelativeLayout
android:id="#+id/clip_from_web_bottom_bar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#drawable/header" >
<Button
android:id="#+id/clip_from_web_previous"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="4dp"
android:background="#android:drawable/ic_media_previous"
android:layout_marginTop="2dp"
/>
<Button
android:id="#+id/clip_from_web_reload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#android:drawable/ic_menu_rotate"
android:layout_marginTop="5dp"
/>
<Button
android:id="#+id/clip_from_web_next"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_toRightOf="#+id/clip_from_web_reload"
android:layout_marginLeft="102dp"
android:background="#android:drawable/ic_media_next"
android:layout_marginTop="2dp"
/>
</RelativeLayout>
Image before Capturing screen shot..
Image after Capturing screen shot..

Take this to another Linear Layout
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/munch"
android:id="#+id/munchscreen"
/>
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="#+id/screenshots"
android:contentDescription="#string/app_name"
/>
</LinearLayout>
Now find Id for this Linear Layout on oncreate() method making it global variable
LinearLayout ll;
ll = (LinearLayout)findViewById(R.id.linear);
now capture this screen
ll.setDrawingCacheEnabled(true);
ll.buildDrawingCache(true);
Bitmap cs = Bitmap.createBitmap(ll.getDrawingCache());
ll.setDrawingCacheEnabled(false);
Now set this bitmap to your ImageView.

try this...
munchscreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View contentView = getWindow().getDecorView().findViewById(android.R.id.content);
contentView.setDrawingCacheEnabled(true);
contentView.buildDrawingCache();
Bitmap drawingCache = contentView.getDrawingCache();
BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), drawingCache);
image = (ImageView) findViewById(R.id.screenshots);
image.setBackgroundDrawable(bitmapDrawable);
}
});

Hey friends I got the solution of above problem.
Follow below steps:-
activity_main.xml
<RelativeLayout
android:id="#+id/toolbar_rl"
android:layout_width="match_parent"
android:layout_height="?android:actionBarSize"
android:background="#color/header_color"
android:visibility="visible">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="0"
tools:ignore="UselessParent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:contentDescription="#string/app_name"
android:padding="#dimen/_8sdp"
android:src="#drawable/ic_back_24" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="#dimen/_10sdp"
android:layout_marginStart="#dimen/_20sdp"
android:layout_weight="1"
android:gravity="center"
android:text="#string/one_time"
android:textColor="#color/white"
android:textSize="#dimen/_13sdp"
tools:ignore="RtlHardcoded" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:contentDescription="#string/app_name"
android:padding="#dimen/_8sdp"
android:src="#drawable/ic_menu_icon" />
</LinearLayout>
</RelativeLayout>
<ScrollView
android:id="#+id/tabScroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1">
<ImageView
android:layout_width="fill_parent"
android:layout_height="210dp"
android:adjustViewBounds="true"
android:background="#drawable/otp_background"
android:contentDescription="#string/app_name"
android:scaleType="fitXY">
</ImageView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_16sdp"
android:background="#fff"
android:gravity="center"
android:text="Enter Unique Identification Number"
android:textColor="#color/header_color"
android:textSize="#dimen/_16sdp" />
<LinearLayout
android:id="#+id/layout_otp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="#dimen/_26sdp"
android:gravity="top|center"
android:orientation="horizontal">
<EditText
android:id="#+id/editTextone"
android:layout_width="#dimen/_40sdp"
android:layout_height="#dimen/_42sdp"
android:inputType="number"
android:maxLength="1"
android:nextFocusRight="#+id/editText_two"
tools:ignore="LabelFor">
<requestFocus />
</EditText>
<EditText
android:id="#+id/editTexttwo"
android:layout_width="#dimen/_40sdp"
android:layout_height="#dimen/_42sdp"
android:inputType="numberDecimal"
android:maxLength="1"
tools:ignore="LabelFor" />
<EditText
android:id="#+id/editTextthree"
android:layout_width="#dimen/_40sdp"
android:layout_height="#dimen/_42sdp"
android:inputType="numberDecimal"
android:maxLength="1"
tools:ignore="LabelFor" />
<EditText
android:id="#+id/editTextfour"
android:layout_width="#dimen/_40sdp"
android:layout_height="#dimen/_42sdp"
android:inputType="numberDecimal"
android:maxLength="1"
tools:ignore="LabelFor" />
<EditText
android:id="#+id/editTextfive"
android:layout_width="#dimen/_40sdp"
android:layout_height="#dimen/_42sdp"
android:inputType="numberDecimal"
android:maxLength="1"
tools:ignore="LabelFor" />
</LinearLayout>
<Button
android:id="#+id/btn_submit"
android:layout_width="#dimen/_220sdp"
android:layout_height="#dimen/_40sdp"
android:layout_gravity="center"
android:layout_marginBottom="#dimen/_46sdp"
android:layout_marginLeft="#dimen/_16sdp"
android:layout_marginRight="#dimen/_16sdp"
android:layout_marginTop="#dimen/_26sdp"
android:background="#drawable/editbox_one"
android:text="#string/capture"
android:textAllCaps="false"
android:textColor="#fff"
android:textSize="#dimen/_16sdp"
android:typeface="serif" />
</TableLayout>
</ScrollView>
This xml looks like.
in MainActivity Add some code.
3.1 Declare the Button.
Button button_submit;
3.2 Initialize the button.
button_submit = (Button) findViewById(R.id.btn_submit);
3.3 Set set OnClickListener to this button and Call one method i.e
takeScreenshot();
Note:- When you click on this button that time automatically capture the screen without title bar and it will store in file explore with current date, time and.jpg extension(Ex. Fri May 11 151938 GMT+0530 2018.jpg) and this captured image as a bitmap if you want you can set this bitmap to ImageView .
The Code of takeScreenshot().
private void takeScreenshot() {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
View u = findViewById(R.id.tabScroll);
u.setDrawingCacheEnabled(true);
ScrollView z = (ScrollView) findViewById(R.id.tabScroll);
z.setBackgroundColor(getResources().getColor(R.color.white));
int totalHeight = z.getChildAt(0).getHeight();
int totalWidth = z.getChildAt(0).getWidth();
u.layout(0, 0, totalWidth, totalHeight);
u.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(u.getDrawingCache());
u.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
} catch (Throwable e) {
e.printStackTrace();
}
}
The output be looks like..
I hope it will helpful for you.

Related

Android Take Screeshot Width Is Too Wide

I am using the code below to programatically capture the screen layout of a linear layout, the problem is that the screenshot image is too wide, as if it is a landsacpe image, my activity is potrait where height is > width but for some reason this code captures a screenshot with width > height and it looks like a horizontal rectangle rather than a vertical rectangle. The view are correctly oriented so it definitely capturing in the correct orrientation (potrait) but just too wide with the views further apart and smaller.
private void takeScreenShot() {
ll_screenshot = findViewById(R.id.ll_screenshot);
try {
cacheDir = new File(android.os.Environment.getExternalStorageDirectory(), "store");
if (!cacheDir.exists()) {
cacheDir.mkdirs();
}
ll_screenshot.setDrawingCacheEnabled(true);
ll_screenshot.measure(
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
ll_screenshot.layout(0, 0, ll_screenshot.getMeasuredWidth(), ll_screenshot.getMeasuredHeight());
ll_screenshot.buildDrawingCache(true);
Bitmap bm = Bitmap.createBitmap(ll_screenshot.getDrawingCache());
ll_screenshot.setDrawingCacheEnabled(false); // clear drawing cache
path = new File(android.os.Environment.getExternalStorageDirectory(),"store") + "/screenshot.jpg";
savePic(bm, path);
shareFile();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
public static void savePic(Bitmap b, String strFileName) {
FileOutputStream fos;
try {
fos = new FileOutputStream(strFileName);
b.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void shareFile() {
String file_path = path.toString();
Intent intentShareFile = new Intent(Intent.ACTION_SEND);
File fileWithinMyDir = new File(file_path);
if(fileWithinMyDir.exists()) {
intentShareFile.setType("image/*");
intentShareFile.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+file_path));
intentShareFile.putExtra(Intent.EXTRA_SUBJECT, "Share File...");
intentShareFile.putExtra(Intent.EXTRA_TEXT, "Share File...");
startActivity(Intent.createChooser(intentShareFile, "Share File"));
}
}
screenshot:
Edit (XML):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/bg_menu_h"
>
<LinearLayout
android:id="#+id/ll_screenshot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/bg_menu_h"
android:layout_weight="1"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:background="#drawable/bg_menu_h"
>
<TextView
android:id="#+id/tvItem"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:textStyle="bold"
android:gravity="center"
android:text=""
android:textSize="25sp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:background="#drawable/bg_menu_h"
>
<TextView
android:id="#+id/tvBrandLeft"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="center"
android:text="Sunlight"
android:textSize="25sp"
android:layout_marginLeft="10dp"
android:layout_weight="1"
/>
<TextView
android:id="#+id/tvBrandRight"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="center"
android:text="Sunlight"
android:textSize="25sp"
android:layout_marginLeft="10dp"
android:layout_weight="1"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:background="#drawable/bg_menu_h"
>
<TextView
android:id="#+id/tvSizeLeft"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="center"
android:text="1KG"
android:textSize="25sp"
android:layout_marginLeft="10dp"
android:layout_weight="1"
/>
<TextView
android:id="#+id/tvSizeRight"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="center"
android:text="1KG"
android:textSize="25sp"
android:layout_marginLeft="10dp"
android:layout_weight="1"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/ll_middle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1"
>
<ImageView
android:id="#+id/ivTrophyLeft"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="bottom|left"
android:background="#android:color/transparent"
android:src="#drawable/questionmark"
android:adjustViewBounds="true"
/>
<ImageView
android:id="#+id/ivItemLeft"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom|left"
android:background="#android:color/transparent"
android:src="#drawable/questionmark"
android:adjustViewBounds="true"
android:layout_weight="1"
android:paddingRight="5dp"
/>
<ImageView
android:id="#+id/ivItemRight"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom|left"
android:background="#android:color/transparent"
android:src="#drawable/questionmark"
android:adjustViewBounds="true"
android:layout_weight="1"
android:paddingRight="5dp"
/>
<ImageView
android:id="#+id/ivTrophyRight"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="bottom|left"
android:background="#android:color/transparent"
android:src="#drawable/questionmark"
android:adjustViewBounds="true"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/ll_price"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
>
<TextView
android:id="#+id/tvPriceLeft"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:textStyle="bold"
android:gravity="center"
android:text="0.00"
android:textSize="35sp"
android:layout_weight="1"
/>
<TextView
android:id="#+id/tvPriceDifference"
android:layout_height="70dp"
android:layout_width="70dp"
android:textStyle="bold"
android:gravity="center"
android:text="0.00"
android:textSize="20sp"
android:background="#drawable/roundstar"
android:textColor="#color/white"
/>
<TextView
android:id="#+id/tvPriceRight"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:textStyle="bold"
android:gravity="center"
android:text="0.00"
android:textSize="35sp"
android:layout_weight="1"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/ll_shop"
android:layout_width="match_parent"
android:layout_height="55dp"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/ivShopLeft"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom|left"
android:background="#android:color/transparent"
android:src="#drawable/shop"
android:adjustViewBounds="true"
android:layout_weight="1"
android:paddingRight="5dp"
android:layout_margin="5dp"
/>
<ImageView
android:id="#+id/ivShopRight"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom|right"
android:background="#android:color/transparent"
android:src="#drawable/shop"
android:adjustViewBounds="true"
android:layout_weight="1"
android:paddingLeft="5dp"
android:layout_margin="5dp"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/ll_bottom_sub"
android:layout_width="match_parent"
android:layout_height="20dp"
android:orientation="horizontal"
android:gravity="right"
>
<ImageView
android:id="#+id/ivLogo"
android:layout_height="match_parent"
android:layout_width="20dp"
android:src="#drawable/ic_launcher"
android:background="#android:color/transparent"
android:scaleType="fitEnd"
/>
<TextView
android:id="#+id/tvCompare"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:textStyle="bold|italic"
android:gravity="left|bottom"
android:text="App"
android:textSize="10sp"
android:layout_marginRight="10dp"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/ll_blank"
android:layout_width="match_parent"
android:layout_height="75dp"
android:orientation="horizontal"
>
</LinearLayout>
in your method takeScreenShot() do this change.
private void takeScreenShot() {
ll_screenshot = findViewById(R.id.main);
----
//other code
----
}
// change to R.id.main inplace of R.id.ll_screenshot

Convert inflated layout into a bitmap android

I am inflating a layout and converting it into and then i will share it using the as usual intents.
inflating the layout using -
LayoutInflater inflater2 = LayoutInflater.from(this);
qr_sheetview = inflater2.inflate(R.layout.dummy_layout, mainlayout, false);
I tried three methods -
convert the inflated view into bitmap using this method
qr_sheetview.setDrawingCacheEnabled(true);
qr_sheetview.measure(View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
qr_sheetview.layout(0, 0, qr_sheetview.getMeasuredWidth(), qr_sheetview.getMeasuredHeight());
qr_sheetview.buildDrawingCache(true);
sharedbmap = Bitmap.createBitmap(qr_sheetview.getDrawingCache());
But using this method my layout gets shrinked and overlapped.
width was 1107 height was 448
I am using Framelayout as rootlayout and relative layout for inner views.
Second method i am using is this - >
sharedbmap = Bitmap.createBitmap(qr_sheetview.getWidth(), qr_sheetview.getHeight(), Bitmap.Config.ARGB_8888);
canvas = new Canvas(sharedbmap);
qr_sheetview.draw(canvas);
in this method error occurred : height and width must be > 0
using this also gives me the same error
qr_sheetview.post(new Runnable() {
#Override
public void run() {
sharedbmap = loadBitmapFromView(qr_sheetview);
shareBitmap(adjustOpacity(sharedbmap), "temporary");
}
});
the third method is this - >
public static Bitmap loadBitmapFromView(View v) {
int specWidth = View.MeasureSpec.makeMeasureSpec(0 /* any */, View.MeasureSpec.UNSPECIFIED);
v.measure(specWidth, specWidth);
Log.d("TAG", "height" + v.getMeasuredHeight());
Log.d("TAG", "width" + v.getMeasuredWidth());
Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
v.draw(c);
return b;
}
in this i am getting the same result as in first.
the Layout is ->
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="match_parent"
android:background="#ffffff"
>
<RelativeLayout
android:id="#+id/mainqrlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<RelativeLayout
android:id="#+id/layout001"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:id="#+id/appname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:gravity="center|bottom"
android:text="Some text"
android:textColor="#212121"
android:textStyle="bold"
android:textSize="24sp" />
<ImageView
android:id="#+id/logo"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_centerHorizontal="true"
android:layout_below="#+id/appname"
android:layout_marginTop="10dp"
android:src="#drawable/logomain" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/layout003"
android:layout_below="#+id/layout001">
<ImageView
android:id="#+id/qrimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:visibility="gone" />
<ImageView
android:id="#+id/qr_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone"
card_view:srcCompat="#drawable/kj_qr_logo_svg" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/layout003"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<TextView
android:id="#+id/vpaname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="kdvkjadsfkasmd"
android:textColor="#212121" />
<TextView
android:id="#+id/qr_vpa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/vpaname"
android:layout_centerHorizontal="true"
android:text="skdmfkjasdf"
android:textColor="#444444" />
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/qr_vpa"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:gravity="center"
android:padding="10dp"
android:text="ckasfdowmdmwkmdwkdmkwmdklmekld \n sdafasdfasdfaasdf"
android:textColor="#727272" />
<TextView
android:id="#+id/companyname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text3"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:layout_marginTop="20dp"
android:gravity="bottom"
android:text="#string/companyname"
android:textColor="#727272"
android:textSize="10sp" />
</RelativeLayout>
</RelativeLayout>
</FrameLayout>

Circular image in imageview from it's ID

I need to make the image that I get from database circular
I have made this code but I don't know how to complete it to make the image circular
please help
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
name = (TextView) findViewById(R.id.names);
date = (TextView) findViewById(R.id.dates);
Story = (TextView) findViewById(R.id.story);
im = (ImageView) findViewById(R.id.img_car);
im.setScaleType(ImageView.ScaleType.FIT_XY);
//ScrollView scroller = new ScrollView(this);
//scroller.addView(Story);
Bundle extras = getIntent().getExtras();
int idf = extras.getInt("index");
Book selectedBook;
db = new SQLhelper(getApplicationContext());
// read the book with "id" from the database
selectedBook = db.readBook(++idf);
name.setText(selectedBook.getTitle());
date.setText(selectedBook.getdate());
Story.setText(selectedBook.getstory()+"\n \n ");
pname = selectedBook.getTitle();
test = selectedBook.getimage();
int id = getResources().getIdentifier("com.example.tarekhy:drawable/" + test, null, null);
im.setImageResource(id);
}
and this is xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="8dp"
android:background="#drawable/customborder">
<ImageView
android:id="#+id/img_car"
android:layout_width="70dp"
android:layout_height="50dp"
android:layout_marginBottom="5dp"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:contentDescription="#null"
android:gravity="center_vertical"
android:onClick="viewbig"
/>
<RelativeLayout
android:id="#+id/imgs"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" >
<LinearLayout
android:id="#+id/layout_car_model"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="horizontal" >
<TextView
android:id="#+id/names"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
android:text="gfhbj"/>
</LinearLayout>
<LinearLayout
android:id="#+id/layout_car_color"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/layout_car_model"
android:orientation="horizontal" >
<TextView
android:id="#+id/dates"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="italic"
/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
<ScrollView android:id="#+id/textAreaScroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_x="0px"
android:layout_y="25px"
android:scrollbars="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_below="#+id/fbedittext"
android:layout_marginTop="22dp"
android:id="#+id/story"
android:textStyle="bold"
android:layout_marginBottom="20px"
/>
</ScrollView>
"
</LinearLayout>
I think that I need to make a style
I tried to do this but I failed
Help please
You Need CircularImageView From CircularImageView
in xml file You have To Replace this code instead of Imageview :-<de.hdodenhof.circleimageview.CircleImageView
android:id ="#+id/img_car"
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_centerInParent="true"
app:border_width="2dp"
app:border_color="#color/dark" />
In Activity File Replace This Code instead of Imageview:-
CircleImageView im= (CircleImageView) findViewById(R.id.img_car);

Issue with an icon on Android's PopupWindow

I have an app in which I'm opening a PopupWindow, with a couple of images, a text box and an icon of a trash bin below the tex box.
The problem is when I'm presenting the PopupWindow, sometimes when I'm opening the window the icon seems small, and on other times (most of the times) the icon seems as I defined it.
popUpView = inflater.inflate(R.layout.activity_packpresent, (ViewGroup)ma.findViewById(R.layout.activity_main));
pw = new PopupWindow(
popUpView,
ma.f.getView().getMeasuredWidth(), //400
ma.f.getView().getMeasuredHeight(), //800
true);
pw.setOnDismissListener(new PopupWindow.OnDismissListener() {
#Override
public void onDismiss() {
System.out.println("Dismissed!!");
Handler handler = new Handler();
handler.post(new Runnable() {
#Override
public void run() {
//MainActivity.packButton.setVisibility(View.VISIBLE);
try {
image.recycle();
} catch (Exception e) {
}
ma.handlePopUpDissmiss();
}
});
}
});
trash = (ImageButton)popUpView.findViewById(R.id.trashBin);
trash.getLayoutParams().height = (int)((MainActivity.wantedPicSize)*(3.0/4.0));
trash.getLayoutParams().width = (int)((MainActivity.wantedPicSize)*(3.0/4.0));
pw.showAtLocation(map, Gravity.BOTTOM, 0, 0);
pw.update();
The layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:facebook="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lID">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rID"
android:background="#drawable/back5">
<ImageView
android:layout_width="125px"
android:layout_height="200px"
android:id="#+id/mImageView"
android:background="#drawable/com_facebook_picker_default_separator_color"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_below="#+id/mImageView"
android:layout_marginTop="50dp"
android:editable="false"
android:visibility="visible"
android:layout_alignRight="#+id/imageViewR"
android:layout_alignEnd="#+id/imageViewR"
android:layout_alignLeft="#+id/imageViewL"
android:layout_alignStart="#+id/imageViewL" />
<ImageView
android:layout_width="125px"
android:layout_height="200px"
android:id="#+id/imageViewRR"
android:background="#drawable/com_facebook_picker_default_separator_color"
android:layout_alignTop="#+id/imageViewR"
android:layout_toRightOf="#+id/button" />
<ImageView
android:layout_width="125px"
android:layout_height="200px"
android:id="#+id/imageViewLL"
android:background="#drawable/com_facebook_picker_default_separator_color"
android:layout_alignTop="#+id/imageViewL"
android:layout_toLeftOf="#+id/button" />
<ImageView
android:layout_width="125px"
android:layout_height="200px"
android:id="#+id/imageViewR"
android:background="#drawable/com_facebook_picker_default_separator_color"
android:layout_alignTop="#+id/mImageView"
android:layout_toRightOf="#+id/mImageView" />
<ImageView
android:layout_width="125px"
android:layout_height="200px"
android:id="#+id/imageViewL"
android:background="#drawable/com_facebook_picker_default_separator_color"
android:layout_above="#+id/editText"
android:layout_toLeftOf="#+id/mImageView" />
<GridView
android:layout_width="300dp"
android:layout_height="300dp"
android:id="#+id/gridView"
android:layout_above="#+id/mImageView"
android:layout_centerHorizontal="true"
android:visibility="invisible"
android:layout_marginBottom="10dp"
android:layout_below="#+id/button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_below="#+id/editText"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:visibility="invisible" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="10dp"
android:id="#+id/imageView"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:layout_alignRight="#+id/imageViewRR"
android:background="#drawable/line"
android:layout_alignLeft="#+id/imageViewLL"
android:layout_below="#+id/gridView" />
<Button
android:layout_width="wrap_content"
android:layout_height="20dp"
android:text="New Button"
android:id="#+id/button2"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/imageViewRR"
android:visibility="invisible" />
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/trashBin"
android:layout_below="#+id/editText"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#drawable/trash_bin" />
</RelativeLayout>
</LinearLayout>
You can try to use a .9 patch image which can be generated here: .9 patch generator
Or you can use any of these icon generators here and see if that works: Android Asset Studio
Both links are very useful.

Android, trying to get buttons to be on buttom of screen

I'm trying to make a simple app to show pictures. The images get scaled to fit the screen then there is a next and prev button below the image. I would like the next and prev buttons to be at the button, but they keep getting drawn at the button of the image. Thus they move up and down, depending how big the picture is, each time you display a new image.
I found the following solution here, but it is not working for me:
android:gravity="bottom"
android:layout_alignParentBottom="true"
This is the xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/backFeetGallery"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/viewimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/background" />
<LinearLayout
android:id="#+id/linearLayout4"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:gravity="bottom"
android:orientation="horizontal" >
<Button
android:id="#+id/butLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12px"
android:text=" Left "
android:textColor="#ff0000ff" />
<Button
android:id="#+id/butFavrest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12px"
android:text=" Favrets "
android:textColor="#ff0000ff" />
<Button
android:id="#+id/butEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12px"
android:text=" email "
android:textColor="#ff0000ff" />
<Button
android:id="#+id/butRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12px"
android:text=" Right "
android:textColor="#ff0000ff" />
</LinearLayout>
</LinearLayout>
source code
public class cFeetView extends cBaseView implements OnClickListener {
cFileNames mFileNames;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.feet);
// add listeners
View mLeft = findViewById(R.id.butRight);
mLeft.setOnClickListener(this);
// add listeners
View mRight = findViewById(R.id.butLeft);
mRight.setOnClickListener(this);
mFileNames=new cFileNames();
mFileNames.Start();
DrawFeet();
}
public void DrawFeet()
{
int screenHeight;
ImageView picImage = (ImageView) findViewById(R.id.viewimage);// R.id.viewpic);
try {
String FileName = "canon20.png";
FileName=mFileNames.Current();
AssetManager assetManager = getAssets();
InputStream inputStream;
inputStream = assetManager.open(FileName);
Bitmap icon = BitmapFactory.decodeStream(inputStream);
int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
screenHeight = getWindowManager().getDefaultDisplay().getHeight();
int bw = icon.getWidth();
int bh=icon.getHeight();
float t = (float) screenWidth / (float) bw;
int iConHeight=(int)((float)bh*t);
picImage.setImageBitmap(icon);
// scale it
picImage.getLayoutParams().width = screenWidth;
picImage.getLayoutParams().height =iConHeight;
Bitmap scaledIcon = Bitmap.createScaledBitmap(icon, screenWidth, iConHeight, false);
} catch (IOException e) {
}
}
// set the top and buttom margins
public void onClick(View v)
{
Intent i;
switch(v.getId())
{
case R.id.butLeft:
mFileNames.Pre();
DrawFeet();
break;
case R.id.butRight:
mFileNames.Next();
DrawFeet();
break;
}
}
} // end class
Instead of LinearLayout, use RelativeLayout in your xml file.
LinearLayout is used only to arrange them in horizontal or vertical directions.
In order to use alignParentBottom you need to use RelativeLayout. And you dont need the gravity attribute.
My suggestion for your particular scenario is that you you have a RelativeLayout and put all your buttons inside a LinearLayout that is aligned at bottom and the ImageView above it.
Psedo:
<RelativeLayout>
<ImageView />
<LinearLayout
android:layout_below="#ImageView_id"
android:layout_alignParentBottom=true>
</LinearLayout>
</RelativeLayout>
Edited with your xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/backFeetGallery"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/viewimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/background" />
<LinearLayout
android:id="#+id/linearLayout4"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_below="#id/viewimage"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:orientation="horizontal" >
<Button
android:id="#+id/butLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12px"
android:text=" Left "
android:textColor="#ff0000ff" />
<Button
android:id="#+id/butFavrest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12px"
android:text=" Favrets "
android:textColor="#ff0000ff" />
<Button
android:id="#+id/butEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12px"
android:text=" email "
android:textColor="#ff0000ff" />
<Button
android:id="#+id/butRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12px"
android:text=" Right "
android:textColor="#ff0000ff" />
</LinearLayout>
</RelativeLayout>
You need to have everything in RelativeLayout. For example, look at this sample code which I made for one button which is aligned to bottom center of screen.
<?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" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Button" />
And here is the complete layout file for your layout
<?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" >
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Button" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Button" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Button" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/button2"
android:text="Button" />
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/button3"
android:text="Button" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:src="#android:drawable/gallery_thumb" />
This is how it looks,

Categories

Resources