I am new to android programming, and I am trying to get a button into an vertical scrolling grid. The buttons are not functional or visible. When I enable "Show layout bounds" in developer options, You see the gridview allocates space for the buttons::
My code is as follows:
layout:
<?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"
android:weightSum="1">
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/gridView1"
android:scrollbars="vertical"
android:numColumns="4"
/>
</LinearLayout>
Fragment:
import android.app.ActionBar;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.fairphone.datatransparency.R;
import java.util.Iterator;
/**
* Created by koen on 11/25/15.
*/
public class NewTimeLineFragment extends Fragment{
public static final String ARG_OBJECT = "object";
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// The last two arguments ensure LayoutParams are inflated
// properly.
View rootView = inflater.inflate(
R.layout.new_time_line_layout, container, false);
Bundle args = getArguments();
ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT);
Button[] buttons = new Button[6];
View.OnClickListener oclBtn = new View.OnClickListener()
{
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "GORGABAL", 5).show();
}
};
for(int i = 0; i < buttons.length; i++){
buttons[i] = new Button(getActivity());
buttons[i].setBackgroundColor(Color.BLACK);
buttons[i].setVisibility(View.VISIBLE);
buttons[i].setOnClickListener(oclBtn);
buttons[i].setLayoutParams(lp);
buttons[i].setText("GORGABAL");
}
GridView gridview;
gridview = (GridView) rootView.findViewById(R.id.gridView1);
ArrayAdapter<Button> adapter = new ArrayAdapter<Button>(this.getActivity(), android.R.layout.simple_list_item_1, buttons);
gridview.setAdapter(adapter);
gridview.setVisibility(View.VISIBLE);
Log.d("Debug", "new_time_line fragment created");
return rootView;
}
}
Related
I am making an android ecommerce app and I downloaded an app template but in that template there is no firebase in the fragment and I want when the user clicks checkout button the products he selected should go to the firebase database but all videos I see they use activities .So please help me.
CartFragment.java
package com.example.shoppingcart.views;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toolbar;
import com.example.shoppingcart.R;
import com.example.shoppingcart.adapters.CartListAdapter;
import com.example.shoppingcart.cartholder;
import com.example.shoppingcart.databinding.FragmentCartBinding;
import com.example.shoppingcart.dataholder;
import com.example.shoppingcart.models.CartItem;
import com.example.shoppingcart.productholder;
import com.example.shoppingcart.viewmodels.ShopViewModel;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.firestore.FirebaseFirestore;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
public class CartFragment extends Fragment implements CartListAdapter.CartInterface {
private static final String TAG = "CartFragment";
private ImageView productImage;
private TextView productname;
private TextView productprice;
private TextView productcategory;
private Spinner productquantity;
UUID uuid = UUID.randomUUID();
String randomUUID = uuid.toString().trim();
ShopViewModel shopViewModel;
FragmentCartBinding fragmentCartBinding;
NavController navController;
Button button;
private void finishActivity() {
if (getActivity() != null) {
getActivity().finish();
}
}
public CartFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
fragmentCartBinding = FragmentCartBinding.inflate(inflater, container, false);
return fragmentCartBinding.getRoot();
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
navController = Navigation.findNavController(view);
final CartListAdapter cartListAdapter = new CartListAdapter(this);
fragmentCartBinding.cartRecyclerView.setAdapter(cartListAdapter);
fragmentCartBinding.cartRecyclerView.addItemDecoration(new DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL));
shopViewModel = new ViewModelProvider(requireActivity()).get(ShopViewModel.class);
shopViewModel.getCart().observe(getViewLifecycleOwner(), new Observer<List<CartItem>>() {
#Override
public void onChanged(List<CartItem> cartItems) {
cartListAdapter.submitList(cartItems);
fragmentCartBinding.placeOrderButton.setEnabled(cartItems.size() > 0);
}
});
shopViewModel.getTotalPrice().observe(getViewLifecycleOwner(), new Observer<Double>() {
#Override
public void onChanged(Double aDouble) {
fragmentCartBinding.orderTotalTextView.setText("Total: PKR " + aDouble.toString());
}
});
button = (Button) getView().findViewById(R.id.placeOrderButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(CartFragment.this.getActivity(), CheckoutActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finishActivity();
}
});
}
#Override
public void deleteItem(CartItem cartItem) {
shopViewModel.removeItemFromCart(cartItem);
}
#Override
public void changeQuantity(CartItem cartItem, int quantity) {
shopViewModel.changeQuantity(cartItem, quantity);
}
}
fragment_cart.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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">
<LinearLayout
android:orientation="vertical"
tools:context=".views.CartFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/cartRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="#layout/cart_row"
tools:itemCount="2"
/>
<Space
android:layout_width="match_parent"
android:layout_height="16dp" />
<TextView
android:id="#+id/orderTotalTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_margin="8dp"
android:text="Total: PKR 26"
android:textAppearance="#style/TextAppearance.MaterialComponents.Headline6" />
<Button
android:id="#+id/placeOrderButton"
style="#style/Widget.MaterialComponents.Button.UnelevatedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_margin="8dp"
android:text="Proceed To Checkout"
android:textAppearance="#style/TextAppearance.MaterialComponents.Caption" />
</LinearLayout>
</ScrollView>
What kind of firebase database you have to use because both of the methods are similar...
activity and fragment method is same not any kind of change to the method ...
i will show you firestore method because i don't know which one you have to use
in fragment first initilize
FirebaseFirestore firebaseFirestore;
oncreateview initialize
firebaseFirestore = FirebaseFirestore.getInstance();
after you can perform all your crud operation and all thing but first two line is require
simple use bro...
I am using BottomNavigationView and I added ListView to my layout. After adding this ListView, the previous screen wont disappear and this new screen overwrites the previous screen. Does someone have an idea why this happens.
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import java.util.ArrayList;
public class favaoritesFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_favorites,container,false);
String[] line =getResources().getStringArray(R.array.JR_East);
ListView listView = (ListView)view.findViewById(R.id.listview);
ArrayAdapter<String>adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,line);
listView.setAdapter(adapter);
return view;
}
}
The issue.
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listview"/>
</RelativeLayout>
Add Background in your parent View of all fragment,
android:background="#android:color/white"
I had tried do Spinner in some Fragment. I was doing that with tutorial on Youtube. So, I just rewrite all from that tutorial.
Firstly, in string : ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item);
my Android Studio didn`t saw android. It is just underlined in red, so I write :
ArrayAdapter adapter = new ArrayAdapter(this.getActivity(), android.R.layout.simple_spinner_item);
there were no errors at startup, but when I try to click on the spinner, the application crashes
This is my Fragment:
package com.example.itss;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class SettingsFragment extends Fragment {
private Spinner timeOfSleep;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.settings_fragment,container, false);
timeOfSleep = v.findViewById(R.id.timeOfsleep);
List<TimeOfSleep> timeOfSleepList = new ArrayList<>();
TimeOfSleep time5 = new TimeOfSleep(5);
timeOfSleepList.add(time5);
TimeOfSleep time10 = new TimeOfSleep(10);
timeOfSleepList.add(time10);
TimeOfSleep time15 = new TimeOfSleep(15);
timeOfSleepList.add(time15);
ArrayAdapter<TimeOfSleep> adapter = new ArrayAdapter<TimeOfSleep>(this.getActivity(), android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
timeOfSleep.setAdapter(adapter);
timeOfSleep.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TimeOfSleep tOS = (TimeOfSleep) parent.getSelectedItem();
displaytimeOfSleep(tOS);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
return v;
}
public void getSelectedtimeOfSleep(View v){
TimeOfSleep tOS = (TimeOfSleep) timeOfSleep.getSelectedItem();
displaytimeOfSleep(tOS);
}
private void displaytimeOfSleep(TimeOfSleep a){
int time = a.getTimeOfSleep();
}
}
Also java class
package com.example.itss;
public class TimeOfSleep {
private int timeOfSleep;
public TimeOfSleep(int timeOfSleep) {
this.timeOfSleep = timeOfSleep;
}
public int getTimeOfSleep() {
return timeOfSleep;
}
public void setTimeOfSleep(int timeOfSleep) {
this.timeOfSleep = timeOfSleep;
}
#Override
public String toString() {
String a = timeOfSleep + " мин";
return a;
}
}
And xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#000000"
android:padding="25dp"
android:paddingBottom="100dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Время засыпания:"
android:background="#color/colorPrimary"
android:textColor="#FFFFFF"
android:onClick="getSelectedtimeOfSleep"/>
<Spinner
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/ic_arrow_drop_down_black_24dp"
android:id="#+id/timeOfsleep">
</Spinner>
</LinearLayout>
</LinearLayout>
In Logs I have just: 16:28 Can't bind to local 8600 for debugger
If you are getting an error saying can't bind, then you need to restart your adb server by killing it first and starting it again.
adb kill-server
adb start-server
You can do this from the terminal
When I scroll past an item and come back to it, it changes it's position until I click on it, then it will correctly align itself. How can I solve this. Sorry I am still at the start of the project so my code is a bit messy.
This is my code:
package com.example.r_corp.androidslauncher;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
PackageManager packageManager;
Intent mainIntent = null;
static ArrayList<app> appList;
static GridView gridView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_all_apps_layout);
gridView = (GridView)findViewById(R.id.GridView);
gridView.setNumColumns(4);
packageManager = getPackageManager();
mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
appList = getApps();
show();
}
public ArrayList<app> getApps(){
List<ResolveInfo>apps = packageManager.queryIntentActivities(mainIntent, 0);
ArrayList<app> Apps = new ArrayList();
for(ResolveInfo ri : apps){
app App = new app();
App.icon = ri.loadIcon(packageManager);
App.Name = ri.loadLabel(packageManager).toString();
Apps.add(App);
}
return Apps;
}
public void show(){
ArrayAdapter<app> adapter = new ArrayAdapter<app>(this, R.layout.item,
appList) {
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.item,null);
}
ImageView appIcon = (ImageView) convertView
.findViewById(R.id.imageView);
appList.get(position).icon.setBounds(10,10,10,10);
appIcon.setImageDrawable(appList.get(position).icon);
appIcon.setLayoutParams(new RelativeLayout.LayoutParams(85, 85));
appIcon.setScaleType(ImageView.ScaleType.CENTER_CROP);
appIcon.setPadding(8, 8, 8, 8);
TextView appName = (TextView) convertView
.findViewById(R.id.textView);
appName.setText(appList.get(position).Name);
return convertView;
}
};
gridView.setFocusable(true);
gridView.setAdapter(adapter);
}
}
class app{
Drawable icon;
String Name;
}
show_all_apps_layout:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:stretchMode="columnWidth"
android:focusableInTouchMode="true"
android:horizontalSpacing="5dp"
android:verticalSpacing="5dp"
android:id="#+id/GridView"/>
item:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
app:srcCompat="#mipmap/ic_launcher" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView"
android:layout_centerHorizontal="true"
android:text="TextView" />
</RelativeLayout>
Thanks in advance for your helpfulness.
I am having some issues with FragmentTransactions. I have sucessfuly called on a fragment from the onClick method but the original mainFragment won't "disappear" or go to back of stack. I have followed the Android Development tutorial on Fragment Transcation. I think the issue is in my Survey.java class or my Details.java class. Any help is appreciated.
Update: (Added Photos below)
Before: http://imm.io/q9vt
After Clicking "Survey Button": http://imm.io/q9wf
See code for all below:
Code from Main.java
package mycompany.nst;
import mycompany.nst.R;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
Fragment _surveyFrag = new Survey();
public void focusFragment(Fragment mainFragment) {
FragmentManager FragMgr = getFragmentManager();
FragmentTransaction transaction = FragMgr.beginTransaction();
try {
transaction.replace(R.id.mainFragment, _surveyFrag);
transaction.addToBackStack(null);
transaction.commit();
} catch(Exception e){};
transaction = null;
FragMgr = null;
}
public void survey_onClick(View view) {
Log.i("onClick", "SURVEY BEGIN");
focusFragment(_surveyFrag);
}
}
Code from main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/hsdarker"
android:baselineAligned="false"
android:orientation="horizontal" >
<fragment
android:id="#+id/listFragment"
android:name="mycompany.nst.ListFragment"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
class="mycompany.nst.ListFragment" >
<!-- Preview: layout=#layout/list -->
</fragment>
<fragment
android:id="#+id/mainFragment"
android:name="mycompany.nst.Details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/listFragment"
class="mycompany.nst.Details" >
<!-- Preview: layout=#layout/details -->
</fragment>
</RelativeLayout>
Code from Details.java <-- The original fragment
package mycompany.nst;
import mycompany.nst.R;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Details extends Fragment {
/** Called when the activity is first created. */
#Override
public View onCreateView(LayoutInflater LIdetails, ViewGroup VGdetails, Bundle SaveInstancedState) {
Log.i("DetailsFragment", "onCreateView");
// Inflate the layout for this fragment
return LIdetails.inflate(R.layout.details, VGdetails, false);
}
}
Code from Details.java <-- The new fragment
package mycompany.nst;
import mycompany.nst.R;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Survey extends Fragment {
/** Called when the activity is first created. */
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.i("SurveyFragment", "onCreateView");
// Inflate the layout for this fragment
return inflater.inflate(R.layout.survey, container, false);
}
}
You can't replace or remove a fragment that's defined in XML- it becomes a non-removable part of the layout. However, if you define the fragments in code, you can replace and remove them at will.