how to copy a button in layout? - android

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

Related

Changing text while pressing button android studio

I've tried a tutorial on youtube but it doesn't work,when I click the button nothing happens.
When I press the button I want to Change the layout activity and also to be set on the new layout one random text from lets say other 10 and on this current layout I wanna have the button which will randomly change the first text to another one.
Here is what codes I've used and didn't worked out.
#I can't add the codes because It doesn't let me post it here..it says something like isn't valid but you see everything in the picture,so I did all that code and when I press on my button It doesn't doing anything,what can be the problem?and If you have any idea or if you know any tutorial to help me achieve what I said I want to do upper I would appreciate really much
#as you can see there is a string option which should give me one text of all of them when I click my button,but it doesn't
sorry for image but it seems like those codes aren't good
<LinearLayout
android:layout_width="332dp"
android:layout_height="184dp"
android:layout_marginStart="36dp"
android:layout_marginLeft="36dp"
android:layout_marginTop="48dp"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Button
android:id="#+id/buttonsports"
android:layout_width="match_parent"
android:layout_height="129dp"
android:background="#drawable/ft"
tools:layout_editor_absoluteX="115dp"
tools:layout_editor_absoluteY="291dp" />
<TextView
android:id="#+id/sports"
android:layout_width="216dp"
android:layout_height="117dp"
android:text="TextView"
tools:layout_editor_absoluteX="95dp"
tools:layout_editor_absoluteY="246dp" />
</LinearLayout>
this is how big my text box is,the "sports" is the id for TextView which is "abc"
In your xml you have a LinearLayout for which you have set the height to:
android:layout_height="184dp"
and inside there is a Button with height:
android:layout_height="129dp"
So for the TextView below there is left maximum 55dp but you set:
android:layout_height="117dp"
This means that at least half the TextView is not visible.
In your code you set a random text to the TextView when you click the Button.
You say that nothing happens.
Is there a case that the text is being set but you can't see it because the TextView is mostly hidden?
Change the heights of the views and you will find out.

How can we set text to include button in layout

I create layout button and i want to use it to other place by include it and i want to change it text in per layout , is there a way??
my button(row_addbutton) :
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/button_banklist_addtoken"
android:layout_width="200dp"
android:layout_height="45dp"
android:background="#drawable/backgroundbutton_withradiusandcolorprimarylight"
android:text="#string/banklist_addtoken"
style="#style/TextAppearanceButton"
android:drawableEnd="#drawable/addtokenicon"
android:layoutDirection="ltr"
android:drawableRight="#drawable/addtokenicon"
android:paddingStart="60dp"
android:paddingEnd="60dp"
android:layout_marginTop="25dp">
</Button>
include it here :
<include
layout="#layout/row_addtoken"
android:layout_width="200dp"
android:layout_height="45dp"
android:layout_below="#+id/linearlayout_banklist_basecontent"
android:layout_centerHorizontal="true"
android:layout_marginStart="70dp"
android:layout_marginTop="-25dp"
android:layout_marginEnd="70dp"
//is there any way to change text button here??
/>
A more sensible way to achieve what you want is to declare a style for such buttons in /res/values/styles.xml. So then you just have to choose your style from the drop-down list and change the button text as you usually do.
You can access all the view of included layout.
You can do it in .java class like this.
Let's assume you have included your layout in activity_main.xml then go to respective java file and in that you can do code like below :
Button button_banklist_addtoken = findViewById(R.id. button_banklist_addtoken);
button_banklist_addtoken.setText("Here is your text")
I hope it will help you

Make use of the default buttons in the Android sdk

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

Dynamic views with one static xml

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

Android Layout Issue

Hi I'm having a daft problem with my android application.
Currently it looks like this:
Is there a way of making the button go to the bottom in the middle? I've tried messing around but same effect. Also tried changing the fill_parent/wrap_content, relative/linear layouts and gravities.
This is the screenshot of the .xml file
many thanks.
There are a couple things you can do to get this, with the relative layout you're using this would work. Add these lines to your button section
android:layout_below="#+id/android:empty"
android:layout_alignParentBottom="true"
android:layout_alignParentCenter="true"
Add these two attributes to your Button
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
and these one to your textview:
android:layout_above="#id/insertion"
android:layout_height="fill_parent"
Read the API reference here:
http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html
May be you want to use a linear layout instead of the relative one...
With the LinearLayout you can place your item without thinking on their relative position.
You can place a vertical linear layout and inside it another layout for the list of reminders.
<LinearLayout android:orientation="vertical">
<ListView android:width="fill_parent"android:weight="2" />
<Button android:width="wrap_content" android:weight="1" />
</LinearLayout>
With weight option, you can choose to make the Button to be painted before the ListView.
EDIT: Reading other answers, I'm considering if you really need a RelativeLayout to place a button under a listview. I think you should learn how to handle simple view before to start using something more complex. If LinearLayout solve your problem, why don't use it? This is a personal observation...

Categories

Resources