Print activity of android studio - android-studio

I want to print my layout activity directly with a button, I don't know is neccesary pass activity to PDF format o directly to print.
Thanksenter image description here

In order to get the root view of your activity as image, do this:
View view = getWindow().getDecorView().findViewById(android.R.id.content);
view.setDrawingCacheEnabled(true);
view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),View. MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
And to print:
PrintHelper photoPrinter = new PrintHelper(this); // Asume that 'this' is your activity
photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT);
photoPrinter.printBitmap("print", bitmap);

Related

Dynamically TextView Or buttons Android Studio

I'm not sure how to do this:
I have a variable that is passed threw an intent to my new activity.
This variable is a Number. That number received threw the intent will be different depending on the user.
So I want to dynamically write buttons or texViews depending on Number variable.
Example : Number = 4;
There is 4 buttons or textviews (with onclick listener each and text written has Button 1, Button 2, et. ).
Example Number = 10;
There is 10 buttons or textviews or etc. (with onclick listeners each).
Not sure how I can approach this problem
You can create new dynamic views something like that:
Button myButton = new Button(this);
myButton.setText("Push Me");
LinearLayout ll = (LinearLayout)findViewById(R.id.yourLinearLayoutId);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
// with addView method you say to your app when you want to add this view inside on your LinearLayout view
ll.addView(myButton, lp);

Windows Phone 8.1 AppBarButton icon with 2 rows of text

I would like to know how to make AppBarButton Icon to has 2 rows of text. I want to make it like in Windows Calendar:
The AppBarButton doesn't display text or arbitrary Xaml in its Icon. It needs to be a symbol from a font, bitmap, or path. For a calendar display like that you'll be best off with a bitmap.
Since you probably don't want to pregenerate 366 icons you can use RenderTargetBitmap to create them on the fly. Assuming "ButtonImageMaster" is a Xaml snippet with the day and month and calendarButton is the AppBarButton:
RenderTargetBitmap rtb = new RenderTargetBitmap();
await rtb.RenderAsync(ButtonImageMaster);
IBuffer pixelBuffer = await rtb.GetPixelsAsync();
string fileName = "calIcon.png";
StorageFile calIconFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync(fileName,CreationCollisionOption.ReplaceExisting);
using (IRandomAccessStream stream = await calIconFile.OpenAsync(FileAccessMode.ReadWrite))
{
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
encoder.SetPixelData(
BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Straight,
(uint)rtb.PixelWidth,
(uint)rtb.PixelHeight,
DisplayInformation.GetForCurrentView().LogicalDpi,
DisplayInformation.GetForCurrentView().LogicalDpi,
pixelBuffer.ToArray());
await encoder.FlushAsync();
}
BitmapIcon icon = new BitmapIcon();
icon.UriSource = new Uri("ms-appdata:///temp/"+fileName);
calendarButton.Icon = icon;
On WP 8.1 Silverlight, you can useWriteableBitmap(FrameworkElement element, Transform transform) constructor of WriteableBitmap (which is derived from ImageSource) to generate a 'screenshot of memory-rendered framework element'. Then you can set it to Bitmap Icon.

Add text label to WPF Bing Maps

Is it possible to add text to specified location (lat/long) in WPF bing maps?
Yes, c# code-behind I'll assume?
// I just created a Location object from the mouseclick but you can replace labelLocation with anything
Microsoft.Maps.MapControl.WPF.Location labelLocation = myMap.ViewportPointToLocation(mousePosition);
// Create a label
Label customLabel = new Label();
customLabel.Content = "Text here";
// With map layers we can add WPF children to lat long (WPF Location obj) on the map.
MapLayer labelLayer = new MapLayer();
labelLayer.AddChild(customLabel, labelLocation );
myMap.Children.Add(labelLayer);

lwuit text area

I developed an RSS Application for two XML files and displayed it on two LWUIT Tabs. The problem is with my LWUIT TextArea, whenever I click on my ListForm (it contains titles from the RssFile), I need to display description information from the RSS File. First time I am able to display the description related to the title clicked in ListForm. If I click the ListForm the next time onwards I am able to display the same description again and again in the textarea..(Eventhough I am getting Related Description from RssFile)
Here is my Code:
private void displayCompleteNewsScreen(News detailNews) {
Label title = new Label(detailNews.getTitle());
form2.setTitleComponent(title);
String Description = detailNews.getDescription();
System.out.println("Description" + Description);//Here i am able to get different Description values Related to myList Screen but in text area it is displaying First one always
big = new TextArea();
big.setEditable(false);
big.setText(Description);
form2.addComponent(pubDate);
form2.addComponent(big);
form2.show();
}
As you are reusing form2 instance you should clear it in displayCompleteNewsScreen method. Call removeAll before calling setTitleComponent.
And don't forget to set form2 Commands again in displayCompleteNewsScreen.

How to assign a string id to UI elements.

I'm developing an app on android and I am generating UI elements in a loop. But I need these elements to have an id with letters and numbers, for example "rl1" or "rl2". I was trying to use the method RelativeLayout.setId() but, that method only accepts int. Is there a way I can set an ID as I want without being limited to numbers?
Thanks.
Here is the code I am trying to make work.
for (int i=1; i < 10; i++)
{
//gets the frameview where the elements will be created.
String LinearLayoutId = "frameview1";
int resID = getResources().getIdentifier(LinearLayoutId, "id", "com.myapp.ERS");
LinearLayout linearLayout = (LinearLayout)findViewById(resID);
//creates the RelativeLayout that will hold the ImageIcon and the TextView
RelativeLayout rl = new RelativeLayout(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,40 );
rl.setLayoutParams(lp);
rl.setId("rl"); /// >>>> I would like here to set and ID of "rl1" for example.
rl.setBackgroundDrawable(getResources().getDrawable(R.drawable.bk36));
//creates the image icon within the layout at the left side
ImageView image = new ImageView(this);
lp = new RelativeLayout.LayoutParams(
40,RelativeLayout.LayoutParams.MATCH_PARENT );
image.setLayoutParams(lp);
String imageicon = "icon_"+i;
resID = getResources().getIdentifier(imageicon, "drawable", "com.myapp.ERS");
image.setImageDrawable(getResources().getDrawable(resID)); //sets the icon
rl.addView(image); //adds the ImageView to the relative layout
//creates the TextView within the layout with a 40 margin to the left
TextView tv = new TextView(this);
lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT );
lp.setMargins(40, 0, 0, 0);
tv.setLayoutParams(lp);
String textViewID = "tv"+i;
resID = getResources().getIdentifier(textViewID, "string", "com.myapp.ERS");
tv.setText(getResources().getString(resID));
tv.setTextColor(Color.BLACK);
tv.setTextSize(25);
rl.addView(tv);//adds the TextView to the relative layout
rl.setOnClickListener(mAddListener);
linearLayout.addView(rl);//adds the RelativeLayout to the LinearLayout
}
and then I have the OnCLickListener like this...
private OnClickListener mAddListener = new OnClickListener()
{
public void onClick(View v){
Intent intent;
Bundle bundle;
String id = getResources().getResourceEntryName(v.getId());
id = id.replaceAll("\\D+","");
int value = Integer.parseInt(id);
intent = new Intent(ERS.this, ShowInfo.class);
bundle = new Bundle();
bundle.putInt("key", value);
System.out.println(v.getId());
intent.putExtras(bundle);
startActivity(intent);
}
};
I have tried to set up numeric IDs, but then when I Look for them with:
String id = getResources().getResourceEntryName(v.getId());
It can't find them.
I had all of this in an xml file to begin with, but it was really long because there are about forty items in the list, and it was complicated for me to go and change a letter for example in all of them. I came up with this idea to generate them at runtime in a for loop. I am testing in the meantime with ten, but I can't get it to work.
If I am doing something incorrect, then pardon me, but I am new to this.
You may still find it easier to go back to XML layouts and use the R class to generate meaningful IDs. Although as you haven't included the original xml file you refer to at the end of the question, so I can only guess at the problem you had with it. It does seem to fit the bill though, and would allow you to create something along the lines of:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/hellotextview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Hi there"/>
The android:id="#+id/hellotextview" generates an id that can be used elsewhere in your project. In your java code you could access that specific TextView with something similar to:
TextView helloText = (TextView) findViewById(R.id.hellotextview);
The R.id.hellotextview is a int automatically generated when the project is built (in gen/R.java), but as you get to pick the name you can assign them something relevant to you and your project. So instead of trying to use strings values such as "rl1" and "rl2" that you mentioned, you could use R.id.rl1 and R.id.rl2.
As well as individual UI elements, you can also use the same technique for strings (in res/values/strings.xml), and other resources stored under the project's res/ folder, such as icons, media files, etc. In the case of strings you would access them getString(R.string.some_name_given_by_you);
See Accessing Resources at the Android Developers site for more info.
Why dont you try using SharedPreferences as an alternative in case you want to access the elements which you give some ID elsewhere in some other activity.

Resources