How to programatically make RelativeLayout that works - java

I have a problem with creating a working Layout in View class that will not crush the program. I think everything is done correctly in the code, but probably there are some setting I don't know about I hope that You guys will reveal that to me.
I am trying to do drawing View with a "Clear" button and RadioGroup.
So first od all, that works fine, but both added content Views (btnReset = "Clear", btnScale = RadioGroup) are hoovered on each other. That is why I want to add one parent Layout, so I can easily manage these Views.
File1.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DrawView tv = new DrawView(this);
setContentView(tv);
addContentView(tv.btnReset, tv.params);
addContentView(tv.btnScale, tv.paramsRadio);}
DrawView.java
public class DrawView extends View {
public Button btnReset;
public RelativeLayout.LayoutParams params;
public RelativeLayout.LayoutParams paramsRadio;
public RadioGroup btnScale;
public DrawView(Context context) {
super(context);
btnReset = new Button(context);
btnReset.setText("Clear Screen");
btnScale = new RadioGroup(context);
btnScale.setOrientation(RadioGroup.HORIZONTAL);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
paramsRadio = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
I omit most of the code because I think its irrelevant, everything works fine but the layout.
Anyway, when I try to create a layout, I simply add a little code:
Changed File1.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DrawView tv = new DrawView(this);
setContentView(tv);
addContentView(tv.layout, tv.params); }
Changed DrawView.java
public class DrawView extends View {
public Button btnReset;
public RelativeLayout.LayoutParams params;
public RelativeLayout.LayoutParams paramsRadio;
public RelativeLayout layout;
public RadioGroup btnScale;
public DrawView(Context context) {
super(context);
btnReset = new Button(context);
btnReset.setText("Clear Screen");
btnScale = new RadioGroup(context);
btnScale.setOrientation(RadioGroup.HORIZONTAL);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
paramsRadio = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
layout.addView(btnReset, params);
layout.addView(btnScale, paramsRadio);
I assume that should work like charm, but unfortunately, when I try to do anything with newly created layout, my app crashes. What is wrong with that code?

Well, after quite long fight I managed to solve my own problem :D I did not do it, like #pskink suggested, because honestly for me it was too advanced. I found much simpler solution.
The thing is, You can create as much views as You want in DrawView.java, but sort them in Layout created in File1.java!
Just like that:
File1.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
DrawView tv = new DrawView(this);
ll.addView(tv.btnReset, tv.params);
ll.addView(tv.btnScale, tv.paramsRadio);
ll.addView(tv);
setContentView(ll);
And DrawView.java stays the same:
public class DrawView extends View {
public Button btnReset;
public LinearLayout.LayoutParams params;
public LinearLayout.LayoutParams paramsRadio;
public RadioGroup btnScale;
public DrawView(Context context) {
super(context);
btnReset = new Button(context);
btnReset.setText("Clear Screen");
btnScale = new RadioGroup(context);
btnScale.setOrientation(RadioGroup.HORIZONTAL);
params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
paramsRadio = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);

Related

Problem with LayoutParam inside a function

I am trying to clean my java code by placing RelativeLayout.LayoutParams inside a function, so that I don't have to create new layoutparams everytime I add new thing.
The problem: I got an error code
Unable to start activity ComponentInfo{onedevz.com.noct/onedevz.com.noct.MainActivity}: java.lang.NullPointerException: Layout parameters cannot be null
it says I got no layout param even though I do have one and I called it before I placed it into a Button. here is the code
public class MainActivity extends AppCompatActivity {
MicButton micbutton;
Button menuBtn,onBtn;
EditText text;
RelativeLayout relativeLayout;
RelativeLayout.LayoutParams lp1,lp2,lp3,lp4;
boolean clicked;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Creating a new RelativeLayout
relativeLayout = new RelativeLayout(this);
// Defining the RelativeLayout layout parameters.
// In this case I want to fill its parent
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
// Defining the layout parameters of the TextView
placement(lp1,100,100,0,200,40,0,RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.ALIGN_PARENT_LEFT);
placement(lp2,100,100,0,350,40,0,RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.ALIGN_PARENT_LEFT);
placement(lp3,100,100,0,500,40,0,RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.ALIGN_PARENT_LEFT);
placement(lp4,100,ViewGroup.MarginLayoutParams.MATCH_PARENT,150,0,40,0,RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.CENTER_HORIZONTAL);
setParameter();
addView();
// Setting the RelativeLayout as our content view
setContentView(relativeLayout, rlp);
}
void placement(RelativeLayout.LayoutParams name,int height,int width,int topMargin,int bottomMargin,int leftMargin,int rightMargin,int rule1,int rule2){
name = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
name.addRule(rule1);
name.addRule(rule2);
name.leftMargin = leftMargin;
name.bottomMargin = bottomMargin;
name.height = height;
name.width = width;
}
void setParameter(){
// Setting the parameters on the TextView
micbutton.setLayoutParams(lp1);
onBtn.setLayoutParams(lp2);
menuBtn.setLayoutParams(lp3);
text.setLayoutParams(lp4);
}
void addView(){
// Adding the TextView to the RelativeLayout as a child
relativeLayout.addView(micbutton);
relativeLayout.addView(text);
}
}
Any help will be appreciated, Thanks.
Your LayoutParameters lp1,lp2,lp3,lp4 have not been initialized. Initialize them and then set it to the TextView.
Solution:
Write like this:
void placement(RelativeLayout.LayoutParams name,int height,int width,int topMargin,int bottomMargin,int leftMargin,int rightMargin,int rule1,int rule2){
name = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
name.addRule(rule1);
name.addRule(rule2);
name.leftMargin = leftMargin;
name.bottomMargin = bottomMargin;
name.height = height;
name.width = width;
void setParameter();
}
void setParameter(){
// Setting the parameters on the TextView
micbutton.setLayoutParams(lp1);
onBtn.setLayoutParams(lp2);
menuBtn.setLayoutParams(lp3);
text.setLayoutParams(lp4);
void addView();
}
void addView(){
// Adding the TextView to the RelativeLayout as a child
relativeLayout.addView(micbutton);
relativeLayout.addView(text);
}
Hopefully it should solve your issue.

what is equal to JPanel (Java) in android

If I want add 2 buttons to JFrame in Java with each press on certain button I create JPanel and add this 2 buttons to the Jpanel then add the JPanel to the JFrame
But in android I tried
public class object extends Activity {
ToggleButton togglebutton;
Button button;
public void onCreate(Bundle bundle){
super.onCreate(bundle);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setWeightSum(100);
LinearLayout.LayoutParams par = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT,30);
LinearLayout.LayoutParams part = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT,70);
togglebutton = new ToggleButton(this);
button = new Button(this);
button.setLayoutParams(par);
button.setLayoutParams(part);
layout.addView(button);
layout.addView(togglebutton);
LinearLayout lay =(LinearLayout)findViewById(R.id.lay);
try {
lay.addView(layout);
}catch(Exception e){
e.printStackTrace();
}
}
}
But didn't work I always get exception with that
What can I do?
Or what is equal to JPanel in Android?
You said nothing about "adding content from one Activity to other" in your initial post and didn't include an a logcat for your exception, but the obvious problem is not calling setContentView while using findViewById.
This code creates the activity you are trying to make.
public class MainActivity extends Activity {
ToggleButton togglebutton;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout rootView = new LinearLayout(this);
rootView.setOrientation(LinearLayout.HORIZONTAL);
rootView.setWeightSum(100);
LinearLayout.LayoutParams par = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT,30);
LinearLayout.LayoutParams part = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT,70);
togglebutton = new ToggleButton(this);
button = new Button(this);
button.setText("Click Me");
button.setLayoutParams(par);
button.setLayoutParams(part);
rootView.addView(button);
rootView.addView(togglebutton);
setContentView(rootView);
}
}
The closest thing to JPanel for android would be the root view of your layout. I think cricket has the right solution and your getting an error beacuse your not setting setContentView(). This will set your root view to the xml layout file specified. For example setContentView(R.layout.main);.

clickable image button in java without xml in android

I want to make image button in java class without using XML(dynamic UI).
I can make this but I can't connect to onClick void.
Can anybody help me?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
LayoutParams params =
new TableRow.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
TableRow table = new TableRow(this);
table.setOrientation(TableRow.VERTICAL);
ImageButton ib = new ImageButton(this);
ib.setImageResource(R.drawable.one);
ib.setLayoutParams(params);
table.addView(ib);
TableRow.LayoutParams layoutParams=
new TableRow.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT );
this.addContentView(table, layoutParams);
}
public void onClick(View v){
Toast.makeText(getBaseContext(),"This button clicked",Toast.LENGTH_SHORT).show();
}
you missed ib.setOnClickListener(...);. If your activity implements View.OnClickListener then it would be
ib.setOnClickListener(this);
You need to assign OnClickListener before using it
If you are implementing OnClickListener in the activity then use (I believe you are because you have onClick method defined)
ib.setOnClickListener(this);
If not then do this
ib.setOnClickListener(new View.OnClickListener()
{
public void onClick(){
// Do as required
}
});

opening new content view

In my Main.class I am creating a dynamic ScrollView and starting it using setContentView(sv) it displays a Spinner and a Button.
The user selects an option from the Spinner and then clicks the Button. I have set a setOnClickListener to the Button.
In the onClick method I am trying to start up a new ContentView. Calling a new class file with the same type of layout as Main.class. Not sure how to go about doing this. I added finish() to the onClick method and it closes the original window but not sure how to open the new one.
Here is the Main.class code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.messages = new Messages(this);
this.datasource = new FacilitiesDataSource(this);
this.datasource.open();
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
Spinner s = new Spinner(this);
s.setId(SPINNER_FACILITIES);
s.setLayoutParams(new Spinner.LayoutParams(-2,-1));
final List<SpinnerObject> list = this.datasource.getFacilitiesList();
final ArrayAdapter<SpinnerObject> adapter = new ArrayAdapter<SpinnerObject>(this, android.R.layout.simple_spinner_dropdown_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);
ll.addView(s);
Button b = new Button(this);
b.setText("Submit");
b.setLayoutParams(new LayoutParams(-2,-1));
b.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
Spinner spin = (Spinner)findViewById(SPINNER_FACILITIES);
Log.v("option picked", Integer.toString(( (SpinnerObject) spin.getSelectedItem () ).getId () ));
/*
* NEED HELP HERE
* NEED HELP HERE
* NEED HELP HERE
*/
//finish();
}
});
ll.addView(b);
setContentView(sv);
}
Here is my Vehicles.class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v("Started onCreate", "Vehicle");
ScrollView sv = new ScrollView(this);
setContentView(sv);
}
I assume you mean you want to start a new Activity (from Main to Vehicles). In that case you can use the following code:
Intent intent = new Intent(Main.this, Vehicle.class);
startActivity(intent);
Make sure all Activity classes are declared in your Android Manifest file, inside the <application> tag, like this:
<activity android:name=".Vehicles" />

How to access views by id from tabHost?

I have tabHost widget with dynamic created tabs.
In each of newly created tab, there is relative layout with some views (textView, listView etc).
My question is: how to access to these views and how to append new content there? I tried with setting ids or tags, but i didn't work.
public class Main extends Activity {
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
final TabHost tabs=(TabHost)findViewById(R.id.tabhost);
tabs.setup();
TabHost.TabSpec spec=tabs.newTabSpec("buttontab");
spec.setContent(R.id.form);
spec.setIndicator("Search-form");
tabs.addTab(spec);
Button btn=(Button)tabs.getCurrentView().findViewById(R.id.buttontab);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
final TabHost.TabSpec spec=tabs.newTabSpec("tag1");
spec.setContent(new TabHost.TabContentFactory() {
public View createTabContent(String tag)
{
// Creating a new RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(Main.this);
// Defining the RelativeLayout layout parameters.
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
TextView t = new TextView(Main.this);
// t.setId(12); //This dosen't work
t.setText("Searching in progress. Please wait...");
ListView resultsList = new ListView(Main.this);
//Here: array adapter etc
// Adding the TextView to the RelativeLayout as a child
relativeLayout.addView(t);
relativeLayout.addView(resultsList);
return relativeLayout;
}
});
spec.setIndicator("Tab with results");
tabs.addTab(spec);
new GetData(Main.this).execute();
}
});
}
public void appendResultsToViews(String result)
{
//Here i need to access TextView from tab with specific tag and append results to TextView, which was created dynamically earlier
}
Thank you for your help.
set tag of child view and use findViewWithTag(tag)

Categories