How to display an Alert Dialog in a fragment - java

I am trying to display a custom Alert Dialog in a fragment. The name of my fragment is MoviesFragFragmentActivity.java
Here is my code:
final AlertDialog dialog1 = new AlertDialog.Builder(MoviesFragFragmentActivity.this).create();
View inflate = getLayoutInflater().inflate(R.layout.custom3,null);
dialog1.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog1.setView(inflate);
TextView t1 = (TextView) inflate.findViewById(R.id.t1);
TextView t2 = (TextView) inflate.findViewById(R.id.t2);
TextView t3 = (TextView) inflate.findViewById(R.id.t3);
LinearLayout b1 = (LinearLayout) inflate.findViewById(R.id.b1);
ImageView i1 = (ImageView) inflate.findViewById(R.id.i1);
LinearLayout bg = (LinearLayout) inflate.findViewById(R.id.bg);
t1.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/en_medium.ttf"), 0);
t2.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/sansation_regular.ttf"), 0);
t3.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/sansation_regular.ttf"), 0);
i1.setImageResource(R.drawable.splash);
i1.getDrawable().setColorFilter(Color.parseColor("#008DCD"), PorterDuff.Mode.SRC_IN);
t1.setText("Oh,No...");
t2.setText("This feature is not available yet, try after new update ");
t3.setText("Try again");
_rippleRoundStroke(bg, "#FAFAFA", "#000000", 40, 0, "#000000");
_CardView(b1, 10, 100, "#008DCD", true);
b1.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){
// Default item selected on bottom navigation onCreate
bottomnavigation4.getMenu().findItem(2).setChecked(true);
SketchwareUtil.showMessage(getApplicationContext(), "Try again later");
dialog1.dismiss();
}
});
dialog1.setCancelable(false);
dialog1.show();
On compilation, it gives me this error:
ERROR in /storage/emulated/0/.sketchware/mysc/680/app/src/main/java/com/liquidapps/movieapp/MoviesFragFragmentActivity.java (at line 601)
final AlertDialog dialog1 = new AlertDialog.Builder(MoviesFragFragmentActivity.this).create();
The constructor AlertDialog.Builder(MoviesFragFragmentActivity) is undefined
How does I display my alert dialog in my Fragment?
I tried using the same code in an activity (Main activity.java) and it works fine, but once I use the code in a fragment, I receive the error.

Use getActivity() instead of MoviesFragFragmentActivity.this

Related

Android Studio - Create an EditText with a click of a button

Can't seem to find a post/video on the net that explains adding new EditText fields with a button. I need to use the edittexts later. Can someone please explain to me how to create this system? Or link a video/post that explains this. I've been searching for a long time but I still haven't found a good explanation. Thanks.
Button mButton = (Button) findViewById(R.id.my_button);
mButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
EditText t = new EditText(myContext);
t.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
root.addView(t);
}
});
root: is the root layout where you want to add the EditText.
use below code
Add this Java File..
LinearLayout linearLayout = findViewById(R.id.editTextContainer);
Button btnShow = findViewById(R.id.btnShow);
if (btnShow != null) {
btnShow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Create EditText
final EditText editText = new EditText(this);
editText.setHint(R.string.enter_something);
editText.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
editText.setPadding(20, 20, 20, 20);
// Add EditText to LinearLayout
if (linearLayout != null) {
linearLayout.addView(editText);
}
}
});
}

Removing TextViews when i clicked backbutton

My problem is I have a button and that button is doing create new textview but that textviews removing when i click back button. How I saved textviews in activity?
My java sourcecodes here
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notlar);
Button btnNotEkle = (Button) findViewById(R.id.btnNotEkle);
final EditText etNot = new EditText(NotEkle.this);
final LinearLayout layoutNotlar = (LinearLayout) findViewById(R.id.layoutNotlar);
final TextView tv1 = (TextView) findViewById(R.id.tvnotOrtalama);
etNot.setInputType(InputType.TYPE_CLASS_NUMBER);
AlertDialog.Builder notEkle = new AlertDialog.Builder(NotEkle.this);
notEkle.setTitle("Notunuz");
notEkle.setView(etNot);
//Positive button
notEkle.setPositiveButton("Tamam", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
tvNot = new TextView(NotEkle.this);//girelen not burdaki textview e yazdırılacak.
girilenNot = etNot.getText().toString();//Girilen notu alıyoruz
tvNot.setText(girilenNot);//girilen notu textviewa veriyoruz
notTopla += Integer.parseInt(girilenNot);//Notları topluyoruz
layoutNotlar.addView(tvNot);
count = layoutNotlar.getChildCount();
dersOrtalamaYazdir=String.valueOf(dersOrtalama());
tv1.setText("Ders Ortalamanız : "+dersOrtalamaYazdir);
dialog.cancel();
}
});
final AlertDialog notEkleCreate = notEkle.create();
btnNotEkle.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
notEkleCreate.show();
}
});
}
}
Try giving your TextView objects ids.
You need to know that when you click back button - by default your activity is destroyed so all views are removed.
When you are adding new TextView you should add information about this TextView (like the text itself) to some list declared as field in your activity.
Then you can save this list when activity is recreated see: onSaveInstanceState/nRestoreInstanceState
You can also pass this list back or to new activity so that they can take actions based on this list.
Following my understanding your TextView had been created inside Dialog and after you press back button the dialog dismisses and all views you created inside will be removed and you can't access it from your Activity.
You may try to create TextView in onCreate, pass and in Dialog just call setText. I hope this is the answer you're looking for.
Cheers.

Android: TextView not updating

I got one EditText and one TextView, and I want to update my TextView in each Iteration (ConsoleWindow is running in a loop; it gets called from a handler and thus is running on the UIthread).
The Problem is that my TextView only gets updated in the first round, and then it keeps the first entry for the rest of the runtime (although the dataString is a different one in each round):
private void ConsoleWindow(String dataString) {
LinearLayout layout = new LinearLayout(getApplicationContext());
if (first2) { //first2 is true when application is launched
// ONLY SET LAYOUT AND EDITTEXT IN FIRST RUN TO SAVE CAPACITY
// LINEAR LAYOUT
setContentView(layout);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setBackgroundColor(Color.parseColor("#000000")); // black
// EDITTEXT
EditText et = new EditText(getApplicationContext());
et.setHint("Enter Command");
layout.addView(et);
first2 = false;
}
// TEXTVIEW
TextView tv = new TextView(getApplicationContext());
tv.setText(dataString); // KEEPS THE SAME UNTIL THE 1ST ROUND
layout.addView(tv);
}
I already tried tv.invalidate() and tv.postInvalidate(), but that didn't have an effect. Could someone help me please?
Put tv a global variable.
private TextView tv;
After this, In your "onCreate()" method:
tv = new TextView(getApplicationContext());
And then:
private void ConsoleWindow(String dataString) {
LinearLayout layout = new LinearLayout(getApplicationContext());
if (first2) { //first2 is true when application is launched
// ONLY SET LAYOUT AND EDITTEXT IN FIRST RUN TO SAVE CAPACITY
// LINEAR LAYOUT
setContentView(layout);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setBackgroundColor(Color.parseColor("#000000")); // black
// EDITTEXT
EditText et = new EditText(getApplicationContext());
et.setHint("Enter Command");
layout.addView(et);
first2 = false;
}
// TEXTVIEW
tv.setText(dataString); // KEEPS THE SAME UNTIL THE 1ST ROUND
layout.addView(tv);
}
Please verify too, if dataString has some text, with something like this
Log.d(TAG , "dataString: " + dataString + "with first time? " + first2.toString());
Try to pass to the setContentView(layout); outside the if statement.Because I can't understand well Why you need this.
LinearLayout layout = new LinearLayout(getApplicationContext());
setContentView(layout);
When first2 is false, you are simply creating a new LinearLayout layout and then without inflating the layout, you are directly adding TextView tv to layout. That's why Textview is not visible.
private void ConsoleWindow(String dataString) {
LinearLayout layout;
TextView tv;
EditText et;
if (first2) {
layout = new LinearLayout(getApplicationContext());
setContentView(layout);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setBackgroundColor(Color.parseColor("#000000"));
// EDITTEXT
et = new EditText(getApplicationContext());
et.setHint("Enter Command");
layout.addView(et);
tv = new TextView(getApplicationContext());
layout.addView(tv);
first2 = false;
}
if(tv != null) {
tv.setText(dataString);
}
}

Android How to change data of Textview by clicking on button

First of all, the app looks like this.
How to make - if I click on Add, on its column the textview is updated with value + 1?
I'm creating TextView and Button in a Loop;
for(int i = 0;i < playercountint; i++)
{
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,(100/playercountint)));
layout.setGravity(Gravity.CENTER_HORIZONTAL);
TextView tt = new TextView(this);
tt.setText("0");
tt.setTextSize(14);
tt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tt.setGravity(Gravity.CENTER_HORIZONTAL);
Button btadd = new Button(this);
btadd.setText("Add");
btadd.setId(2);
btadd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
????? what here?
}
});
}
What should be written in onClick function , so only TextView value which is in buttons column is changed.
There is one outer LinearLayout which is horizontally oriented, and those which are created within a loop is vertical Linear Layouts.
Maybe i should create a template for 1 loop iteration?
First make your textview final and them set it's value in onClick
for(int i = 0;i < playercountint; i++)
{
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,(100/playercountint)));
layout.setGravity(Gravity.CENTER_HORIZONTAL);
final TextView tt = new TextView(this);
tt.setText("0");
tt.setTextSize(14);
tt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tt.setGravity(Gravity.CENTER_HORIZONTAL);
Button btadd = new Button(this);
btadd.setText("Add");
btadd.setId(2);
btadd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
tt.setText(String.valueOf(Integer.parseInt(tt.getText())+1));
}
});
}
You can write one function that returns linearlayout:
public View getPlayerView(){
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,(100/playercountint)));
layout.setGravity(Gravity.CENTER_HORIZONTAL);
final TextView tt = new TextView(this);
tt.setText("0");
tt.setTextSize(14);
tt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tt.setGravity(Gravity.CENTER_HORIZONTAL);
Button btadd = new Button(this);
btadd.setText("Add");
btadd.setId(2);
btadd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
tt.setText(String.valueOf(Integer.parseInt(tt.getText())+1));
}
});
layout.add(tt);
layout.add(btadd);
return layout;
}
Call this function in a loop.
tt.setText("something");
tt.invalidate();
my bad I didn't even notice it was a for loop uh you need to hold a reference to the text view.. make your textview final
tt.setText(Integer.parse(tt.getText().toString()) + 1);
But, I strongly recommend you design UI (layout) in the *.xml file!
See more: http://developer.android.com/guide/topics/ui/declaring-layout.html
You should provide your TextView with an unique id (.setId(...)). Having this, in your onClick method simply use something like this:
final TextView tv = (TextView) findViewById(R.id.your_textview_id);
final String cur_text = tv.getText();
Integer plus_one = Integer.valueOf(cur_text);
plus_one++;
tv.setText(plus_one.toString());
Didn't try it but should work.
static int counter = 0;
btn_Add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter ++;
tv_Display.setText("Your Total is : " + counter);
}
});

Show/Hide ImageView on Button click

I have a RelativeLayout completely created within java, not using xml layout.
I have a few buttons on the left if my screen. I want to click on one button and show a default image from my res/drawable next to the button and make it disappear again on second click.
What I have tried was toggling the visibility but my onClick() raises a FATAL EXCEPTION, NullPointer Exception.
This is my code so far. Hardcoded the image shows right when I set picview.setVisibility(View.INVISIBLE); by hand. What I am doing wrong in the onClick() ?
private ImageView picview;
//*snip* loads of other code
//Show Image Button
ImageButton show_pic = new ImageButton(this);
show_pic.setBackgroundColor(Color.WHITE);
show_pic.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
if(picview.getVisibility() == View.INVISIBLE)
{
picview.setVisibility(View.VISIBLE);
}
else if (picview.getVisibility() == View.VISIBLE)
{
picview.setVisibility(View.INVISIBLE);
}
}
});
params = new RelativeLayout.LayoutParams(40, 40);
params.topMargin = 10;
params.leftMargin = 10;
params.addRule(RelativeLayout.BELOW, button2_id);
rl.addView(show_pic, params);
//Imageview loaded from drawable
ImageView picview = new ImageView(this);
params = new RelativeLayout.LayoutParams(200, 400);
params.topMargin = 0;
params.leftMargin = 30;
params.addRule(RelativeLayout.RIGHT_OF, button2_id);
picview.setImageResource(R.drawable.my_image);
picview.setVisibility(View.INVISIBLE);
rl.addView(picview, params);
this.setContentView(rl);
You are accidentally creating two copies of picview. Shorten this line:
ImageView picview = new ImageView(this);
To:
picview = new ImageView(this);
(Your field variable private ImageView picview; never changed from null, so when you clicked your Button you see the NullPointerException...)
change
show_pic.setOnClickListener(new OnClickListener() to
show_pic.setOnClickListener(new View.OnClickListener()

Categories

Resources