Dynamic user interface creation problem - android

How to Create dynamic user interface with events...
Hi Friends i want to create a user interface. in which i will have some buttons and labels
but the number of buttons and lables will be changing time to time depending upon the data retrieved from the server...
Can any one please guide me to do solve this issue... with usefuls links and guidence..

This uses two linear layouts to achieve a grid. The higher level LinearLayout is defined in linearlayout.xml, and holds element views vertically. Within each of these vertical views, a Row (custom LinearLayout) is instanciated, and in doing so, specifies the event handler(s). This LinearLayout holds its elements horizontally.
public class YourActivity extends Activity {
#Override
public void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(R.layout.linearlayout);
ViewGroup main = (ViewGroup) findViewById(R.id.linearlayout);
main.addView(new Row(this));
main.addView(new Row(this));
}
private class Row extends LinearLayout {
public Row(Context context) {
super(context);
TextView text = new TextView(context);
Button button = new Button(context);
text.setText("Text");
button.setText("Button");
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Button Clicked", Toast.LENGTH_SHORT).show();
}
});
this.addView(text);
this.addView(button);
}
}
}
linearlayout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearlayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</LinearLayout>
Is this what you were looking for?

Related

How to alternate between 2 layouts in the same activity?

I want to know how to get the same activity to show 2 layouts. I explain, I have an activity with a login, and within this same are 2 buttons (one called "login" and another "SingUp") the idea is that when you click on one of these 2 shows a different layout for each button, but without having to change all activity in one. I achieved it partially by adding the SetContentView to the Onclic of each button, and it works but only the first time, without the activity it starts with the login layout and then I click on the singUp button, if it changes layout but if later I want to return to the login layout I can not anymore I have to restart the app.What I want to achieve is to change layout in the same activity using buttons, whatever method they mention or they help me to know which part of my code is wrong :(Thank you.
public class MainActivity extends AppCompatActivity {
private Button btnLogin,btnSignup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnLogin=(Button)findViewById(R.id.btnLogin);
btnSignup=(Button)findViewById(R.id.btnSignup);
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.activity_main);
}
});
btnSignup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.activity_sign_up);
}
});
}
}
with the previous code and managed to alternate between 2 layouts in the same activity, but the buttons only work the first time if I try to go to a layout by pressing a button for the second time, this simply does not work until I restart the app.
Option 1
Try recreating the Activity. Save which view to load then recreate.
private Button btnLogin,btnSignup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(preference.getInt(ACTIVITY)==1)
setContentView(R.layout.activity_main);
else
setContentView(R.layout.activity_sign_up);
btnLogin=(Button)findViewById(R.id.btnLogin);
btnSignup=(Button)findViewById(R.id.btnSignup);
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
preference.setInt(ACTIVITY, 1);
recreate();
}
});
btnSignup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
preference.setInt(ACTIVITY, 2);
recreate();
}
});
}
Option 2
Use fragments. Create 2 fragments and inflate them into activity on onclick of button
Option 3
Use VISIBLE/GONE with predefined layouts inside activity_main
Create one layout with two separate views that match_parent's height and width.
Hide / show each one when switching views
<LinearLayout
android:id="#+id/parent"
android:width="match_parent"
android:height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/main"
android:width="match_parent"
android:height="match_parent"
android:orientation="vertical"
android:visibility="visibile">
<!-- Add child views here -->
</LinearLayout>
<LinearLayout
android:id="#+id/sign_up"
android:width="match_parent"
android:height="match_parent"
android:orientation="vertical"
android:visibility="gone">
<!-- Add child views here -->
</LinearLayout>
</LinearLayout>
Then programmatically...
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mainView.setVisibility(View.VISIBLE);
loginView.setVisibility(View.GONE);
}
});
btnSignup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mainView.setVisibility(View.GONE);
loginView.setVisibility(View.VISIBLE);
}
});
You can approach it in other ways using Fragments, which one could argue that's the more appropriate way.
Switch between two fragments

Refering to a xml layout inside a widget's class

So, I am trying to get reference to a xml layout from the class of a simple widget I made.
So ,my widget contains an ImageView and two TextViews.I will add the code for this widget, just so no one gets confused.
public class Item extends LinearLayout{
TextView tv1,tv2;
ImageView img;
public Item(Context context,int resid, String t1, String t2) {
super(context);
setOrientation(HORIZONTAL);
setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
img = new ImageView(context);
tv1 = new TextView(context);
tv2 = new TextView(context);
img.setBackgroundResource(resid);
img.setVisibility(View.VISIBLE);
img.setLayoutParams(new ViewGroup.LayoutParams(200, 200));
tv1.setText(t1);
tv1.setTextSize(15);
tv1.setGravity(Gravity.CENTER);
tv1.setLayoutParams(new ViewGroup.LayoutParams(250, 100));
tv2.setText(t2+"lei");
tv2.setTextSize(15);
tv2.setGravity(Gravity.CENTER);
tv2.setLayoutParams(new ViewGroup.LayoutParams(250,100));
tv1.setBackgroundColor(Color.GREEN);
tv2.setBackgroundColor(Color.BLUE);
addView(img);
addView(tv1);
addView(tv2);
}
So, as you can see, there's a clickListener added for each "Item".What I want to do ,is to be able to refer to a xml layout that is a second activity ,so that I can manipulate what is in this layout from within this widget.
The second activity's class:
public class Final extends Activity {
LinearLayout fl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.final_layout);
View rootView = getWindow().getDecorView().findViewById(android.R.id.content);
fl = (LinearLayout) rootView.findViewById(R.id.fl);
}
}
The XML file for this second activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/fl"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
So, once again, to sum up, I want to be able to add stuff to this second activity ,from the class Item ,but I am not able to take reference to the XML layout coresponding to the second activity.
So,practically ,the only way to do this, is to pass variables through the onclick method.
What I am doing right now is this: I get two String variables and one Int.Strings being used for the text wrote in textViews and int for the resource ID for the imageView's backgroundResource, pass them to the second activity and use them there to re-create the Item.
If anyone needs more details on this, leave a comment here and I'll do my best to help.

I want to start another activity when it is clicked?

Well I have an activity which contains nothing but a relative layout with brown background,
and I want to start another activity if the users clicks anywhere on the screen, how would i do that ??
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mainscreen);
}
First, give ID to your RelativeLayout by putting android:id="#+id/relativeLayout" in your layout file then.
RelativeLayout l = (RelativeLayout) findViewById(R.id.relativeLayout1);
l.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
// startActivity here
}
});
or without using your RelativeLayout just implement the onTouchEvent(MotionEvent) of activity
public boolean onTouchEvent(MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_UP)
{
// start activity
}
return super.onTouchEvent(event);
}
Note: Not yet tried this code.
Add onClick attribute to your layout xml, and implement onClick method in your activity to start a new activity.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="onClick"
android:orientation="vertical" >
In your activity add an onClick method.
public void OnClick(View v) {
startActivity(new Intent(this,MyNewActivity.class));
}
I think your Mainscreen has Parent layout either Linear,Relative,Frame. Take reference of that and handle click listener.
Ex:
If your Parent layout is Linear.
LinearLayout linear=(LinearLayout)findViewById(R.id.linear);
linear.setOnClickListener(this);

How to Switch Views at Runtime?

I have the following code in my accounting application:
// switch View to the Customer layout, widget id's are the same on both layouts
private void hideExpenseView() {
setContentView(R.layout.customer_invoices);
}
// switch View to the Supplier layout
private void hideIncomeView() {
setContentView(R.layout.supplier_invoices);
}
The above does not work, as when you switch the ContentView, you lose all variable mappings. You have to map variables after you setContentView() unfortunately.
If this worked, this would be a beautifully simple solution for my app. See, I've named the widgets in both xml layouts the same ids. Instead of hiding elements of one xml layout based on different states, I switch the entire View to the appropriate layout - whether entering a Customer sales invoice, or a Supplier expense invoice.
By switching Views, I would have basically 6 lines of code taking care of the UI transition, very simple.
I hope this is still possible in another capacity, can someone please push me in the right direction?
Check out ViewSwitcher : see http://developer.android.com/reference/android/widget/ViewSwitcher.html
That, or base your activities layout in a framelayout that includes supplier_invoices.xml and customer_invoices.xml. Then your homegrown hide-n-show will be g2g. Tho, you might need to change the ids still.
You can wrap your views in two LinearLayouts, one for R.layout.customer_invoices and another for R.layout.supplier_invoices.
You need to implement your own findViewById.
private static final int LAYOUT_EXPENSE = 1;
private static final int LAYOUT_INCOME = 2;
private int currentLayout = LAYOUT_EXPENSE;
private LinearLayout expenseContainer, incomeContainer;
// switch View to the Customer layout, widget id's are the same on both layouts
private void hideExpenseView() {
switchLayout(LAYOUT_INCOME);
}
// switch View to the Supplier layout
private void hideIncomeView() {
switchLayout(LAYOUT_EXPENSE);
}
private void switchLayout(int layout) {
currentLayout = layout;
if (layout == LAYOUT_EXPENSE) {
expenseContainer.setVisibility(VISIBLE);
incomeContainer.setVisibility(GONE);
} else {
expenseContainer.setVisibility(GONE);
incomeContainer.setVisibility(VISIBLE);
}
}
public View findViewById(int id) {
if (layout == LAYOUT_EXPENSE) return expenseContainer.findViewById(id);
else return incomeContainer.findViewById(id);
}
I think you got my idea.
Do like this
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/customer_invoices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- put customer_invoices related tools like TextView, Button, ImageView here -->
</LinearLayout>
<LinearLayout
android:id="#+id/supplier_invoices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- put supplier_invoices related tools like TextView, Button, ImageView here -->
</LinearLayout>
</LinearLayout>
Java code:
public class TestActivity extends Activity {
View supplier_invoices,customer_invoices;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
supplier_invoices = findViewById(R.id.supplier_invoices);
customer_invoices = findViewById(R.id.customer_invoices);
}
// switch View to the Customer layout, widget id's are the same on both layouts
private void hideExpenseView() {
setContentView(R.layout.customer_invoices);
customer_invoices.setVisibility(View.VISIBLE);
supplier_invoices.setVisibility(View.GONE);
}
// switch View to the Supplier layout
private void hideIncomeView() {
supplier_invoices.setVisibility(View.VISIBLE);
customer_invoices.setVisibility(View.GONE);
}
}

Android: swapping two overlapping views

Can anyone tell me what's wrong with this implementation? All I want to do here is have two overlapping views that swap places when you tap the screen. Unless I'm just using it wrong, View.bringToFront() does nothing?
Below is all the code in my app. Note that I added padding to the 'backView' just to make sure the two were actually overlapping. Indeed I could see both on the screen. While tapping the top view does indeed trigger the onClick method, nothing visibly changes in response to the calls to bringToFront.
public class MainActivity extends Activity implements OnClickListener {
private ImageView frontView;
private ImageView backView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
frontView = (ImageView) findViewById(com.example.R.id.FrontView);
backView = (ImageView) findViewById(com.example.R.id.BackView);
frontView.setOnClickListener(this);
backView.setOnClickListener(this);
backView.setPadding(10,0,0,0);
}
private boolean flag;
public void onClick(View v) {
if (!flag) {
backView.bringToFront();
}
else {
frontView.bringToFront();
}
flag = !flag;
}
}
and the corresponding layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/FrontView"
android:src="#drawable/front"
/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/BackView"
android:src="#drawable/back"
/>
</RelativeLayout>
Maybe it's the layout I'm using? I'm not sure... I've tried FrameLayout and LinearLayout as well.
I would try swapping content views instead of ImageViews.
Put each imageView in a different layout and then it is easy:
public void onClick(View v) {
if (!flag) {
setContentView(R.layout.main_front);
frontView = (ImageView) findViewById(com.example.R.id.FrontView);
frontView.setOnClickListener(this);
}
else {
setContentView(R.layout.main_back);
backView = (ImageView) findViewById(com.example.R.id.BackView);
backView.setOnClickListener(this);
}
flag = !flag;
}
There are a couple of Components that you can use that do this for you.
ViewAnimator, ViewFlipper and ViewSwitcher. You can set the animations you require etc and they hand the rest.
here's one example.
http://www.androidpeople.com/android-viewflipper-example/
Given your example, do you have to call invalidate() on the parent after you've called bringToFront() ?

Categories

Resources