I need to load a URL inside my app with the click of an ImageView. I get the app to load until I start trying to assign intents (on the MainActivity.java) to my ImageViews (on activity_main.xml).
I have tried ImageButton vs ImageView; and both come with their own set of issues I can't seem to find answers to. I finally gave up on the imageButton and am now trying my luck at ImageView. All Android components are up-to-date which includes the software itself, the gradle, and plugins.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/mainBackground"
tools:context=".MainActivity">
<ImageView
android:id="#+id/s"
android:layout_width="0dp"
android:layout_height="0dp"
android:clickable="true"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="16dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:visibility="visible"
app:layout_constraintBottom_toTopOf="#+id/y"
app:layout_constraintEnd_toStartOf="#+id/v"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/s"
tools:visibility="visible" />
<ImageView
android:id="#+id/v"
android:layout_width="0dp"
android:layout_height="0dp"
android:clickable="true"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:visibility="visible"
app:layout_constraintBottom_toTopOf="#+id/q"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/s"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/v"
tools:visibility="visible" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.app.sega;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import java.net.URI;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView img_s = (ImageView) findViewById(R.id.s);
img.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("http://southeastgeorgiatoday.com"));
startActivity(intent);
}
});
ImageView img_v = (ImageView) findViewById(R.id.q);
img.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("http://us7.maindigitalstream.com/2780/"));
startActivity(intent);
}
});
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.sega" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<data android:scheme="http" android:host="www.southeastgeorgiatoday.com" />
<data android:scheme="http" android:host="us7.maindigitalstream.com/2780/" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The objective is to open the url of the intent inside the app with the click of the referenced ImageView. But since including this intent script on the MainActivity.java it will not build, let alone install and launch. I have tried it both on the virtual and actual device via Android's Emulator and it will not install on either.
Here is the error message I get from the LogCat during the build process...
C:\Users\webwi\Documents\_MobileApps\SEGA\app\src\main\java\com\app\sega\MainActivity.java:23: error: cannot find symbol
img.setOnClickListener(new View.OnClickListener() {
^
symbol: variable img
location: class MainActivity
C:\Users\webwi\Documents\_MobileApps\SEGA\app\src\main\java\com\app\sega\MainActivity.java:33: error: cannot find symbol
img.setOnClickListener(new View.OnClickListener() {
^
symbol: variable img
location: class MainActivity
2 errors
Also, it might be worth mentioning in both of the intent lines that begin with "img.setOnClickListener..." the 'img' before the '.' is red.
Obviously there is something wrong with my intents, I'm just not sure what? Any advice.
Related
I would like to implement a toolbar in my app, but I don't necessarily want to support versions of android that are older than Oreo.
I have a toolbar, it shows up, it replaced the action bar, but I don't know how to make it act like an action bar and place buttons on it properly.
This is the xml of the activity I'd want the toolbar for:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">
<Toolbar
android:minHeight="?android:attr/actionBarSize"
android:title="Lyrics"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:titleTextColor="#DDDDDD"
android:background="#112244"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="4dp"
android:id="#+id/lyricsToolbar">
</Toolbar>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:minHeight="30px"
android:id="#+id/lyricsListView"/>
</LinearLayout>
And this is in the .cs file:
var lyricsToolbar = FindViewById<Toolbar>(Resource.Id.lyricsToolbar);
SetActionBar(lyricsToolbar);
The toolbar does appear, but I don't know how to properly put buttons on it. All the tutorials and code I've found are putting a menu on an appcompat support library version. I'd like to add an "add lyrics" button to the toolbar on it's right side and nothing else (it would be just a plus sign and if you click it it opens up another activity where adding new lyrics is possible).
Is this a wrong approach?
How should I do this? Should I make the app more backward-compatible, otherwise I can't properly use toolbars?
Any help is appreciated,
Thank you
Do you want to achieve it like following GIF?
If so, you could add this plus icon directly like following layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Toolbar
android:minHeight="?android:attr/actionBarSize"
android:title="Lyrics"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:titleTextColor="#DDDDDD"
android:background="#112244"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="4dp"
android:id="#+id/lyricsToolbar">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/ic_menu_add"
android:layout_gravity="end"
android:id="#+id/Toolbar_add"
/>
</Toolbar>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:minHeight="30px"
android:id="#+id/lyricsListView"/>
</LinearLayout >
And set the click event listener.
[Activity(Label = "#string/app_name", Theme = "#style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
var imageView =FindViewById<ImageView>(Resource.Id.Toolbar_add);
imageView.Click += ImageView_Click;
}
private void ImageView_Click(object sender, System.EventArgs e)
{
var intent=new Intent(this, typeof(LyricsActivity));
StartActivity(intent);
}
If you want to hide the action bar, please do not forget to add the <item name="windowNoTitle">true</item> in styles.xml like following code.
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="windowNoTitle">true</item>
</style>
I can drag the toolbar manually and toolbar and navigation drawer are not working
together, collapsingtoolbar is collapsing a little but its is not merging into appbar. And why are there so many requirements for posting a question here , just adding these lines to meet the error of mostly code in your body
XML containing collapsing toolbar
<!-- TODO: Update blank fragment layout -->
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.NavigationView
android:id="#+id/nav_view1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header"
app:menu="#menu/drawer_view" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="320dp"
android:text="What is PMKVY?"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:textSize="30sp"
android:textStyle="bold"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="217dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="400dp"
android:fontFamily="serif"
android:text="#string/pmkvy_string"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textColor="?android:attr/statusBarColor"
android:textSize="18sp"
android:textStyle="bold"
tools:layout_editor_absoluteX="-1dp"
tools:layout_editor_absoluteY="273dp" />
<Button
android:id="#+id/button6"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="140dp"
android:layout_marginTop="720dp"
android:background="#android:color/black"
android:drawableRight="#drawable/ic_play_circle"
android:text="Watch Video"
android:textAlignment="center"
android:textColor="#android:color/background_light"
android:textStyle="bold"
tools:layout_editor_absoluteX="141dp"
tools:layout_editor_absoluteY="519dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="840dp"
android:text="How to Enroll?"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="910dp"
android:fontFamily="serif"
android:text="#string/enroll_string"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textColor="?android:attr/statusBarColor"
android:textSize="18sp"
android:textStyle="bold"
tools:layout_editor_absoluteX="-1dp"
tools:layout_editor_absoluteY="565dp" />
<Button
android:id="#+id/button7"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView3"
android:layout_centerHorizontal="true"
android:layout_marginTop="150dp"
android:background="#android:color/black"
android:text="Call 1800 102 6000"
android:textAlignment="center"
android:textColor="#android:color/background_light"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="1230dp"
android:gravity="center"
android:text="OR"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:textSize="30sp"
android:textStyle="bold" />
<Button
android:id="#+id/button8"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="160dp"
android:layout_marginTop="1170dp"
android:background="#android:color/black"
android:text="Enroll"
android:textAlignment="center"
android:textColor="#android:color/background_light"
android:textStyle="bold" />
</RelativeLayout>
</ScrollView>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="#+id/appBar"
android:layout_width="match_parent"
android:layout_height="300dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsingToolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|enterAlways"
android:minHeight="20dp"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginStart="25dp"
app:expandedTitleMarginEnd="30dp"
app:title="PMKVY">
<ImageView
android:id="#+id/toolbarImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
android:src="#drawable/pmkvy_logo"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="250dp"
android:background="#color/colorPrimaryDark"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.DrawerLayout>
.java file of xml
package com.example.shrey.myapplication;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
public class ScrollingActivity1 extends AppCompatActivity {
private DrawerLayout drawerlayout1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_activity_scrolling1);
drawerlayout1 = findViewById(R.id.drawer_layout2);
// set status bar color code
if (Build.VERSION.SDK_INT >= 21) {
Window window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(this.getResources().getColor(R.color.material_blue_grey_950));
}
Toolbar toolbar = findViewById(R.id.toolbar1);
setSupportActionBar(toolbar);
ActionBar actionbar = getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setHomeAsUpIndicator(R.drawable.ic_navdrawer);
// <!-- code to change title and its color. -->
actionbar.setTitle(Html.fromHtml("<font color='#ffffff'>PMKVY</font>"));
NavigationView navigationView = findViewById(R.id.nav_view1);
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
// set item as selected to persist highlight
menuItem.setChecked(true);
// close drawer when item is tapped
drawerlayout1.closeDrawers();
// Add code here to update the UI based on the item selected
// For example, swap UI fragments here
return true;
}
});
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
drawerlayout1.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
}
An English font is working in HTML in webview, but the non-English font (Hindi) is not displaying in HTML in webview.
Here is my source code.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.wptrafficanalyzer.webviewcustomfont"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="in.wptrafficanalyzer.webviewcustomfont.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
demo.html
<html>
<head>
<meta http-equiv="content-type" content="text/html;" charset="UTF-8">
<style>
/** Specify a font named "MyFont",
and specify the URL where it can be found: */
#font-face {
font-family: "MyFont";
src: url('file:///android_asset/fonts/BLKCHCRY.TTF');
}
h2 { font-family:"MyFont"}
#font-face {
font-family: "MyFontSD";
src: url('file:///android_asset/fonts/SHREE-DEV7-1076H.TTF');
}
h1 { font-family:"MyFontSD"}
</style>
</head>
<body>
<h2>
Welcome to BLACK CHANCERY FONT
</h2>
<h1>
1234567890
Am{X
</h1>
</body>
</html>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello_world" />
</RelativeLayout>
MainActivity.java
package in.wptrafficanalyzer.webviewcustomfont;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.webkit.WebView;
public class MainActivity extends Activity {
WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Getting reference to WebView of the activity_main layout
mWebView = (WebView) findViewById(R.id.webview);
// Loading an HTML page into webview
mWebView.loadUrl("file:///android_asset/demo.html");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Note:
HTML file demo.html is copied in the assets folder
BLKCHCRY.TTF font file is copied in fonts folder
SHREE-DEV7-1076H.TTF font file is copied in fonts folder
Link to download fonts (SHREE-DEV7-1076H.TTF and BLKCHCRY.TTF)
i have problem with my code. i have created button "sign up", i want when i click on that button a dialog called "dialog_signup appear.
The problem is it doesnt appear. and i always get this error so far
**2>A numeric comparison was attempted on "$(_DeviceSdkVersion)" that evaluates to "" instead of a number, in condition "$(_DeviceSdkVersion) >= 21".
2>Build FAILED.
2>
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
========== Deploy: 0 succeeded, 1 failed, 0 skipped ==========**
Here is my code
MainActivity.cs
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Content.Res;
namespace GameLandPlus
{
[Activity(Label = "GameLandPlus", MainLauncher = true, Icon = "#drawable/icon")]
public class MainActivity : Activity
{
private Button mbtnSignup;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
mbtnSignup = FindViewById<Button>(Resource.Id.btnsignup);
mbtnSignup.Click += (object sender, EventArgs args) =>
{
FragmentTransaction transaction = FragmentManager.BeginTransaction();
dialog_signup signupDialog = new dialog_signup();
signupDialog.Show(transaction, "dialog fragment");
};
}
}
}
-->
main.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/background_light"
android:weightSum="100"
android:minWidth="25px"
android:minHeight="25px">
<ImageView
android:src="#drawable/images"
android:layout_width="match_parent"
android:layout_weight="20"
android:layout_height="0dp"
android:id="#+id/imageView1" />
<TextView
android:text="Create Account"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_weight="20"
android:layout_height="0dp"
android:textColor="#000000"
android:id="#+id/createaccount"
android:textSize="35sp"
android:gravity="center"
android:textStyle="bold" />
<Button
android:layout_width="match_parent"
android:layout_weight="10"
android:layout_height="0dp"
android:gravity="center"
android:id="#+id/signin"
android:textColor="#FFFFFF"
android:background="#drawable/butto1"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:text="Sign in"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5" />
<TextView
android:text="or"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_weight="15"
android:layout_height="0dp"
android:id="#+id/or"
android:textColor="#000000"
android:textSize="35sp"
android:gravity="center"
android:textStyle="bold" />
<Button
android:text="Sign Up"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:id="#+id/btnsignup"
android:textColor="#FFFFFF"
android:background="#drawable/butto1"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:shadowColor="#00ffffff"
android:shadowRadius="5"
android:drawableRight="#drawable/goodmess" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="35"
android:minHeight="15px"
android:minWidth="15px">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar1"
android:background="#drawable/progressbar"
android:indeterminate="true"
android:layout_centerInParent="true" />
</RelativeLayout>
</LinearLayout>
-->
the dialog page i want to pop up when i click on the button
dialogSignup.axml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#F1F3F4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minHeight="300dp"
android:minWidth="300dp">
<EditText
android:inputType="textPersonName"
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="#+id/Firstname"
android:background="#drawable/editstyle"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="25dp"
android:hint="Username"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#000" />
<EditText
android:layout_below="#+id/Firstname"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="#+id/textEmail"
android:background="#drawable/editstyle"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:hint="Email/Phone Number"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#000" />
<EditText
android:layout_below="#+id/textEmail"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="#+id/password"
android:background="#drawable/editstyle"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:hint="Password"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#000" />
<Button
android:layout_below="#+id/password"
android:text="Sign Up"
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="#+id/btnDialogEmail"
android:textColor="#FFFFFF"
android:background="#drawable/butto1"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:shadowColor="#00ffffff"
android:shadowRadius="5"
android:drawableRight="#drawable/goodmess" />
</RelativeLayout>
-->
and the dialog_signup.cs
using Android.Views;
using Android.Widget;
namespace GameLandPlus
{
class dialog_signup:DialogFragment
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState)
{
base.OnCreateView(inflater, container, saveInstanceState);
var view = inflater.Inflate(Resource.Layout.dialogSignup, container, false);
return view;
}
public override void OnActivityCreated(Bundle savedInstanceState)
{
Dialog.Window.RequestFeature(WindowFeatures.NoTitle);//set title
base.OnActivityCreated(savedInstanceState);
Dialog.Window.Attributes.WindowAnimations = Resource.Style.dialog_animation;
}
}
}
-->
i do not include the slideup/slideright/butto1 because is the drawable file.
Please can u check on it.
I already change emulator so far. but i dont know where is the problem.
Any suggestion?
i'm beginning with JavaFX.
I'm testing to create a FXML project and trying to display a context menu when i click into a row of the tableview, but i canĀ“t see my context menu.
My code is:
package fxmltableview;
import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
import javafx.beans.binding.Bindings;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
/**
*
* #author aCedano
*/
public class FXMLTableViewController implements Initializable {
#FXML private TableView<Person> tableView;
#FXML private TableColumn<Person, String> firstNameColumn;
#FXML private TableColumn<Person, String> lastNameColumn;
#FXML private TableColumn<Person, String> testColumn;
#FXML private ContextMenu resultadoContextMenu;
Connection con;
private DBClass objDbClass;
private ObservableList<Person> data;
#Override
public void initialize(URL location, ResourceBundle resources) {
assert tableView != null : "fx:id=\"tableView\" was not injected: check your FXML file...";
firstNameColumn.setCellValueFactory(new PropertyValueFactory<>("test"));
lastNameColumn.setCellValueFactory(new PropertyValueFactory<>("firstName"));
testColumn.setCellValueFactory(new PropertyValueFactory<>("lastName"));
objDbClass = new DBClass();
try{
con = objDbClass.getConnection();
buildData();
tableView.setRowFactory(
(TableView<Person> tableView1) -> {
final TableRow<Person> row = new TableRow<>();
final ContextMenu rowMenu = new ContextMenu();
MenuItem editItem = new MenuItem("Edit");
MenuItem removeItem = new MenuItem("Delete");
removeItem.setOnAction((ActionEvent event) -> {
tableView1.getItems().remove(row.getItem());
});
rowMenu.getItems().addAll(editItem, removeItem);
row.contextMenuProperty().bind(
Bindings.when(Bindings.isNotNull(row.itemProperty()))
.then(rowMenu)
.otherwise((ContextMenu)null));
return row;
});
tableView.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent e) -> {
if (e.getButton() == MouseButton.SECONDARY)
resultadoContextMenu.show(tableView, e.getScreenX(), e.getScreenY());
});
}
catch(ClassNotFoundException | SQLException ce){
}
}
//Populate table
public void buildData(){
data = FXCollections.observableArrayList();
try{
String SQL = "Select * from tbltest Order By test";
ResultSet rs = con.createStatement().executeQuery(SQL);
while(rs.next()){
Person cm = new Person();
cm.setFirstName(rs.getString("test"));
cm.setLastName(rs.getString("nombre"));
cm.setTest(rs.getString("apellido"));
data.add(cm);
}
tableView.setItems(data);
tableView.getItems();
}
catch(SQLException e){
System.out.println("Error on Building Data");
}
}
}
I'm using Scene builder and my FXML file is:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.paint.*?>
<?import javafx.geometry.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.cell.*?>
<?import javafx.collections.*?>
<?import fxmltableview.*?>
<!--/*<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" fx:controller="fxmltableview.FXMLDocumentController">
<children>
<Button layoutX="126" layoutY="90" text="Click Me!" onAction="#handleButtonAction" fx:id="button" />
<Label layoutX="126" layoutY="120" minHeight="16" minWidth="69" fx:id="label" />
</children>
</AnchorPane>*/-->
<GridPane alignment="CENTER" hgap="10.0" prefHeight="689.0" prefWidth="710.0" vgap="10.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="fxmltableview.FXMLTableViewController">
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
<children>
<Button mnemonicParsing="false" text="Button" GridPane.rowIndex="1">
<effect>
<DropShadow />
</effect>
</Button>
<Label style="-fx-font: NORMAL 20 Tahoma;" text="Address Book" textOverrun="CENTER_ELLIPSIS" GridPane.rowIndex="2">
<font>
<Font name="Gill Sans MT Bold" size="13.0" />
</font>
<textFill>
<LinearGradient endX="1.0" endY="1.0" startX="0.5769230769230769">
<stops>
<Stop color="BLACK" />
<Stop color="WHITE" offset="1.0" />
</stops>
</LinearGradient>
</textFill></Label>
<TableView fx:id="tableView" prefHeight="265.0" prefWidth="502.0" tableMenuButtonVisible="true" GridPane.columnIndex="0" GridPane.rowIndex="3">
<columns>
<TableColumn fx:id="firstNameColumn" prefWidth="100" text="First Name">
<cellValueFactory>
<PropertyValueFactory property="firstName" />
</cellValueFactory>
<contextMenu>
<ContextMenu fx:id="resultadoContextMenu">
<items>
<MenuItem mnemonicParsing="false" text="Unspecified Action" />
</items>
</ContextMenu>
</contextMenu>
</TableColumn>
<TableColumn fx:id="lastNameColumn" prefWidth="100" text="Last Name">
<cellValueFactory>
<PropertyValueFactory property="lastName" />
</cellValueFactory>
</TableColumn>
<TableColumn fx:id="testColumn" prefWidth="300" text="Email Address">
<cellValueFactory>
<PropertyValueFactory property="test" />
</cellValueFactory>
</TableColumn>
</columns>
<items>
<FXCollections fx:factory="observableArrayList">
<!--/* <Person email="jacob.smith#example.com" firstName="Jacob" lastName="Smith" />
<Person email="isabella.johnson#example.com" firstName="Isabella" lastName="Johnson" />
<Person email="ethan.williams#example.com" firstName="Ethan" lastName="Williams" />
<Person email="emma.jones#example.com" firstName="Emma" lastName="Jones" />
<Person email="michael.brown#example.com" firstName="Michael" lastName="Brown" />/*-->
</FXCollections>
</items>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
</children>
<columnConstraints>
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
<RowConstraints />
<RowConstraints />
<RowConstraints />
</rowConstraints>
</GridPane>
You have null condition to show your context menu. Add data to your tableView and click on row. (I think your ResultSet is empty)
tableView.setItems(data);