display of image requires two clics instead of one - image

A piece of code I created to open an image requires that I click twice on the words to open the image. I don't understand why it doesn't respond to just one click.
In the layout here is the TextView
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="The New Yorker"
android:id="#+id/the_new_yorker"
android:textStyle="italic"
android:textColor="#color/blue"
android:onClick="new_yorker_cartoon"/>
and here is the corresponding function in the java file
public void new_yorker_cartoon(View view) {
final TextView TextGenerique = (TextView) findViewById(R.id.the_new_yorker);
View.OnClickListener monEcouteur = new View.OnClickListener() {
#Override
public void onClick(View v) {
String imageUrl = "http://www.lapasserelle.com/english/l09/imgs/new_yorker.gif";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(imageUrl), "image/gif");
startActivity(intent);
}
};
TextGenerique.setOnClickListener(monEcouteur);
}
What I don't understand is why I need to click twice on the words New Yorker to display the image new_yorker.gif on my phone.

It might be that the first click is to activate the screen and the second to get the response.

Related

How to open content pages through navigation menu in Xamarin native sample project

I am new to Xamarin and building a native app.
I saw many sites explaining the left side navigation menu
but they do not explain how to open other pages.
I wanted to show different content pages on the navigation click.
On one button click show only text.
Other button show gallery.
Please tell me how can I do this.
Thank you in advance!!!
In Xamarin.Android, when you want to navigate to a new layout, you could follow the steps below.
Create the layout in Resource/Layout folder and create acticity.cs in app project
TextLayout.xml
<TextView
android:id="#+id/tv_Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
GralleryLayout.xml
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Add the control in main layout.
<Button
android:id="#+id/btn_TextLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text Layout"/>
<Button
android:id="#+id/btn_GralleryLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btn_TextLayout"
android:text="Grallery Layout"/>
Add the code below to navigate to the activity you want.
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
var btn_TextLayout = FindViewById<Button>(Resource.Id.btn_TextLayout);
btn_TextLayout.Click += Btn_TextLayout_Click;
var btn_GralleryLayout = FindViewById<Button>(Resource.Id.btn_GralleryLayout);
btn_GralleryLayout.Click += Btn_GralleryLayout_Click;
}
private void Btn_GralleryLayout_Click(object sender, System.EventArgs e)
{
var intent = new Intent(this, typeof(GralleryActivity));
StartActivity(intent);
}
private void Btn_TextLayout_Click(object sender, System.EventArgs e)
{
var intent = new Intent(this, typeof(TextActivity));
StartActivity(intent);
}
Open the view of TextLayout and GralleryLayout.
TextActivity.cs
SetContentView(Resource.Layout.TextLayout);
var tv_Text = FindViewById<TextView>(Resource.Id.tv_Text);
tv_Text.Text = "Only the text";
GralleryActvity.cs
var ImageView = FindViewById<ImageView>(Resource.Id.imageView1);
ImageView.SetImageResource(Resource.Drawable.pig);
Result:
You could download from the Android/App1 folder on GitHub for reference.
https://github.com/WendyZang/Test.git

JButton open an image

im designing a program for my school that displays the teachers schedules at the click of a button. I have everything figured out but how to open an image by clicking on the specific teachers name. here is some of my code any help is appreciated. Thanks
`JButton gabster = new JButton("Mrs. Gabster");
gabster.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JPanel gabster = new JPanel();
}
});`
i need to have the button open a new image from my workspace
public void actionPerformed(ActionEvent ae)
{
ImageIcon pic = new ImageIcon("picture.jpg");
JLabel picture = new JLabel(pic);
panel.add(picture);
}
Create a method for when the button is clicked.

custom grid view, with image and checkbox crashing

in MainActivity.java I am reading a list from a txt file then on a show list button I am able to display the list, with list items, images and checkbox using custom grid view. Now with another button click I am trying to get checked item's position so as I can use these selected items. the code I have is giving me a null pointer exception error on . view.getCheckedItemPositions();
MainActivity.java
if(v.getId()==R.id.toast)
{
Toast.makeText(this,"positions" + adapter.getCheckedItemPositions(view), Toast.LENGTH_SHORT).show();
}
}
public class CustomAdapter extends BaseAdapter {
#Override
public View getView(int position, View view, ViewGroup parent) {
LinearLayout layout = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
view = new View(getApplicationContext());
TextView text=new TextView(getApplicationContext());
text.setTextColor(Color.BLACK);
text.setText(content.get(position));
CheckBox checkBox = new CheckBox(getApplicationContext());
checkBox.setChecked(!checkBox.isChecked());
checkBox.setChecked(true);
layout.addView(checkBox);
layout.addView(text);
return layout;
}
public List<Integer> getCheckedItemPositions(GridView view){
SparseBooleanArray checked = new SparseBooleanArray();
checked = view.getCheckedItemPositions();
List<Integer> positions = new ArrayList<>();
int checksize=checked.size();
for(int i=0; i<checksize; i++) {
if (checked.valueAt(i)) {
positions.add(checked.keyAt(i));
}
}
return positions;
}
}
}
Activity_main.xml
<GridView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/gridView"
android:layout_below="#+id/button"
android:layout_alignParentStart="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toast"
android:id="#+id/toast"
android:layout_alignParentTop="true"
android:layout_alignEnd="#+id/gridView"
android:layout_marginEnd="49dp" />
</RelativeLayout>
logcat
--------- beginning of crash
09-18 18:31:49.144 31751-31751/com.example.administrator.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.administrator.myapplication, PID: 31751
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.util.SparseBooleanArray.size()' on a null object reference
at com.example.administrator.myapplication.MainActivity$CustomAdapter.getCheckedItemPositions(MainActivity.java:158)
at com.example.administrator.myapplication.MainActivity.onClick(MainActivity.java:96)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
09-18 18:32:26.135 31751-31758/com.example.administrator.myapplication W/art﹕ Suspending all threads took: 102.748ms
09-18 18:32:27.078 31751-31758/com.example.administrator.myapplication W/art﹕ Suspending all threads took: 78.288ms
I got the answer..
I created a link hash map with string as position of the check box and value as the boolean value at that position. Like
LinkedHashMap mylist = new LinkedHashMap<>();
Now in order to get the status of the checked item and monitor it in runntime we have to set a Listener on checkbox either using
checkBox.setOnCheckedChangeListener();
or //
checkbox.setOnClickListener()
either of these two works.
Earlier I was just setting up the checkbox but not setting up a listener on the check box as above
checkBox.setChecked(!checkBox.isChecked());
checkBox.setChecked(true);
Now with setOnClickListener all I have to do is store the position and boolean value of the checkbox to the LinkHashmap.
checkBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
saveValues("check" + position, true);
Toast.makeText(MainActivity.this,"position: "+getValues("check"+position), Toast.LENGTH_SHORT).show();
}
else {
saveValues("check"+position,false);
}
}
});
Later on I can see the checked items using
public Boolean getValues(String a){
return mylist.get(a).booleanValue();
}
I am really thankful to the contributors of the below post
Android: checkbox listener

Android : change the position of alert dialog icon

I have an alert dialog in android xml file.my alert dialog has an icon in the left side of it.I want to change the position of the icon to the right side.
I dont want to use custum dialog too
like this :
For setting layout direction of alert dialog to RTL you can use OnShowListener method.
after setting title , message , .... use this method.
dialog = alertdialogbuilder.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dlg) {
dialog.getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL); // set title and message direction to RTL
}
});
dialog.show();
a) If you have it as a drawable in the dialog title (TextView), just use drawableRight
android:drawableRight="#drawable/yourIcon"
b) If it is an ImageView/ImageButton in a RelativeLayout, use alignParentRight
android:layout_alignParentRight="true"
c) If it is an ImageView/ImageButton in a LinearLayout, put it after the TextView
<LinearLayout
android:layout_width="300dp"
android:layout_height="300dp">
<TextView
android:text="some text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="#drawable/yourIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Hope this helps you.
All you need is to create a style tag in res/values/styles and write :
<style name="alertDialog LayoutDirection">
<item name="android:layoutDirection">rtl</item>
</style>
and then add this after alert dialog initializing in your activity :
alertDialog.setView(R.style.alertDialog);
for example :
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog.setMessage("حذف بشه؟")
.setCancelable(true);
alertDialog.setPositiveButton("آره", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "yes", Toast.LENGTH_SHORT).show();
}
});
alertDialog.setNegativeButton("نه", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "nooo", Toast.LENGTH_SHORT).show();
}
});
alertDialog.show();
alertDialog.setView(R.style.alertDialog);
Im afraid you have no other choice then customizing.
The common dialogs are pretty static in UI changes. You can set another title, buttons or change the image but you can't reposition them.

Xamarin forms: hiding blue line from selected tab with a CustomTabRenderer for android

I made a customrenderer for showing images in the tabs for the Android platform.
Now i'd like to know how i can remove the bottom line that shows which tab is selected.
Also, where can i get info about creating custom renderers? i looked on youtube but the example with the rounded corners doesn't show much...
namespace Plopsa.Android
{
public class CustomTabRenderer: TabbedRenderer
{
private Activity _activity;
protected override void OnModelChanged(VisualElement oldModel, VisualElement newModel)
{
base.OnModelChanged(oldModel, newModel);
_activity = this.Context as Activity;
}
// May put this code in a different method - was just for testing
public override void OnWindowFocusChanged(bool hasWindowFocus)
{
// Here the magic happens: get your ActionBar and select the tab you want to add an image
ActionBar actionBar = _activity.ActionBar;
if (actionBar.TabCount > 0)
{
ActionBar.Tab tabOne = actionBar.GetTabAt(0);
tabOne.SetIcon(Resource.Drawable.icon_tab1);
ActionBar.Tab tabTwo = actionBar.GetTabAt(1);
tabTwo.SetIcon (Resource.Drawable.icon_tab2);
ActionBar.Tab tabThree = actionBar.GetTabAt(2);
tabThree.SetIcon(Resource.Drawable.icon_tab3);
ActionBar.Tab tabFour = actionBar.GetTabAt(3);
tabFour.SetIcon(Resource.Drawable.icon_tab4);
}
base.OnWindowFocusChanged(hasWindowFocus);
}
}
You can achieve the desired result by setting the Tabbar.axml class 'android:tabIndicatorColor' to the same value as the 'android:background'.
Below is the code showing the desired result with a tab indicator matching the tab background color. This is all contained in the Tabbar.axml file. You can register the tabs in the MainActivitiy.cs.
Tabbar.axml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.TabLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:tabTextAppearance="#style/MyCustomTabText"
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Light"
app:tabTextColor="#color/primaryOrange"
app:tabIndicatorColor="?attr/colorPrimary"
app:tabIndicatorHeight="0dp"
app:tabSelectedTextColor="#color/primaryOrange"
app:tabGravity="fill"
app:tabMode="fixed" />
MainActivity.cs
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.
base.OnCreate(bundle);
Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
Example Result

Resources