Dynamically changing layout in application - java

I want my users to have the ability to choose a few buttons which would be transferred to a different page, like a "favourite buttons" page. To do this, I need to change the layout from the app. I would like the modified layout to persist even after closing the app. Is it possible to do this?

For This you should make some activity with different layout.
If you don't want to add more activity just use this:
private void SwitchLayout2()
{
RelativeLayout Layout1 = (RelativeLayout)findViewById(R.id.layout1);
RelativeLayout Layout2 = (RelativeLayout)findViewById(R.id.layout2);
// Enable Layout 2 and Disable Layout 1
Layout1 .setVisibility(View.GONE);
Layout2.setVisibility(View.VISIBLE);
}
private void SwitchLayout1()
{
RelativeLayout Layout1 = (RelativeLayout)findViewById(R.id.layout1);
RelativeLayout Layout2 = (RelativeLayout)findViewById(R.id.layout2);
// Enable Layout 1 & Disable Layout2
Layout1.setVisibility(View.VISIBLE);
Layout2.setVisibility(View.GONE);
}

Related

Create dynamically text fields

I'm trying to get my button to create a text field where the user can input information. This way they can only create as many lines as they would like. Also, is there a way to create multiple fields at once?
So, it'll end up being something like this:
"Add Event" (rest of the screen is blank until they click on that button)
Text field 1/ Text field 2/ Text field 3
(once they press that button and of course without the underlines, just an example)
So they can put in information that they want there. If they want another row, they click on the add button again.
Am I supposed to be using an onClickListener? I'm confused as to how I would go about making the button create that field for the user.
public class BudgetScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_budget_screen);
Button addBillExpense = (Button) findViewById(R.id.addBillExpense);
addBillExpense.setOnClickListener(new Button.OnClickListener() {
public void onClick (View v) {
TextView inputField = new TextView(BudgetScreen.this);
}
});
}
}
That is what I have so far. I've been stuck on this for a hot minute. I am aware that I haven't used "inputField" yet.
Suppose you have the following layout xml:
<LinearLayout ...>
<Button .../>
<LinearLayout ...
android:id="#+id/holder"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
in button onClickListener you can have something like:
LinearLayout layout = (LinearLayout) findViewById(R.id.holder);
EditText et = new EditText(this);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
layout.addView(et,lp);
You can change the LayoutParams to get the layout you like.
If you want multiple EditText in a single row, you can do the following:
final int NUM_EDITTEXT_PER_ROW = 3;
LinearLayout layout = (LinearLayout) findViewById(R.id.holder);
Display display = ((WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth()/NUM_EDITTEXT_PER_ROW;
LinearLayout tempLayout = new LinearLayout(this);
tempLayout.setOrientation(LinearLayout.HORIZONTAL);
for(int i=0;i<NUM_EDITTEXT_PER_ROW;i++){
EditText et = new EditText(this);
LayoutParams lp = new LayoutParams(width,LayoutParams.WRAP_CONTENT);
tempLayout.addView(et,lp);
}
layout.addView(tempLayout);

Empty ListView show another activity

I am making an application which uses ListView to show DB elements. But in the first opening or when user will delete everything DB will be empty and then ListView will show blank screen. I want to show a message when it happens. Not only TextView but also Button. For example "DB now empty. Click on button and try to add some records". ListView 's setEmptyView method allows to add only TextView . Is it really possible to create a layout and show it when ListView is empty?
You can use the property setEmptyView.
If the list is empty and your desired display.
ListView listView = (ListView) findViewById(R.id.lst);
View child = getLayoutInflater().inflate(R.layout.empty_view, null);
((ViewGroup)listView.getParent()).addView(child);
listView.setEmptyView(child);
You can dynamically add a button in the activity layout if your list is empty.
Set onClickListner on the button and override the onClick().
Refer below code:
LinearLayout linear = (LinearLayout)findViewById(R.id.layout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button btn = new Button(this);
btn.setId("btnDynamic");
btn.setText("Go to create entry page");
linear.addView(btn, params);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(view.getContext(),
"Button clicked index = " + id_, Toast.LENGTH_SHORT)
.show();
}
});
Let me know if this helps.
You can pre-define a layout containing text view and and button as you stated in your question and set it's visibility = gone in your xml or in java code. When the List View will be empty you can set visibility the visibility of the listview.setVisibility(View.GONE) and set your's layout (containing button and a textview) layout.setVisibility(View.VISIBLE)

LinearLayout inside AlertDialog Not Scrolling

I have set a linear layout inside a alert dialog in which I basically have a few Edit texts.
The problem I am facing is that I am not able to scroll down to see every edit text created dynamically.
Here's the snippet:
Context context = this.getApplicationContext();
LinearLayout layoutCreateMerch = new LinearLayout(context);
layoutCreateMerch.setOrientation(LinearLayout.VERTICAL);
layoutCreateMerch.setVerticalScrollBarEnabled(true);
final AlertDialog.Builder alert = new AlertDialog.Builder(Act.this);
alert.setTitle("Submit");
final EditText Name = new EditText(Act.this);
final EditText Desc = new EditText(Act.this);
...
...
So, How can I add a scroll view at runtime in this alert dialog's Linear Layout?
I suppose LinearLayouts don't need a scroll bar/view but this isn't working out for me :|
Also, inflating this would be other option but for now..can I do something abt this?
Thanks for reading.. :)
In theory it goes like this:
ScrollView scroll = new ScrollView(context);
scroll.setBackgroundColor(android.R.color.transparent);
scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
scroll.addView(yourLinearLayout);
sO basically just put you linearLayout to ScrollView and set it as dialogView in the end like,
alertDialog.setView(scroll);
Please use <ScrollView ></ScrollView> to wrap your layout.

Android Popup with EditText, keyboard blocks text, background scrolls

I have a Popup which holds a couple of EditTexts in a ScrollView.
When i click on an EditText it scrolls up the View in background, not the Popup, therefore the keyboard blocks the EditText.
Any ideas ?
PopupWindow popUp = new PopupWindow(this);
popUp.update(0, 0, display.getWidth(), display.getHeight());
popUp.setFocusable(true);
LinearLayout llh = new LinearLayout(this)
llh.setOrientation(LinearLayout.HORIZONTAL);
ScrollView sv = new ScrollView(this);
EditText e1,e2,e3...
llh.addView(e1);
llh.addView(e2);
llh.addView(e3);
...
sv.addView(llh);
popUp.setContentView(llh);
popUp.showAtLocation(llh, Gravity.BOTTOM, 0, 0);
This is "launched" from a View which holds a ScrollView aswell.
If there is a workaround for this, it would be great.
I have faced the same problem as you, and to solve it, i created a custom Dialog and with a custom theme (R.style.ThemeDialogCustom in the following example). I also set dialog.requestWindowFeature(Window.FEATURE_NO_TITLE) and dialog.setCanceledOnTouchOutside(true); and i set a custom layout with setContentView. Something like this:
Dialog dialog = new Dialog(activityContext, R.style.ThemeDialogCustom);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCanceledOnTouchOutside(true);
dialog.setContentView(R.layout.custom_dialog_layout);
In other words, the above piece of code tries to immitate the behavior of a PopupWindow.
EDIT For clarification purposes, if you need to access a view that was defined in the dialog's custom layout, you can access it like this:
EditText e1 = (EditText) dialog.findViewById(R.id.e1);
Hope that helps.

Create a button and add it to a view programmatically

I'm creating a version of the popular Minesweeper game for Android. I'm trying to programmatically create a button and add it to a RelativeLayout. I've found something very similar here: How do I programmatically add buttons into layout one by one in several lines?
When I try to run it I get a NullPointerException at:
RelativeLayout layout1 = (RelativeLayout) findViewById(R.layout.game);
Here's the whole block of code:
public void create() {
RelativeLayout layout1 = (RelativeLayout) findViewById(R.layout.game);
for(int i = 0; i < gridSize; i++) {
if(grid[i] == 0) { //if grid pos. indicates an empty cell
Button empty = new Button(this);
empty.setBackgroundResource(R.drawable.emptybutton); //set background to empty
empty.setId(i); //set id to value of i
empty.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
layout1.addView(empty); //add the button to the relativeLayout view
//((Button) findViewById(i)).setOnClickListener(emptyListener);
}
Thanks in advance for any responses
have set the layout xml of the Activity by setContentView(R.layout.xxxx)?
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
...
this
RelativeLayout layout1 = (RelativeLayout) findViewById(R.layout.game);
should be
RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.relative_id);
R.id... used for mapping control and RelativeLayout is a control.
I think you're getting a blank screen because you haven't set the content view.
What i mean is that the codes does what it's supposed to do however, you should remove the "setContentView()" method at the top and place it at the end, then you should set it to the RelativeLayout before you close the onCreate() method! Something like this:
public void create() {
RelativeLayout layout1 = new RelativeLayout(this);
for(int i = 0; i < gridSize; i++) {
if(grid[i] == 0) { //if grid pos. indicates an empty cell
Button empty = new Button(this);
empty.setBackgroundResource(R.drawable.emptybutton); //set background to empty
empty.setId(i); //set id to value of i
empty.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
layout1.addView(empty); //add the button to the relativeLayout view
//((Button) findViewById(i)).setOnClickListener(emptyListener);
}
}
setContentView(layout1);
}
Also notice that I've changed the declaration of the Relativelayout a little bit.
I hope this helps. :) !
You must enter the ID of the RelativeLayout, not the xml fil name.
try with
RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.yourRelativeLayoutViewID);

Categories

Resources