How can i programmatically make my AlertDialog Scrollable ?
My AlertDialog is defined like this :
private void DisplayAlertChoice(String title, String msg, final int pos)
{
alert = new AlertDialog.Builder(this);
alert.setTitle(title);
final LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
CheckBox[] t=new CheckBox[15];
for (i=0;i<currentBattery.temperatureNumber;i++)
{
t[i]=new CheckBox(this);
t[i].setText("title");
t[i].setChecked(false);
t[i].setOnCheckedChangeListener(this);
layout.addView(t[i]);
}
alert.setView(layout);
alert.show();
}
I tried a few solutions found in the net but didn't work for me.
Can someone give me a pertinent solution to make it scrollable programmatically ?
i tried it, but it forces my app to stop.
ScrollView can have just a child. It is an Android constraint. So, doing
final LinearLayout layout = new LinearLayout(this);
ScrollView scrollView = new ScrollView(this);
layout.setOrientation(LinearLayout.VERTICAL);
CheckBox[] t=new CheckBox[15];
for (i=0;i<currentBattery.temperatureNumber;i++)
{
t[i]=new CheckBox(this);
t[i].setText("title");
t[i].setChecked(false);
t[i].setOnCheckedChangeListener(this);
scrollView.addView(t[i]);
}
will make your app crash, because you are adding more than one child to the ScrollView. But if you do
final LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
CheckBox[] t=new CheckBox[15];
for (i=0;i<currentBattery.temperatureNumber;i++)
{
t[i]=new CheckBox(this);
t[i].setText("title");
t[i].setChecked(false);
t[i].setOnCheckedChangeListener(this);
layout.addView(t[i]);
}
final ScrollView scrollView = new ScrollView(this);
scrollView.addView(layout);
it should work smoothly.
Related
I want to design a seekbar with textview (with a background colour) on its two sides. I am writing the code in Android Studio. I have implemented the design. However I am not getting the desired output.
Can anyone help me regarding this.
The following is my code snippet:
SeekBar sb1 = new SeekBar(this);
TextView tv1 = new TextView(this);
TextView tv2 = new TextView(this);
tv1.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_launcher_background,0,0,0);
LinearLayout.LayoutParams sblayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
sblayoutParams.setMargins(30, 30, 30, 30);
tv1.setText("0");
tv1.setLayoutParams(sblayoutParams);
sb1.setLayoutParams(sblayoutParams);
tv2.setText("100");
tv2.setLayoutParams(sblayoutParams);
LinearLayout sblinearLayout = findViewById(R.id.rootContainer);
// Add SeekBar to LinearLayout
if (sblinearLayout != null) {
sblinearLayout.addView(sb1);
sblinearLayout.addView(tv1);
sblinearLayout.addView(tv2);
}
I have programmatically added Card View. I just want to make it clickable and show animation while it is clicked. Here is my Code
CardView cardView = new CardView(this);
LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
cardView.setLayoutParams(layoutParams);
cardView.setRadius(15);
cardView.setPadding(25, 25, 25, 25);
cardView.setCardBackgroundColor(Color.MAGENTA);
TextView textView = new TextView(this);
textView.setLayoutParams(layoutParams);
textView.setText("Programmatically set");
textView.setGravity(Gravity.CENTER);
textView.setTextColor(Color.WHITE);
cardView.addView(textView);
LinearLayout linearLayout = findViewById(R.id.linearLayout1);
linearLayout.addView(cardView);
int[] attrs = new int[]{R.attr.selectableItemBackground};
TypedArray typedArray = this.obtainStyledAttributes(attrs);
int selectableItemBackground = typedArray.getResourceId(0, 0);
typedArray.recycle();
cardView.setForeground(this.getDrawable(selectableItemBackground));
cardView.setClickable(true);
With the Material Components Library just use:
cardView.setRippleColor(ContextCompat.getColorStateList(this,R.color.selector_card));
You can use this:
cardView.setClickable(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
TypedValue typedValue = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, typedValue, true);
cardView.setBackgroundResource(typedValue.resourceId);
}
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);
}
}
Admob ad not showing in my application. I am sensing ad is loading but somehow its not showing because of my layout arrangement. but i couldn't locate it. here is my code
protected void onSetContentView() {
final RelativeLayout relativeLayout = new RelativeLayout(this);
final FrameLayout.LayoutParams relativeLayoutLayoutParams = new FrameLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
this.mRenderSurfaceView = new RenderSurfaceView(this);
mRenderSurfaceView.setRenderer(mEngine);
final FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.FILL_PARENT,
FrameLayout.LayoutParams.FILL_PARENT,Gravity.CENTER);
final android.widget.RelativeLayout.LayoutParams surfaceViewLayoutParams = new RelativeLayout.LayoutParams(layoutParams);
surfaceViewLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
relativeLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams);
FrameLayout frameLayout = new FrameLayout(this);
adView = new AdView(this, AdSize.BANNER, "##############");
adView.refreshDrawableState();
adView.setVisibility(AdView.VISIBLE);
frameLayout.addView(adView);
relativeLayout.addView(frameLayout);
this.setContentView(relativeLayout, relativeLayoutLayoutParams);
}
You have told your SurfaceView to FILL_PARENT, so it is taking all the space leaving none for the AdView sibling.
final FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.FILL_PARENT,
FrameLayout.LayoutParams.FILL_PARENT,Gravity.CENTER);
Note also there is no need for the FrameLayout wrapping the AdView.
I suggest you follow jquery404's advice and define the layout in XML. You'll get a much better understanding of your ui design.
String[] textArray={"one","two","asdasasdf asdf dsdaa"};
int length=textArray.length;
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for(int i=0;i<length;i++){
TextView tv=new TextView(getApplicationContext());
tv.setText(textArray[i]);
relativeParams.addRule(RelativeLayout.BELOW, tv.getId());
layout.addView(tv, relativeParams);
}
I need to do something like that.. so it would display as
one
two
asdfasdfsomething
on the screen..
If it's not important to use a RelativeLayout, you could use a LinearLayout, and do this:
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
Doing this allows you to avoid the addRule method you've tried. You can simply use addView() to add new TextViews.
Complete code:
String[] textArray = {"One", "Two", "Three", "Four"};
LinearLayout linearLayout = new LinearLayout(this);
setContentView(linearLayout);
linearLayout.setOrientation(LinearLayout.VERTICAL);
for( int i = 0; i < textArray.length; i++ )
{
TextView textView = new TextView(this);
textView.setText(textArray[i]);
linearLayout.addView(textView);
}
Try this code:
final String[] str = {"one","two","three","asdfgf"};
final RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
final TextView[] tv = new TextView[10];
for (int i=0; i<str.length; i++)
{
tv[i] = new TextView(this);
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams
((int)LayoutParams.WRAP_CONTENT,(int)LayoutParams.WRAP_CONTENT);
params.leftMargin = 50;
params.topMargin = i*50;
tv[i].setText(str[i]);
tv[i].setTextSize((float) 20);
tv[i].setPadding(20, 50, 20, 50);
tv[i].setLayoutParams(params);
rl.addView(tv[i]);
}
public View recentView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Create a relative layout and add a button
relativeLayout = new RelativeLayout(this);
btn = new Button(this);
btn.setId((int)System.currentTimeMillis());
recentView = btn;
btn.setText("Click me");
relativeLayout.addView(btn);
setContentView(relativeLayout);
btn.setOnClickListener(new View.OnClickListener() {
#Overr ide
public void onClick(View view) {
//Create a textView, set a random ID and position it below the most recently added view
textView = new TextView(ActivityName.this);
textView.setId((int)System.currentTimeMillis());
layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.BELOW, recentView.getId());
textView.setText("Time: "+System.currentTimeMillis());
relativeLayout.addView(textView, layoutParams);
recentView = textView;
}
});
}
This can be modified to display each element of a String array in different TextViews.
You're not assigning any id to the text view, but you're using tv.getId() to pass it to the addRule method as a parameter. Try to set a unique id via tv.setId(int).
You could also use the LinearLayout with vertical orientation, that might be easier actually. I prefer LinearLayout over RelativeLayouts if not necessary otherwise.