How to Place TextView and EditText on same line without using xml? - android

I used xml and very much familiar with xml only for layouts.
I am trying to code in activity and want to develop view like,
textview editetxt
textview edittext
textview edittext button
LinearLayout lLayout = (LinearLayout)findViewById(R.id.linearLayout);
TextView textViewName = new TextView (MainActivity.this);
textViewName .setText("Name:");
Edittext editTextName= new Edittext (MainActivity.this);
lLayout.addView(textViewName );
lLayout.addView(editTextName);
It will obviously come vertical, but how to place horizontal...
I searched but not got any solution. I tried but, I don't know how to place vertical layout first and inside of it horizontal...
-please help me to sort out this issue if any one knows...

you can create another LinearLayout hLayout inside the lLayout(vertical) and set the orientation of this new layout to be horizontal using
hLayout.setOrientation(LinearLayout.Horizontal);
Add it to llayout
lLayout.addView(hlayout);
Now place both your textview nad edittext inside the hlayout.

package manish.example.testproject;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
int m = 3; // here write number of view want show
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout main = new LinearLayout(this);
main.setOrientation(LinearLayout.VERTICAL);
for (int k = 0; k < m; k++) {
LinearLayout li = new LinearLayout(this);
li.setOrientation(LinearLayout.HORIZONTAL);
TextView t = new TextView(this);
t.setText("Name : ");
EditText e = new EditText(this);
li.addView(t);
li.addView(e);
main.addView(li);
}
setContentView(main);
}
}

I don't understand exactly, but as per my understanding, I am writing my code
LinearLayout lLayout = (LinearLayout)findViewById(R.id.layout1);
LinearLayout Layout2 = new LinearLayout(this);
TextView textViewName = new TextView (MainActivity.this);
textViewName .setText("Name:");
EditText editTextName= new EditText (MainActivity.this);
lLayout.setOrientation(LinearLayout.VERTICAL);
lLayout.addView(Layout2);
Layout2.setOrientation(LinearLayout.HORIZONTAL);
Layout2.addView(textViewName );
Layout2.addView(editTextName);

Related

Displaying linear layout VS R.layout.activity main

So I'm new to android development and I'm having a bit a of a trouble.
I'm developing an an app which is going to have a similar background every time but imports a new image every time the user touches the screen.
HOWEVER my problem is that I'm having an issue right now in which I'm trying to load a new activity when a user click a button, 1) I followed one tutorial which had me use XML to add the button and program MAIN_activity to to switch to second_Activity using setcontent(R.layout.main_Activity) and works fine.
2) I also started another tutorial which had me use setContent(layout1) where layout one is in fact a LinearLayout which you addView(stuff) such as a button and program it to switch the second activity, but I'm failing terribly.
long story short, using this line setcontent(R.layout.main_Activity) overrides the setContent(layout1) info and I cant combine them. In addition I dont know how to make a button and click to switch activity except using the first method, I'm open to suggestions.
package self.name.firstandroidprogram;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
LinearLayout layout1;
EditText number1Text;
EditText number2Text;
Button calcButton, switchButton;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout1 = new LinearLayout(this);
number1Text = new EditText(this);
number2Text = new EditText(this);
calcButton = new Button(this);
switchButton = (Button)findViewById(R.id.button1);
////////////////////////////////////////////////////////////////////////BUTTON ACTIVITY SWTICH
switchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Class2.class);
startActivity(intent);
}
});
////////////////////////////////////////////////////////////////////////////
answerText.setText("0");
calcButton.setText("X");
layout1.addView(number1Text);
layout1.addView(number2Text);
layout1.addView(calcButton);
layout1.addView(answerText);
layout1.addView(switchButton);
setContentView(R.layout.activity_main);// Works
// setContentView(layout1) failes when i run
}
I am not sure, but try it
public class MainActivity extends Activity {
LinearLayout layout1;
EditText number1Text;
EditText number2Text;
Button calcButton, switchButton;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout1 = new LinearLayout(this);
// Do it before adding views in it.
setContentView(layout1);
number1Text = new EditText(this);
number2Text = new EditText(this);
calcButton = new Button(this);
switchButton = (Button)findViewById(R.id.button1);
////////////////////////////////////////////////////////////////////////BUTTON ACTIVITY SWTICH
switchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Class2.class);
startActivity(intent);
}
});
////////////////////////////////////////////////////////////////////////////
answerText.setText("0");
calcButton.setText("X");
layout1.addView(number1Text);
layout1.addView(number2Text);
layout1.addView(calcButton);
layout1.addView(answerText);
layout1.addView(switchButton);
// setContentView(R.layout.activity_main);// Works
}
You can set LinearLayout as your main layout. When user clicks the switch button, you need to set background of the LinearLayout
activity_main.xml :
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/main"
android:background="#drawable/default"
>
<!-- put your Button here switch button -->
</LinearLayout>
MainActivity.java :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switchButton = (Button)findViewById(R.id.button1);
LinearLayout lv1 = (LinearLayout) findViewById(R.id.main);
switchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// we have reference of LinearLayout as lv1
// we will change background of lv1 here when user clicks
lv1.setBackgroundResource(<your_new_background_image_id>);
}
});
<your_new_background_image_id> can be fetched from /res/drawable/ folder. You store 10-20 images of small size in drawable and rename them with similar name i.e. image1, image2 etc
When setting them as background, you can code like :
int[] imageArray = {R.drawable.image1, R.drawable.image2,...}
lv1.setBackgroundResource(imageArray[(i++)%10]);
If you are creating view programatically you have to set each view LayourParameters using view.setLayoutParams()
Try this
layout1 = new LinearLayout(this);
EditText number1Text = new EditText(this);
EditText number2Text = new EditText(this);
Button calcButton = new Button(this);
calcButton.setText("X");
//impt : width=MATCH_PARENT and height=MATCH_PARENT
layout1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
layout1.setOrientation(LinearLayout.VERTICAL); //setting LL orientation
layout1.addView(number1Text);
layout1.addView(number2Text);
layout1.addView(calcButton);
setContentView(layout1);

Programmatically set a textview in Android

I currently have:
final TextView tv = new TextView(this);
final RelativeLayout rL = new RelativeLayout(this);
final EditText editText = (EditText)findViewById(R.id.editText);
final Button b1 = (Button)findViewById(R.id.b1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
rL.addView(tv);
tv.setText(editText.getText());
editText.setText("");
}
});
In my onCreate method but my textView does not show up on the screen when text is inputted and my button is pressed? Is there code to set where it is set up on the screen of my phone?
Here is your problem
final RelativeLayout rL = new RelativeLayout(this);
This RelativeLayout that contains the TextView is not even displayed on the screen, all you are doing is creating a RelativeLayout.
What you should do instead is add a RelativeLayout to your XML layout file (same one containing the EditText and Button and do the following
final RelativeLayout rL = (RelativeLayout)findViewById(R.id.myRelativeLayout);
...
rL.addView(tv);
Now since you are referencing an actual RelativeLayout, your text will be visible.
Hope I made some sort of sense.
Do you have a base layout? You're adding the EditText to the RelativeLayout, but you need to add the RelativeLayout to some already existing layout.
First, inflate some base layout. Then do a findViewById on that layout. Use this to call addView(editText);
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/base_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</RelativeLayout>
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
RelativeLayout rl = (RelativeLayout)findViewById(R.layout.base_layout);
rl.addView(yourTextView);
}
}

Add TextView dynamically and getTag()

I have one class calls "SplitText" and extends AsyncTask.
There i added a dynamic textview:
TextView masterTV = new TextView(mActivity);
tv.setTag("masterTV");
...
masterTV.setText(Activity_Start.chapterContentList.get(0));
LinearLayout sv = (LinearLayout) mActivity.findViewById(R.id.linearlayout);
sv.removeAllViews();
sv.addView(masterTV);
This works fine.
Now i want to get the text from this TextView from an other class calls Activity_Start which extends Activity..
on there onOptionsItemSelected() i have:
case R.id.menu_previous:
LinearLayout ll = (LinearLayout) findViewById(R.id.linearlayout);
TextView tv = (TextView) ll.findViewWithTag("masterTV");
Log.e("previs", tv.getText().toString());
return true;
But this returns always a NullPointerException..
What i do wrong?
UPDATE:
I have found an other way to get my TextView:
LinearLayout ll = (LinearLayout) findViewById(R.id.linearlayout);
TextView tv = (TextView) ll.getChildAt(0);
This isn't the exactly way i want but i runs and do what i want.
(Provided your Textview is child no 0)

Android: programmatically adding buttons to a layout

I'm trying to get an add button to add another button to the layout, based on the edittext to the left of the button. The point is for a person to list the rooms in their house, and then when they type in each room, a new button is generated so they can click the room, and then start working on the next page.
I had an xml layout all done, and then I realized I'm "programmatically" adding buttons, so I redid the layout programmatically, and then in the switch/case (that's how I do onclicks) for the add button I tried to add a button to the view, but it's getting very tricky. I'd like to have a scrollview below the edittext and add buttons, and as they add all the rooms to their house it eventually is populated with a scrollable list of buttons for their entire home. Is there a way to add buttons programmatically to an xml'd layout. I was thinking you can but everything I'm trying just isn't working.
Thanks for your help everybody, any recommendations you have would be greatly appreciated.
First Edit (in response to Tanuj's solution)
My XML file (not sure if we're going to use this or just use the java):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/tvAddARoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tvAddARoom" />
<EditText
android:id="#+id/etAddARoom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/etAddARoom" />
<Button
android:id="#+id/btnAddARoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btnAdd" />
<TextView
android:id="#+id/tvSelectARoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tvSelectARoom" />
<TextView
android:id="#+id/tvNoRooms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tvNoRooms" />
<Button
android:id="#+id/btnViewAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btnViewAll" />
</LinearLayout>
And the Java. This isn't at all correct, as in the java I'm creating the whole layout instead of using the layout above. Just not sure if I can bridge the two.
package com.bluej.movingbuddy;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
//import android.widget.ScrollView;
import android.widget.TextView;
public class EstimatorByRoom extends Activity implements OnClickListener {
String roomName;
EditText etAddARoom;
LinearLayout layout;
LinearLayout.LayoutParams layoutParam;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//setContentView(R.layout.estimatorbyroom);
LayoutParams params =
new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
//create a layout
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
//create a text view
TextView tvAddARoom = new TextView(this);
tvAddARoom.setText("Add a Room");
tvAddARoom.setLayoutParams(params);
//create an edittext
EditText etAddARoom = new EditText(this);
etAddARoom.setHint("Living Room, Dining Room, etc.");
etAddARoom.setLayoutParams(params);
//create a button
Button btnAddARoom = new Button(this);
btnAddARoom.setText("Add");
btnAddARoom.setLayoutParams(params);
//adds the textview
layout.addView(tvAddARoom);
//add the edittext
layout.addView(etAddARoom);
//add the button
layout.addView(btnAddARoom);
//create the layout param for the layout
LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
this.addContentView(layout, layoutParam);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btnAddARoom:
//add a room
//this part isn't working!
roomName = etAddARoom.getText().toString();
Button createdButton = new Button(this);
createdButton.setText(roomName);
layout.addView(createdButton);
this.addContentView(layout, layoutParam);
//if no rooms make tvnorooms disappear
break;
}
}
}
Try this :
//the layout on which you are working
LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout_tags);
//set the properties for button
Button btnTag = new Button(this);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btnTag.setText("Button");
btnTag.setId(some_random_id);
//add button to the layout
layout.addView(btnTag);
Try this code:
LinearLayout l_layout = (LinearLayout) findViewById(R.id.linear_layout);
l_layout.setOrientation(LinearLayout.VERTICAL); // or HORIZONTAL
Button btn1 = new Button(this);
btn1.setText("Button_text");
l_layout.addView(btn1);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// put code on click operation
}
});
that is a way to create button dynamically and add in Layout.
remember that when you create button programmatically you just use this not Class_name.this
public class AndroidWalkthroughApp1 extends Activity implements View.OnClickListener {
final int TOP_ID = 3;
final int BOTTOM_ID = 4;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// create two layouts to hold buttons
LinearLayout top = new LinearLayout(this);
top.setId(TOP_ID);
LinearLayout bottom = new LinearLayout(this);
bottom.setId(BOTTOM_ID);
// create buttons in a loop
for (int i = 0; i < 2; i++) {
Button button = new Button(this);
button.setText("Button " + i);
// R.id won't be generated for us, so we need to create one
button.setId(i);
// add our event handler (less memory than an anonymous inner class)
button.setOnClickListener(this);
// add generated button to view
if (i == 0) {
top.addView(button);
}
else {
bottom.addView(button);
}
}
// add generated layouts to root layout view
LinearLayout root = (LinearLayout)this.findViewById(R.id.root_layout);
root.addView(top);
root.addView(bottom);
}
#Override
public void onClick(View v) {
// show a message with the button's ID
Toast toast = Toast.makeText(AndroidWalkthroughApp1.this, "You clicked button " + v.getId(), Toast.LENGTH_LONG);
toast.show();
// get the parent layout and remove the clicked button
LinearLayout parentLayout = (LinearLayout)v.getParent();
parentLayout.removeView(v);
}
}
Each button needs to have an onclicklistener to tell it what to do. this can be added to your java code under where you state your button.
Button createdButton = new Button(this);
createdButton.setOnClickListener(new OnClickListener()
{
code you want implemented
}
I would add an id to your LinearLayout in xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#id/llContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
And then change your onClick to this:
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnAddARoom:
//add a room
//Find you parent layout which we'll be adding your button to:
LinearLayout layout = (LinearLayout) findViewById(R.id.llContainer);
roomName = etAddARoom.getText().toString();
Button createdButton = new Button(this);
createdButton.setText(roomName);
createdButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
layout.addView(createdButton);
//if no rooms make tvnorooms disappear
break;
}
}

use SwipeView for a layout rather than images

I'm using jason fry's SwipeView, he uses it for imageviews though, I'm struggling to replace it with a layout.
at the moment it works if I replace the ImageView with a TextView but how would I replace the Imageview with a layout
any help is appreciated thanks
package com.example;
import android.graphics.Typeface;
import android.text.Layout;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.R;
import uk.co.jasonfry.android.tools.ui.PageControl;
import uk.co.jasonfry.android.tools.ui.SwipeView;
import uk.co.jasonfry.android.tools.ui.SwipeView.OnPageChangedListener;
import android.app.Activity;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.ImageView;
public class MyActivity extends Activity
{
SwipeView mSwipeView;
LinearLayout ll;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ll = new LinearLayout(this);
ll = loadLayout();
PageControl mPageControl = (PageControl) findViewById(R.id.page_control);
mSwipeView = (SwipeView) findViewById(R.id.swipe_view);
//loadImages();
for(int i=0; i<4;i++)
{
mSwipeView.addView(new FrameLayout(this));
}
TextView i0 = new TextView(this);
TextView i1 = new TextView(this);
i0.setText("page 1");
i1.setText("page 2");
((FrameLayout) mSwipeView.getChildContainer().getChildAt(0)).addView(ll);
((FrameLayout) mSwipeView.getChildContainer().getChildAt(1)).addView(ll);
SwipeImageLoader mSwipeImageLoader = new SwipeImageLoader();
mSwipeView.setOnPageChangedListener(mSwipeImageLoader);
mSwipeView.setPageControl(mPageControl);
}
private class SwipeImageLoader implements OnPageChangedListener
{
public void onPageChanged(int oldPage, int newPage)
{
if(newPage>oldPage)//going forwards
{
if(newPage != (mSwipeView.getPageCount()-1))//if at the end, don't load one page after the end
{
TextView v = new TextView(MyActivity.this);
v.setText("page :"+(newPage+1));
((FrameLayout) mSwipeView.getChildContainer().getChildAt(newPage+1)).addView(ll);
}
if(oldPage!=0)//if at the beginning, don't destroy one before the beginning
{
((FrameLayout) mSwipeView.getChildContainer().getChildAt(oldPage-1)).removeAllViews();
}
}
else //going backwards
{
if(newPage!=0)//if at the beginning, don't load one before the beginning
{
TextView v = new TextView(MyActivity.this);
v.setText("page :"+(newPage+1));
((FrameLayout) mSwipeView.getChildContainer().getChildAt(newPage-1)).addView(ll);
}
if(oldPage != (mSwipeView.getPageCount()-1))//if at the end, don't destroy one page after the end
{
((FrameLayout) mSwipeView.getChildContainer().getChildAt(oldPage+1)).removeAllViews();
}
}
}
}
private LinearLayout loadLayout()
{
//logo
ImageView logo = new ImageView(this);
logo.setImageResource(R.drawable.image001);
logo.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
// espace
TextView espace = new TextView(this);
espace.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
espace.setText(" ");
// wrap prest
LinearLayout wprest = new LinearLayout(this);
//Prestation
TextView txt_pres = new TextView(this);
txt_pres.setText(" Prestation n° ");
txt_pres.setTextColor(R.color.black);
// plaid
TextView plaid = new TextView(this);
plaid.setTextColor(R.color.black);
plaid.setTypeface(null, Typeface.BOLD);
plaid.setText("4558");
// -
TextView tiret = new TextView(this);
tiret.setTextColor(R.color.black);
tiret.setTypeface(null, Typeface.BOLD);
tiret.setText(" - ");
// plaid
TextView platyp = new TextView(this);
platyp.setTextColor(R.color.black);
platyp.setTypeface(null, Typeface.BOLD);
platyp.setText("ECHANGE");
wprest.addView(txt_pres);
wprest.addView(plaid);
wprest.addView(tiret);
wprest.addView(platyp);
LinearLayout ll = new LinearLayout(this);
ll.setBackgroundResource(R.color.white);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
ll.addView(logo);
ll.addView(espace);
ll.addView(wprest);
return ll;
}
}
Why don't you try another ViewPager library, such as:
android-viewflow (https://github.com/pakerfeldt/android-viewflow)
Android-ViewPagerIndicator (https://github.com/JakeWharton/Android-ViewPagerIndicator)
because using it, you can easily custom children layout.
I see it's the most popular ViewPager libraries. For more detail, you can check out my "Android UI Patterns" app on Market: https://market.android.com/details?id=com.groidify.uipatterns. There are many useful samples for developers.
Yo
I use this library all over the place for more than just images. I've used it with complex layouts, so it does work.
I've since added some new layout objects, but not documented them or created examples of how to use them (I'm a busy guy! :p ) One of them is called PageView, it uses an adapter, like a ListView, so if you know how to use a ListView you should be able to use this. The key is using the .setAdapter(BaseAdapter adapter) method.
Make sure you're using the latest one from github. Here's a link straight to the PageView class code on github: https://github.com/fry15/uk.co.jasonfry.android.tools/blob/master/src/uk/co/jasonfry/android/tools/widget/PageView.java

Categories

Resources