<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="100sp"
android:gravity="center"
android:text="Hello World"
android:layout_marginTop="200dp"
android:paddingLeft="60dp"
android:textColor="@color/colorPrimaryDark"
android:textSize="30dp"
android:textAlignment="center"
android:layout_gravity="center"
android:textAllCaps="true"
android:textStyle="bold|italic"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="53dp"
tools:layout_editor_absoluteY="239dp" />
<Button
android:id="@+id/btnChange"
android:layout_width="170dp"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:text="Click me"
android:layout_marginTop="10dp"
android:background="@drawable/button_background"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners
android:radius="14dp"
/>
<gradient
android:angle="45"
android:centerX="35%"
android:centerColor="#47A891"
android:startColor="#E8E8E8"
android:endColor="#000000"
android:type="linear" />
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp" />
<size
android:width="270dp"
android:height="60dp"
/>
<stroke
android:width="3dp"
android:color="#878787"
/>
</shape>
Our design is ready,it's time to write some java code.Goto your mainActivity.java
- Define a textView and Button
- do casting of these
write code for changing the text of textView on button click.
Mainactivity.java
package com.example.sohailmustafa.helloworld;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
Button btnChange;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView)findViewById(R.id.textView);
btnChange=(Button)findViewById(R.id.btnChange);
btnChange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textView.setText("Hello Android");
}
});
}
}
Comments
Post a Comment