I have a issue trying to dynamically add some linear layout into a linearlayout container after user have clicked on a button.
private void AddView() {
MyView myView1 = new MyView("Name");
this.mainLinearLayout.addView(myView1);
}
This code works great in activity's onCreate method but not after handling user event.
Do you have any idea why it's not working ? (I mean nothing appears on UI)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AddView(); => works great
}
playButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AddView(); => not working
}
});
Thanks,
I think that AddView is wrong.
private void addView() {
LayoutParams lparams = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
MyView myView1 = new MyView("Name");
myView1.setLayoutParams(lparams);
this.mainLinearLayout.addView(myView1);
}
Related
I have an overlay on my screen which will show some buttons to control the feature. This overlay will have a dynamic list of buttons which will be created based on the number of steps. Example if steps are 3 then 3 button will get created. I have created a LinearLayout inside this buttons will get created. I have created a method which will set all the button attributes, but somehow it's giving me null pointer exception.
In onCreateView I have initialize the buttonArray.
Here is my fragment:
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
mContext = getActivity();
gc = GlobalClass.getInstance(mContext);
db = DataBaseHelper.getInstance(mContext);
sectionButtons = new Button[3];
/*if(savedInstanceState!=null)
mVisible = savedInstanceState.getBoolean("visible",false);*/
manager = SharedPreferenceManager.getInstance(mContext);
//check wether to play video in cover mode or another mode
inflater.inflate(R.layout.fragment_video_side_by_side, container, false);
}
and onViewCreated() I am calling my method addSections():
// show overlay with buttons in onTouch
parentLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (!mVisible) {
overLay.setVisibility(View.VISIBLE);
addSections(3);
mVisible = true;
} else {
overLay.setVisibility(View.GONE);
mVisible = false;
}
}
return true;
}
});
Method:
public void addSections(int numOfSection){
if(numOfSection==0)
return;
else {
for (int i = 0; i < numOfSection; i++) {
//set the properties for button
sectionButtons[i].setLayoutParams(new LinearLayout.LayoutParams(50, 50));
sectionButtons[i].setBackgroundResource(R.drawable.round_button);
sectionButtons[i].setText("Section "+i+"");
sectionButtons[i].setId(i);
//add button to the layout
mSectionLayout.addView(sectionButtons[i]);
}
}
}
mSectionLayout is my parent LinearLayout. While debugging I found my sectionButtons is not showing null but while setting the attributes it's throwing nullPointer exception.
Create Dynamic Buttons and attach with the Layout
You can easily create the dynamic button by the following way:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
TextView textView = new TextView(this);
textView.setText("Text View ");
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
layout.addView(textView, p);
Button buttonView = new Button(this);
buttonView.setText("Button");
buttonView.setOnClickListener(mThisButtonListener);
layout.addView(buttonView, p);
}
private OnClickListener mThisButtonListener = new OnClickListener() {
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Hello !",
Toast.LENGTH_LONG).show();
}
};
}
Using this code, you easily generate a list of buttons on the layout.
Happy coding
I have a simple problem with views in Android. I have two view rootView and containerView, containerView is contained in rootView, but I don't understand why when I click on containerView rootView is triggered.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
LinearLayout rootView = new LinearLayout(this);
rootView.setBackgroundColor(Color.BLUE);
rootView.setGravity(Gravity.CENTER);
rootView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
rootView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("esx","ROOT VIEW ID: "+v.getId());
}
});
LinearLayout containerView = new LinearLayout(this);
containerView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,100));
containerView.setBackgroundColor(Color.CYAN);
rootView.addView(containerView);
setContentView(rootView);
}}
I want that when I click containerView, nothing happens. Is there a way to do this?
Thanks
Try setting containerView.setClickable(true)
onClick provides a View argument. Just compare it and see if it's the rootView. If that doesn't work, try to see if that ISN'T the containerView (you'll need to declare it before adding the listener.
I have LinearLayout1, LinearLayout2 and a Button in MainActivity. When I click the Button, I want it to jump from LinearLayout1 to LinearLayout2. How can I do that?
You can do like this :
LinearLayout mLinearLayout1 = (LinearLayout)findViewById(R.id.linearlayout_1);
LinearLayout mLinearLayout2 = (LinearLayout)findViewById(R.id.linearlayout_1);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mLinearLayout1.setVisibility(View.GONE);
mLinearLayout2.setVisibility(View.VISIBLE);
}
});
Thanks to all of you specially Mike M
i get my answer with:
public class MainActivity extends AppCompatActivity {
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
final LinearLayout mLinearLayout1 = (LinearLayout)findViewById(R.id.liner1);
final LinearLayout mLinearLayout2 = (LinearLayout)findViewById(R.id.liner2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mLinearLayout1.removeView(button);
mLinearLayout2.addView(button);
}
});
}
}
If you want one of the many different views (not LinearLayouts) to be displayed right on the main activity (may depend on a condition or a state engine or time based interval), you probably can use ViewFlipper.
You can hide and show one or the other, for example:
LinearLayout mLinearLayout1 = (LinearLayout) findViewById(R.id.linearlayout_1);
LinearLayout mLinearLayout2 = (LinearLayout) findViewById(R.id.linearlayout_1);
mLinearLayout1.setVisibility(View.GONE);
mLinearLayout2.setVisibility(View.VISIBLE);
But If you want to show an hide some views with diifferent behavior I recommend you using Fragments.
How do I make a single button width to fill parent programmatically? I have done this but it cannot seem to work it is still located on top left with width just wrapping the content... here is some of the code on the button creation...
public class TEST_GIFDisplay extends Activity implements View.OnClickListener {
SurfaceView sview;
GifRun gr = new GifRun();
Button btnBack;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sview = new SurfaceView(this);
sview.setZOrderOnTop(true);
gr.LoadGiff(sview, this, R.drawable.smiley);
LinearLayout linearLayout1 = new LinearLayout(this);
linearLayout1.setOrientation(LinearLayout.VERTICAL);
linearLayout1.setBackgroundColor(Color.parseColor("#27ae60"));
linearLayout1.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
LinearLayout linearLayout2 = new LinearLayout(this);
linearLayout2.setOrientation(LinearLayout.VERTICAL);
linearLayout2.setBackgroundColor(Color.parseColor("#27ae60"));
linearLayout2.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams p = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
p.weight = 1.0f;
btnBack = new Button (this);
btnBack.setText("Back");
btnBack.setBackgroundColor(Color.parseColor("#f1c40f"));
btnBack.setGravity(Gravity.CENTER_HORIZONTAL);
btnBack.setLayoutParams(p);
btnBack.setOnClickListener(this);
linearLayout2.addView(btnBack);
linearLayout1.addView(linearLayout2);
linearLayout1.addView(sview);
setContentView(linearLayout1);
}
#Override
public void onClick(View view) {
btnBack = (Button) view;
btnBack.setBackgroundColor(Color.parseColor("#2980b9")); //color change
new CountDownTimer(100, 50) {
#Override
public void onTick(long arg0) {
// TODO Auto-generated method stub
}
#Override
public void onFinish() {
btnBack.setBackgroundColor(Color.parseColor("#f1c40f")); // original color
}
}.start();//end color changed
finish();
}
}
You are adding the Button to linearLayout2. You should change the linearLayout2's width to MATCH_PARENT
linearLayout2.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
I hope this helps.
P.S: You can create Button's selectors for pressed and selected states, instead of using timer to show a pressed button effect. Here is a basic link that can help you in achieving this : android button selector
im having problems with a simple task.
I have 2 Buttons and i want only one to be visible at same time, so when you touch one it hides and the other appears.
This is my code:
fromAnex = new Button(this);
fromAnex.setText("from");
fromAnex.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ONClickListener", "from anex");
returnFromAnex();
}
});
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.LEFT_OF,plano.getId());
params.addRule(RelativeLayout.CENTER_VERTICAL);
rl.addView(fromAnex,params);
fromAnex.setVisibility(View.GONE);
toAnex = new Button(this);
toAnex.setText("to");
toAnex.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ONClickListener", "Show anex");
showAnex();
}
});
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.RIGHT_OF,plano.getId());
params2.addRule(RelativeLayout.CENTER_VERTICAL);
rl.addView(toAnex,params2);
private void showAnex()
{
fromAnex.setVisibility(View.VISIBLE);
toAnex.setVisibility(View.GONE);
private void returnFromAnex()
{
fromAnex.setVisibility(View.GONE);
toAnex.setVisibility(View.VISIBLE);
I really cant understand why but that first call to fromAnex.setVisibility(View.GONE); is working as expected, the first call to showAnex() at fromAnex.setVisibility(View.VISIBLE);
is working too but toAnex.setVisibility(View.GONE); right under doesnt work.
And after that nothing happens with buttons visibility when i touch.
Someone can see whats wrong with my code?
Sorry for my bad english and thanks.
EDIT:
That two methods arent complete, the rest are not related with this buttons visibility problem.
When i set visibility to gone just after creating the button it works but then i cant set visibility to gone again, thats the problem.
Taking your snippet I tried run the following and it works perfectly.I guess you are making some small mistake some where as DevTest said.
public class MainActivity extends Activity {
Button fromAnex,toAnex;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout rl=new RelativeLayout(getApplicationContext());
RelativeLayout.LayoutParams relPra=new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
rl.setLayoutParams(relPra);
fromAnex = new Button(this);
fromAnex.setText("from");
fromAnex.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ONClickListener", "from anex");
returnFromAnex();
}
});
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_VERTICAL);
rl.addView(fromAnex,params);
fromAnex.setVisibility(View.GONE);
fromAnex.setId(1);
toAnex = new Button(this);
toAnex.setId(2);
toAnex.setText("to");
toAnex.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ONClickListener", "Show anex");
showAnex();
}
});
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.RIGHT_OF,fromAnex.getId());
params2.addRule(RelativeLayout.CENTER_VERTICAL);
rl.addView(toAnex,params2);
setContentView(rl);
}
private void showAnex()
{
fromAnex.setVisibility(View.VISIBLE);
toAnex.setVisibility(View.GONE);
}
private void returnFromAnex()
{
fromAnex.setVisibility(View.GONE);
toAnex.setVisibility(View.VISIBLE);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}