Popup With Text Only onOptionsItemSelected - java

I have a tab "About" in my onOptionsItemSelected dropdown I want popup box to appear with a few lines of text info about the app when you click "About"
What is the best way to do this?

<?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:padding="20dip">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="#string/app_descrip"
android:textColor="?android:attr/textColorPrimaryInverse" />
<TextView
android:id="#+id/about_credits"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="20dip"
android:textSize="16sp"
android:text="#string/app_credits"
android:autoLink="web"
android:textColor="?android:attr/textColorPrimaryInverse" />
</LinearLayout>
Inflate the layout
protected void about() {
View messageView = getLayoutInflater().inflate(R.layout.about, null, false);
TextView textView = (TextView) messageView.findViewById(R.id.about_credits);
int defaultColor = textView.getTextColors().getDefaultColor();
textView.setTextColor(defaultColor);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.app_icon);
builder.setTitle(R.string.app_name);
builder.setView(messageView);
builder.create();
builder.show();
}
Call it on menu click

Related

Android onClick on Linear Layout

I have following code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="horizontal"
android:clickable="true"
android:onClick="openGithubUrl">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#mipmap/github_icon"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/github_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/github_url"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
I want to add a onCLick listener on LinearLayout(#+id/list-item). Is this possible? After the onClick fired I would like to get the content of the child Textview #+id/github_url.
Yes you can set an onClickListener to the linear layout, in your use case,
listItem.setOnClickListener() in the activity.
LinearLayout listItem;
TextView tvGithubUrl;
tvGithubUrl = (TextView) findViewById(R.id.github_url)
listItem = (LinearLayout) findViewById(R.id.list_item)
listItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Get the github_url text here
tvGithubUrl.getText().toString();
}
});

How to create custom progressDialog

At any devices we have different position of progressBar inside of the progressDialog. So I need to customize it.
When I try like this:
public class CustomProgressDialog extends ProgressDialog {
public CustomProgressDialog(Context context) {
super(context);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progress_fragment_dialog);
}
}
<?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">
<ProgressBar
android:layout_width="wrap_content"
android:layout_gravity="center"
android:background="#color/colorPrimary"
android:layout_height="match_parent"
android:id="#+id/progressBar" />
</LinearLayout>
This progress dialog shows only progressBar without dialog. But I need dialog too.
Create custom dialog with progress bar in it for making it consistent for different devices.
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");
// set the custom dialog components - title, ProgressBar and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("COUSTOM PROGRESS TITLE");
ProgressBar prog = (ProgressBar) dialog.findViewById(R.id.myprogress);
prog.setVisibilty(VISIBLE);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ProgressBar
android:id="#+id/myprogress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:layout_marginRight="5dp" />
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:layout_toRightOf="#+id/image"/>/>
<Button
android:id="#+id/dialogButtonOK"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text=" Ok "
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="#+id/myprogress"
/>
</RelativeLayout>

How to make View appear when button is clicked in android

How is that possible? Thanks! Should I put my View (RelativeLayout) on-top another View and make it invisible and when the user clicks the button it sets it to visible? Or is there another easier way that opens a Dialog(?) in the middle of the screen?
Thanks!
You need a layout to put the child view onto. Safest is Linear Layout since adding multiple child is easy
Fisrt create a xml file in layouts.
for eg in layout
add a file named say iv.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" >
<ImageView
android:id="#+id/iv_iv"
android:layout_width="200dp"
android:layout_height="120dp"
android:padding="2dp"
android:scaleType="fitCenter"
android:src="#drawable/person_default_page" />
In the layout where you want to add create a linear layout with some it say #+id/Row_id
Then in the code when you need to add the view
LinearLayout ll = = (LinearLayout) findViewById(R.id.Row_id);
ll.removeAllViews();
View element = LayoutInflater.from(this).inflate(R.layout.iv, ll, false);
ImageView image_iv = (ImageView) element.findViewById(R.id.ri_iv);
ll.addView(element);
Yes you can make it with an alert dialog. If you want to customize dialog use this
-First create an xml for your dialog
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="#drawable/header_logo"
android:layout_width="match_parent"
android:layout_height="64dp"
android:scaleType="center"
android:background="#FFFFBB33"
android:contentDescription="#string/app_name" />
<EditText
android:id="#+id/username"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="#string/username" />
<EditText
android:id="#+id/password"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint="#string/password"/>
and override this method
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialog_signin, null))
// Add action buttons
.setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// sign in the user ...
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
LoginDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
}
More info on Devlopper android

adding view to ViewFlipper causes java.lang.StackOverflowError

I want to have a dialog with buttons
whenever i click on a specific button, I want the dialog to "flip" and show another layout. A click on another button will return to the original dialog's view.
I try to use ViewFlipper as follows:
xmls:
send_feedback_dialog.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/feedback" />
</LinearLayout>
...
social_actions_dialog.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:id="#+id/main_activity_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent" >
<ViewFlipper
android:id="#+id/viewFlipper"
android:layout_width="300dp"
android:layout_height="407dp"/>
<RelativeLayout
android:id="#+id/main_activity_card_face"
android:layout_width="300dp"
android:layout_height="407dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#android:color/white"
android:clickable="true"
android:onClick="onCardClick"
android:padding="5dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
...
code:
final ViewFlipper viewFlipper = (ViewFlipper) mDialog
.findViewById(R.id.viewFlipper);
View feedbackview = View.inflate(mContext,
R.layout.send_feedback_dialog, viewFlipper);
// ((ViewGroup)
// feedbackview.getParent()).removeView(feedbackview);
// viewFlipper.addView(feedbackview);
// View socialActions = View.inflate(mContext,
// R.layout.social_actions_dialog, viewFlipper);
// ((ViewGroup) socialActions.getParent())
// .removeView(socialActions);
// viewFlipper.addView(socialActions);
private void flipDialog(ViewFlipper viewFlipper,
boolean isSocialActionsShown, AlphaAnimation alphaIn,
AlphaAnimation alphaOut) {
if (isSocialActionsShown) {
isSocialActionsShown = false;
viewFlipper.setInAnimation(alphaIn);
viewFlipper.setOutAnimation(alphaOut);
// Show the next Screen
viewFlipper.showNext();
} else {
isSocialActionsShown = true;
viewFlipper.setInAnimation(alphaIn);
viewFlipper.setOutAnimation(alphaOut);
viewFlipper.showPrevious();
}
}
I used to get java.lang.StackOverflowError and then i have commented some rows and now the flip is executed but nothing changes.
how can i make it properly work?
what is the right way to organize the dialog layout in different files?
i have tried to create the dialog in a different file but it requires many things and maybe it's better to use it as an inner class

Why my simple Toast doesn't want to appear and the app crashes?

I everyone, I am trying to make such a dumb thing: center the text inside a Toast which is created inside an onclick event listener.
I have created a TextView inside the layout I use and when I try to call the event that should the display the toast the app crashes with this exception:
03-19 23:20:40.258: E/AndroidRuntime(3364): java.lang.IllegalArgumentException: View not
attached to window manager
This is my code:
toastTextView = (TextView) findViewById(R.id.toastView);
Toast toast = Toast.makeText(MyActivity.this, "Very very very long long text that should be displayed in the middle of this toast",
Toast.LENGTH_SHORT);
toast.setView(toastTextView);
toast.show();
The xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:id="#+id/chart" android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1"/>
<TextView
android:id="#+id/toastView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content">
</LinearLayout>
Where's the problem with this simple code?
Thanks for the attention!
EDIT: this is the layout of the custom_toast.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
</LinearLayout>
You are using a view that is already attached to another view hierarchy. You have to inflate your custom toast view separately like this:
custom_toast.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/black"
android:padding="5dp">
<TextView
android:id="#+id/tvToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
And that's how you use it:
LayoutInflater inflater = LayoutInflater.from(context);
View customToastView = inflater.inflate(R.layout.custom_toast, null);
TextView tvToast = (TextView) customToastView.findViewById(R.id.tvToast);
tvToast.setText("This is a custom toast with centered text");
Toast toast = new Toast(context);
toast.setView(customToastView);
toast.setDuration(Toast.LENGTH_SHORT);
toast.show();
My guess is that you have to set the toast_layout.xml as the view of your toast, not only the single textView out of it.
Try to add an id to your outermost linear layout, ...
toast_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toast_layout_root"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout .../>
<TextView .../>
</LinearLayout>
... inflate it with the layout inflater and set this as your toast view.
java.file
View toastRootView = getLayoutInflater().inflate(R.layout.toast_layout,
(ViewGroup) findViewById(R.id.toast_layout_root));
TextView text = findViewById(R.id.toastView);
text.setText("Very [...] long text [...]");
Toast toast = new Toast(MyActivity.this)
.setView(toastRootView)
.setDuration(Toast.LENGTH_SHORT)
.show();
So "toast_layout" is the name of the xml file containing the layout and toast_layout_root is the id of the root element in this file (the outermost linear layout).
Hope this helps,
Kai
Follow up link:
http://meddington.hubpages.com/hub/How-To-Toast-In-Android
Try using a SpannableString to format your Toast, you can get a lot of mileage out of that without worrying about custom views.
String fancyText = "MyFancy long text";
SpannableStringBuilder span = new SpannableStringBuilder(fancyText);
span.setSpan(new AlignmentSpan(Layout.Alignment.ALIGN_CENTER), 0, fancyText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Toast.makeText(MyActivity.this, span, Toast.LENGTH_SHORT).show();

Categories

Resources