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
Related
I want to add specific XML attribute for the view in android layout file.
I am getting lint message to add contentDescription to ImageView for more 400 ImageView and I don't want to add it manually by going to each ImageView XML tag. so is there any way to automatically add missing attribute with its value in android studio.
Right click the layout folder, click Replace in Path. Then in string to match put in
<ImageView
then in string to replace field, put
<ImageView android:contentDescription="yourText"
Then, press Find. Finally press All Files.
Moxdroid:
And finally Reformat and Rearrange the code.
Me:
You don't need to reformat. Your ImageView output will look like this
<ImageView android:contentDescription="yourText"
android:id="#+id/img_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:background="#drawable/back_selector" />
Unless OCD kicks in, this is totally fine i guess.
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'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'm very new to Android.
I created a ImageButton. The main.xml includes code segment as following.
<ImageButton
android:id="#+id/button1"
android:layout_width="50px"
android:layout_height="50px"
android:src="#drawable/cat"
android:layout_x="50px"
android:layout_y="52px"
>
Where do i have to put the "cat.png" i have. Do i need to rename it to something else?
Put it in the res/drawable/ folder in your Android project. Keep the name cat.png
you can simply put in any one folder it will work no need to change xml for that.
I'm working on an Android widget which essentially places a button on the homescreen. The button uses a selector in order to show a default state and a pressed state. Each state has its own image, as you'll see in the code below.
I already have code to change the hue of an image and return a new StateListDrawable for use in the ImageButton.
My question: How do I actually apply the StateListDrawable to the ImageButton's android:background attribute using the RemoteView?
Here is the XML source for the widget layout:
<LinearLayout
android:id="#+id/LinearLayout01"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<ImageButton
android:id="#+id/ImageButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:background="#drawable/buttonselector" />
</LinearLayout>
Instead of using the hardcoded "#drawable/buttonselector" it needs to be the dynamic selector I'm generating.
I have posted an answer link text which I think is similar to your problem. This, however, involves two layouts that are almost similar. The only difference is the part that should change.