how to implement spinner with floating label? - android-layout

I tried using AutoCompleteTextView to do this.
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="#dimen/_64sdp"
android:layout_marginTop="#dimen/_10sdp"
android:layout_marginLeft="#dimen/_5sdp"
android:layout_marginRight="#dimen/_5sdp"
android:background="#color/colorWhite"
android:elevation="#dimen/_2sdp"
android:textColorHint="#color/colorDarkGreyOne">
<AutoCompleteTextView
android:id="#+id/autoCompleteTvCountry"
style="#style/StyleEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableRight="#drawable/ic_arrow_down"
android:hint="#string/label_country"
android:background="#android:color/transparent"
android:paddingTop="#dimen/_10sdp"
android:paddingLeft="#dimen/_10sdp"
android:layout_marginRight="#dimen/_10sdp"
android:textSize="17.3sp"/>
</com.google.android.material.textfield.TextInputLayout>
and using array adapter:
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, countryNameList);
AutoCompleteTextView mTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTvCountry);
mTextView.setAdapter(dataAdapter);
mTextView.setKeyListener(null);
mTextView.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event){
((AutoCompleteTextView) v).showDropDown();
return false;
}
});
I am facing two problems:
1. unable to get the #Override method onItemSelected which is required and
2. In the dropdown list of country, countries are too close like below image
how to solve this problem or any other way to do spinner with floating lebel having #Override method onItemSelected?

Related

popup window on my map Fragment android studio

can anyone help me with this i want to show a popupwindow on top of my map fragment when i click on a button it will display a popupwindow that contains text for example the text will be how the user will navigate the map
public class FirstFragment extends Fragment {
public FirstFragment() {
// require a empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_first, container, false);
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.maps); //use SuppoprtMapFragment for using in fragment instead of activity MapFragment = activity SupportMapFragment = fragment
mapFragment.getMapAsync(new OnMapReadyCallback() {
public void onMapReady(#NonNull GoogleMap googleMap) {
GoogleMap mMap = googleMap;
mMap.getUiSettings().setMyLocationButtonEnabled(true);
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.setBuildingsEnabled(true);
mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter(requireContext()));
mMap.setMapStyle(
MapStyleOptions.loadRawResourceStyle(requireContext(), R.raw.style));
CameraPosition googlePlex = CameraPosition.builder()
.target(new LatLng(14.5768, 121.0332))
.zoom(15)
.bearing(0)
.tilt(60)
.build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(googlePlex), 4000, null);
mMap.addMarker(new MarkerOptions()
.icon(bitmapDescriptorFromVector(getActivity(), R.drawable.ic_baseline_location_city_24))
.position(new LatLng(14.5768, 121.0332))
.title("Mandaluyong");
KmlLayer layer = null;
try {
layer = new KmlLayer(mMap, R.raw.mandaluyong, getContext());
} catch (Exception e) {
e.printStackTrace();
}
for (KmlPlacemark placemark : layer.getPlacemarks()) {
}
layer.addLayerToMap();
mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
#Override
public void onMapLongClick(LatLng point) {
mMap.addMarker(new MarkerOptions()
.position(point)
.title("Home")
.snippet("Your PlaceMarker!!!"));
this is the xml file i've added a floating action button on it now when click it i want to display a popup window that will show on top of my map fragment ive tried different things and it crashes or doesnt show the popup ive decided to ask here because i am stuck in this part
}
<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"
tools:context=".MainActivity"
xmlns:app="http://schemas.android.com/apk/res-auto">
<fragment
android:id="#+id/maps"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/infobutton"
android:src="#drawable/ic_baseline_info_24"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="97dp"
android:clickable="true"
android:rotation="0" />
<FrameLayout
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginLeft="60dp"
android:layout_marginTop="300dp">
</FrameLayout>
</RelativeLayout>
here are the java and xml files of the map fragment that i used
[maps app pic][1]
[1]: https://i.stack.imgur.com/xaBIR.png

Display Image In Android Studio On Device Connected Externally

I am new to Android Learning. I am trying to make a customAdapter that will display an image and a text in every row. In my Main_Activity, I have following code:`
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String [] foods= {"Biryani","Matar Qeema","Chicken"};
ListAdapter faizisAdapter= new CustomAdapter(this,foods);
ListView faizisListView= (ListView)findViewById(R.id.faizisListView);
faizisListView.setAdapter(faizisAdapter);
faizisListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String food= String.valueOf(parent.getItemAtPosition(position));
Toast.makeText(MainActivity.this,food,Toast.LENGTH_LONG).show();
}
});
}
}
Here is my CustomAdapter:
class CustomAdapter extends ArrayAdapter<String>{
CustomAdapter(Context context, String []foods)
{
super(context,R.layout.custom_row, foods);
}
public View getView(int position, #Nullable View convertView, #NonNull
ViewGroup parent) {
LayoutInflater faisizInflater = LayoutInflater.from(getContext());
View faizisView = faisizInflater.inflate(R.layout.custom_row, parent,
false);
String food = getItem(position);
TextView _foodsDisplay = (TextView)
faizisView.findViewById(R.id._foodDisplay);
ImageView _imageDisplay = (ImageView)
faizisView.findViewById(R.id._imageDisplay);
_imageDisplay.setImageResource(R.drawable.zaheer8423);
_foodsDisplay.setText(food);
return faizisView;
}
}
My Main_Activity_XML_FILE is:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
tools:context=".MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/faizisListView"
>
</ListView>
XML_FILE_FOR_customAdapter:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/_imageDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/zaheer8423"
/>
<TextView
android:id="#+id/_foodDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
I haved saved image file in app/src/main/res/drawable/zaheer8423.jpg. I have connected my android device with android studio externally. When I run the app It opens and suddenly closes.
I have seen ways that use bitmap but I could not use them effectively, because I have very little knowledge about them.
Please help me here.
Looks like your Image size is big,You might need to scale down image something like this.
// Decode with inSampleSize
BitmapFactory.Options option = new BitmapFactory.Options();
option.inSampleSize = scale;
//You need to calculate appropriate sample size
Bitmap bitmap =
BitmapFactory.decodeResource(getContext().getResources(),R.drawable.zaheer8423,option);
imageDisplay.setImageBitmap(bitmap);
As its adapter you need to manage caching the bitmap and creating bitmap should be done in worker thread (Non Main thread i.e. UI Thread ) to avoid ANRs.

changing an edit text from another activiy after clicking a button

Okay so I'm new at programming, and for a school project I have to make a coffee shop app in android studio.
What I want to know, is how can I after clicking a button, edit a space for text to add text about the item they will buy but place it in another activity.
The thing is I want to make a kind of add to cart thing, and after going to the cart tab, there is an edit text where you see how much the account will be.
Can anyone help me with this??
You could use the Alert Dialog with Edit Text and pass the values using Shared Preference.
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater=MainActivity.this.getLayoutInflater();
//this is what I did to added the layout to the alert dialog
View layout=inflater.inflate(R.layout.editxml,null);
alert.setView(layout);
alert.setTitle("Enter Name");
final EditText usernameInput=(EditText)layout.findViewById(R.id.editText1);
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
//do your stuff here
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alert.show();
editxml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:id="#+id/editText1" />
</LinearLayout>

Android alert dialog button background

In Android Alert Dialog : dialog.getButton is not available
How to change the background of the Positive button in laert dialog
You need to write it after
dialog.show();
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(NewApplication.this);
alertDialogBuilder.setCancelable(false).setNegativeButton("Yes",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
alertDialog.getButton(Dialog.BUTTON_NEGATIVE).
setBackgroundColor(Color.parseColor("#3399ff"));
I think this has been addressed here before, IRC.
Check these answers posted earlier on a similar issue:
Android Button modify Question.
Good links and answers there.
First off, the
'
`DialogInterface.OnClickListener`'
listener — must not be NULL .
Second make sure you imported the correct Android classes, there are a lot of them, and some are version specific.
For example:
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.os.Handler;
import android.util.Log;
Here is a quick and dirty sample of code in Java that illustrates the use of some special Android imports you need for an application. Remember Android uses many special classes and objects all its own.
final AlertDialog d = new AlertDialog.Builder(this)...create();
//final Button b1 = d.getButton(Dialog.BUTTON_POSITIVE);
editName.addTextChangedListener(new TextWatcher() //
{
public void afterTextChanged(Editable ed) {
Button b1 = d.getButton(Dialog.BUTTON_POSITIVE);
//comment out sections that may or may not work //
b1.setEnabled(ed.length() > 0);
private static final class [More ...] ButtonHandler extends Handler {
// Button clicks have Message.what as the BUTTON{1,2,3} constant//
private static final int MSG_DISMISS_DIALOG = 1;
private WeakReference<DialogInterface> mDialog;
public void // whatever classes you want //
ButtonHandler(DialogInterface dialog) {
mDialog = new WeakReference<DialogInterface>(dialog);
}
Use override to set replacement buttons or new button messages in a new section. It is one way to use the same button positioning for a new function. For example:
#Override
public void [More ...]
handleMessage(Message msg) {
switch (msg.what) {
case DialogInterface.BUTTON_POSITIVE:
case DialogInterface.BUTTON_NEGATIVE:
case DialogInterface.BUTTON_NEUTRAL:
((DialogInterface.OnClickListener)
msg.obj).onClick(mDialog.get(), msg.what);
break;
case MSG_DISMISS_DIALOG:
((DialogInterface) msg.obj).dismiss();
}
Hope this helps answer your question.
Code illustrated for illustration only, it is not the actual code, you need to write your own.
As an additional note: If you are using eclipse or other GUI interface, it tends to add a lot of additional and unnecessary code. Try coding on a text editor like Notepad++ and you will find it to be cleaner and smoother.
U try to Custom Alert dialog
<LinearLayout
android:id="#+id/ll_head"
android:layout_width="match_parent"
android:layout_height="30dp"
android:orientation="horizontal"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:background="#color/colorPrimaryDark"
app:layout_constraintRight_toRightOf="parent">
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="63dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_below="#+id/ll_head"
android:layout_alignParentStart="true"
android:background="#color/colorAccent"
android:layout_marginLeft="36dp"
android:layout_marginStart="36dp"
android:layout_marginBottom="36dp"
android:id="#+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button"
android:layout_alignBottom="#+id/button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="#android:color/holo_blue_bright"
android:layout_marginRight="53dp"
android:layout_marginEnd="53dp" /> </RelativeLayout>
public class MainActivity extends AppCompatActivity {
Dialog alertDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView=(TextView) findViewById(R.id.text);
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDialog=new Dialog(MainActivity.this);
alertDialog.setContentView(R.layout.dialog);
alertDialog.show();
}
});
}
}

Android: Attempting to switch between two Images and display them as the same size

So, basically, I just want to display an image, and when that image is clicked, I want to replace it with another image (and go back and forth between the two images onClick). This works fine, but I want to display them as the exact same size, but no matter what I try, they're not appearing as the same size on screen.
Here's my java file:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ImageView baneling = (ImageView) findViewById(R.id.bane);
baneling.setTag("alive");
baneling.setClickable(true);
baneling.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(baneling.getTag().equals("alive"))
{
baneling.setImageResource(R.drawable.banelingexplosion);
baneling.setTag("dead");
resizeImage(baneling);
}
else
{
baneling.setImageResource(R.drawable.baneling);
baneling.setTag("alive");
}
}
});
}
public void resizeImage(ImageView v)
{
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT);
v.setLayoutParams(layoutParams);
}
Here's my xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dip" >
<ImageView
android:id="#+id/bane"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/baneling" />
</LinearLayout>
I've tried a ton of things and have successfully resized the images, but can never get them to be the same size, even why I explicitly assign them numerical pixel values in the xml and in the .java.
Setting the android:scaleType="fitXY" attribute in your ImageView will make sure your image fills the entire bounds of the ImageView. That should help with explicitly setting the width and height of your image.
Nice Starcraft theme, by the way.

Resources