Hello Fellow developers!
I have developed an Android App that uses the Flickr API to display any image upon users request. I am the final step of the development. Everything is looking great, except for when you click on any image to expand it in detail view & when you click the return arrow to go back - DOES NOT respond...
Any help would be greatly appreciated:)
ViewPhotoDetails.java:
package com.example.flickrbrowser;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import androidx.annotation.RequiresApi;
public class ViewPhotoDetailsActivity extends BaseActivity {
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photo_details);
activateToolbarWithHomeEnabled();
Intent intent = getIntent();
Photo photo = (Photo) intent.getSerializableExtra(PHOTO_TRANSFER);
TextView photoTitle = (TextView) findViewById(R.id.photo_title);
photoTitle.setText("Title: " + photo.getTitle());
TextView photoTags = (TextView) findViewById(R.id.photo_tags);
photoTags.setText("Tags: " + photo.getTags());
TextView photoAuthor = (TextView) findViewById(R.id.photo_author);
photoAuthor.setText(photo.getAuthor());
ImageView photoImage = (ImageView) findViewById(R.id.photo_image);
Picasso.with(this).load(photo.getLink())
.error(R.drawable.placeholder)
.placeholder(R.drawable.placeholder)
.into(photoImage);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_photo_details, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return false;
}
}
photo_details.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:flickr="http://schemas.android.com/apk/res-auto"
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.flickrbrowser.ViewPhotoDetailsActivity"
tools:showIn="#layout/photo_details">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/app_bar"
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize" />
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
flickr:cardBackgroundColor="?colorPrimary"
flickr:cardCornerRadius="8dp"
flickr:cardPreventCornerOverlap="false"
flickr:contentPaddingTop="16dp"
flickr:contentPaddingBottom="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/photo_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:scaleType="centerCrop"
android:src="#drawable/placeholder" />
<TextView
android:id="#+id/photo_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|center"
android:paddingTop="8dp"
android:textColor="#color/flickrSecondaryTextColor"
android:textSize="14sp" />
</FrameLayout>
<TextView
android:id="#+id/photo_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:textColor="#color/flickrPrimaryTextColor"
android:textSize="14sp" />
<TextView
android:id="#+id/photo_tags"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:textColor="#color/flickrPrimaryTextColor"
android:textSize="10sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</ScrollView>
Source Code on GitHub
App UI
Back button has id: android.R.id.home. So you can handle it as
if (item?.itemId == android.R.id.home) {
finish()
}
or just modify your
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
if (item.itemId == R.id.action_settings) {
// TODO - handle 'settings' click
}
return super.onOptionsItemSelected(item)
}
instead of returning just true/false so home button will be handled by super method.
Just as #Boken said, the back (also called the "up") button is a menu button which has id android.R.id.home so when that button is clicked you can handle the click like so:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
//You can also use if-else statements instead of switch (switch is a better option)
case R.id.action_settings:
return true;
case android.R.id.home: //when the back button is clicked
// handle this action here
return true;
default:
return super.onOptionsItemSelected(item);
}
}
if you want to return to the previous screen you use onBackPressed() like so:
case android.R.id.home: //when the back button is clicked
onBackPressed();
return true;
(More Direct Answer) Was not checking for that menu item. Had to change onOptionsItemSelected (in ViewPhotoDetailsActivity.java) to
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
} else if (id == android.R.id.home) {
finish();
}
return false;
}
Related
I tried Action Bar like this
I want like when i click on Actionbar's Item it will print a Toast but it is not working tried a lot.
I Created 2 custom layout,one for Notification custom item and another for Task custom item.
custom_action_notification_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
style="?attr/actionButtonStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:focusable="true">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:src="#drawable/noti"/>
<TextView
android:id="#+id/cart_badge"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="right|end|top"
android:layout_marginTop="3dp"
android:layout_marginLeft="15dp"
android:background="#drawable/badge_circle"
android:gravity="center"
android:padding="3dp"
android:textColor="#android:color/white"
android:text="0"
android:textSize="10sp"/>
</RelativeLayout>
custom_action_task_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
style="?attr/actionButtonStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:focusable="true">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:src="#drawable/task"
android:id="#+id/imageView5" />
<TextView
android:id="#+id/cart_badge1"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="15dp"
android:layout_gravity="right|end|top"
android:background="#drawable/badge_circle"
android:gravity="center"
android:padding="3dp"
android:textColor="#android:color/white"
android:text="0"
android:textSize="10sp"
android:layout_alignTop="#+id/imageView5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
navigation.menu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:apps="http://schemas.android.com/tools">
<item
android:id="#+id/action_refresh"
android:icon="#drawable/noti"
app:showAsAction="always"
android:actionLayout="#layout/custom_action_notification_layout"
android:title="Refresh"/>
<item
android:id="#+id/action_task"
android:orderInCategory="100"
android:actionLayout="#layout/custom_action_task_layout"
android:title="Task"
android:icon="#drawable/task"
app:showAsAction="always" /></menu>
Now in my navigationdrawer.class i put thiscode onCreateOptionsMenu(), get ActionView of Notification and Task items and Set OnClick listeners to those ActionView.
navigation.class
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.navigation, menu);
// Notification
final MenuItem itemNotification = menu.findItem(R.id.action_refresh);
MenuItemCompat.setActionView(itemNotification, R.layout.custom_action_notification_layout);
View actionViewNotification = MenuItemCompat.getActionView(itemNotification);
// RelativeLayout notifCount = (RelativeLayout) MenuItemCompat.getActionView(itemNotification);
actionViewNotification.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onOptionsItemSelected(itemNotification);
}
});
final MenuItem itemNotification1 = menu.findItem(R.id.action_task);
MenuItemCompat.setActionView(itemNotification1, R.layout.custom_action_task_layout);
View actionViewNotification1 = MenuItemCompat.getActionView(itemNotification1);
// RelativeLayout notifCount1 = (RelativeLayout) MenuItemCompat.getActionView(itemNotification1);
actionViewNotification1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onOptionsItemSelected(itemNotification1);
}
});
Now finally in onOptionsItemSelected() the code is like
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh: {
// Do something
Toast toast = Toast.makeText(this, "Notification clicked", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return true;
}
case R.id.action_task: {
// Do something
Toast toast = Toast.makeText(this, "Task clicked", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return true;
}
}
Tried in many ways but not working at all please help out from this..
1. Create two custom layout, one for Notification custom item and another for Task custom item.
custom_action_notification_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
style="?attr/actionButtonStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:focusable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/icon_notification"/>
<TextView
android:id="#+id/cart_badge"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="right|end|top"
android:layout_marginEnd="-5dp"
android:layout_marginRight="-5dp"
android:layout_marginTop="3dp"
android:background="#drawable/badge_background"
android:gravity="center"
android:padding="3dp"
android:textColor="#android:color/white"
android:text="0"
android:textSize="10sp"/>
</FrameLayout>
custom_action_task_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
style="?attr/actionButtonStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:focusable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/icon_task"/>
<TextView
android:id="#+id/cart_badge"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="right|end|top"
android:layout_marginEnd="-5dp"
android:layout_marginRight="-5dp"
android:layout_marginTop="3dp"
android:background="#drawable/badge_background"
android:gravity="center"
android:padding="3dp"
android:textColor="#android:color/white"
android:text="0"
android:textSize="10sp"/>
</FrameLayout>
2. Create a menu XML containing Notification and Task item. Use attribute app:actionLayout to set custom layout to each item.
custom_menu.xml
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_notification"
android:icon="#drawable/icon_notification"
android:title="Notification"
app:actionLayout="#layout/custom_action_notification_layout"
app:showAsAction="always"/>
<item
android:id="#+id/action_task"
android:icon="#drawable/icon_task"
android:title="Task"
app:actionLayout="#layout/custom_action_task_layout"
app:showAsAction="always"/>
</menu>
3. In your Activity onCreateOptionsMenu(), get ActionView of Notification and Task items and Set OnClick listeners to those ActionView.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.custom_menu, menu);
// Notification
final MenuItem itemNotification = menu.findItem(R.id.action_notification);
View actionViewNotification = MenuItemCompat.getActionView(itemNotification);
actionViewNotification.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onOptionsItemSelected(itemNotification);
}
});
// Task
final MenuItem itemTask = menu.findItem(R.id.action_task);
View actionViewTask = MenuItemCompat.getActionView(itemTask);
actionViewTask.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onOptionsItemSelected(itemTask);
}
});
return true;
}
4. Finally, In onOptionsItemSelected() do the rest:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_notification: {
// Do something
Toast toast = Toast.makeText(this, "Notification clicked", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return true;
}
case R.id.action_task: {
// Do something
Toast toast = Toast.makeText(this, "Task clicked", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return true;
}
}
return super.onOptionsItemSelected(item);
}
FYI, I have checked Notification and Task menu item click event by showing Toast messages.
OUTPUT:
UPDATE:
In your updated code use:
MenuItemCompat.setActionView(itemNotification, R.layout.custom_action_notification_layout);
View actionViewNotification = MenuItemCompat.getActionView(itemNotification);
Instead of
View actionViewNotification = MenuItemCompat.getActionView(itemNotification);
MenuItemCompat.setActionView(itemNotification, R.layout.custom_action_notification_layout);
Hope this will help~
I'm making a chat activity for a school project and I want the newest messages at the bottom of the listView. Adding the code below does not work. I simply want the newest message to appear at the bottom and make the listView automatically scroll to the last list item. Thanks in advance!
android:transcriptMode="alwaysScroll"
android:stackFromBottom="true"
Link to image of current situation
XML:
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#1a1a1a"
android:id="#+id/relativeLayout_Messages"
android:layout_below="#id/my_toolbar"
android:layout_alignBottom="#+id/imageView_Separator">
<ListView
android:id="#+id/listView_Messages"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stackFromBottom="true"
/>
</RelativeLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/imageView_Separator"
android:background="#1a1a1a"
android:src="#drawable/seperator"
android:layout_above="#+id/relativeLayout_ChatInput"
/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="#+id/relativeLayout_ChatInput"
android:background="#1a1a1a">
<EditText
android:layout_width="wrap_content"
android:layout_height="50dp"
android:id="#+id/editText_ChatInput"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="5dp"
android:background="#drawable/search_field"
android:layout_toStartOf="#+id/imageButton_Send"
android:layout_alignBottom="#+id/imageButton_Send"
android:inputType="textPersonName"
android:hint="#string/chat_field_hint"
android:textColor="#f4f5f6"
android:textColorHint="#f4f5f6"/>
<ImageButton
android:layout_width="32dp"
android:layout_height="32dp"
android:id="#+id/imageButton_Send"
android:background="#android:color/transparent"
android:src="#drawable/send_button"
android:scaleType="fitCenter"
android:layout_marginTop="10dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="6dp"
android:layout_marginBottom="8dp"
android:layout_alignParentEnd="true"
android:onClick="sendButtonClicked"
/>
</RelativeLayout>
</RelativeLayout>
JAVA
package com.example.muhryn.resonatem;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.content.Context;
import android.view.inputmethod.InputMethodManager;
import android.widget.*;
import java.util.*;
public class ChatActivity extends AppCompatActivity{
public void hideKeyboard() {
try {
InputMethodManager imm=(InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
} catch (Exception ex) { }
}
private ListView chatListView;
private Button submitButton;
private EditText chatEditText;
private Chat chat;
private ArrayList receivedList=new ArrayList();
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
chatListView=(ListView)findViewById(R.id.listView_Messages);
chatEditText=(EditText)findViewById(R.id.editText_ChatInput);
chat=new Chat("A","Resonate");
chat.setListener(new Chat.Listener(){
public void received(final String received) {
System.out.println("Received: "+received);
runOnUiThread(new Runnable() {
public void run() {
hideKeyboard();
receivedList.add(0, received);
chatListView.setAdapter(new ChatList(ChatActivity.this, receivedList));
}
});
//////((ArrayAdapter)chatListView.getAdapter()).insert(received,0);
}
});
}
public void sendButtonClicked(View view){
String textToSubmit=chatEditText.getText().toString().trim();
if(textToSubmit.isEmpty())
return;
if(chat.submitted(textToSubmit)) {
receivedList.add(0,"Sent: "+textToSubmit);
chatListView.setAdapter(new ChatList(ChatActivity.this, receivedList));
} else
Toast.makeText(this,"Failed to submit "+textToSubmit+".",Toast.LENGTH_SHORT).show();
}
#Override
public void onStop () {
super.onStop();
chat.stop();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.chat_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.view_profile:
finish();
return true;
case R.id.report_match:
finish();
return true;
case R.id.add_match:
finish();
return true;
case R.id.unmatch:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Try the below code.
chatListView.smoothScrollByOffset(receivedList.size()-1);
It will scroll down to latest chat.
how do I combine the
slected item of 3 spinners i have created and link it to a button??
here is my code...
package com.example.nikhiljoshi.time_table;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
char branch1;
Spinner spbranch,spsem,spdiv;
ArrayAdapter<CharSequence>adapter;
ArrayAdapter<CharSequence>adapter1;
ArrayAdapter<CharSequence>adapter2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button Submit = (Button) findViewById(R.id.button);
spbranch = (Spinner)findViewById(R.id.spinner);
spsem = (Spinner)findViewById(R.id.spinner2);
spdiv = (Spinner)findViewById(R.id.spinner3);
adapter = ArrayAdapter.createFromResource(this,R.array.branch,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spbranch.setAdapter(adapter);
spbranch.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int i;
// branch1=spbranch.getItemAtPosition(i).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
adapter1 = ArrayAdapter.createFromResource(this,R.array.sem,android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spsem.setAdapter(adapter1);
adapter2 = ArrayAdapter.createFromResource(this,R.array.division,android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spdiv.setAdapter(adapter2);
Submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(view, "this is my Toast message!!! =)",
// Toast.LENGTH_LONG).show();
// Intent intent = new Intent(v.getContext(), nik.class);
// startActivityForResult(intent, 0);
}
});
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
layout...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Branch"
android:id="#+id/textView"
android:layout_marginTop="105dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="27dp"
android:layout_marginStart="27dp"
android:textSize="20dp" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner"
android:layout_marginLeft="34dp"
android:layout_marginStart="34dp"
android:layout_alignTop="#+id/textView"
android:layout_alignLeft="#+id/button"
android:layout_alignStart="#+id/button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sem"
android:id="#+id/textView2"
android:layout_below="#+id/spinner"
android:layout_alignLeft="#+id/textView"
android:layout_alignStart="#+id/textView"
android:layout_marginTop="30dp"
android:textSize="20dp" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner2"
android:layout_alignTop="#+id/textView2"
android:layout_alignLeft="#+id/spinner"
android:layout_alignStart="#+id/spinner" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Division"
android:id="#+id/textView3"
android:layout_below="#+id/spinner2"
android:layout_alignLeft="#+id/textView2"
android:layout_alignStart="#+id/textView2"
android:layout_marginTop="30dp"
android:textSize="20dp" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner3"
android:layout_alignTop="#+id/textView3"
android:layout_alignLeft="#+id/spinner2"
android:layout_alignStart="#+id/spinner2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="#+id/button"
android:layout_below="#+id/spinner3"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter the details"
android:id="#+id/textView4"
android:textSize="20dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
when i press the submit button, the following things should happen
for eg
if(branch=='cs'&& sem=='5' && div=='A')
then new activity
how do i do that?
You can use Spinner.getSelectedItem() to get the currently selected values from your three spinners, then use those values to determine if you should launch your Activity.
I have a ViewFlipper populated with multiple ImageView. but now, i wanted to populate the view flipper with images from mysql database. But i don't know how to implement it.
here's my ViewFlipper.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Login">
<ViewFlipper
android:id="#+id/view_flipper"
android:layout_width="fill_parent"
android:flipInterval="5000"
android:inAnimation="#android:anim/slide_in_left"
android:outAnimation="#android:anim/slide_out_right"
android:layout_height="fill_parent"
android:layout_centerInParent="true">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/login_pic1"/>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/login_pic2"/>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/login_pic3"/>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/login_pic4"/>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/login_pic5"/>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/pic6"/>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/pic7"/>
</ViewFlipper>
</RelativeLayout>
and here is my MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button btn_login = (Button)findViewById(R.id.login);
Button btn_signup = (Button)findViewById(R.id.create);
final EditText txt_username = (EditText)findViewById(R.id.username);
final EditText txt_password = (EditText)findViewById(R.id.password);
getSupportActionBar().hide();
ViewFlipper flipper = (ViewFlipper)findViewById(R.id.view_flipper);
flipper.startFlipping();
Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.logo);
Bitmap circularBitmap = ImageConverter.getRoundedCornerBitmap(bitmap, 80);
ImageView circularImageView = (ImageView)findViewById(R.id.logo);
circularImageView.setImageBitmap(circularBitmap);
View.OnClickListener login_click = new View.OnClickListener(){
//login form validation
public void onClick(View view) {
String user = txt_username.getText().toString();
String pass = txt_password.getText().toString();
if(user.length()==0)
{
txt_username.requestFocus();
txt_username.setError("Username cant be empty");
}
else if(pass.length()==0){
txt_password.requestFocus();
txt_password.setError("Password cant be empty");
}
else
{
Toast.makeText(Login.this,"Login Successful", Toast.LENGTH_LONG).show();
startActivity(new Intent(Login.this,Homepage.class));
}
}
};
btn_login.setOnClickListener(login_click);
View.OnClickListener signup_click = new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Login.this,Registration.class));
}
};
btn_signup.setOnClickListener(signup_click);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_login, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
and here my JSON.
Hope you can help me :)
I have been working on various Android apps for quite some time now and am trying to finalize them. One of these apps involves creating a text field with a button. Below the button and the text field is some text (aligned left) and a spinner(calendar) which aligned right. Here is my code...
MainActivity.java
package com.miller.lab2;
import android.R;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_item);
Spinner spinner = (Spinner) findViewById(R.id.listofmonths);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.monthlist, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onClick(View view) {
EditText input = (EditText) findViewById(R.id.text);
String string = input.getText().toString();
Toast.makeText(this, string, Toast.LENGTH_LONG).show();
}
Resources res = getResources();
String[] monthlist = res.getStringArray(R.array.monthlist);
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.miller.lab2.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:labelFor="#+id/text"
android:text="#string/enter_message" />
<TextView
style="#+style/CustomText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button"
android:layout_toLeftOf="#+id/button"
android:text="#string/text"
android:textColor="#347C17" />
<!-- <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button"
android:layout_toRightOf="#+id/button"
android:text="#string/text2" /> -->
<Spinner
android:id="#+id/monthlist"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
<EditText
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/enter_message"
android:inputType="text"
android:text="#string/text_field" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/text"
android:layout_centerInParent="true"
android:text="#string/button" />
</RelativeLayout>
main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.miller.lab2.MainActivity" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
</menu>
I'm getting the following errors in my MainActivty.java:
listofmonths cannot be resolved or is not a field
monthlist cannot be resolved or is not a field
main cannot be resolved or is not a field
action_settings cannot be resolved or is not a field
text cannot be resolved or is not a field
Any help would be greatly appreciated
You are importing the R class from android
Delete this import import android.R; and reimport the R class from your project