Created
September 14, 2018 17:30
-
-
Save arslan74/ef4a53d6ac64b841f47b7a13f67ad754 to your computer and use it in GitHub Desktop.
Example ViewGroup and View
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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" | |
android:padding="16dp" | |
tools:context=".MainActivity"> | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:id="@+id/title" | |
android:gravity="center_horizontal" | |
android:hint="*****" | |
android:textSize="24sp" | |
/> | |
<EditText | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="10dp" | |
android:hint="Enter Text Here" | |
android:id="@+id/input"/> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="10dp" | |
android:orientation="horizontal"> | |
<Button | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:text="clear" | |
android:layout_marginRight="5dp" | |
android:id="@+id/clear"/> | |
<Button | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:layout_marginLeft="5dp" | |
android:text="Add" | |
android:onClick="addbutton" | |
android:id="@+id/add"/> | |
</LinearLayout> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.arslan.bs_app; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
public class MainActivity extends AppCompatActivity { | |
// views | |
private TextView titleTextView; | |
private Button addButton ,clearButton; | |
private EditText inputEditText; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
titleTextView= findViewById(R.id.title); | |
addButton= findViewById(R.id.add); | |
clearButton= findViewById(R.id.clear); | |
inputEditText =findViewById(R.id.input); | |
addButton.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
if(inputEditText.getText().toString().trim().length() == 0){ | |
Toast.makeText(MainActivity.this , "Enter Text" , Toast.LENGTH_SHORT).show(); | |
return; | |
} | |
if(inputEditText != null){ | |
if(titleTextView !=null){ | |
titleTextView.setText(inputEditText.getText().toString()); | |
} | |
} | |
} | |
}); | |
clearButton.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
if(inputEditText.getText().toString().trim().length() == 0){ | |
if(titleTextView.getText().toString().length() != 0){ | |
titleTextView.setText(""); | |
} | |
Toast.makeText(MainActivity.this , "No Text To Clear" , Toast.LENGTH_SHORT).show(); | |
return; | |
} | |
if(titleTextView !=null){ | |
titleTextView.setText(""); | |
} | |
if(inputEditText != null){ | |
inputEditText.setText(""); | |
} | |
} | |
}); | |
}//end of onCreate | |
public void addbutton(View view){ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment