I find a nice layout on
http://thepseudocoder.wordpress.com/2011/10/13/android-tabs-viewpager-swipe-able-tabs-ftw/
I add buttons to the xml fragments but I do not know how to implement the buttons, I mean, I do not know how to do for the buttons work.
Each Fragment will load it's own layout, and the layout itself consists of a few buttons here and there - am I right?
If so, you can retrieve the Buttons into variables. Consider the following:
Button myButtonOne = (Button) myView.findViewById(R.id.button_one);
myButtonOne.setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View view) {
handleButtonClick(view.getId());
}
}
);
private void handleButtonClick(final int resourceId) {
switch( resourceId ) {
case R.id.button_one:
//Do something
break;
case R.id.button_two:
//Do something
break;
case R.id.button_three:
//Do something
break;
case R.id.button_four:
//Do something
break;
case R.id.button_five:
//Do something
break;
default:
break;
}
}
That way, you add your OnClickListeners, and then maintain them via the handleButtonClick(int) method (present in the class).
Related
my app contains a fragment with a webview and a edit text.i have a navigation drawer with 3 choices and use a switch condition to handle selection,on 3 selections i update the same webview with different html files using the foolowing code ..but my app crashes..
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
Fragment fragment;
switch (position) {
case 0:
WebView wv = (WebView) findViewById(R.id.webView);
wv.loadUrl("file:///android_asset/src/web.html"); //app crashes
fragment = getFragmentManager().findFragmentByTag(WebFragment.TAG);
if (fragment == null) {
fragment = new WebFragment();
}
getFragmentManager().beginTransaction().replace(R.id.container, fragment, WebFragment.TAG).commit();
break;
case 1: fragment = getFragmentManager().findFragmentByTag(WebFragment.TAG);
if (fragment == null) {
fragment = new WebFragment();
}
getFragmentManager().beginTransaction().replace(R.id.container, fragment, WebFragment.TAG).commit();
break;
case 2: fragment = getFragmentManager().findFragmentByTag(WebFragment.TAG);
if (fragment == null) {
fragment = new WebFragment();
}
getFragmentManager().beginTransaction().replace(R.id.container, fragment, WebFragment.TAG).commit();
break;
}
}
my logcat logs http://pastebin.com/snbzASTq
please provide a workaround for this
I have a TreeView, with many TreeItem. what i want to do is that i enable modification for the selected TreeItem and disable it for others.
To get all the TreeView to be modifiable i use :
syTree.setEditable(true);
syTree.setCellFactory(TextFieldTreeCell.forTreeView());
}
syTree.setOnEditCommit(new EventHandler<TreeView.EditEvent<String>>() {
#Override
public void handle(TreeView.EditEvent<String> t) {
syTree.getRoot().getChildren().set(syTree.getRow(t.getTreeItem()), new TreeItem<String>(t.getNewValue()));
System.out.println("setOnEditCommit");
//}
}
});
syTree.setOnEditCancel(new EventHandler<TreeView.EditEvent<String>>() {
#Override
public void handle(TreeView.EditEvent<String> t) {
System.out.println("setOnEditCancel");
}
});
This line just change all the TreeItems to TextField when trying to modify :
syTree.setCellFactory(TextFieldTreeCell.forTreeView());
How to do it for a specific TreeItem ?
Any help please ?
I think you are saying you want some cells to be editable, and some not to be editable, depending on some condition on the TreeItem they are displaying. If so, it's possible, you just need to do a little more work with your cell factory:
Callback<TreeView<String>, TreeCell<String>> defaultCellFactory = TextFieldTreeCell.forTreeView();
syTree.setCellFactory((TreeView<String> tv) -> {
TreeCell<String> cell = defaultCellFactory.call(tv);
cell.treeItemProperty().addListener((obs, oldTreeItem, newTreeItem) -> {
if (newTreeItem == null) {
cell.setEditable(false);
} else if ( /* newTreeItem should be editable */) {
cell.setEditable(true);
} else {
cell.setEditable(false);
}
});
return cell ;
});
I would like to display in a toast which network connection is available.
But when I start my app it shows me the layout, but it doesn't make a toast.
Did I forget something?
// Now i shows every second a toast. it beginns with the right one but then it goes to the next one..
I also have a button now, which shows me the networktyp in a textview. But it always just show the 4g..
Thanks in advance for your help!
Button start;
TextView ergebniss;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button)findViewById(R.id.start);
start.setOnClickListener(this);
ergebniss = (TextView) findViewById(R.id.textView1);
}
public void getNetworkClass(Context context) {
TelephonyManager mTelephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
int networkType = mTelephonyManager.getNetworkType();
switch (networkType) {
case TelephonyManager.NETWORK_TYPE_GPRS:
case TelephonyManager.NETWORK_TYPE_EDGE:
case TelephonyManager.NETWORK_TYPE_CDMA:
case TelephonyManager.NETWORK_TYPE_1xRTT:
case TelephonyManager.NETWORK_TYPE_IDEN:
Toast.makeText(getApplicationContext(), "2G", Toast.LENGTH_LONG).show();
ergebniss.setText("2G");
case TelephonyManager.NETWORK_TYPE_UMTS:
case TelephonyManager.NETWORK_TYPE_EVDO_0:
case TelephonyManager.NETWORK_TYPE_EVDO_A:
case TelephonyManager.NETWORK_TYPE_HSDPA:
case TelephonyManager.NETWORK_TYPE_HSUPA:
case TelephonyManager.NETWORK_TYPE_HSPA:
case TelephonyManager.NETWORK_TYPE_EVDO_B:
case TelephonyManager.NETWORK_TYPE_EHRPD:
case TelephonyManager.NETWORK_TYPE_HSPAP:
Toast.makeText(getApplicationContext(), "3G", Toast.LENGTH_LONG).show();
ergebniss.setText("3G");
case TelephonyManager.NETWORK_TYPE_LTE:
Toast.makeText(getApplicationContext(), "4G", Toast.LENGTH_LONG).show();
ergebniss.setText("4G");
}
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
getNetworkClass(this);
}
}
You need to at least show the Toast.
Toast.makeText(getApplicationContext(), "2G", Toast.LENGTH_LONG).show();
And you're missing a break; (or return ..) statement after the Toast.makeText() calls.
Good evening,
I ask myself the following head breaks:
I would like to create a Favorites button that has the two following states:
1st state: "Add to Favorites"
2nd state (on OnClick event): "Remove from favorites"
But I'd also be able to return to the 2nd state: "Add to Favorites" by a 2nd OnClick event ect..
Does anyone have a solution for it with a simple OnClickListener it seems impossible.
I finally solved my problem using a custom checkbox !!Is the best way to use favorite things because you can get the state of your drawable !!
like that :
favoris_button = (CheckBox) findViewById(R.id.star);
favoris_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
favoris_button.setText("Supprimer des categories");
}
else
favoris_button.setText("Ajouter aux favoris");
}
});
Run this code inside your listener or wrap a function around it and pass it the boolean and call it when you need to. Startup, reset or whenever you want to change the state. If the button is clicked you need to flip the state.
private Boolean check;
favoris_button = (Button) findViewById(R.id.promotion_favoris);
favoris_button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
if (check == true)
{
favoris_button.setBackgroundColor(R.drawable.btn_orange9);
favoris_button.setText("Supprimer des favoris");
}
else
{
favoris_button.setBackgroundColor(R.drawable.btn_red9);
favoris_button.setText("Ajouter aux favoris");
}
check ^= true;
}
});
From the examples at Xamarin.com you can build basic M.T. Dialog apps, but how do you build a real life application?
Do you:
1) Create a single DialogViewController and tree every view/RootElement from there or,
2) Create a DialogViewController for every view and use the UINavigationController and push it on as needed?
Depending on your answer, the better response is how? I've built the example task app, so I understand adding elements to a table, click it to go to the 'next' view for editing, but how to click for non-editing? How to click a button, go next view if answer is number 1?
Revised:
There is probably no one right answer, but what I've come up with seems to work for us. Number 2 from above is what was chosen, below is an example of the code as it currently exists. What we did was create a navigation controller in AppDelegate and give access to it throughout the whole application like this:
public partial class AppDelegate : UIApplicationDelegate
{
public UIWindow window { get; private set; }
//< There's a Window property/field which we chose not to bother with
public static AppDelegate Current { get; private set; }
public UINavigationController NavController { get; private set; }
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
Current = this;
window = new UIWindow (UIScreen.MainScreen.Bounds);
NavController = new UINavigationController();
// See About Controller below
DialogViewController about = new AboutController();
NavController.PushViewController(about, true);
window.RootViewController = NavController;
window.MakeKeyAndVisible ();
return true;
}
}
Then every Dialog has a structure like this:
public class AboutController : DialogViewController
{
public delegate void D(AboutController dvc);
public event D ViewLoaded = delegate { };
static About about;
public AboutController()
: base(about = new About())
{
Autorotate = true;
about.SetDialogViewController(this);
}
public override void LoadView()
{
base.LoadView();
ViewLoaded(this);
}
}
public class About : RootElement
{
static AboutModel about = AboutVM.About;
public About()
: base(about.Title)
{
string[] message = about.Text.Split(...);
Add(new Section(){
new AboutMessage(message[0]),
new About_Image(about),
new AboutMessage(message[1]),
});
}
internal void SetDialogViewController(AboutController dvc)
{
var next = new UIBarButtonItem(UIBarButtonSystemItem.Play);
dvc.NavigationItem.RightBarButtonItem = next;
dvc.ViewLoaded += new AboutController.D(dvc_ViewLoaded);
next.Clicked += new System.EventHandler(next_Clicked);
}
void next_Clicked(object sender, System.EventArgs e)
{
// Load next controller
AppDelegate.Current.NavController.PushViewController(new IssuesController(), true);
}
void dvc_ViewLoaded(AboutController dvc)
{
// Swipe location: https://gist.github.com/2884348
dvc.View.Swipe(UISwipeGestureRecognizerDirection.Left).Event +=
delegate { next_Clicked(null, null); };
}
}
Create a sub-class of elements as needed:
public class About_Image : Element, IElementSizing
{
static NSString skey = new NSString("About_Image");
AboutModel about;
UIImage image;
public About_Image(AboutModel about)
: base(string.Empty)
{
this.about = about;
FileInfo imageFile = App.LibraryFile(about.Image ?? "filler.png");
if (imageFile.Exists)
{
float size = 240;
image = UIImage.FromFile(imageFile.FullName);
var resizer = new ImageResizer(image);
resizer.Resize(size, size);
image = resizer.ModifiedImage;
}
}
public override UITableViewCell GetCell(UITableView tv)
{
var cell = tv.DequeueReusableCell(skey);
if (cell == null)
{
cell = new UITableViewCell(UITableViewCellStyle.Default, skey)
{
SelectionStyle = UITableViewCellSelectionStyle.None,
Accessory = UITableViewCellAccessory.None,
};
}
if (null != image)
{
cell.ImageView.ContentMode = UIViewContentMode.Center;
cell.ImageView.Image = image;
}
return cell;
}
public float GetHeight(UITableView tableView, NSIndexPath indexPath)
{
float height = 100;
if (null != image)
height = image.Size.Height;
return height;
}
public override void Selected(DialogViewController dvc, UITableView tableView, NSIndexPath indexPath)
{
//base.Selected(dvc, tableView, path);
tableView.DeselectRow(indexPath, true);
}
}
#miquel
The current idea of a workflow is an app that starts with a jpg of the Default.png that fades into the first view, with a flow control button(s) that would move to the main app. This view, which I had working previous to M.T.D. (MonoTouch.Dialog), which is a table of text rows with an image. When each row is clicked, it moves to another view that has the row/text in more detail.
The app also supports in-app-purchasing, so if the client wishes to purchase more of the product, then switch to another view to transact the purchase(s). This part was the main reason for switching to M.T.D., as I thought M.T.D. would be perfect for it.
Lastly there would be a settings view to re-enable purchases, etc.
PS How does one know when the app is un-minimized? We would like to show the fade in image again.
I have been asking myself the same questions. I've used the Funq Dependency Injection framework and I create a new DialogViewController for each view. It's effectively the same approach I've used previously developing ASP.NET MVC applications and means I can keep the controller logic nicely separated. I subclass DialogViewController for each view which allows me to pass in to the controller any application data required for that particular controller. I'm not sure if this is the recommended approach but so far it's working for me.
I too have looked at the TweetStation application and I find it a useful reference but the associated documentation specifically says that it isn't trying to be an example of how to structure a MonoTouch application.
I use option 2 that you stated as well, it works pretty nicely as you're able to edit the toolbar options on a per-root-view basis and such.
Option 2 is more feasible, as it also gives you more control on each DialogViewController. It can also helps if you want to conditionally load the view.