Rotate Text and Change Position of Image at Runtime in Android Studio

Introduction

 
In today's article, you will learn how to rotate text and how to change the position of an image programmatically at run time. You can rotate the text again and again after a regular interval to create a nice effect. Rotating text and changing the position of an image creates a good visual effect. It attracts the user of the app.
 
Step 1
 
Open "activity_main.xml" in the layout and add the following code to it:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  6.     android:paddingRight="@dimen/activity_horizontal_margin"  
  7.     android:paddingTop="@dimen/activity_vertical_margin"  
  8.     android:paddingBottom="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity"  
  10.     android:background="#c79488">  
  11.    
  12.   <TextView  
  13.       android:layout_width="wrap_content"  
  14.       android:layout_height="wrap_content"  
  15.       android:text="@string/hello_world"  
  16.       android:id="@+id/txt"  
  17.       android:layout_marginTop="90dp"  
  18.       android:layout_marginLeft="90dp"  
  19.       android:textSize="@dimen/txtSize"/>  
  20.   <ImageView  
  21.           android:id="@+id/im"  
  22.           android:layout_width="80dp"  
  23.           android:layout_height="80dp"  
  24.           android:layout_marginTop="160dp"  
  25.           android:layout_marginLeft="10dp"  
  26.             />  
  27.    
  28. </RelativeLayout> 
The layout looks like:
 
im1.jpg
 
Step 2
 
Open "MainActivity.java" and add the following code in it:
  1. package com.textrotation;  
  2.    
  3. import android.content.Context;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.app.Activity;  
  7. import android.os.Handler;  
  8. import android.view.Menu;  
  9. import android.widget.ImageView;  
  10. import android.widget.RelativeLayout;  
  11. import android.widget.TextView;  
  12.    
  13. public class MainActivity extends Activity {  
  14.     TextView txt;  
  15.     static int flag=0;  
  16.     private Handler mHandler;  
  17.     final Context context=this;  
  18.     ImageView im;  
  19.     RelativeLayout.LayoutParams par;  
  20.    
  21.     private Runnable mCountUpdater = new Runnable() {  
  22.         private int mCount = 0;  
  23.    
  24.         @Override  
  25.         public void run() {  
  26.             if(mCount>5)  
  27.             {  
  28.                 Intent i=new Intent(context,Second.class);  
  29.                 startActivity(i);  
  30.    
  31.             }  
  32.             else  
  33.             {  
  34.                rotate();  
  35.                 flag++;  
  36.                 mCount++;  
  37.                 mHandler.postDelayed(this900);  
  38.             }  
  39.         }  
  40.     };  
  41.    
  42.     public void rotate()  
  43.     {  
  44.         if(flag%2==0)  
  45.         {  
  46.             txt.setRotation(20);  
  47.             im.setImageResource(R.drawable.image1);  
  48.         }  
  49.         else  
  50.         {  
  51.             txt.setRotation(-20);  
  52.             im.setImageResource(R.drawable.image2);  
  53.             par = (RelativeLayout.LayoutParams)im.getLayoutParams();  
  54.             par.leftMargin += 60;  
  55.    
  56.              im.setLayoutParams(par);  
  57.         }  
  58.    
  59.     }  
  60.     @Override  
  61.     protected void onCreate(Bundle savedInstanceState) {  
  62.         super.onCreate(savedInstanceState);  
  63.         setContentView(R.layout.activity_main);  
  64.         txt=(TextView)findViewById(R.id.txt);  
  65.         im=(ImageView)findViewById(R.id.im);  
  66.         mHandler = new Handler();  
  67.         mHandler.post(mCountUpdater);  
  68.    
  69.     }  
  70.    
  71.    
  72.     @Override  
  73.     public boolean onCreateOptionsMenu(Menu menu) {  
  74.         // Inflate the menu; this adds items to the action bar if it is present.  
  75.         getMenuInflater().inflate(R.menu.main, menu);  
  76.         return true;  
  77.     }  
  78.    

For rotating the text you just need to use the setRotation function on the object of Image. In the code above, Handler has been used to introduce a delay between two rotations. This creates a good visual effect. LayoutParams is used on the image object to change its position. I have repeated this 5 times. After this another activity is loaded.
 
I have used 2 images above namely "image1" and "image2". Copy the images required to the clipboard. Right-click on "res" then select "New" -> "Android resource directory". Name this directory as "drawable" and select the "resource type" as "drawable". Paste the images copied to the clipboard to this folder. Now you can use these images.
 
Step 3
 
Now create a layout for the second activity. Right-click on layout then select "New"->"Layout Resource file". Name this file "second_layout" and add the following code to it:
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.               android:orientation="vertical"  
  3.               android:layout_width="match_parent"  
  4.               android:layout_height="match_parent"  
  5.               android:background="#454545">  
  6.   <TextView  
  7.       android:layout_width="fill_parent"  
  8.       android:layout_height="wrap_content"  
  9.       android:textSize="50dp"  
  10.       android:textColor="#FFFFFF"  
  11.       android:text="BYEE"  
  12.       android:layout_marginLeft="30dp"  
  13.       android:layout_marginTop="190dp"  
  14.       android:id="@+id/txtsec"/>  
  15.    
  16. </LinearLayout> 
The layout looks like:
 
im2.jpg
 
Step 4
 
Right-click on the package then select "New" -> "Java class". Name this file "Second" and add the following code to it:
  1. package com.textrotation;  
  2.    
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.ImageView;  
  6. import android.widget.TextView;  
  7.    
  8. public class Second extends Activity {  
  9.     TextView im;  
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.second_layout);  
  14.         im=(TextView)findViewById(R.id.txtsec);  
  15.         im.setRotation(20);  
  16.     }  

Step 5
 
Do not forget to add the name of the new activity in "AndroidManifest":
  1. <activity android:name=".Second"  
  2.                 android:label="Second"/> 
Note the use of text rotation.
 
The output snapshots:
 
chhavi.gif
 
Note the change in image position.
 
The second activity:
 
im8.jpg
 
Thank you... Enjoy coding :) 

Recommended Ebook

Printing in C# Made Easy

Download Now!
Similar Articles