Android reusable view and OnClickListener - include

I am trying to implement a reusable view and create an OnClickListener that will work for all classes. My code is below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout = (LinearLayout)findViewById(R.id.layout1);
Button button = (Button) findViewById(R.id.layout1).findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.layout1).findViewById(R.id.button2);
Button button3 = (Button) findViewById(R.id.layout1).findViewById(R.id.button3);
applyListener(layout,listener);
applyListener(layout,listener);
applyListener(layout,listener);
}
OnClickListener listener = new OnClickListener() {
public void onClick(View view) {
switch (view.getId()){
case R.id.button1:
Toast.makeText(getApplicationContext(), "ONE",Toast.LENGTH_LONG).show();
Intent intent = new Intent(MainActivity.this, P2.class);
startActivity(intent);
break;
case R.id.button2:
Toast.makeText(getApplicationContext(), "TWO",Toast.LENGTH_LONG).show();
Intent intent2 = new Intent(MainActivity.this, P3.class);
startActivity(intent2);
break;
case R.id.button3:
Toast.makeText(getApplicationContext(), "THREE",Toast.LENGTH_LONG).show();
Intent intent3 = new Intent(MainActivity.this, P4.class);
startActivity(intent3);
break;
}
}
private Context getContext() {
// TODO Auto-generated method stub
return null;
}
};
private static void applyListener(View child, OnClickListener listener) {
if (child == null)
return;
if (child instanceof ViewGroup) {
applyListener((ViewGroup) child, listener);
}
else if (child != null) {
if(child.getId() == R.id.button1) {
child.setOnClickListener(listener);
}
if(child.getId() == R.id.button2) {
child.setOnClickListener(listener);
}
if(child.getId() == R.id.button3) {
child.setOnClickListener(listener);
}
}
}
private static void applyListener(ViewGroup parent, OnClickListener listener) {
for (int i = 0; i < parent.getChildCount(); i++) {
View child = parent.getChildAt(i);
if (child instanceof ViewGroup) {
applyListener((ViewGroup) child, listener);
} else {
applyListener(child, listener);
}
}
}
}
The problem is that once it moves out of the MainActivty, it will crash as soon as I press any button. I am sure it is to do with the line:
Intent intent = new Intent(MainActivity.this, P2.class);
and I think the problem is related to the fact that it is referencing a class that is not active eg MainActivity.this. I have tried replacing MainActivity.this with:
this
and
getContext()
and
getBaseContext()
but they do not work.
I have also tried
Intent intent = new Intent("com.example.APANEL.P2.class");
but this does not work either.
I am sure the applyListener method is being implemented properly as the buttons are all children and are therefore being implemented using the method:
private static void applyListener(ViewGroup parent, OnClickListener listener)
I have read the android documentation and every post I can find, but I cannot seem to find a working solution.
Any help would be greatly apreciated

I solved it myself by doing the following:
In the MainActivity class I created the method:
public void setup(){
LinearLayout layout = (LinearLayout)findViewById(R.id.layout1);
Button button = (Button) findViewById(R.id.layout1).findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.layout1).findViewById(R.id.button2);
Button button3 = (Button) findViewById(R.id.layout1).findViewById(R.id.button3);
applyListener(layout,listener);
}
Then, all classes that re-use the view eg display the button panel must extend MainActivity, and in the onCreate() method, they implement the method setup()
To summarise, it was nothing to do with the Intent, but was to do with the fact that in the subsequent classes, the listeners were not set up.

Related

Intent function while running application get crashed

When clicked on this textview the app get crashed and didn't change the activity it is suppose to switch from main activity to another how to resolve it?
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText etEmail;
private EditText etPassword;
private TextView tvLogin;
private TextView tvSignup;
private Button btnSignin, btMr;
private FirebaseAuth firebaseAuth;
AwesomeValidation awesomeValidation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
awesomeValidation = new AwesomeValidation(ValidationStyle.BASIC);
updateUI();
firebaseAuth = FirebaseAuth.getInstance();
}
private void updateUI() {
etEmail = (EditText) findViewById(R.id.etEmail);
tvSignup = (TextView) findViewById(R.id.tvSignup);
etPassword = (EditText) findViewById(R.id.etPassword);
tvLogin = (TextView) findViewById(R.id.tvLogin);
btnSignin = (Button) findViewById(R.id.btnLogin);
tvSignup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
signup();
}
public void signup() {
Intent i = new Intent(MainActivity.this, Registration.class);
startActivity(i);
}
});
Here I used awesome validation for validating my form but while running the app shows the toast message, but do not register the user to fire base it validate my form but do not register
String regexPassword = "(?=.*[a-z])(?=.*[A-Z])(?=.*[\\d])(?=.*[~`!##\\$%\\^&\\*\\(\\)\\-_\\+=\\{\\}\\[\\]\\|\\;:\"<>,./\\?]).{8,}";
awesomeValidation.addValidation(MainActivity.this, R.id.etEmail, android.util.Patterns.EMAIL_ADDRESS, R.string.etEmailerr);
awesomeValidation.addValidation(MainActivity.this, R.id.etPassword, regexPassword, R.string.etPasserr);
btnSignin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (awesomeValidation.validate()) {
Toast.makeText(MainActivity.this, "Data Recieved Successfully", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "ERROR", Toast.LENGTH_SHORT).show();
}
}
});
}
This part register the user to firebase, but after adding this awesome validation this do not work app shows a toast message from validation and do not register or change activity how can i merge both so that my form get validate and also get register to my firebase auth
public void btnLogin_Click(View v) {
final ProgressDialog progressDialog = ProgressDialog.show(MainActivity.this, "Please Wait....", "Processing...", true);
(firebaseAuth.signInWithEmailAndPassword(etEmail.getText().toString(), etPassword.getText().toString()))
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
progressDialog.dismiss();
if (task.isSuccessful()) {
Toast.makeText(MainActivity.this, "LOGIN SUCCESSFULL", Toast.LENGTH_LONG).show();
Intent signin = new Intent(MainActivity.this, Dashboard.class);
startActivity(signin);
} else {
Log.e("ERROR", task.getException().toString());
Toast.makeText(MainActivity.this, task.getException().getMessage(), Toast.LENGTH_LONG).show();
}
}
});
}
#Override
public void onClick(View view) {
}
}
this is due to the regex function.
the values used in activity1 regex get differ in another activity2 due to which the intent can't switch from one activity to another.
values passed the the regex should be same in all the activity linked.

DisplayAlert With changing Text xamarin forms

I have a requirement where i have to show the status of the download on a DisplayAlert. But with changing text on it asynchronously.
How to achieve this?
DisplayAlert("Download Info", "Downloading.....", "Ok");
I want to show status like...
Connected to server
Downloading
Download Complete
Here is a simple "Dynamic Alert" for Forms and iOS using UIAlertController and Android using a DialogFragment and a Xamarin.Forms dependency service:
Dependency Interface:
public interface IDynamicAlert
{
void Show(string title, string message);
void Update(string message);
void Dismiss();
}
iOS IDynamicAlert Dependency Implementation:
public class DynamicAlert : IDynamicAlert
{
UIAlertController alert;
public void Show(string title, string message)
{
if (alert != null) throw new Exception("DynamicAlert already showing");
alert = UIAlertController.Create(title, message, UIAlertControllerStyle.Alert);
var rootVC = UIApplication.SharedApplication.Windows[0].RootViewController;
rootVC.PresentViewController(alert, true, () =>
{
});
}
public void Update(string message)
{
if (alert == null) throw new Exception("DynamicAlert is not showing, call Show first");
alert.Message = message;
}
public void Dismiss()
{
if (alert == null) throw new Exception("DynamicAlert is not showing, call Show first");
alert.DismissViewController(true, () =>
{
alert.Dispose();
alert = null;
});
}
}
Example Usage:
var alert = DependencyService.Get<IDynamicAlert>();
if (alert != null)
{
alert.Show("StackOverflow", "Starting your request...");
await Task.Delay(2000); // Do some work...
alert.Update("Your request is processing...");
await Task.Delay(2000); // Do some work...
alert.Update("Your request is complete...");
await Task.Delay(750);
alert.Dismiss();
}
else
{
throw new Exception("IDynamicAlert Dependency not found");
}
Output:
Android Version:
The android version consists of a couple of parts, a DialogFragment subclass and the IDynamicAlert implementation that uses the custom DialogFragment.
Android DialogFragment Subclass:
public class DynamicAlertDialogFragment : DialogFragment
{
AlertDialog alertDialog;
readonly Context context;
public static DynamicAlertDialogFragment Instance(Context context, string title, string message)
{
var fragment = new DynamicAlertDialogFragment(context);
Bundle bundle = new Bundle();
bundle.PutString("title", title);
bundle.PutString("message", message);
fragment.Arguments = bundle;
return fragment;
}
public DynamicAlertDialogFragment(Context context)
{
this.context = context;
}
public override Dialog OnCreateDialog(Bundle savedInstanceState)
{
var title = Arguments.GetString("title");
var message = Arguments.GetString("message");
alertDialog = new AlertDialog.Builder(context)
.SetIcon(Android.Resource.Drawable.IcDialogInfo)
.SetTitle(title)
.SetMessage(message)
.Create();
return alertDialog;
}
public void SetMessage(string message)
{
(context as Activity).RunOnUiThread(() => { alertDialog.SetMessage(message);});
}
}
Android IDynamicAlert Dependency Implementation:
public class DynamicAlert : IDynamicAlert
{
const string FRAGMENT_TAG = "DynamicAlert_Fragment";
DynamicAlertDialogFragment fragment;
static FormsAppCompatActivity currentActivity;
public static FormsAppCompatActivity CurrentActivity { set { currentActivity = value; } }
public void Show(string title, string message)
{
if (currentActivity == null) throw new Exception("DynamicAlert.CurrentActivity needs assigned");
var fragMgr = currentActivity.FragmentManager;
var fragTransaction = fragMgr.BeginTransaction();
var previous = fragMgr.FindFragmentByTag(FRAGMENT_TAG);
if (previous != null)
{
fragTransaction.Remove(previous);
}
fragTransaction.DisallowAddToBackStack();
fragment = DynamicAlertDialogFragment.Instance(currentActivity, title, message);
fragment.Show(fragMgr, FRAGMENT_TAG);
}
public void Update(string message)
{
if (fragment == null) throw new Exception("DynamicAlert is not showing, call Show first");
fragment.SetMessage(message);
}
public void Dismiss()
{
if (fragment == null) throw new Exception("DynamicAlert is not showing, call Show first");
fragment.Dismiss();
fragment.Dispose();
fragment = null;
}
}
Android Init / Usage:
When creating the AlertDialog in the DialogFragment we need access to the current Activity and when using Xamarin.Forms, that is normally the MainActivity that is a FormsAppCompatActivity subclass. Thus you will need to initialize the DynamicAlert.CurrentActivity static property with this Activity in your MainActivity.OnCreate subclass:
Example:
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
////////////
DynamicAlert.CurrentActivity = this;
////////////
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
Android Output:

Why can't I access the Assets folder of my Xamarin.Android project this way?

I want to set the Typeface of the TextView to a font in the Assets folder. The problem-code is "var font = Typeface.CreateFromAsset(Assets, "Enter-The-Grid.ttf");," not the first use, but the second one towards the end of my code (the red squiggly line appears under "Assets").
namespace UndergroundSports.Android
{
[Activity]
public class CityPage : Activity
{
Sport[] sports = Sport.Sports;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
this.SetContentView(Resource.Layout.CityPage);
var font = Typeface.CreateFromAsset(Assets, "Enter-The-Grid.ttf");
Button bttJoin = FindViewById<Button>(Resource.Id.bttJoin);
bttJoin.Click += (sender, e) =>
{
gotoJoinPage();
};
bttJoin.Typeface = font;
ListView lstSports = FindViewById<ListView>(Resource.Id.lstSport);
lstSports.Adapter = new SportsAdapter(this, sports);
lstSports.ItemClick += (object sender, AdapterView.ItemClickEventArgs e) =>
{
Sport selectedFromList = sports[e.Position];
Global.Instance.CurrentSport = selectedFromList;
gotoMembersPage();
};
}
private void gotoJoinPage()
{
var intent = new Intent(this, typeof(JoinPage));
StartActivity(intent);
}
private void gotoMembersPage()
{
var intent = new Intent(this, typeof(MembersPage));
StartActivity(intent);
}
public class SportsAdapter : BaseAdapter<Sport>
{
Sport[] items;
Activity context;
public SportsAdapter(Activity context, Sport[] items) : base()
{
this.context = context;
this.items = items;
}
public override long GetItemId(int position)
{
return position;
}
public override Sport this[int position]
{
get { return items[position]; }
}
public override int Count
{
get { return items.Length; }
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if (view == null)
view = context.LayoutInflater.Inflate(global::Android.Resource.Layout.SimpleListItem1, null);
TextView txtView = view.FindViewById<TextView>(global::Android.Resource.Id.Text1);
var font = Typeface.CreateFromAsset(Assets, "Enter-The-Grid.ttf");
txtView.Text = items[position].Name;
txtView.Gravity = GravityFlags.Center;
txtView.Typeface = font;
return view;
}
}
}
}
But when I tried to create a variable containing the font I got an error telling me:
Cannot access a nonstatic member of outer type Android.Content.Context' via nested typeUndergroundSports.Android.CityPage.SportsAdapter' (CS0038) (UndergroundSportsAndroid)"
From looking at related questions I think I need to either create an instance of the Assets object or make it static.
I'm pretty new to C# and don't really understand what's going on. I would appreciate it if someone could explain why I'm unable to access Assets in this part of my code. The part that confuses me the most is that I use the exact same line of code to access the font earlier within the same file without getting that error.
var font = Typeface.CreateFromAsset(context.Assets, "Enter-The-Grid.ttf");
Pass your activity's instance to your adapter via constructor, and use it to access Assests
public class SportsAdapter : BaseAdapter<Sport>
{
Sport[] items;
Activity context;
public SportsAdapter(Activity context, Sport[] items) : base()
{
this.context = context;
this.items = items;
}
....
public override View GetView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if (view == null)
view = context.LayoutInflater.Inflate(global::Android.Resource.Layout.SimpleListItem1, null);
TextView txtView = view.FindViewById<TextView>(global::Android.Resource.Id.Text1);
var font = Typeface.CreateFromAsset(context.Assets, "Enter-The-Grid.ttf");
txtView.Text = items[position].Name;
txtView.Gravity = GravityFlags.Center;
txtView.Typeface = font;
return view;
}
}
Also, make sure your .ttf file's build action is set to AndroidAssests. Right the .tff file > Build Action > AndroidAsset

Cannot resolve symbol 'R' please help me

public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
go
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
First you need have an import for R - but your developer tool must import this dependence automatically. Probably you have an error in other code or some problems with your resources. Watch your error logs.

JavaFX more Scenes

Hi Guys i build a GUI and on this GUI is a Button and when I press the Button a second GUI appears, on the second GUI is also a Button and when i press the Button it goes back
GU1
btn.setOnAction(new EventHandler <ActionEvent>(){
public void handle(ActionEvent arg0) {
try {
new GUI2().start(primaryStage);
} catch (Exception e) {
e.printStackTrace();
}
}
});
My Questions!
Is GUI1 still running when i press the Button?
GUI2
btn.setOnAction(new EventHandler <ActionEvent>(){
public void handle(ActionEvent arg0) {
try {
//back to the main menu
new GUI1().start(primaryStage);
} catch (Exception e) {
e.printStackTrace();
}
}
});
When i press the Button, does it go back to the same instance when beginning the program? Or make it a new Instance witch has the same look, and use it more RAM;
How should it works, when i want to open the second GUI in a external Window
When i press the Button, does it go back to the same instance when beginning the program?
No, a new instance is created based on your code new GUI2().start(primaryStage);. Always remember that thenew keyword ALWAYS creates a new object.
How should it works, when i want to open the second GUI in a external Window?
There are lots of ways to do this.
Method 1
If it happen that you created two applications, both extending the Application class, this method should work.
public class MultiWindowFX {
private static final Logger logger = Logger.getGlobal();
public static class GUI1 extends Application {
private final Button buttonShowGUI2;
private final GUI2 gui2;
public GUI1() {
buttonShowGUI2 = new Button("Show GUI 2");
gui2 = new GUI2();
}
public Button getButtonShowGUI2() {
return buttonShowGUI2;
}
#Override
public void start(Stage primaryStage) throws Exception {
//add an action event on GUI2's buttonShowGUI1 to send front GUI1
gui2.getButtonShowGUI1().setOnAction(gui2ButtonEvent -> {
if (primaryStage.isShowing()) primaryStage.toFront();
else primaryStage.show();
});
//button with action to show GUI 2
buttonShowGUI2.setOnAction(actionEvent -> {
try {
if (gui2.getPrimaryStage() == null) gui2.start(new Stage());
else gui2.getPrimaryStage().toFront();
} catch (Exception ex) {
logger.log(Level.SEVERE, null, ex);
}
});
//set scene and its root
Pane root = new StackPane(buttonShowGUI2);
Scene stageScene = new Scene(root, 400, 250);
//set stage
primaryStage.setScene(stageScene);
primaryStage.centerOnScreen();
primaryStage.setTitle("GUI 1");
primaryStage.show();
}
public static void launchApp(String... args) {
GUI1.launch(args);
}
}
public static class GUI2 extends Application {
private Stage primaryStage;
private final Button buttonShowGUI1;
public GUI2() {
buttonShowGUI1 = new Button("Show GUI 1");
}
public Button getButtonShowGUI1() {
return buttonShowGUI1;
}
public Stage getPrimaryStage() {
return primaryStage;
}
#Override
public void start(Stage primaryStage) throws Exception {
//get stage reference
this.primaryStage = primaryStage;
//set scene and its root
Pane root = new StackPane(buttonShowGUI1);
Scene stageScene = new Scene(root, 400, 250);
//set stage
primaryStage.setScene(stageScene);
primaryStage.centerOnScreen();
primaryStage.setTitle("GUI 2");
primaryStage.show();
}
public static void launchApp(String... args) {
GUI2.launch(args);
}
}
public static void main(String... args) {
GUI1.launchApp(args);
}
}
Method 2
For me, this is the best approach especially if you want window ownership and modality works.
public class GUI1 extends Application {
#Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Show GUI2");
btn.setOnAction(actionEvent -> {
//prepare gui2
Stage gui2Stage = createGUI2();
//set window modality and ownership
gui2Stage.initModality(Modality.APPLICATION_MODAL);
gui2Stage.initOwner(primaryStage);
//show
gui2Stage.show();
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 400, 250);
primaryStage.setTitle("GUI 1");
primaryStage.setScene(scene);
primaryStage.show();
}
private Stage createGUI2() {
Button btn = new Button();
btn.setText("Show GUI1");
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 150);
Stage gui2Stage = new Stage();
gui2Stage.setTitle("GUI 2");
gui2Stage.setScene(scene);
//add an action event to GUI2's button, which hides GUI2 and refocuses to GUI1
btn.setOnAction(actionEvent -> gui2Stage.hide());
return gui2Stage;
}
public static void main(String[] args) {
launch(args);
}
}
...and among other methods. Choose the approach that fits to your requirements.

Resources