I want my app to provide a function to add a button like described by Avadhani Y in How to create Button Dynamically in android?
I copied this code and the button appears when I press the +-symbol but it isn't permanent. When I close the app and open it again, the button disappears.
For a minimal working example I
created a Blank Android Application Project set up by Eclipse
added part of the method described in How to create Button Dynamically in android? to MainActivity.java (The second case with the minusbutton generates several error messages, so I left it out.):
public void onClick(View v){
switch(v.getId()){
case (R.id.plusbutton):
Button myButton = new Button(this);
myButton.setText("Add Me");
LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);
break;
}
}
imported the necessary packages. Actually I'm not sure whether I chose the right one for the line with the LayoutParams - eclipse listed several packages - I chose android.view.ViewGroup.LayoutParams
changed the activity_main.xml file to the one below.
added some strings to the strings.xml file
My activity_main.xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/buttonlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.button_test.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/plusbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="#string/plus" />
</LinearLayout>
The problem is that you can't save to resources such as layout files at runtime. So, your options (AFAIK) are to save the params and attributes of the Button into a db or other persistent storage then check at startup each time to recreate the Button. This would be very cumbersome, I would imagine, and very error-prone if not careful.
Another option, if there is a finite number of Buttons available to be made, is to simply create them in your xml then use the visibility attribute to change from gone/invisible to visible when needed.
Android is "stateless" - in other words, unless you "save" changes, they are discarded. What you need to do is include a way to persist any changes that you expect to survive beyond the user's immediate interaction with your app. Also, you should assume that any change might be discarded immediately afterward - in other words, don't "wait" to save changes. If the user's phone rings, your app might go to the background and get killed before onPause or onStop or onDestroy are ever called.
Look into saving a "flag" for the visibility of the button into SharedPreferences and then read the flag in onCreate or onStart or onResume. It's likely you could use any of these, but you will find that sometimes one or another works best (or one doesn't work right at all).
Related
This is my keyboard zoomin view when i click the edit text.
Problem is:
It is way too big only one small strip of edit text is enough.
Keyboard sometimes floating sometimes docking dont know what controls it.
No need of that finish button and green line
What is the callback function once the input in done
This is the method request soft keyboard popup
public void editText(){
Toast.makeText(mContext, "Editing Text",Toast.LENGTH_SHORT).show();
InputMethodManager imm = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
mEditText.setVisibility(VISIBLE);
mEditText.requestFocus();
mEditText.setText(mText);
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
}
Properties of edit text
<EditText
android:id="#+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:hint="annotation text"
android:inputType="text"
android:textSize="14sp"
android:visibility="gone"/>
It a default layout when you change your device orientation to landscape.
Add
android:imeOptions="flagNoExtractUi|flagNoFullscreen"
to disable the fullscreen editing view
<EditText
android:id="#+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:imeOptions="flagNoExtractUi|flagNoFullscreen"
android:hint="annotation text"
android:inputType="text"
android:textSize="14sp"/>
flagNoExtractUi Used to specify that the IME does not need to show its extracted text UI. For input methods that may be fullscreen,
often when in landscape mode, this allows them to be smaller and let
part of the application be shown behind. Though there will likely be
limited access to the application available from the user, it can make
the experience of a (mostly) fullscreen IME less jarring. Note that
when this flag is specified the IME may not be set up to be able to
display text, so it should only be used in situations where this is
not needed.
flagNoFullscreen Used to request that the IME never go into fullscreen mode. Applications need to be aware that the flag is not a
guarantee, and not all IMEs will respect it.
As you can tell from this picture, I have multiple buttons set up in a keyboard style manner. I have each button's onClick in xml pointing to my btnKeyPress method. I plan on setting a switch/case for this method but wanted to know if v.getId is a very conventional way of differentiating between which button was pressed and wanting to know if there is a better way. I'm mainly worried about if v.getId will change or will stay constant according to my log no matter which phone the app is run on.
For example: Here is my Space and Backspace key's XML attributes
<Button
android:id="#+id/btnKeySpace"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="25"
android:onClick="btnKeyPress"
android:text="Space" />
<Button
android:id="#+id/btnKeyBackspace"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="75"
android:onClick="btnKeyPress"
android:text="Bkspc" />
Here is what gets called
public void btnKeyPress(View v){
System.out.println(v.getId());
}
v.getId() will always return the android:id set in your layout xml. So yes, you won't have problems if you use it to identify your buttons.-
switch (v.getId()) {
case R.id.btnKeySpace:
break;
case R.id.btnKeyBackspace:
break;
// ...
}
I want to have a "comments" button on my Android e-reader app that displays the number of comments currently posted inside the icon... so basically I want a comment bubble with a number inside it.
I could have multiple drawables, each being the bubble with a different number inside of it, and use a switch statement to choose which one to load each time based on the int number_of_comments field of the element being displayed. This approach seems a little wasteful though, and in any case I have a feeling there's a more elegant way to do it.
Any ideas?
You can do better. You can have a textview on top of the image view and keep updating its value everytime a new comment is added. You can define the overlap in xml like below and adjust your code logic accordingly to increase the comment count. For now I have just set up a dummy text Hello to show on top of the ImageView. You can add your comment count using the TextView's setText method.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativelayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/myImageSouce" />
<TextView
android:id="#+id/myImageViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/myImageView"
android:layout_alignTop="#+id/myImageView"
android:layout_alignRight="#+id/myImageView"
android:layout_alignBottom="#+id/myImageView"
android:layout_margin="1dp"
android:gravity="center"
android:text="Hello"
android:textColor="#000000" />
</RelativeLayout>
Hope this helps...
check out this 3rd party created widget
Android ViewBadger
You can use it to create the little number bubbles that you are looking for I think. This gives you the benefit of not having modify your layouts so much to achieve what you are trying to get.
Here is the sample code to apply a "badge"
View target = findViewById(R.id.target_view);
BadgeView badge = new BadgeView(this, target);
badge.setText("1");
badge.show();
I have an issue with a virtual keyboard.
This Soft Keyboard Covers Data Fields - and can't See what's going on.
I want to use this soft keypad and at the same time it should not become a problem to use data fields.
So how can I manage the data fields in such cases?
Encase the layout that you already have set up within a ScrollView. This will allow the content to scroll when the software keyboard shows. Keep in mind a ScrollView has to be formatted as such.
<Scrollview android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<!-- Your content -->
</LinearLayout>
</ScrollView>
You may also consider setting the following attribute on your Activity in the Android Manifest to further specify the behavior.
From here (http://developer.android.com/guide/topics/manifest/activity-element.html):
android:windowSoftInputMode=["stateUnspecified",
"stateUnchanged", "stateHidden",
"stateAlwaysHidden", "stateVisible",
"stateAlwaysVisible", "adjustUnspecified",
"adjustResize", "adjustPan"]
I find that adjustResize works better for applications contained in ScrollViews in my particular case.
I want to customize my ExpandableList. My issue is that I need a button and expandable list on single activity. Can I achieve that? I have seen all the examples but all extends ExpandableListActivity not the Activity in which I can put all the widgets in one activity.
Any help would be appreciated.
According to the documentation this task should not be too hard.
the first thing that you will have to do is create a new xml file to hold your custom layout. The file should be saved in your res/layout folder and be named something like "my_custom_expandable_list_view_layout.xml", it should look something like this:
<?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">
<ExpandableListView android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<Button android:id="#id/my_button_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click Me"/>
</LinearLayout>
The import part of that layout file is that you include an "ExpandableListView" and give it the id of "android"list".
The next thing that you will need to do is let your activity know that you are using a custom layout by calling setContentView() in your activities onCreate(). The call should look something like this
setContentView(R.layout.my_custom_expandable_list_view_layout);
At this point you should be able to run you program and see a large button at the bottom of the screen. In order to do something with this button you will need to access it via a call to findViewById() in your Activity like this
Button myButton = (Button)findViewById(R.id.my_button_id);
Once you have that myButton object you can add a click listener or whatever else you would like to do. You can pretty much add anything else you want by just adding new things to the layout file.