How to Display ActionBar when Dialog is shown in Android? My ActionBar should be enabled even when dialog is appeared.
In my app it is becoming dim when dilog is appeared.
import android.app.*;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.*;
public class ShowPopUp extends Activity {
PopupWindow popUp;
LinearLayout layout;
TextView tv;
LayoutParams params;
LinearLayout mainLayout;
Button but;
boolean click = true;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
popUp = new PopupWindow(this);
layout = new LinearLayout(this);
mainLayout = new LinearLayout(this);
tv = new TextView(this);
but = new Button(this);
but.setText("Click Me");
but.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (click) {
popUp.showAtLocation(mainLayout, Gravity.BOTTOM, 10, 10);
popUp.update(50, 50, 300, 80);
click = false;
} else {
popUp.dismiss();
click = true;
}
}
});
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
tv.setText("Hi this is a sample text for popup window");
layout.addView(tv, params);
popUp.setContentView(layout);
// popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10);
mainLayout.addView(but, params);
setContentView(mainLayout);
}
}
Hope it helps you
Related
I'm learning how to make UI programatically in Android. I'm trying to make an app with just one button, which when clicked, a toast pops up saying how many times the button has been clicked.
package com.example.android.dynamicui;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private int mCount=0;
float den = getResources().getDisplayMetrics().density;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Linear Layout
LinearLayout mLayout = new LinearLayout(this);
mLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
mLayout.setOrientation(LinearLayout.VERTICAL);
mLayout.setGravity(Gravity.CENTER);
//Button
AppCompatButton mButton = new AppCompatButton(this);
mButton.setText("Click me!");
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
buttonParams.setMargins(0,(int)(15*den + 0.5f),0,0);
mButton.setLayoutParams(buttonParams);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mCount++;
Toast.makeText(getBaseContext(), "You pressed button for "+mCount+" times!" , Toast.LENGTH_SHORT ).show();
}
});
//Adding button to Linear Layout
mLayout.addView(mButton,buttonParams);
setContentView(mLayout);
}
}
Please set the on click on button after the layout has been set i.e.:-
public class MainActivity extends AppCompatActivity {
private int mCount=0;
float den = getResources().getDisplayMetrics().density;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Linear Layout
LinearLayout mLayout = new LinearLayout(this);
mLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
mLayout.setOrientation(LinearLayout.VERTICAL);
mLayout.setGravity(Gravity.CENTER);
//Button
AppCompatButton mButton = new AppCompatButton(this);
mButton.setText("Click me!");
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
buttonParams.setMargins(0,(int)(15*den + 0.5f),0,0);
mButton.setLayoutParams(buttonParams);
//Adding button to Linear Layout
mLayout.addView(mButton,buttonParams);
setContentView(mLayout);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mCount++;
Toast.makeText(getBaseContext(), "You pressed button for "+mCount+" times!" , Toast.LENGTH_SHORT ).show();
}
});
}
}
It was crashing because I was using getResources() method, before onCreate(), which passed a null value to the 'den' variable.
i want a pop-up to display one of d string data present in a database in random or in a sequence. what code should i b using for this kind of a pop-up. am very new to android development. i would appreciate help. please don't leave this unanswered.
package popupTest.popupTest;
import android.R.layout;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
public class popupTest extends Activity {
PopupWindow popUp;
LinearLayout layout;
TextView tv;
LayoutParams params;
LinearLayout mainLayout;
Button but;
boolean click = true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
popUp = new PopupWindow(this);
layout = new LinearLayout(this);
mainLayout = new LinearLayout(this);
tv = new TextView(this);
but = new Button(this);
but.setText("Click Me");
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
tv.setText("Hi this is a sample text for popup window");
layout.addView(tv, params);
popUp.setContentView(layout);
// popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10);
mainLayout.addView(but, params);
setContentView(mainLayout);
Handler handler = new Handler();
handler.postDelayed(new Runnable(){
public void run() {
// TODO Auto-generated method stub
popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10);
popUp.update(50, 50, 300, 80);
}
}, 1000);
//Use this to dismiss as per your need...
// popUp.dismiss();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
popUp.dismiss();
return false;
}
}
will this code help me?
A Toast is the easiest for this:
Toast.makeText(getApplicationContext(), "Button is clicked", Toast.LENGTH_LONG).show();
Example: http://www.mkyong.com/android/android-toast-example/
For poping up some information, there are two most commonly used methods.
Toast
AlertDialog
Toast:
it appears for a specific amount of time, displays some info and then disappears.
Dialog:
It displays your message but doesn't goes away from screen untill the user presses a button for example "Ok" button.
For toast:
Toast.makeText(getApplicationContext(), "your message here", Toast.LENGTH_LONG).show();
For Dialog:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getApplicationContext());
alertDialogBuilder.setTitle("Your Title");
alertDialogBuilder.setMessage("This is to notify you");
alertDialogBuilder.show();
alertDialogBuilder.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which)
{
// Write your code here to execute after dialog closed
Toast.makeText(getApplicationContext(),"You clicked on OK", Toast.LENGTH_SHORT).show();
}
});
i've created an android application which creates 50 button dynamically,which works perfectly, but the problem is when i press one button which is defined statically which results in changing the button (text named as 5) background color to yellow.
can anyone please tell me some solution for this
my code is as given below
my Android Platform is 2.3.3
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
public class MyMain extends Activity {
Button change;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
change= (Button) findViewById(R.id.change);
setContentView(R.layout.mymain);
createCalender();
change.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// what to write here in order to change the color of button titled as 5 to yellow as its background color
}
});
}
public void createCalender()
{
LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 1.0f);
param.setMargins(10, 10, 10, 10);
LinearLayout rowLayout=null;
Button[][] buttons = new Button[10][5];
int count=51;
int tab=1;
for (int i = 0; i<10; i++)
{
if(count%5==1)
{
rowLayout = new LinearLayout(this);
rowLayout.setBackgroundColor(Color.BLACK);
rowLayout.setWeightSum(5);
layoutVertical.addView(rowLayout,param);
count=count-5;
}
for(int j=0;j<5;j++)
{
buttons[i][j]=new Button(this);
buttons[i][j].setText(""+tab);
buttons[i][j].setHeight(55);
buttons[i][j].setWidth(80);
buttons[i][j].setTextColor(Color.BLACK);
buttons[i][j].setBackgroundColor(Color.GREEN);
tab++;
rowLayout.addView(buttons[i][j],param);
}
}
}
}
Try something like:
if("5".equals(v.getText())){
v.setBackgroundColor(Color.YELLOW);
}
i've created an android application which creates 50 button dynamically,which works perfectly, but the problem is when i put some background color for these buttons dynamically the press effect of the buttons is being lost
can anyone please tell me some solution for retaining the press effect of the button click
my code is as given below
my Android Platform is 2.3.3
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
public class MyMain extends Activity {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mymain);
createCalender();
}
public void createCalender()
{
LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 1.0f);
param.setMargins(10, 10, 10, 10);
LinearLayout rowLayout=null;
Button[][] buttons = new Button[10][5];
int count=51;
int tab=1;
for (int i = 0; i<10; i++)
{
if(count%5==1)
{
rowLayout = new LinearLayout(this);
rowLayout.setBackgroundColor(Color.BLACK);
rowLayout.setWeightSum(5);
layoutVertical.addView(rowLayout,param);
count=count-5;
}
for(int j=0;j<5;j++)
{
buttons[i][j]=new Button(this);
buttons[i][j].setText(""+tab);
buttons[i][j].setHeight(55);
buttons[i][j].setWidth(80);
buttons[i][j].setTextColor(Color.BLACK);
buttons[i][j].setBackgroundColor(Color.GREEN);
tab++;
rowLayout.addView(buttons[i][j],param);
}
}
}
}
After buttons[i][j].setBackgroundColor(Color.GREEN);
buttons[i][j].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
v.setBackgroundColor(Color.GRAY); // Choose whichever color
new Handler().postDelayed(new Runnable() {
public void run() {
v.setBackgroundColor(Color.GREEN);
// Button Click Code Here
}
}, 100L); // Change this value to whatever is suitable
}
});
I have to resize one of the views or both...
It seems that once setContentView has been called a new setCotentView cannot be called. I was trying to re size the content by creating again the whole layout.
Please tell me how to do it...
Here is the code I have so far:
package v.w;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
public class VariableWidthActivity extends Activity {
/** Called when the activity is first created. */
LinearLayout RLGreen;
LinearLayout RLChange;
Button btnClick;
LinearLayout RLYellow ;
Button btClick;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RLChange = new LinearLayout(this);
RLChange.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
RLChange.setOrientation(LinearLayout.VERTICAL);
RLGreen = new LinearLayout(this);
RLGreen.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
RLGreen.setBackgroundColor(Color.GREEN);
RLGreen.setOrientation(LinearLayout.HORIZONTAL);
btnClick = new Button(this);
btnClick.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT) );
//btnClick.setBackgroundColor(Color.RED);
btnClick.setText("BUTTON");
RLYellow = new LinearLayout(this);
RLYellow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
200));
RLYellow.setBackgroundColor(Color.YELLOW);
RLChange.addView(RLGreen);
RLGreen.addView(RLYellow);
RLGreen.addView(btnClick);
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
setContentView(RLChange);
btClick=new Button(this);
btClick.setText("BUTTON");
btClick.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT) );
//btnClick.setBackgroundColor(Color.RED);
RLYellow.addView(btClick);
btClick.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//RLChange.forceLayout();
RLChange.removeAllViews();
RLChange.addView(RLYellow);
RLChange.addView(RLGreen);
RLGreen.addView(btnClick);
setContentView(RLChange);
}
});
}
}