Add Link into Android App

Once when you made your initial application with AdMob in it you need to put some functionality to it. If we make application for calculation of greatest common divisor, we can put in app→res→layout→activity_main.xml (design view) some large text widgets, some number text fields and one button.

activity-main-xml-design-view
We need now to declare in app→java→cirvirlab.gcd→MainActivity.java inside the declaration of public class MainActivity extends AppCompatActivity text view control, for example txtN.

MainActivity-java

Next step in giving functionality to your app is to map text view control on graphical object text field. So again in app→java→cirvirlab.gcd→MainActivity.java in function onCreate map this text view on text fields.

Map-TextView-on-TextField

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

txtN = (TextView)findViewById(R.id.editTextN);

txtM = (TextView)findViewById(R.id.editTextM);

Now, go to text mode of app→res→layout→activity_main.xml

activity-main-xml-text-view
In button, declare function on event onClick
android:onClick=”calculateGCD”

onClick-declaration

And then write function in app→java→cirvirlab.gcd→MainActivity.java. Initial code just for checking functionality can be as follows:
public void calculateGCD(View view)
{

String str_message = “Welcome”;

txtN.setText(str_message);

}

Code-for-testing

Once, when functionality is tested, use try{} catch(Throwable e){} structure to write safe code:

public void calculateGCD(View view)
{

try

{

int x = Integer.valueOf(String.valueOf(txtN.getText()));

int y = Integer.valueOf(String.valueOf(txtM.getText()));

int N, M, R;

if( x > y ) {N = x; M = y;}

else { N = y; M = x; }

if( (N>0)&&(M>0) )

{

R = N % M;

while( R > 0)

{

N = M;

M = R;

R = N % M;

}

txtOut.setText(String.valueOf(M));

}

}

catch (Throwable e)

{

}

}

Now, we can add link into android app as following.
On inserted TextView control put the next code:
app→java→cirvirlab.gcd→MainActivity.java, in function onCreate(Bundle savedInstanceState):

txtLink = (TextView)findViewById(R.id.textView4);
String linkedText = “Read more <a href=’http://www.cirvirlab.com/index.php/c-sharp-code-examples/greatest-common-divisor-code-in-c.html’>on the blog</a>”;
txtLink.setText(Html.fromHtml(linkedText));
txtLink.setMovementMethod(LinkMovementMethod.getInstance());

Code-for-link-in-Android-app
And the final touch is to change default icon in android app. Path of that icon is something like:

E:\Program Files\Android\Android Studio\plugins\android\lib\templates\gradle-projects\NewAndroidModule\root\res\mipmap-xhdpi\ic_launcher.png

Put there your new app icon in png format with dimension 96X96 in pixels. Then go to app→res→mipmap then right click new→Image Asset and browse to your new app icon.

Add-new-App-Icon

Your application is now ready to be launched!

External links:

Add link into android app on Stackoverflow
Change icon in android app on Stackoverflow

Posted in android

Leave a Reply

Your email address will not be published. Required fields are marked *

*