I have an xml file right now which has a TableLayout within a LinearLayout. Within the TableLayout are TableRows with Buttons. Within my Java code, I do a setOnClickListener for each button.
The problem is I have several xml files like this which are exactly the same, except within the xml the ID and text's of the different buttons are different.
So one file it will be
<Button android:id="#+id/one" android:text="one" />
<Button android:id="#+id/two" android:text="two" />
and another file it will be
<Button android:id="#+id/three" android:text="three" />
<Button android:id="#+id/four" android:text="four" />
In Java, in one part of a switch/case statement I have a case that is:
setContentView(R.layout.a);
Button bOne = (Button) findViewById(R.id.one);
Button bTwo = (Button) findViewById(R.id.two);
and another I have:
setContentView(R.layout.b);
Button bThree = (Button) findViewById(R.id.three);
Button bFour = (Button) findViewById(R.id.four);
The redundancy goes out to more than two xml files, this is just an example.
Is there any way to do this and not have multiple layout files, but to do this all in one layout file? The xml files are all exactly the same, but for the id and text portions of the buttons.
I really don't care how this is done - in Java, in xml, or however. It's just that I make minor tweaks to the layout once in a while, and don't want to have to continue to tweaking every file with exactly the same layout but for the button id's and texts. I'd like to tweak the layout once and have it work across all of them when I change the layout. If I have to put the entire layout file in Java, that would be fine.
just keep one xml file (a.xml). say the ids of the two buttons be id1 and id2. use setContentView(R.layout.a);. Next declare 2 buttons say b1 and b2. set b1 = (Button) findViewById(R.id.id1) and b2 = (Button) findViewById(R.id.id2). put setOnClickListener for the buttons in the switch-case
If it is always just those 2 buttons, you could have a variable keeping track of an arbitrary int that represents what screen you're on. Keep the same buttons, change their text / layout params, and on the OnClick() method test that variable to see what screen you're on.
Why not just lose the xml files and just make it all dynamic. You can do everything in code that you can in xml. Here is a tutorial: Dynamic Layout Tutorials
Related
i want to make 16 buttons in one XML file but i don't want to make all of them individually ...how can i make it happen , like making one button and then copy it for many times??
like this picture :
http://i62.tinypic.com/t7cvie.png
i tried making a button
Button button = (Button) findViewById(R.id.Button1);
what shoud i do next?
and then i want to swipe the buttons so i would have other 16 buttons and go on ....
what should i do? i got confused.
help me?
Not quite sure if you were looking for this .
But there are multiple ways to reuse your code in android. One of the way to re-use your button definition that I prefer to use is to define the layout of the button first and include it where ever needed. Each include can be given unique id. Below are the steps to follow:
Define your android button layout in **layout folde**r first. This would be your only definition.
example: my_layout.xml
android:gravity="center|bottom" >
<Button
android:id="#+id/button_register"
android:background="#drawable/bordered_rounded_button"
android:layout_width="fill_parent"
style="#android:attr/buttonBarButtonStyle"
android:layout_height="30dp"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:shadowColor="#4D56A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_weight="0.5"
android:text="#string/button_register"
android:textColor="#color/WhiteSmoke"
android:textSize="14sp"
/>
then you can include this in your view/xml 15 times ( or any number of times) with unique id of each include .
like this :
<include android:id="#+id/include_layout_id"
layout="#layout/my_layout" <!-- make sure this matches your layout file name-->
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
Good Luck!
The first you must get layout where you want to add buttons, after initialize button and when add it to layout
LinearLayout layout = (LinearLayout) findViewById(R.id."layout id");
setContentView(new GraphTemperature(getApplicationContext()));
Button newButton = new Button(this);
newButton.setText("New Button");
newButton.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
layout.addView(myButton);
put this code in for loop i<=15;
You can't use the same ids in same layout if there behavior is different. So you have to create 16 Buttons in your layout. But put your button definition to your style.xml
I would like to make a button, a simple button, having the default Search icon that is used in Android.
However, I do not want to make my own xml file and put the images in the Drawable folder, because i know they already exist in the Android sdk.
So why not making use of them?
I tried to make something like this:
android:background="#android:drawable/...." but there in this directory it seems that all the files are png file not xml file able to interact with the user (on button pressed, etc..)
I hope an expert can help solving this problem.
You don't need an xml file for the button to work. The png files in the drawables are just for the image. You can create a button programmatically or in the xml but you still have to create it somewhere because the Button instance is what is used for the onClick() and not necessarily the xml. Either way you must have an xml file for your Layout to use in setContentView() so you can put a Button in that layout file or create it in your Java code but either way, you have to create a Button
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a TextView" />
<Button android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button" />
</LinearLayout>
in this Button you can set the background or use an ImageButton instead and set `android:src="#drawable/..."
Then in your code you still have to get the button instance
Button btn1 = (Button) findViewById(R.id.button)
after you have called your layout like
setContentView(R.layout.your_layout_file);
If you use an ImageButton just replace Button with ImageButton which is what it sounds like you want. Hope this helps it make a little more sense for you
I have spent literally two days trying to sort this issue. If anyone could help I would be massively appreciative.
What I'm trying to achieve:
Have a ListView, whereby the player can add new entries (players), through a text field (for the player name), and then a submit button. In each field of the ListView, I display the player name, and then two ImageButtons. One with a male symbol, and one with a female symbol. The male symbol is toggled by default, and the user can set the player as being male or female by toggling either the male button or the female button. Finally, once the user moves onto the next screen (a new activity), the application will save the player names and the attached sex to some form of storage and proceed to the next activity.
What I have achieved:
I have a simple array adapter, which upon the player adding a new player name to the list, I run the notifyDataSetChanged() on it. The adapter also is set to use a custom row layout file. Inside the layout file, it looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<RelativeLayout android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="#+id/relativeLayout1" android:layout_marginTop="5dp">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/playerName"
android:layout_centerVertical="true" android:text="Derp" android:textStyle="bold" android:layout_marginLeft="5dp" android:textSize="22dp" android:textColor="#color/male_blue"></TextView>
<ImageButton android:layout_height="60dp"
android:layout_width="60dp" android:onClick="maleClickHandler"
android:src="#drawable/male_icon_settings" android:id="#+id/buttonA" android:layout_alignParentRight="true" android:layout_marginRight="65dp"></ImageButton>
<ImageButton android:onClick="femaleClickHandler"
android:layout_height="60dp" android:layout_width="60dp" android:id="#+id/buttonB"
android:layout_alignParentRight="true" android:layout_marginRight="5dp" android:src="#drawable/female_icon_settings"></ImageButton>
</RelativeLayout>
</LinearLayout>
The two buttons on each row reference to methods in the class file. Here is my code for this:
public void maleClickHandler(View v) {
RelativeLayout vwParentRow = (RelativeLayout) v.getParent();
ImageButton maleButton = (ImageButton) vwParentRow.getChildAt(1);
maleButton.setImageDrawable(getResources().getDrawable(
R.drawable.male_icon_selected));
vwParentRow.refreshDrawableState();
}
public void femaleClickHandler(View v) {
RelativeLayout vwParentRow = (RelativeLayout) v.getParent();
ImageButton femaleButton = (ImageButton) vwParentRow.getChildAt(2);
femaleButton.setImageDrawable(getResources().getDrawable(
R.drawable.female_icon_selected));
vwParentRow.refreshDrawableState();
}
I haven't yet implemented any inter-connectivity between these two buttons, to allow only one to be active at a time, or to even untoggle one, since I think I might be taking the wrong approach entirely.
The problem:
Upon adding new entries to the list, AFTER toggling one and/or the other male/female buttons, things get really buggy, and the male/female toggled icon might move as it should, along with the attached player string, or more likely, those toggled will stay on that first row (array position 0 of the list), or even move into the second list position, AND copy themselves as being toggled onto the row above.
How you can help...?
I have attached an image below of my screen, from the emulator, to help illustrate my points
Screenshot!
I think that I might need to use some form of custom adapter; I have done so much reading around on the subject, but I can't find anything relevant to what I am trying to achieve, so if you could point me in the right direction, or even try and put together the most basic solution to this type of problem, I would be very grateful.
Finally, when I get this working, which form of storage would be best for storing player names, and their sex? I would like the user to be able to keep the player list after they quit the application and restarted it.
Thanks for any help! :)
You will need to use a Custom Adapter, which in itself should be able to track the male/female flag for each of it's entries.
Your method will not work since the state of the buttons are managed by the getView method of the adapter. Even if you change them by digging through the children, the next time when the getView method is called, it's going to mess up things.
A lot of this depends on how many players you expect to have in your game. If it's a number that would likely fit on one screen (or very close to it), the ListView is actually unnecessary. ListViews and adapters aren't really a convenience method as much as they are a tool to improve performance. They only keep in memory what is on the screen and recycle old, already-displayed Views for new rows when you scroll--this is why some of your button states are being copied to different rows.
There are a couple of ways you could fix this:
You could write a custom adapter yourself as Kumar Bibek suggests. In this adapter, you would want to override the getView() method to make sure each button has the correct state each time the method is called.
You could also simply use a ScrollView populated with a few of your rows manually if you don't have enough data to warrant using a ListView. This way you wouldn't need to worry about your rows being recycled and button states being out of wack.
In addition, you might want to look into using a RadioGroup for the gender selector (I can't think of a much better use for radio buttons since they are made to be mutually exclusive).
Also, the outer LinearLayout in your row XML file looks unnecessary.
As far as storage, you could either use an SQLite database or SharedPreferences. SharedPreferences requires no setup, but I feel like an SQLite database is more suited to your needs.
I'm creating an IM client/server application for the Android OS, and am currently putting together the user interface. Right now, my interface consists of a EditText element, and a Button element. When I tap on the EditText element, a keyboard pops up and allows you to type.
What I would like to have is something like the text entry area and send button in the default Android SMS app. Something like this:
The text input field and Send button would stay at the bottom of the screen, and when tapped on, the keyboard would push the text field and button up above it.
Is this possible using only EditText and Button elements?
Thank you for any suggestions or advice!
Try setting android:windowSoftInputMode=adjustResize for the activity in AndroidManifest.xml.
You can find details here.
Is this possible using only EditText and Button elements?
Answer-This type of functionality is possible in any type of view
I give just short tutorial on your question
Generally we use only linearlayout in xml file.But at view level android gives many more feature like Relative layout and much more.At this time we just discuss about the relative layout because it can solve your purpose.
In Relative layout it not use the android:orientation feature like linear layout it used another feature.In relative layout take some points in your mind...
we always give id to every view using android:id="#+id/GiveName"
for alignment of any view we used android:layout_toLeftOf="#id/givesname" same for
right,above and below where givesname=id of that view from which this view is align.
Ex. is gives example with screen shot
After this i give the sample xml file in which you get the above type of feature in your question
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llWriteCommentWall" android:layout_height="fill_parent"
android:layout_width="fill_parent" android:background="#ffffff">
<Button android:id="#+id/btButtonComments"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Comments"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"/>
<EditText android:id="#+id/etEdittext"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:hint="Write a comment.... "
android:layout_marginLeft="2dip" android:layout_marginRight="2dip"
android:layout_toLeftOf="#id/btButtonComments"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
ScreenShot of above example
In this Example we used android:layout_alignParentBottom="true" - this attribute is the main reason for view like this,it always align any view in bottom even softkeyboard is shown.it contain boolean value,true gives always align bottom and false nothing.
the other relative attribute is android:layout_alignParentRight="true",android:layout_alignParentLeft="true" ,android:layout_alignParentTop="true"-all attribute give feature as written.
Lastly you include this xml file at any java file through setContentView(xmlFileName)
I add a RadioButton in my layout.
It is unchecked to begin with. And when I click on it , it becomes checked (as shown in emulator). But when when i click on it again, it does not become unchecked again?
<RadioButton android:checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/option1"/>
If you're looking for checkbox behavior with radio button appearance, you could pass in the xml style to a checkbox.
<CheckBox android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/option1"
style="#android:style/Widget.DeviceDefault.Light.CompoundButton.RadioButton/>
This can be useful in some cases (ex. using radio buttons in a RecyclerView) but you should be careful because the user expects radio buttons to behave a certain way. If you're allowing the user to make multiple selections you should probably use a normal checkbox, as mentioned in the comments above.
Hope this helps!
If you are only using one radio box for checking on and off, maybe you should use checkbox or toggle button instead.
http://developer.android.com/resources/tutorials/views/hello-formstuff.html
Scroll down and see checkbox and toggle button.
When using radios you usually have more than one and choose between them. Like easy, medium, hard.
If you are having more than 1 radio buttons to work with then add "RadioGroups" as follows:
<RadioGroup android:id="#+id/group1" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="vertical">
<RadioButton android:id="#+id/radio1" android:text="madras"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<RadioButton android:id="#+id/radio2" android:text="bombay"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</RadioGroup>
Have a look at this example , i am sure this will make your idea clear regarding Radio Buttons.
Also refer this Android Developer - Form Stuff page .
After searching a lot on SO, I came up with a not-so-good but decent workaround.
Declare a boolean variable for each RadioButton you have, initialize it with false, and change the state of the variable and the RadioButton at every click.
boolean isToggledRadio1 = false;
RadioButton radio1 = (RadioButton) findViewById(R.id.radiobutton1);
radio1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
isToggledRadio1 = !isToggledRadio1; //Switch boolean value
RadioButton rb = (RadioButton)v;
rb.setChecked( isToggledRadio1 );
}
});
I know it's ideal to use a Checkbox, but if someone needs the radiobutton, then they need the radiobutton.
This is not an optimal solution, as it will basically toggle the button twice every time the user clicks the button (one is the default behaviour, the second time is inside your onclick function), so if you're using an OnCheckedChangeListener you will probably get two calls for the same click.
There's another workaround, which is to change the android:button in a checkbox to another drawable with an xml template, but it's a bit more complex, requires at least 2 more files for the states.
That is a conceptual question: Radio buttons allow you to choose between several options (represented by the other radio buttons). Typically, one radio button out of a group is always checked, even if in an initial state no buttons may be checked if you do not declare one as a default value. That means also that a single button does not allow to toggle its state unless other radio buttons are present in the same group - if there is only one option, you will have to choose it.
If you want a binary toggle, you will want to use a checkbox instead.
Solution is set checked[true/false] intermediate in Java code
ToggleButton t = (ToggleButton) findViewById(R.id.toggle_button);
t.setChecked(true);
// t.setChecked(false);