Why does the keyboard overlap/push the edittext? - android

When run my app and click on the edittext to write something, the keyboard pushes/overlaps the edittext, why?
This is before the overlap
This is after the overlap
Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:id="#+id/title"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:text="HJÆLP......"
android:textSize="30dp"
android:background="#00796b"
android:textStyle="bold"
android:textColor="#ffffff" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/suggestion"
android:hint="Hvad er dit forslag?"
android:gravity="top"
android:layout_below="#+id/title"
android:layout_alignParentStart="true"
android:layout_marginTop="61dp"
android:layout_alignParentEnd="true"
android:editable="true"
android:textAlignment="textStart"
android:layout_above="#+id/suggestknap" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send forslag"
android:id="#+id/suggestknap"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="157dp"
android:textColor="#ffffff"
android:background="#00796b" />
This is the java class for the fragment:
public class SuggestFrag extends Fragment implements View.OnClickListener {
Button suggestButton;
TextView title;
EditText suggestion;
Annonce annonce;
#Override
public View onCreateView(LayoutInflater i, ViewGroup container, Bundle savedInstanceState) {
View rod = i.inflate(R.layout.frag_suggest, container, false);
suggestButton = (Button)rod.findViewById(R.id.suggestknap);
suggestButton.setOnClickListener(this);
title = (TextView)rod.findViewById(R.id.title);
suggestion = (EditText)rod.findViewById(R.id.suggestion);
annonce = ((AnnonceDisplay)this.getActivity()).getAnnonce();
title.setText(annonce.getItemname());
return rod;
}
#Override
public void onClick(View view) {
if(view == suggestButton) {
AlertDialog alert = new AlertDialog.Builder(getActivity())
.setMessage(suggestion.getText().toString())
.setNeutralButton("ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
}
).show();
}
}
}

You can set your activity's windowSoftInputMode flag to adjustPan in android manifest file to prevent this.
<activity
...
android:windowSoftInputMode="adjustPan">
</activity>

Related

call another java activity from fragment class

Hey i have a simple question. i have a mobile_navigation that contain code to call a fragment class, and i have the fragment class but i want to call / pass to another java class. for example i have Inputdata.xml that when tap will go to fragment class nah i want when i tap the input xml it will instantly go to another java class not stuck in fragment class
here is the example code for mobile_navigation
<fragment
android:id="#+id/nav_pantuan"
android:name="com.joshua.r0th.crud2.ui.gallery.GalleryFragment"
android:label="#string/pantauan"
tools:layout="#layout/fragment_pantauan" />
and here is the GalleryFragment code
public class GalleryFragment extends Fragment {
private GalleryViewModel GalleryViewModel;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
GalleryViewModel =
ViewModelProviders.of(this).get(GalleryViewModel.class);
View root = inflater.inflate(R.layout.fragment_pantauan, container, false);
final TextView textView = root.findViewById(R.id.text_gallery);
GalleryViewModel.getText().observe(this, new Observer<String>() {
#Override
public void onChanged(#Nullable String s) {
textView.setText(s);
}
});
return root;
}
}
and this is the another java class that i willing to reach
public class pantauan extends AppCompatActivity {
database1 myDb;
EditText editNomorRumah,editJentikDalam,editJentikLuar;
Button btnAddData;
Button btnViewAll;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_pantauan);
myDb = new database1(this);
editNomorRumah = (EditText)findViewById(R.id.nomorrmh);
editJentikDalam = (EditText)findViewById(R.id.jentikdirumah);
editJentikLuar = (EditText)findViewById(R.id.jentikdiluarrumah);
btnAddData = (Button)findViewById(R.id.tambahdata);
btnViewAll = (Button)findViewById(R.id.lihatdata);
AddData();
viewAll();
}
//fungsi tambah
public void AddData() {
btnAddData.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean isInserted = myDb.insertData(editNomorRumah.getText().toString(),
editJentikDalam.getText().toString(),
editJentikLuar.getText().toString() );
if(isInserted == true)
Toast.makeText(pantauan.this,"Data Iserted",Toast.LENGTH_LONG).show();
else
Toast.makeText(pantauan.this,"Data Not Iserted",Toast.LENGTH_LONG).show();
}
}
);
}
//fungsi menampilkan data
public void viewAll() {
btnViewAll.setOnClickListener(
new View.OnClickListener(){
#Override
public void onClick(View v) {
Cursor res = myDb.getAllData();
if(res.getCount() == 0) {
// show message
showMessage("Error","Noting Found");
return;
}
StringBuffer buffer = new StringBuffer();
while (res.moveToNext() ) {
buffer.append("NomorRumah :"+ res.getString(0)+"\n");
buffer.append("JentikDalam :"+ res.getString(1)+"\n");
buffer.append("JentikLuar :"+ res.getString(2)+"\n");
}
// show all data
showMessage("Data",buffer.toString());
}
}
);
}
//membuat alert dialog
public void showMessage(String title, String Message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(Message);
builder.show();
}
}
and here is the Input.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#drawable/gradient"
android:orientation="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="70dp"
android:background="#drawable/gradient"
android:elevation="4dp"
android:orientation="vertical"
android:padding="20dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="30dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Nomor Rumah" />
<EditText
android:id="#+id/nomorrmh"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/roundtext" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jentik Di Dalam Rumah" />
<EditText
android:id="#+id/jentikdirumah"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/roundtext" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jentik Di Luar Rumah" />
<EditText
android:id="#+id/jentikdiluarrumah"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/roundtext"
android:layout_marginBottom="10dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_height="match_parent"
android:layout_gravity="center">
<Button
android:id="#+id/tambahdata"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:background="#d67601"
android:text="Tambah Data"
android:textAllCaps="false"
android:textColor="#fff"
android:textSize="18sp" />
<Button
android:id="#+id/lihatdata"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:background="#d67601"
android:text="contoh lihat data"
android:textAllCaps="false"
android:textColor="#fff"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
<TextView
android:id="#+id/textviewadd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="26dp"
android:gravity="center_horizontal"
android:text="Input Data"
android:textColor="#fff"
android:textSize="26sp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/text_gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
My goal is to pass the fragment into the java class that i want, because i dont know how to put that input class to the fragment always get cannot be cast to androidx.fragment.app.Fragment when i replace the GalleryFragment with pantauan.java code. thank you

How to use one button to getText and setText to 3 different EditTexts and TextViews

It's quite simple. I have a button that has an onClickListener which takes a text from EditText, hides it and sets it to TextView which it shows in the same place.
Now, i want to do that to 2 more different edit texts and text views with the same button. Code below.
XML with my buttons:
<?xml version="1.0" encoding="utf-8"?>
<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 = ".profil"
android:orientation = "vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="230dp"
android:background="#color/pozadina"
android:id="#+id/rltv">
<ImageView
android:id="#+id/slikaProfil"
android:layout_width="130dp"
android:layout_height="160dp"
android:src="#drawable/profilna"
android:layout_centerInParent="true"/>
<TextView
android:id="#+id/textProfil1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/userProfil"
android:layout_below="#+id/slikaProfil"
android:layout_centerInParent="true"
android:textSize="20sp"
android:textStyle="bold"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_below="#+id/rltv">
<TextView
android:id="#+id/imeProfil"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Account info"
android:textSize="30sp"
android:textStyle="bold"
android:layout_marginLeft="50dp"
android:layout_marginTop="30dp"
android:layout_marginBottom="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ime i prezime"
android:layout_marginLeft="70dp"
android:drawableLeft="#drawable/profil"
android:drawablePadding="5dp"
android:id="#+id/imeProfil1"
android:textSize="16sp"
android:layout_below="#+id/imeProfil"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Upisite ime i prezime"
android:layout_marginTop="-5dp"
android:layout_marginLeft="70dp"
android:id="#+id/imeProfil2"
android:textSize="16sp"
android:background="#null"
android:layout_below="#+id/imeProfil1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Telefon"
android:layout_marginLeft="70dp"
android:drawableLeft="#drawable/telefon"
android:drawablePadding="5dp"
android:id="#+id/telefonProfil1"
android:textSize="16sp"
android:layout_below="#+id/imeProfil2"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Upisite broj telefona"
android:layout_marginTop="-5dp"
android:layout_marginLeft="70dp"
android:id="#+id/telefonProfil2"
android:textSize="16sp"
android:background="#null"
android:layout_below="#+id/telefonProfil1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="E-mail"
android:layout_marginLeft="70dp"
android:drawableLeft="#drawable/mail"
android:drawablePadding="5dp"
android:id="#+id/mailProfil1"
android:textSize="16sp"
android:layout_below="#+id/telefonProfil2"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Upisite e-mail"
android:layout_marginTop="-5dp"
android:layout_marginLeft="70dp"
android:id="#+id/mailProfil2"
android:textSize="16sp"
android:background="#null"
android:layout_below="#+id/mailProfil1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Adresa"
android:layout_marginLeft="70dp"
android:drawableLeft="#drawable/adresa"
android:drawablePadding="5dp"
android:id="#+id/adresaProfil1"
android:textSize="16sp"
android:layout_below="#+id/mailProfil2"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Upisite adresu"
android:layout_marginTop="-5dp"
android:layout_marginLeft="70dp"
android:id="#+id/adresaProfil2"
android:textSize="16sp"
android:background="#null"
android:layout_below="#+id/adresaProfil1"
/>
<Button
android:id="#+id/prihvati"
android:layout_width="150dp"
android:layout_height="70dp"
android:layout_below="#+id/adresaProfil2"
android:layout_marginLeft="70dp"
android:text="#string/submit"
android:onClick="zamijeni"/>
</RelativeLayout>
</RelativeLayout>
My java file:
public class profil extends Fragment {
private Button dugme4;
private TextView text1;
private EditText text2;
public profil(){
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.activity_profil, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
text1 = view.findViewById(R.id.imeProfil1);
text2 = view.findViewById(R.id.imeProfil2);
dugme4 = view.findViewById(R.id.prihvati);
text2.setVisibility(View.INVISIBLE);
dugme4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String tekst = text2.getText().toString();
if (text1.getVisibility()== View.VISIBLE){
text1.setVisibility(View.INVISIBLE);
text2.setVisibility(View.VISIBLE);
}else if (text2.getVisibility() == View.VISIBLE){
text2.setVisibility(View.INVISIBLE);
text1.setVisibility(View.VISIBLE);
text1.setText(tekst);
}
}
});
}
}
So as you see i have an if function that on click gets the text, makes the edit text invisible and text visible and at the end sets the text to the textView.
Should i do a few more if functions or should it be done differently.
You should implement the same logic to access the text from EditText and display it in the TextView, you can make a function which should be called on the click of this button.
You can achive this more efficiently by use only a edittext.. no need to use text view.. I am giving a sample demo below
public class profil extends Fragment {
private Button dugme4;
//private TextView text1;
private EditText text2;
public profil(){
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.activity_profil, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// text1 = view.findViewById(R.id.imeProfil1);
text2 = view.findViewById(R.id.imeProfil2);
dugme4 = view.findViewById(R.id.prihvati);
// text2.setVisibility(View.INVISIBLE);
dugme4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (text2.isEnabled()){
//make it not editable and non focusable
text2.setEnable(false);
text2.setCursorVisible(false);
//for clearing underline of editText so that it looks like textview
text2.setBackgroundResource(android.R.color.transparent);
}else{
text2.setEnabled(true);
text2.setCursonVisible(true);
// set any color what you want as underline
text2.setBackgroundResource(android.R.color.black);
}
}
});
}
}
Try it.hope it will solve your problem by reducing extra element and complexity.

Scroll View not working in Alert Dialog

Somehow the scroll view is not working . The message alert dialog box is showing is really big and so I need to implement vertical scrollbar. I tried to get data from previous asked question but it isn't solving my issue please help.
I need to show the alert dialog on button click event.
benefits.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog ad = new AlertDialog.Builder(Panchgavya.this).create();
ad.setCancelable(false); // This blocks the 'BACK' button
ad.setMessage(getString(R.string.benefits));
ad.setTitle("Benefits");
ad.setButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
ad.show();
TextView textView = (TextView) ad.findViewById(android.R.id.message);
textView.setScroller(new Scroller(Panchgavya.this));
textView.setVerticalScrollBarEnabled(true);
textView.setMovementMethod(new ScrollingMovementMethod());
}
});
My XML File Code:
<?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"
tools:context=".Design.Panchgavya"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#color/Green"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Panchgavya"
android:textColor="#color/white"/>
</android.support.v7.widget.Toolbar>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6"
android:orientation="vertical">
<TextView
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end"
android:text="#string/panchgavya"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:orientation="vertical">
<TextView
android:id="#+id/panchgavya_tv_cow_dung"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cow Dung"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textStyle="bold"
android:textColor="#color/black"/>
<TextView
android:id="#+id/panchgavya_tv_cow_urine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Cow Urine"
android:textStyle="bold"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#color/black"/>
<TextView
android:id="#+id/panchgavya_tv_cow_milk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Cow Milk"
android:textStyle="bold"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#color/black"/>
<TextView
android:id="#+id/panchgavya_tv_ghee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:text="Ghee"
android:textStyle="bold"
android:textColor="#color/black"/>
<TextView
android:id="#+id/panchgavya_tv_dahi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:text="Dahi"
android:textStyle="bold"
android:textColor="#color/black"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true">
<Button
android:id="#+id/panchgavya_btn_benefits"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Benefits"
android:background="#color/Green"
android:textColor="#color/white"
/>
</LinearLayout>
</LinearLayout>
You can create custom layout for your dialog and set any property easily.
For your requirement, Crate a layout for your required Dialog. Put android:scrollbars = "vertical" in your textView inside your layout. And textview.setMovementMethod(new ScrollingMovementMethod());.
You can set custom layout on your by following method.
public void showDialog(Activity activity, String msg){
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.dialog);
TextView text = (TextView) dialog.findViewById(R.id.text_dialog);
text.setText(msg);
Button dialogButton = (Button) dialog.findViewById(R.id.btn_dialog);
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Textview
android:id="#+id/txtDescription"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/margin_16dp"
android:paddingRight="#dimen/margin_16dp"
android:paddingBottom="#dimen/margin_16dp"
android:paddingTop="#dimen/margin_10dp"
android:text="#string/dummy_text_"
android:textSize="#dimen/font_14dp"
android:textColor="#color/colorPrimary"
android:layout_below="#+id/imgClose"/>
<ImageView
android:id="#+id/imgClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_close"
android:layout_alignParentRight="true"
android:layout_marginRight="#dimen/margin_10dp"
android:layout_marginTop="#dimen/margin_10dp" />
</RelativeLayout>
</ScrollView>
// in your java file put below code
private void showPopup() {
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.dialog_terms_services,
null);
dialogBuilder.setView(dialogView);
TextView txtDescription = dialogView.findViewById(R.id.txtDescription);
ImageView imgClose = dialogView.findViewById(R.id.imgClose);
txtDescription.setText(message);
final AlertDialog b = dialogBuilder.create();
b.show();
imgClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
b.dismiss();
}
});
}
// after than call that showPopup() methos on button click.Hope it works for you
class extending DialogFragment can have UI elements as per your requirement.
public class OrderDetailFragment extends DialogFragment{
#Override
public void onStart() {
super.onStart();
Dialog d = getDialog();
if (d!=null){
int width = ViewGroup.LayoutParams.MATCH_PARENT;
int height = ViewGroup.LayoutParams.WRAP_CONTENT;
d.getWindow().setLayout(width, height);
}
}
public static OrderDetailFragment getInstance(GeneralListDataPojo dataList){
OrderDetailFragment orderDetailFragment=new OrderDetailFragment();
Bundle bundle = new Bundle();
bundle.putParcelable(DATA, dataList);
orderDetailFragment.setArguments(bundle);
return orderDetailFragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View root_view = inflater.inflate(R.layout.fragment_order_detail, container, false);
GeneralListDataPojo mDataList = getArguments().getParcelable(DATA);//My class which holds data implemented Parcelable
//population of data from mDataList
return root_view;
}
void closeDialog(){
this.dismiss();
}
}
fragment_order_detail is having my requirement specific element's (ScrollView/LinearLayouts/Buttons etc)
GeneralListDataPojo is the class which holds data implements Parcelable for transferring data between components
Now you can invoke this DialogFragment from your Fragment like this. (Im invoking from a Fragment. Change the FragmentManager retrieval accordingly if you are using from Activity)
OrderDetailFragment orderDetailFragment=OrderDetailFragment.getInstance((GeneralListDataPojo) responseObj);
FragmentManager fragmentManager=getFragmentManager();
orderDetailFragment.show(fragmentManager,"OrderDetailFragment");
Create The Custom Dialog layout Using Scroll View in your own custom Layout
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog= new Dialog(getApplicationContext());
dialog.setContentView(R.layout.activity_dialog);
Button click= dialog.findViewById(R.id.click);
click.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});

How to delete space when I hide a CardView

How can I delete the space when I hide a CardView? I doing an app with android and Firebase and I show the information from firebase in cardviews, but when the info isn't correct the cardview must disappear. The point is that the cardview disappear, but still uses a space in the layout, I've tried the answers in
setVisibility(GONE) view becomes invisible but still occupies space But it doesn't work for me.
#Override
protected void onStart() {
super.onStart();
firebaseAuth.addAuthStateListener(firebaseAuthListener);
final FirebaseRecyclerAdapter<CursosDB, MainActivity.CursosPViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<CursosDB, MainActivity.CursosPViewHolder>(
CursosDB.class,
R.layout.design_row_cursos,
MainActivity.CursosPViewHolder.class,
myRef)
{
#Override
protected void populateViewHolder(MainActivity.CursosPViewHolder viewHolder, final CursosDB Cmodel, int position)
{
String estado = Cmodel.getSTATUS().toString();
if (estado.equals("OK")){
viewHolder.setPhotoURL(getApplicationContext(), Cmodel.getURI());
viewHolder.setTitle(Cmodel.getTITLE());
viewHolder.setCiudad(Cmodel.getPLACE());
viewHolder.setLevel(Cmodel.getLEVEL());
viewHolder.setDur(Cmodel.getDURACION());
viewHolder.setPrice(Cmodel.getCOSTO());
}else{
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) viewHolder.mView.getLayoutParams();
layoutParams.setMargins(0,0,0,0);
viewHolder.mView.setLayoutParams(layoutParams);
viewHolder.mView.setVisibility(View.INVISIBLE);
viewHolder.mView.setVisibility(View.GONE);
//viewHolder.mView.setVisibility(View.GONE);
//Toast.makeText(getApplicationContext(), "Ningún curso aprobado aún", Toast.LENGTH_SHORT).show();
}
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(getApplicationContext(), Cmodel.getUSERID(), Toast.LENGTH_LONG).show();
//Toast.makeText(getApplicationContext(), Cmodel.getTITLE(), Toast.LENGTH_SHORT).show();
//Toast.makeText(UsersList.this, user_key, Toast.LENGTH_LONG).show();
}
});
}
};
mCourses.setAdapter(firebaseRecyclerAdapter);
}
This is my layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardview_cursos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:onClick="positionAction"
android:padding="4dp"
android:paddingBottom="4dp"
android:paddingEnd="4dp"
android:paddingStart="4dp"
android:paddingTop="4dp"
app:cardElevation="4dp">
<LinearLayout android:id="#+id/layout_to_hide"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/Cimg"
android:layout_width="match_parent"
android:layout_height="165dp"
android:scaleType="center"
app:srcCompat="#drawable/knowit_logo" />
<TextView
android:id="#+id/Ctitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/Cimg"
android:layout_marginLeft="14dp"
android:layout_marginStart="14dp"
android:paddingTop="8dp"
android:text="Titulo"
android:textStyle="bold" />
<TextView
android:id="#+id/Clugar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Ctitle"
android:layout_alignStart="#+id/Ctitle"
android:layout_below="#+id/Ctitle"
android:layout_marginTop="10dp"
android:text="Lugar" />
<TextView
android:id="#+id/textView29"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Clugar"
android:layout_alignStart="#+id/Clugar"
android:layout_below="#+id/Clugar"
android:layout_marginTop="10dp"
android:text="$" />
<TextView
android:id="#+id/Cprice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView29"
android:layout_marginBottom="4dp"
android:layout_toEndOf="#+id/textView29"
android:layout_toRightOf="#+id/textView29"
android:paddingLeft="5dp"
android:text="0.0" />
<TextView
android:id="#+id/Cdur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/Clugar"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="126dp"
android:layout_marginRight="126dp"
android:text="Duración" />
<TextView
android:id="#+id/Cnivel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/Cprice"
android:layout_alignLeft="#+id/Cdur"
android:layout_alignStart="#+id/Cdur"
android:layout_marginBottom="0dp"
android:text="Nivel" />
</RelativeLayout>
</LinearLayout> </android.support.v7.widget.CardView>
This is the mView:
public static class CursosPViewHolder extends RecyclerView.ViewHolder {
View mView;
public CursosPViewHolder(final View itemView) {
super(itemView);
mView = itemView;
}
public void setTitle(String title){
TextView post_title = (TextView)mView.findViewById(R.id.Ctitle);
post_title.setText(title);
}
public void setCiudad (String ciudad){
TextView post_city = (TextView)mView.findViewById(R.id.Clugar);
post_city.setText(ciudad);
}
public void setPhotoURL(Context ctx, String pgotouserurl) {
ImageView post_image =(ImageView)mView.findViewById(R.id.Cimg);
Picasso.with(ctx).load(pgotouserurl).into(post_image);
} }
I hope you can help me.
First the XML code you've posted ils wrong closed,(must finish with a CardView tag)
On addition be sure you're pointing the CardView layout with mView field ans Only use GONE or INVISIBLE,
INVISIBLE change your Panel ( Root view ) structure but GONE don't

Wrong custom-dialog size

I got a problem with a custom dialog in an android application.
I got a Custom Dialog with a ListView inside it. The Dialog itself is 600dp width and the ListView is 600dp as well.
The problem is that the dialog is more than that width value:
This is the Dialog layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="600dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="#+id/promotionListView"
android:layout_width="600dp"
android:layout_height="400dp"
android:scrollbarStyle="insideOverlay"
android:dividerHeight="1dp" />
<LinearLayout
android:layout_width="600dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/promotion_dialog_abort"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/annulla"/>
<Button
android:id="#+id/promotion_dialog_apply"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/applica"/>
</LinearLayout>
</LinearLayout>
This is the layout of the single row: (note that both the width of the layout and the sum of single views is 600dp)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="600dp" <-- I tried "wrap_content" and "match_parent" with the same result
android:layout_height="wrap_content">
<CheckBox
android:id="#+id/promotionCheck"
android:layout_width="50dp"
android:layout_height="50dp"
android:gravity="center"
android:layout_gravity="center" />
<TextView
android:id="#+id/promotionText"
android:layout_width="350dp"
android:layout_height="50dp"
android:gravity="center_vertical"
android:layout_gravity="center"
android:textColor="#android:color/white"
android:textSize="#dimen/customerdetail_headertextsize" />
<TextView
android:id="#+id/promotionFactor"
android:layout_width="100dp"
android:layout_height="50dp"
android:gravity="center_vertical|right"
android:layout_gravity="center"
android:textColor="#android:color/white"
android:textSize="#dimen/customerdetail_headertextsize" />
<TextView
android:id="#+id/promotionPrice"
android:layout_width="100dp"
android:layout_height="50dp"
android:gravity="center_vertical|right"
android:layout_gravity="center"
android:textColor="#android:color/white"
android:textSize="#dimen/customerdetail_headertextsize" />
</LinearLayout>
And finally the Custom Dialog Class
public class PromotionDialog extends Dialog {
private boolean authCode;
private Set<OrderDetailDiscount> promotions;
private List<OrderDetailDiscount> selectedPromotions;
private ListView mainListView;
public Response response = Response.CANCEL;
private Button buttonAbort;
private Button buttonAccept;
public PromotionDialog(Context context, boolean authCode, Set<OrderDetailDiscount> promotions) {
super(context);
this.authCode = authCode;
this.promotions = promotions;
selectedPromotions = new ArrayList<>();
setTitle("Promozioni");
setContentView(R.layout.isfa_promotiondialog);
buttonAbort = (Button) findViewById(R.id.promotion_dialog_abort);
buttonAccept = (Button) findViewById(R.id.promotion_dialog_apply);
}
}
I really can't understand what the problem is, I tried to manually resize the titlebar with no success... what could I possibbly try?
You should use wrap_content or match_parent instead of fixed width/height values in order to support different screen sizes and to simulate table rows you can apply layout_weight to each child (column) of a horizontal LinearLayout.
Moreover, I suggest you to use AlertDialog.Builder APIs.
Here's what I achieved trying to recreate your dialog using my suggestions:
And here's the code:
MainActivity.java
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AlertDialog.Builder mBuilder = new AlertDialog.Builder(this);
mBuilder.setTitle("Discounts");
mBuilder.setAdapter(new MyAdapter(), null);
mBuilder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
mBuilder.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
mBuilder.create().show();
}
class RowItem{
public String field1;
public String field2;
public String field3;
public RowItem(String f1,String f2, String f3){
field1=f1;
field2=f2;
field3=f3;
}
}
private class MyAdapter extends BaseAdapter{
private final ArrayList<RowItem> rows = new ArrayList<RowItem>();
public MyAdapter(){
rows.add(new RowItem("Sales","1","5%"));
rows.add(new RowItem("Extra Sales","2","50%"));
rows.add(new RowItem("Super Sales","3","70%"));
rows.add(new RowItem("Completely Free","4","100%"));
}
#Override
public int getCount(){
return rows.size();
}
#Override
public Object getItem(int p){
return rows.get(p);
}
#Override
public long getItemId(int p){
return p;
}
#Override
public View getView(int position, View v, ViewGroup parent){
v=getLayoutInflater().inflate(R.layout.row_layout,parent,false);
LinearLayout row = (LinearLayout)v.findViewById(R.id.linearlayout);
CheckBox checkBox = (CheckBox)v.findViewById(R.id.checkbox);
TextView field1 = (TextView)v.findViewById(R.id.field1);
TextView field2 = (TextView)v.findViewById(R.id.field2);
TextView field3 = (TextView)v.findViewById(R.id.field3);
row.setBackgroundColor(Color.parseColor(position%2==0?"#212121":"#424242"));
RowItem item = rows.get(position);
field1.setText(item.field1);
field2.setText(item.field2);
field3.setText(item.field3);
return v;
}
}
}
row_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearlayout"
android:orientation="horizontal"
android:weightSum="1"
android:layout_width="match_parent" android:layout_height="wrap_content">
<CheckBox
android:layout_width="0dp"
android:layout_weight="0.1"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/checkbox"
android:checked="false" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="myText"
android:id="#+id/field1"
android:layout_weight="0.6" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="myText"
android:id="#+id/field2"
android:layout_weight="0.15" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="myText"
android:id="#+id/field3"
android:layout_weight="0.15" />
</LinearLayout>

Categories

Resources