dynamic textsize change in ice cream sandawich - android

I am trying to change text size when button is clicked.
xml :
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello_world"
android:textSize="30sp"
android:layout_margin=""/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_marginTop="72dp"
android:layout_toLeftOf="#+id/textView1"
android:text="Button1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button1"
android:layout_toRightOf="#+id/textView1"
android:text="Button2" />
This is my code :
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtmain=(TextView)findViewById(R.id.textView1);
txtmain.setTextSize(TypedValue.COMPLEX_UNIT_SP ,30);
//txtmain.setTextSize(TypedValue.COMPLEX_UNIT_SP ,30);
txtmain.setTextAppearance(getApplicationContext(), 12);
btn1=(Button)findViewById(R.id.button1);
btn2=(Button)findViewById(R.id.button2);
txtmain.setBackgroundColor(Color.YELLOW);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
txtmain=(TextView)findViewById(R.id.textView1);
txtmain.setTextSize(TypedValue.COMPLEX_UNIT_SP ,30);
System.out.println("txtmain get height:"+txtmain.getHeight());
//Toast.makeText(getApplicationContext(),"txtmain get
//height:"+txtmain.getHeight() , Toast.LENGTH_LONG).show();
}
});
btn2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
txtmain=(TextView)findViewById(R.id.textView1);
txtmain.setTextSize(TypedValue.COMPLEX_UNIT_SP ,80);
System.out.println("txtmain get height:"+txtmain.getHeight());
//Toast.makeText(getApplicationContext(),"txtmain get
//height:"+txtmain.getHeight() , Toast.LENGTH_LONG).show();
}
});
When I click button 1, it gives proper output but when I click button 2 after clicking button1 output changes.
Here is my output :

This would appear to be quite similar to known a known issue on ICS, see https://code.google.com/p/android/issues/detail?id=22493 and https://code.google.com/p/android/issues/detail?id=17343. The second of these suggests that a workaround is to add a "\n" to the text in the text view. Reading up those pages and those they link to may help resolve the issue for you.

The problem is
txtmain.setHeight(41);
in first button click, So it will change the height of the textview from WRAP CONTENT to a fixed size. Just remove it..

Why are you setting the text box height wen you click the button1?

Related

Android - Change Image View On Click

I have a set of image view that I want to change when it is clicked. The first view is set by default, and I want the second one to appear when the first one is clicked. I also want the reverse to occur when the second one is visible. How can I do this? (Example: Default is Img 1 default, img 2 hidden. On click, img 2 shows, img 1 is hidden. On click again, reverse occurs).
<ImageView
android:id="#+id/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_alarm_off_75dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="134dp" />
<ImageView
android:id="#+id/img2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_alarm_on_75dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:visibility="gone"
android:layout_marginTop="134dp" />
Use the Following Code:
ImageView img1=(ImageView)findViewById(R.id.img1);
ImageView img2=(ImageView)findViewById(R.id.img2);
img1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
img1.setVisibility(View.GONE);
img2.setVisibility(View.VISIBLE);
}
});
img2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
img2.setVisibility(View.GONE);
img1.setVisibility(View.VISIBLE);
}
});
in both of button onclick you must check to see if other button is not visible then set it visible and if it is visible then do not do any thing. you can check visibility of buttons with this code
if(mybutton.getVisibility()==View.VISIBLE)
{
//set visibiliy of mybutton
}

Android Button onClickListener() not firing on single click

I am making an android app in which I have a screen whose xml looks like:
<LinearLayout
android:id="#+id/btns"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:orientation="horizontal" >
<Button
android:id="#+id/viewCalendar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/bg_name_2"
android:gravity="center"
android:layout_margin="0.5dp"
android:text="View Calendar"
android:textColor="#android:color/primary_text_dark" />
<Button
android:id="#+id/createNewMeeting"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/bg_name_2"
android:gravity="center"
android:layout_margin="0.5dp"
android:text="Add New Meeting"
android:textColor="#android:color/primary_text_dark" />
</LinearLayout>
Now you can see I have two Buttons View Calendar and Add New Meeting. The problem is View Calendar button responses on single click while Add New Meeting button responses on double click (Note that this happens only when I come into this screen first time after app starts and works fine until the app closed). When app closed and start again then first time again on this screen this button firing after two taps and so on...
Here is the Java code nothing new just setting onClickListener on Button
addNew = (Button) findViewById(R.id.createNewMeeting);
addNew.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// code for add new meeting
Intent intent = new Intent(getActivity(),
ActivityReminderNew.class);
startActivity(intent);
}
});
viewCalendar = (Button) findViewById(R.id.viewCalendar);
viewCalendar.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// code for calendar view
}
});
R&D: I have been searching for this problem but mostly I get the answers like to set android:focusableInTouchMode="true" or android:focusable="true" in xml but its not solving my problem.
Hope I explained my problem in detail and you guys could understand my issue. Any type of help would be appreciated.

Android image moving on button click

bee to android programming and learning things.
i have the following code with me and what i need is to move only two selected images when button click happen
first user will select two images
then on button click they will move to left direction
here is my code
<ImageView
android:id="#+id/imLady"
android:layout_width="160dp"
android:layout_height="238dp"
android:src="#drawable/lady"
android:layout_marginTop="50dp"/>
<ImageView
android:id="#+id/imLady2"
android:layout_width="160dp"
android:layout_height="238dp"
android:src="#drawable/lady"
android:layout_marginTop="50dp"/>
<ImageView
android:id="#+id/imLady3"
android:layout_width="160dp"
android:layout_height="238dp"
android:src="#drawable/lady"
android:layout_marginTop="50dp"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Move" />
looking forward for suggestions,solutions from u guys:) regards
you need to setAnimations to your ImageViews in onClick() function of Button
ImageView v1,v2;
Button b;
TranslateAnimation ta1,ta2;
...
public void onCreate(Bundle savedInstances)
{
super.onCreate(savedInstances);
setContentView(R.layout.layoutname);
...
initiate and refer button and imageviews
...
ta1=new TranslateAnimation(fromx,tox,fromy,toy);
ta1.setDuration(1000); //1 second
ta2=new TranslateAnimation(fromx,tox,fromy,toy);
ta2.setDuration(1000); //1 second
b.setOnClickListener(new View.onClickListener(){
#override
public void onClick(View arg0)
{
v1.startAnimation(ta1);
v2.startAnimation(ta2);
}
});
}
refer to TranslateAnimation Documentation here

Android: Unable to Prevent buttons from increasing size and overlapping with other buttons

I am new to Android Programming, what I am trying to do in this Android Application is to create a xml page filled with buttons.
When I click the button, the button would change to light green color and when I click it again, it would change to light grey
The error: I am getting is when I click the button, it increases in size and overlaps with the other buttons, please help me out here, it is not user friendly in this case
attached below is the code:
lockerbooking.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/sisButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="28dp"
android:text="#string/sis"
/>
<Button
android:id="#+id/solButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/soeButton"
android:layout_alignBottom="#+id/soeButton"
android:layout_alignParentRight="true"
android:text="#string/sol" />
<Button
android:id="#+id/soeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/sisButton"
android:layout_alignBottom="#+id/sisButton"
android:layout_centerHorizontal="true"
android:text="#string/soe" />
</RelativeLayout>
Code:
makeBooking.java
public class makeBooking extends Activity {
Button sisButton;
Button solButton;
Button soeButton;
Button sobButton;
super.onCreate(savedInstanceState);
// Get the message from the intent
setContentView(R.layout.lockerbookpage);
Intent intent = getIntent();
// Initialize TextViews
sisButton = (Button) findViewById(R.id.sisButton);
solButton = (Button) findViewById(R.id.solButton);
soeButton = (Button) findViewById(R.id.soeButton);
sobButton = (Button) findViewById(R.id.sobButton);
}
public OnClickListener solButtonListener = new OnClickListener(){
boolean flag = true;
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(flag){
solButton.setBackgroundColor(Color.GREEN);
}
else{
solButton.setBackgroundColor(Color.LTGRAY);
}
flag=!flag;
}
};
...The code goes on
Please help me out here, I am eager to learn
to avoid overlapping of buttons, use fixed width and height for buttons:
change this:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
to some this like this:
android:layout_width="100dp" //what ever size suits your layout
android:layout_height="50dp" //fixing this,will not overlap the buttons

android transparent ImageButton not clickable in MvvmCross

How does one create an ImageButton with transparent background that still clickable (still acts like a Button)?
This is the xml snippet:
<ImageButton
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:paddingTop="7dp"
android:paddingBottom="7dp"
android:src="#drawable/serverschedule"
android:background="#null"
android:clickable="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/predict"
local:MvxBind="Click PredictCmd" />
I have also tried android:background="#00000000" and android:background="#android:color/transparent"and in all cases, I do get the desired visual effect but button no longer can be clicked.
I am using MvvmCross framework to binding to the Click event of the button, hence there is no code behind.
I am testing against API Level 15, if this matters.
EDIT Added entire axml for button.
EDIT Adding MVVM framework as it may have something to do with problem.
TIA.
Thanks for all of the suggestions.
This is what finally worked for me:
android:background="?android:attr/selectableItemBackground"
Based on responses from this thread.
Please provide width and height of your button.
Also try this :
ImageButton theButton = (ImageButton )findViewById(R.id.theButton);
theButton.setVisibility(View.VISIBLE);
theButton.setBackgroundColor(Color.TRANSPARENT);
theButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// DO STUFF
}
});
Hope this helps.
Do certain Necessary changes
<ImageButton
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:paddingTop="7dp"
android:paddingBottom="7dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/serverschedule"
android:background="#null"
android:id="#+id/mybutton"
</ImageButton
Into Your code:
mybutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
//Your Stuff here
}
});
try this...
<ImageButton
android:id="#+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/timeText"
android:layout_centerHorizontal="true"
android:background="#android:color/transparent"
android:clickable="true"
android:contentDescription="image button"
android:src="#drawable/anchor" />
set clickable attribute to true, and handle onclick event in your activity like
findViewById(R.id.imageButton).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "text", Toast.LENGTH_SHORT).show();
}
});
No change in your xml file.. even no need to add this android:clickable="true"
edit your java file as below code...
ImageButton imageButton;
#Override
public void onCreate(Bundle savedInstanceState) {
imageButton = (ImageButton) findViewById(R.id.imageButton1);
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(MyAndroidAppActivity.this,
"ImageButton is clicked!", Toast.LENGTH_SHORT).show();
}
});
}
}
If this doesn't work you might have problem with your parent layout. Please post that if so....
what's the meaning of the "acts as a button" ? when you use
android:background="#null"
your ImageButton will have no background and you can use selector as the src ,you'll get the same acts as a button,also can click as usual
selector like this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_bg_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/button_bg_pressed" android:state_focused="true"/>
<item android:drawable="#drawable/button_bg_normal"/>
</selector>
also see enter link description here
back.setBackgroundColor(Color.TRANSPARENT);
works for all android versions

Categories

Resources