How to put text over an imagebutton not via xml - android

I'm currently developing an android app with eclipse. Basicaly, I need to put the text over an imagebutton. There are some conditional statements in order to put text over that imagebutton, that's why I can't set the text in xml
This is how I set the imagebutton in xml:
<ImageButton
android:id="#+id/question1"
android:layout_width="150dip"
android:layout_height="50dip"
android:layout_gravity="center_vertical|center"/>
This is how I set the image source and text of the button:
buttonQuestion1 = (ImageButton) findViewById(R.id.question1);
TextView listContent = (TextView)findViewById(R.id.question1);
String question1 = getQuestion.get(0);
buttonQuestion1.setImageResource(R.drawable.button);
listContent.setText(question1);
The code to set the image source works fine. I use TextView and setText in order to put text over the imagebutton but when I try to run it, the app crashes. Anyone know how to set text over an imagebutton in a right way?

This line
TextView listContent = (TextView)findViewById(R.id.question1);
gives error because R.id.question1 is not a TextView, it's an ImageButton.
You can use normal <Button/> instead for layout
<Button
android:id="#+id/question1"
android:layout_width="150dip"
android:layout_height="50dip"
android:layout_gravity="center_vertical|center"/>
When you want to set the text and image resource, you can call
buttonQuestion1.setBackgroundResource(R.drawable.button);
buttonQuestion1.setText(question1);

Related

Set gravity to two different texts in one textview Android

I need to set two texts(Text1 and Text2) in one single TextView.Following are my requirements.
Text1 should be bigger than Text2 in font size.
Text2 would below Text1. Both are centrally aligned.
Text1 should be given gravity: center so that it resembles other layout.Text 2 will always be below Text1
I'm still unclear about your question, do you want to display both texts at the same time or not?
If you're only going to display one 'style' at a time, mayo's answer is right. You can use a switch statement to implement it:
switch(textStyle){
case 1: myTextView.setTextAppearance(getApplicationContext(), R.style.styleText1);
break;
case 2: myTextView.setTextAppearance(getApplicationContext(), R.style.styleText2);
break
}
More info here.
But if you want to display both at the same time, a WebView is your only option:
Webview wv;
//Specify Text1 and Text2 as strings.
String text = "<html><body style=\"font-size:25px;\">"+"<p align=\"center\">"+ Text1 +"</p>"+"<style=\"font-size:15px;\">"+"<p align=\"center\">"+ Text2 +"</p>"+"</body></html>";
wv.loadData(""+text, "text/html", "utf-8");
You cannot do that with only one TextView.
Basically one TextView keeps a text and some properties of that text. That properties are applied to the whole TextView, ie, to the whole text.
If you want to have different properties for Text1 and for Text2 you must have two TextViews.
At the end any UI element in android is given by an xml, in this case a TexView is something like:
<TextView android:text="#string/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffd300"
android:gravity="center_horizontal"
android:textSize="40sp"
android:id="#+id/textViewTitle"
android:layout_gravity="center" />
Here in property text you will set your Text1 or your Text2. In this case we are using #string/title which means that we are using an string called "title" from the string resources.
Reference:
http://developer.android.com/reference/android/widget/TextView.html
Combining two texts in a single TextView is not possible. It spoils the respect of the TextView itself.
You will need a Vertical LinearLayout with a background and border resembling to your TextView. Inside this layout, you will need to have two TextView's for text1 and text2 respectively. You can apply your desired properties to the individual TextView's
you can use html code in Textview
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#android:color/white"
android:textSize="25sp" />
and in java file
TextView mTitle = (TextView) toolbar.findViewById(R.id.title);
mTitle.setText(Html.fromHtml("<b>S T A C K </b><font color='white'>O V E R F L O W</font>"));

dynamic display of test without using textview : Android

Actually I want to show some text using dynamic variable , along with want to take an another input from user so I want a input text thing & send button too.
I googled but every place i am getting that i need to use this kind of code
TextView textView = new TextView(this);
textView.setTextSize(20);
textView.setText("score :"+test+"\n x : 3\n y : 8");
setContentView(textView);
But using this full layout becomes textview & i cant add button , if i do , i can't see them on screen .All i see is the text above.
If I'm reading this correctly, you want to have a layout which is something like this:
<RelativeLayout ....>
<EditText .... />
<Button ..... />
<TextView android:id="#+id/scoreDisplay" ..... />
</RelativeLayout>
Then, in your activity's onCreate:
setContentView(R.layout......);
TextView tv = findViewById(R.id.scoreDisplay);
tv.setTextSize(20);
tv.setText("score :"+test+"\n x : 3\n y : 8");
This will have a layout containing 3 elements (an EditText, a Button, and a TextView).

Android XML primitive access

I need to edit an xml file, the problem is that I have multiple xmls and in all my classes/activities I can't put the xml as the main View.
So I must acces an xml file without setting it as main view on any activity.Is that even possible?
I mean I can make: View j = (View) findViewById(R.layout.mytest);
But how can I edit a button's text for ex in that mytest.xml file?
although you can inflate any layout using LayoutInflator and make any changes for that instance in it , but you can not look for a persistent change in layout . so first setContentView , then change for that view only .
So you're saying you want to dynamically edit a layout xml file??? Example, you have a layout like...
<LinearLayout>
...
<Button
android:id="#+id/myButton"
...
android:text="Some text" />
</LinearLayout>
...and you want to dynamically edit android:text="Some text" at run time???
If so, that isn't the way to do it. Simply leave the android:text attribute out of the layout xml and just set the text of the button at runtime.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContenView(R.layout.mytest);
Button btn = (Button) findViewById(R.id.myButton);
btn.setText("Whatever");
}

How to create an ImageButton with an overlayed image?

I have a button that starts out as a placeholder, but once the user authenticates, it changes to a custom image for that user.
<ImageButton android:id="#+id/button"
android:background="#null"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="#drawable/button_default"/>
And then later:
ImageButton ib = (ImageButton)findViewById(R.id.button);
ib.setImageBitmap(previouslyDecodedBitmap);
But this looks terrible. I can't figure out how to style it properly so that the newly decoded bitmap is the right size and behaves like an ImageButton. I suspect there is some combination of widgets I can use other than ImageButton to achieve this? I was hoping I could just nest an ImageView on top of the ImageButton by adding it as a child to ImageButton, but that doesn't seem to be allowed (it is in Silverlight...).
Anyway, any suggestions on how to properly do this are welcome. Thanks.
One way would be to use frame layout and place buttons and image one over the other , top being imageview , keep it invisible (android:visibility = "invisible")
On clicking the button and authenticating , make it visible over the button or else even you can hide the button below and show only image on top.

android image button

How can i create a button with no text and an image centered horizontally ?
I don't want to use an ImageButton because I want to define a different backgound image
You just use an ImageButton and make the background whatever you want and set the icon as the src.
<ImageButton
android:id="#+id/ImageButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/album_icon"
android:background="#drawable/round_button" />
just use a Button with android:drawableRight properties like this:
<Button android:id="#+id/btnNovaCompra" android:layout_width="wrap_content"
android:text="#string/btn_novaCompra"
android:gravity="center"
android:drawableRight="#drawable/shoppingcart"
android:layout_height="wrap_content"/>
You can just set the onClick of an ImageView and also set it to be clickable, Or set the drawableBottom property of a regular button.
ImageView iv = (ImageView)findViewById(R.id.ImageView01);
iv.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
You can use the button :
1 - make the text empty
2 - set the background for it
+3 - you can use the selector to more useful and nice button
About the imagebutton you can set the image source and the background the same picture and it must be (*.png) when you do it you can make any design for the button
and for more beauty button use the selector //just Google it ;)

Categories

Resources