Keme Appdev Reviwer

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

<RelativeLayout

Example layout for textview


<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/article_heading"
android:background="@color/design_default_color_primary"
android:textColor="@android:color/white"
android:padding="@dimen/padding_regular"

android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large"
android:textStyle="bold"
android:text="@string/article_title"/>

Textview with url


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/article_text"
android:id="@+id/article"
android:autoLink="web"
android:lineSpacingExtra="@dimen/line_spacing"
android:padding="@dimen/padding_regular"
/>

Example layout for scrolling text


<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/article_subheading">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

Example for receiving reply


public static final String LOG_TAG = MainActivity.class.getSimpleName();
public static final String EXTRA_MESSAGE =
"com.example.twoactivities.extra.MESSAGE";
public static final int TEXT_REQUEST = 1;

private EditText mMessageEditText;


private TextView mReplyHeadTextView;
private TextView mReplyTextView;
mMessageEditText = findViewById(R.id.editText_main);
mReplyHeadTextView = findViewById(R.id.text_header_reply);
mReplyTextView = findViewById(R.id.text_message_reply);

@Override
public void onActivityResult(int requestCode, int resultCode, Intent
data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == TEXT_REQUEST){
if (resultCode == RESULT_OK){
String reply =
data.getStringExtra(SecondActivity.EXTRA_REPLY);
mReplyHeadTextView.setVisibility(View.VISIBLE);
mReplyTextView.setText(reply);
mReplyTextView.setVisibility(View.VISIBLE);
}
}
}

public void launchSecondActivity(View view) {


Log.d(LOG_TAG,"Button Clicked");
Intent intent = new Intent(this, SecondActivity.class);
String message = mMessageEditText.getText().toString();
intent.putExtra(EXTRA_MESSAGE,message);
startActivityForResult(intent, TEXT_REQUEST);
}
}
example for returning reply

public void returnReply(View view) {


String reply = mReply.getText().toString();
Intent replyIntent = new Intent();
replyIntent.putExtra(EXTRA_REPLY, reply);
setResult(RESULT_OK, replyIntent);
finish();
}
}
example for edit text
<EditText
android:id="@+id/editText_second"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginBottom="16dp"
android:ems="10"
android:hint="@string/edittext_second"
android:inputType="textLongMessage"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button_second"
app:layout_constraintStart_toStartOf="parent" />
Example of lifecycle
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_second);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v,
insets) -> {
Insets systemBars =
insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right,
systemBars.bottom);
return insets;
});

Log.d(LOG_TAG,"-------");
Log.d(LOG_TAG,"onCreate");

mReply = findViewById(R.id.editText_second);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = findViewById(R.id.text_message_reply);
textView.setText(message);
}
public void returnReply(View view) {
String reply = mReply.getText().toString();
Intent replyIntent = new Intent();
replyIntent.putExtra(EXTRA_REPLY, reply);
setResult(RESULT_OK, replyIntent);
Log.d(LOG_TAG, "End SecondActivity");
finish();
}

@Override
public void onStart(){
super.onStart();
Log.d(LOG_TAG,"onStart");
}
@Override
public void onPause(){
super.onPause();
Log.d(LOG_TAG,"onPause");
}
@Override
public void onRestart(){
super.onRestart();
Log.d(LOG_TAG,"onRestart");
}
@Override
public void onResume(){
super.onResume();
Log.d(LOG_TAG,"onResume");
}
@Override
public void onStop(){
super.onStop();
Log.d(LOG_TAG,"onStop");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(LOG_TAG, "onDestroy");
}
}

<LinearLayout (open website, open location, message)


public class MainActivity extends AppCompatActivity {
private EditText mWebsiteEditText;
private EditText mLocationEditText;
private EditText mShareTextEditText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
mWebsiteEditText = findViewById(R.id.website_edittext);
mLocationEditText = findViewById(R.id.location_edittext);
mShareTextEditText = findViewById(R.id.share_edittext);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main),
(v, insets) -> {
Insets systemBars =
insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right,
systemBars.bottom);
return insets;
});
}

public void openWebsite(View view) {


String url = mWebsiteEditText.getText().toString();
Uri webpage = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, webpage);

if (intent.resolveActivity(getPackageManager()) !=null){
startActivity(intent);
} else {
Log.d("ImplicitIntents", "Can't handle this!");
}
}

public void openLocation(View view) {


String loc = mLocationEditText.getText().toString();
Uri addressUri = Uri.parse("geo:0,0?q="+ loc);
Intent intent = new Intent(Intent.ACTION_VIEW, addressUri);

if (intent.resolveActivity(getPackageManager())!=null) {
startActivity(intent);
}else {
Log.d("ImplicitIntents", "Can't handle the intent!");
}
}
public void shareText(View view) {
String txt = mShareTextEditText.getText().toString();
String mimeType = "text/plain";
ShareCompat.IntentBuilder
.from(this)
.setType(mimeType)
.setChooserTitle(R.string.edittext_share)
.setText(txt)
.startChooser();
}
}

multi line in address, radio button, phone number, spinner


<TextView
android:id="@+id/address_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:text="@string/address_label_text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/name_label"
/>

<EditText
android:id="@+id/address_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/enter_address_hint"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:inputType="textMultiLine"
app:layout_constraintBaseline_toBaselineOf="@+id/address_label"
app:layout_constraintStart_toEndOf="@id/address_label"
/>

<TextView
android:id="@+id/phone_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp"
android:text="@string/phone_label_text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/address_text"
/>

<EditText
android:id="@+id/phone_text"
android:layout_width="134dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:ems="10"
android:hint="@string/enter_phone_hint"
android:inputType="phone"
app:layout_constraintBaseline_toBaselineOf="@+id/phone_label"
app:layout_constraintStart_toEndOf="@id/phone_label"
/>

<TextView
android:id="@+id/note_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:text="@string/note_label_text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/phone_label"
/>

<EditText
android:id="@+id/note_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:gravity="start|top"
android:hint="@string/enter_note_hint"
android:inputType="textCapSentences|textMultiLine"
app:layout_constraintBaseline_toBaselineOf="@+id/note_label"
app:layout_constraintStart_toEndOf="@id/note_label"
tools:layout_editor_absoluteX="64dp" />

<TextView
android:id="@+id/delivery_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:text="@string/choose_delivery_method"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/note_text" />

<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/delivery_label">

<RadioButton
android:id="@+id/sameday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/same_day_messenger_service"
android:onClick="onRadioButtonClicked" />

<RadioButton
android:id="@+id/nextday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/next_day_ground_delivery"
android:onClick="onRadioButtonClicked" />

<RadioButton
android:id="@+id/pickup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pick_up"
android:onClick="onRadioButtonClicked" />

</RadioGroup>

<Spinner
android:id="@+id/label_spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="24dp"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/phone_text"
app:layout_constraintTop_toBottomOf="@+id/address_text" />

Clickable image
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here
if (item.getItemId() == R.id.action_order) {
displayToast(getString(R.string.action_order_message));
return true;
} else if (item.getItemId() == R.id.action_status) {
displayToast(getString(R.string.action_status_message));
return true;
} else if (item.getItemId() == R.id.action_favorites) {
displayToast(getString(R.string.action_favorites_message));
return true;
} else if (item.getItemId() == R.id.action_contact) {
displayToast(getString(R.string.action_contacts_message));
return true;
} else {
// Handle other cases if needed
}

return super.onOptionsItemSelected(item);
}

@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this,
R.id.nav_host_fragment_content_main);
return NavigationUI.navigateUp(navController, appBarConfiguration)
|| super.onSupportNavigateUp();
}
public void displayToast(String message){
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).
show();
}

public void showDonutOrder(View view) {


displayToast(getString(R.string.donut_order_message));

public void showIceCreamOrder(View view) {


displayToast(getString(R.string.ice_cream_order_message));
}

public void showFroyoOrder(View view) {


displayToast(getString(R.string.froyo_order_message));
}
}

For alert
public void onClickShowAlert(View view) {
AlertDialog.Builder myAlertBuilder = new
AlertDialog.Builder(MainActivity.this);
myAlertBuilder.setTitle("Alert");

myAlertBuilder.setMessage(R.string.click_ok_to_continue_or_cancel_to_stop);
myAlertBuilder.setPositiveButton(R.string.ok_button, new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),
R.string.toast_ok_message, Toast.LENGTH_SHORT).show();
}
});
myAlertBuilder.setNegativeButton(R.string.cancel_button, new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),
R.string.toast_cancel_message, Toast.LENGTH_SHORT).show();
}
});
myAlertBuilder.show(); // Show the alert dialog
}

date picker
public class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {

@Nullable
@Override
public DatePickerDialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);

// Create a new instance of DatePickerDialog and return it


return new DatePickerDialog(getActivity(), this, year, month, day);
}

@Override
public void onDateSet(DatePicker datePicker, int year, int month, int
day) {
// Get a reference to the MainActivity and call the method to process
the date
MainActivity activity = (MainActivity) getActivity();
if (activity != null) {
activity.processDatePickerResult(year, month, day);
}
}
}

// Method to show the date picker when the button is clicked


public void showDatePicker(View view) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(),
getString(R.string.datepicker));
}

// Method to process the date picker result


public void processDatePickerResult(int year, int month, int day) {
// Month is 0-based, so you may want to add 1 to the month value
String selectedDate = day + "/" + (month + 1) + "/" + year;
Toast.makeText(this, getString(R.string.date_) + selectedDate,
Toast.LENGTH_SHORT).show();
}
}

You might also like