Dialog box buttons not visible on smaller screens and resolutions? - java

I'm trying to show a tutorial when user runs the app for the first time, by using dialog boxes. However, the "back" and "next" buttons aren't visible on smaller screens and/or resolutions, so the user can't proceed with the tutorial.
I tried making the dialog box a ScrollView but it didn't help, buttons are still invisible. What's the best way to resolve this? Thanks in advance!
EDIT: here's an example of the first dialog shown to user:
AlertDialog.Builder w1 = new AlertDialog.Builder(this);
LayoutInflater w1Inflater = LayoutInflater.from(this);
View w1Layout = w1Inflater.inflate(R.layout.dialog, null);
ImageView image1 = (ImageView) w1Layout.findViewById(R.id.my_image);
image1.setImageResource(R.drawable.ic_launcher);
w1.setView(w1Layout);
w1.setTitle("Welcome");
w1.setMessage(Html.fromHtml("Before you start using the app, get familiar with the User Interface!"));
w1.setCancelable(false);
w1.setPositiveButton("Next", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch3();
num = 3;
((ViewGroup)w1Layout.getParent()).removeView(w1Layout);
}
});
And here's the dialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:id="#+id/my_image"/>
</LinearLayout>
</ScrollView>

Related

How do I inflate dialog at the bottom of my layout?

My swipe to dismiss dialog is currently showing at the center of my layout which matches the parent's width. No left/right margins. I want to place it on the bottom.
This is my XML layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_fmcg_popup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_gravity="bottom"
android:layout_weight="1"
android:background="#drawable/button_border">
<android.support.constraint.ConstraintLayout
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="17dp"
android:layout_marginTop="13dp"
android:layout_marginEnd="17dp"
android:layout_marginBottom="13dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
// other code goes here
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
This is the code for swipe to dismiss dialog. I am currently using a swipe to dismiss dialog library.
View dialog = LayoutInflater.from(this).inflate(R.layout.dialog_fmcg_popup, null);
TextView tvfmcg2 = dialog.findViewById(R.id.tv_fmcg2);
tvfmcg2.setText(message);
swipeDismissDialog = new SwipeDismissDialog.Builder(this)
.setView(dialog)
.setOnSwipeDismissListener(new OnSwipeDismissListener() {
#Override
public void onSwipeDismiss(View view, SwipeDismissDirection direction) {
Preferences.setString(Prefkey.last_qualified_fmcg_voucher_on_remove, message);
}
})
.setFlingVelocity(0)
.setOverlayColor(0)
.build()
.show();
}
You can try below code this will show the dialog at the bottom of the screen:
Dialog dialog
//code to initialize dialog here
Window window = dlg.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.BOTTOM;
wlp.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(wlp);

How to transform Dialog into DialogFragment?

Coming from VB world I learned today that dialogs are not modal in Java. I have the task of converting a program we have written in VB.net for windows based Motorola devices to Java using Android Studio for Android Motorola devices.
I am trying to make it as similar to forms in windows.
I am at a form that has that has several messageboxes that prompt for input.
I tried doing java but noticed that dialogs don't always wait for response.
I have read that using dialogfragments can accomplish what I am trying to do.
Here is the XML that I had for the first dialog.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorBackgroundGray"
>
<TextView
android:id="#+id/theText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Holding Text"
android:textAlignment="center"
android:textColor="#color/colorBlack"
android:textSize="30dp" />
<Button
android:id="#+id/buttonYes"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_below="#id/message"
android:layout_margin="8dp"
android:onClick="onClickYes"
android:text="Yes"
android:textSize="26dp" />
<Button
android:id="#+id/buttonNo"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_below="#id/message"
android:layout_margin="8dp"
android:onClick="onClickNo"
android:text="No"
android:textSize="26dp" />
</RelativeLayout>
Here is how I was calling the dialog.
final Dialog dialog = new Dialog(ReturnAreaActivity.this, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
dialog.setContentView(R.layout.dialog_location);
dialog.setTitle("New Location Setup");
TextView message = (TextView) dialog.findViewById(R.id.theText);
message.setText("Is this location a Pick Bin");
Button buttonYes = (Button) dialog.findViewById(R.id.buttonYes);
Button buttonNo = (Button) dialog.findViewById(R.id.buttonNo);
dialog.show();
Having question on what changes would need to be done to turn the dialog into a dialogFragment. I appreciate and clarity anyone can shed. Also the calling of the dialogFragments would be done during a loop
Thanks
First extend DialogFragment and use AlertDialog.Builder to create the dialog inside method onCreateDialog.
public class MyDialogFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(inflater.inflate(R.layout.dialog_location, null))
.setMessage("Is this location a Pick Bin")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Handle "yes" button click here.
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Handle "no" button click here.
}
});
return builder.create();
}
}
Then you can show DialogFragment by using show() method:
DialogFragment newFragment = new MyDialogFragment();
newFragment.show(getSupportFragmentManager(), "MyDialogFragment")
Small advise - don't use dp in your widget textSize, use sp. You can read the explanation here.

Textview.setText throwing nullpointer despite the view being in the correct contentView

Before any of you tell me that this has been answered many times before, let me clarify most of those who've asked this question either gave the wrong view id or set a different ContentView than where the TextView was located. I have already used this function in a different where it worked fine. I've tried using a string literal in the TextView.setText(), but it was futile.
contactstory.xml file
<?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">
<TextView
android:id="#+id/textyman"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="100dp"
android:text="TextView" />
<Button
android:id="#+id/close"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Close" />
</LinearLayout>
Function in java file
public void ShowStory(int pos)
{
final Dialog dialog = new Dialog(this);
dialog.setTitle(srcnames[pos]);
dialog.setContentView(R.layout.contactstory);
String username=getIntent().getStringExtra("username");
LoginDataBaseAdapter loginDataBaseAdapter=new
LoginDataBaseAdapter(this);
loginDataBaseAdapter.open();
String story=loginDataBaseAdapter.GetStory(pos,username);
TextView t1=(TextView)findViewById(R.id.textyman);
Button btnend=(Button)findViewById(R.id.close);
if(TextUtils.isEmpty(story)){
t1.setText(username+" hasn't added any story for this song");
}
else {
t1.setText(story); //Exception is thrown here
}
btnend.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
Logcat
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.sherry.escuchame.ContactInfo.ShowStory(ContactInfo.java:157)
at
com.example.sherry.escuchame.ContactInfo.onContextItemSelected
(ContactInfo.java:140)
at android.app.Activity.onMenuItemSelected(Activity.java:2660)
at android.support.v4.app.FragmentActivity.onMenuItemSelected
(FragmentActivity.java:408)
at
android.support.v7.app.AppCompatActivity.onMenuItemSelected
(AppCompatActivity.java:195)
at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected
(WindowCallbackWrapper.java:113)
Don't set pass layout id directly like R.layout.contactstory instead use layout inflater to inflate layout like below code
View view = LayoutInflater.from(context).inflate(R.layout.contactstory,null);
dialog.setContentView(view);
TextView t1=(TextView)view.findViewById(R.id.textyman);

android gallery app with sliding pictures

As in the title. I want to have grid of pictures which consists of 2 columns and every picture must have the same dimensions. The user should be able to click on each picture to enter the full screen mode and then slide pictures from left to right (full sized pictures in that mode). By far, I made a grid of clickable elements but without functionality of sliding them. Here is my code:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.miki.androidtwo.MainActivity">
<GridView
android:id="#+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2"
android:scrollbars="vertical"
android:gravity="center">
</GridView>
</RelativeLayout>
activity_full_image.xml
<?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"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
tools:context="com.example.miki.androidtwo.FullImageActivity">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="I want to see other doggos" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridView = (GridView) findViewById(R.id.grid);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
/**
* On Click event for Single Gridview Item
* */
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Sending image id to FullScreenActivity
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
}
});
}
}
Other classes are FullImageActivity and ImageAdapter; they are pretty obvious and simple so I won't post them.
How can I implement this sliding pictures functionality?
How can i determine dimensions of pictures in a grid that will be independent of the device?
Actually I think I hard-coded it with the line:
imageView.setLayoutParams(new GridView.LayoutParams(480,402));
If you want to slide the images while they are not full screen, you can use a HorizontalScrollView, or if you want to slide them while they are fullscreen, you can use a ViewPager, along with a PagerAdapter.
So to populate the grid imageView.setLayoutParams is fine to use for setting the size of each imageview like the way you did. When a user clicks on an image, you are going to go to a new Activity that is setup with a ViewPager and a PagerAdapter. This way the user can slide through fullscreen images.

black bar at bottom Revmob ad android

I am using revmob banner ad in my app. The problem I am facing is:
Before banner display at bottom of layout view "Black bar is showing" when i click it hides or remove from the view. I don't know why. Please tell me how to solve this issue.
Code is this
RevMobBanner banner;
void showBanner() {
revmob = RevMob.start(this); // RevMob Media ID configured in the AndroidManifest.xml file
banner = revmob.createBanner(this);
runOnUiThread(new Runnable() {
#Override
public void run() {
ViewGroup view = (ViewGroup) findViewById(R.id.adLayout);
view.addView(banner);
}
});
}
Xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/adLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:visibility="visible">
</RelativeLayout>
</RelativeLayout>
showbanner()// calling from activity
Thanks.

Categories